This repository has been archived by the owner on Nov 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig-overrides.js
98 lines (78 loc) · 3.52 KB
/
config-overrides.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const path = require('path');
const cloneDeep = require('lodash.clonedeep');
const rewireHotLoader = require('react-app-rewire-hot-loader');
const AutoDllPlugin = require('autodll-webpack-plugin');
const pkg = require('./package.json');
const ruleChildren = loader => loader.use || loader.oneOf || (Array.isArray(loader.loader) && loader.loader) || [];
const findIndexAndRules = (rulesSource, ruleMatcher) => {
let result = undefined;
const rules = Array.isArray(rulesSource) ? rulesSource : ruleChildren(rulesSource);
rules.some((rule, index) => (result = ruleMatcher(rule) ? { index, rules } : findIndexAndRules(ruleChildren(rule), ruleMatcher)));
return result;
};
const findRule = (rulesSource, ruleMatcher) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
return rules[index];
};
const cssRuleMatcher = rule => rule.test && String(rule.test) === String(/\.css$/);
const createLoaderMatcher = loader => rule => rule.loader && rule.loader.indexOf(`${path.sep}${loader}${path.sep}`) !== -1;
const cssLoaderMatcher = createLoaderMatcher('css-loader');
const postcssLoaderMatcher = createLoaderMatcher('postcss-loader');
const fileLoaderMatcher = createLoaderMatcher('file-loader');
const addBeforeRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
rules.splice(index, 0, value);
};
const addAfterRule = (rulesSource, ruleMatcher, value) => {
const { index, rules } = findIndexAndRules(rulesSource, ruleMatcher);
rules.splice(index + 1, 0, value);
};
module.exports = (config, env) => {
rewireHotLoader(config, env);
if (env === 'development') {
config.plugins = [
...config.plugins,
new AutoDllPlugin({
entry: {
vendor: Object.keys(pkg.dependencies),
},
filename: '[name].dll.js',
path: 'static/js',
inject: true,
debug: true,
}),
];
}
// rewire css modules
const cssRule = findRule(config.module.rules, cssRuleMatcher);
const sassRule = cloneDeep(cssRule);
const cssModulesRule = cloneDeep(cssRule);
cssRule.exclude = /\.module\.css$/;
cssModulesRule.test = /\.module\.css$/;
const cssModulesRuleCssLoader = findRule(cssModulesRule, cssLoaderMatcher);
cssModulesRuleCssLoader.options = {
camelCase: true,
localIdentName: env === 'development' ? '[path][name]__[local]' : '[hash:base64:8]',
modules: true,
...cssModulesRuleCssLoader.options,
};
addBeforeRule(config.module.rules, fileLoaderMatcher, cssModulesRule);
const sassResourcesLoader = {
loader: require.resolve('sass-resources-loader'),
options: {
resources: path.resolve(__dirname, 'src/app/shared/scss/main.scss'),
},
};
const sassLoader = require.resolve('sass-loader');
sassRule.test = /\.scss$/;
sassRule.exclude = /\.module\.scss$/;
addAfterRule(sassRule, postcssLoaderMatcher, sassResourcesLoader);
addAfterRule(sassRule, postcssLoaderMatcher, sassLoader);
addBeforeRule(config.module.rules, fileLoaderMatcher, sassRule);
const sassModulesRule = cloneDeep(cssModulesRule);
sassModulesRule.test = /\.module\.scss$/;
addAfterRule(sassModulesRule, postcssLoaderMatcher, sassResourcesLoader);
addAfterRule(sassModulesRule, postcssLoaderMatcher, sassLoader);
addBeforeRule(config.module.rules, fileLoaderMatcher, sassModulesRule);
return config;
};