config-overrides.js
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const {
override,
addBabelPlugin,
addBundleVisualizer,
} = require("customize-cra");
const findSassModuleRule = (config) => {
let sassModuleRuleIndex;
const loaders = config.module.rules.find((ruleItem) => {
if (!ruleItem.oneOf || !ruleItem.oneOf.length) {
return false;
}
sassModuleRuleIndex = ruleItem.oneOf.findIndex(
(loaderItem) =>
loaderItem.test &&
loaderItem.test.toString() === "/\\.module\\.(scss|sass)$/"
);
return sassModuleRuleIndex !== -1;
});
const sassModuleRule = loaders.oneOf[sassModuleRuleIndex];
return sassModuleRule;
};
const setCssModuleLocalIdentName = (localIdentName) => (config) => {
const sassModuleRule = findSassModuleRule(config);
const cssLoaderOption = (sassModuleRule.use || sassModuleRule.loader).find(
(loaderItem) =>
loaderItem.loader && loaderItem.loader.includes("/css-loader/")
);
cssLoaderOption.options.modules = { localIdentName };
return config;
};
const setupReactHotLoader = () => (config) => {
config = addBabelPlugin("react-hot-loader/babel")(config);
config.entry = ["react-hot-loader/patch", ...config.entry];
if (!config.resolve.alias) {
config.resolve.alias = {};
}
config.resolve.alias["react-dom"] = "@hot-loader/react-dom";
return config;
};
if (process.env.NODE_ENV === "development") {
module.exports = override(setupReactHotLoader());
} else {
module.exports = override(
setCssModuleLocalIdentName("[hash:base64:5]"),
addBundleVisualizer({}, true)
);
}