-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustomize-cra.js
339 lines (296 loc) · 8.88 KB
/
customize-cra.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
const flow = require("lodash.flow");
const addBundleVisualizer = (options = {}, behindFlag = false) => config => {
const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
.BundleAnalyzerPlugin;
// if behindFlag is set to true, the report will be created only if
// the `--analyze` flag is added to the `yarn build` command
if (behindFlag ? process.argv.includes("--analyze") : true) {
config.plugins.push(
new BundleAnalyzerPlugin(
Object.assign(
{
analyzerMode: "static",
reportFilename: "report.html"
},
options
)
)
);
}
return config;
};
const getBabelLoader = config => {
const babelLoaderFilter = rule =>
rule.loader &&
rule.loader.includes("babel") &&
rule.options &&
rule.options.plugins;
// First, try to find the babel loader inside the oneOf array.
// This is where we can find it when working with [email protected].
let loaders = config.module.rules.find(rule => Array.isArray(rule.oneOf))
.oneOf;
let babelLoader = loaders.find(babelLoaderFilter);
// If the loader was not found, try to find it inside of the "use" array, within the rules.
// This should work when dealing with [email protected].* versions.
if (!babelLoader) {
loaders = loaders.reduce((ldrs, rule) => ldrs.concat(rule.use || []), []);
babelLoader = loaders.find(babelLoaderFilter);
}
return babelLoader;
}
const addBabelPlugin = plugin => config => {
getBabelLoader(config).options.plugins.push(plugin);
return config;
};
const addBabelPreset = preset => config => {
getBabelLoader(config).options.presets.push(preset);
return config;
};
const addDecoratorsLegacy = () => config =>
addBabelPlugin(["@babel/plugin-proposal-decorators", { legacy: true }])(
config
);
const disableEsLint = () => config => {
let eslintRules = config.module.rules.filter(
r => r.use && r.use.some(u => u.options && u.options.useEslintrc !== void 0)
);
eslintRules.forEach(rule => {
config.module.rules = config.module.rules.filter(r => r !== rule);
});
return config;
};
const addWebpackAlias = alias => config => {
if (!config.resolve) {
config.resolve = {};
}
if (!config.resolve.alias) {
config.resolve.alias = {};
}
Object.assign(config.resolve.alias, alias);
return config;
};
const addWebpackResolve = resolve => config => {
if (!config.resolve) {
config.resolve = {};
}
Object.assign(config.resolve, resolve);
return config;
}
const adjustWorkbox = adjust => config => {
config.plugins.forEach(p => {
if (p.constructor.name === "GenerateSW") {
adjust(p.config);
}
});
return config;
};
const useEslintRc = configFile => config => {
const eslintRule = config.module.rules.filter(
r => r.use && r.use.some(u => u.options && u.options.useEslintrc !== void 0)
)[0];
eslintRule.use[0].options.useEslintrc = true;
eslintRule.use[0].options.ignore = true;
eslintRule.use[0].options.configFile = configFile;
delete eslintRule.use[0].options.baseConfig;
const rules = config.module.rules.map(r =>
r.use && r.use.some(u => u.options && u.options.useEslintrc !== void 0)
? eslintRule
: r
);
config.module.rules = rules;
return config;
};
const enableEslintTypescript = () => config => {
const eslintRule = config.module.rules.filter(
r => r.use && r.use.some(u => u.options && u.options.useEslintrc !== void 0)
)[0];
eslintRule.test = /\.([j,t]sx?|mjs)$/;
const rules = config.module.rules.map(r =>
r.use && r.use.some(u => u.options && u.options.useEslintrc !== void 0)
? eslintRule
: r
);
config.module.rules = rules;
return config;
};
const useBabelRc = () => config => {
getBabelLoader(config).options.babelrc = true;
return config;
};
const babelInclude = include => config => {
getBabelLoader(config).include = include;
return config;
};
const override = (...plugins) => flow(...plugins.filter(f => f));
const addBabelPlugins = (...plugins) => plugins.map(p => addBabelPlugin(p));
const addBabelPresets = (...plugins) => plugins.map(p => addBabelPreset(p));
const fixBabelImports = (libraryName, options) =>
addBabelPlugin([
"import",
Object.assign(
{},
{
libraryName
},
options
),
`fix-${libraryName}-imports`
]);
const addLessLoader = (loaderOptions = {}) => config => {
const mode = process.env.NODE_ENV === "development" ? "dev" : "prod";
// Need these for production mode, which are copied from react-scripts
const publicPath = require("react-scripts/config/paths").servedPath;
const shouldUseRelativeAssetPaths = publicPath === "./";
const shouldUseSourceMap =
mode === "prod" && process.env.GENERATE_SOURCEMAP !== "false";
const lessRegex = /\.less$/;
const lessModuleRegex = /\.module\.less$/;
const localIdentName = loaderOptions.localIdentName || "[path][name]__[local]--[hash:base64:5]";
const getLessLoader = (cssOptions) => {
return [
mode === "dev"
? require.resolve("style-loader")
: {
loader: require("mini-css-extract-plugin").loader,
options: Object.assign(
{},
shouldUseRelativeAssetPaths ? { publicPath: "../../" } : undefined
)
},
{
loader: require.resolve("css-loader"),
options: cssOptions
},
{
loader: require.resolve("postcss-loader"),
options: {
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
require("postcss-preset-env")({
autoprefixer: {
flexbox: "no-2009"
},
stage: 3
})
],
sourceMap: shouldUseSourceMap
}
},
{
loader: require.resolve("less-loader"),
options: Object.assign(loaderOptions, {
source: shouldUseSourceMap
})
}
];
};
const loaders = config.module.rules.find(rule => Array.isArray(rule.oneOf))
.oneOf;
// Insert less-loader as the penultimate item of loaders (before file-loader)
loaders.splice(loaders.length - 1, 0, {
test: lessRegex,
exclude: lessModuleRegex,
use: getLessLoader({
importLoaders: 2
}),
sideEffects: mode === "prod"
}, {
test: lessModuleRegex,
use: getLessLoader({
importLoaders: 2,
modules: true,
localIdentName: localIdentName
})
});
return config;
};
// Use this helper to override the webpack dev server settings
// it works just like the `override` utility
const overrideDevServer = (...plugins) => configFunction => (
proxy,
allowedHost
) => {
const config = configFunction(proxy, allowedHost);
const updatedConfig = override(...plugins)(config);
return updatedConfig;
};
// to be used inside `overrideDevServer`, makes CRA watch all the folders
// included `node_modules`, useful when you are working with linked packages
// usage: `yarn start --watch-all`
const watchAll = () => config => {
if (process.argv.includes("--watch-all")) {
delete config.watchOptions;
}
return config;
};
// to be used to disable chunk according to:
// https://github.com/facebook/create-react-app/issues/5306#issuecomment-433425838
const disableChunk = () => config => {
config.optimization.splitChunks = {
cacheGroups: {
default: false,
},
};
config.optimization.runtimeChunk = false;
return config;
};
const addPostcssPlugins = (plugins) => config => {
const rules = config.module.rules.find(rule => Array.isArray(rule.oneOf))
.oneOf;
rules.forEach(r => r.use && r.use.forEach(u => {
if (u.options && u.options.ident === "postcss") {
if (!u.options.plugins) {
u.options.plugins = () => [...plugins];
}
if (u.options.plugins) {
const originalPlugins = u.options.plugins;
u.options.plugins = () => [...originalPlugins(), ...plugins];
}
}
}));
return config;
}
// This will remove the CRA plugin that prevents to import modules from
// outside the `src` directory, useful if you use a different directory
const removeModuleScopePlugin = () => config => {
config.resolve.plugins = config.resolve.plugins.filter(
p => p.constructor.name !== "ModuleScopePlugin"
);
return config;
};
const addTslintLoader = (options) => config => {
config.module.rules.unshift({
test: /\.(ts|tsx)$/,
loader: require.resolve("tslint-loader"),
options,
enforce: "pre",
});
return config;
};
module.exports = {
override,
addBundleVisualizer,
addBabelPlugin,
addDecoratorsLegacy,
disableEsLint,
addWebpackAlias,
addWebpackResolve,
adjustWorkbox,
useEslintRc,
enableEslintTypescript,
addBabelPlugins,
fixBabelImports,
useBabelRc,
addLessLoader,
overrideDevServer,
watchAll,
babelInclude,
addBabelPreset,
addBabelPresets,
disableChunk,
addPostcssPlugins,
getBabelLoader,
removeModuleScopePlugin,
addTslintLoader,
};