-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathwebpack.js
403 lines (359 loc) · 10.7 KB
/
webpack.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import { getBabelLoader } from "../utilities";
export 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;
};
export 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;
};
export const addWebpackAlias = alias => config => {
if (!config.resolve) {
config.resolve = {};
}
if (!config.resolve.alias) {
config.resolve.alias = {};
}
Object.assign(config.resolve.alias, alias);
return config;
};
export const addWebpackResolve = resolve => config => {
if (!config.resolve) {
config.resolve = {};
}
Object.assign(config.resolve, resolve);
return config;
};
export const addWebpackPlugin = plugin => config => {
config.plugins.push(plugin);
return config;
};
export const adjustWorkbox = adjust => config => {
config.plugins.forEach(p => {
if (p.constructor.name === "GenerateSW") {
adjust(p.config);
}
});
return config;
};
export const adjustStyleLoaders = callback => config => {
const mode = process.env.NODE_ENV === 'production' ? 'prod' : 'dev';
const loader = mode === 'prod' ? 'css-extract-plugin' : 'style-loader';
const loaders = config.module.rules.find(rule => Array.isArray(rule.oneOf))
.oneOf;
const styleLoaders = loaders.filter(({ use }) => use && use[0] && (use[0].loader || use[0]).includes(loader));
styleLoaders.forEach(loader => callback(loader));
return config;
};
export 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;
};
export 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;
};
export const addLessLoader = (loaderOptions = {}, customCssModules = {}) => config => {
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const postcssNormalize = require("postcss-normalize");
const cssLoaderOptions = loaderOptions.cssLoaderOptions || {};
const { localIdentName } = loaderOptions;
let cssModules = loaderOptions.cssModules || { localIdentName };
if (!cssModules.localIdentName) {
cssModules = customCssModules;
}
cssModules.localIdentName = cssModules.localIdentName || "[local]--[hash:base64:5]";
const lessRegex = /\.less$/;
const lessModuleRegex = /\.module\.less$/;
const webpackEnv = process.env.NODE_ENV;
const isEnvDevelopment = webpackEnv === "development";
const isEnvProduction = webpackEnv === "production";
const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== "false";
const publicPath = config.output.publicPath;
const shouldUseRelativeAssetPaths = publicPath === "./";
// copy from react-scripts
// https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/webpack.config.js#L93
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
isEnvDevelopment && require.resolve("style-loader"),
isEnvProduction && {
loader: MiniCssExtractPlugin.loader,
options: shouldUseRelativeAssetPaths ? { publicPath: "../../" } : {}
},
{
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
}),
postcssNormalize()
],
sourceMap: isEnvProduction && shouldUseSourceMap
}
}
].filter(Boolean);
if (preProcessor) {
loaders.push(
{
loader: require.resolve("resolve-url-loader"),
options: {
sourceMap: isEnvProduction && shouldUseSourceMap
}
},
{
loader: require.resolve(preProcessor),
// not the same as react-scripts
options: Object.assign(
{
sourceMap: true
},
loaderOptions
)
}
);
}
return loaders;
};
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: getStyleLoaders(
Object.assign(
{
importLoaders: 2,
sourceMap: isEnvProduction && shouldUseSourceMap
},
cssLoaderOptions
),
"less-loader"
)
},
{
test: lessModuleRegex,
use: getStyleLoaders(
Object.assign(
{
importLoaders: 2,
sourceMap: isEnvProduction && shouldUseSourceMap
},
cssLoaderOptions,
{
modules: cssModules
}
),
"less-loader"
)
}
);
return config;
};
// 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`
export 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
export const disableChunk = () => config => {
config.optimization.splitChunks = {
cacheGroups: {
default: false
}
};
config.optimization.runtimeChunk = false;
return config;
};
// to be used to ignore replace packages with global variable
// Useful when trying to offload libs to CDN
export const addWebpackExternals = externalDeps => config => {
let externals = config.externals;
if (!externals) {
externals = externalDeps;
} else if (Array.isArray(externalDeps)) {
externals = externalDeps.concat(externals);
} else if (
Array.isArray(externals) ||
externalDeps.constructor === Function ||
externalDeps.constructor === RegExp
) {
externals = [externalDeps].concat(externals);
} else if (externalDeps instanceof Object && externals instanceof Object) {
externals = {
...externals,
...externalDeps
};
}
config.externals = externals;
return config;
};
export 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
export const removeModuleScopePlugin = () => config => {
config.resolve.plugins = config.resolve.plugins.filter(
p => p.constructor.name !== "ModuleScopePlugin"
);
return config;
};
/**
* Add the provided module to the webpack module rules array.
*
* @param rule The rule to be added
* @see https://webpack.js.org/configuration/module/#modulerules
*/
export const addWebpackModuleRule = rule => config => {
for (let _rule of config.module.rules) {
if (_rule.oneOf) {
_rule.oneOf.unshift(rule);
break;
}
}
return config;
};
export const addTslintLoader = options => config => {
config.module.rules.unshift({
test: /\.(ts|tsx)$/,
loader: "tslint-loader",
options,
enforce: "pre"
});
return config;
};
/**
* Override the webpack target.
*
* @param target What to set the webpack target as (can be string or function).
*
* @see https://webpack.js.org/configuration/target/
*/
export const setWebpackTarget = target => config => {
config.target = target;
return config;
};
/**
* override the webpack publicPath
*
* @param path What to set the webpack publicPath as.
* @see https://webpack.js.org/configuration/output/#outputpublicpath
*/
export const setWebpackPublicPath = path => config => {
if (path) {
if (!(path.startsWith("http") || path.startsWith("https"))) {
if (!path.startsWith("/")) {
path = "/" + path;
}
}
if (!path.endsWith("/")) {
path = path + "/";
}
config.output.publicPath = path;
}
return config;
};
/**
* override the webpack optimization.splitChunks
*
* @param configuration of optimization.splitChunks
* @see https://webpack.js.org/plugins/split-chunks-plugin/
*/
export const setWebpackOptimizationSplitChunks = splitChunks => config => {
if (splitChunks && typeof splitChunks === "object") {
config.optimization.splitChunks = splitChunks;
}
return config;
};
/**
* Sets the `stats` object in Webpack config
* This may not work in development mode
*
* @param stats Stats configuration in Webpack
* @see https://webpack.js.org/configuration/stats/
*/
export const setWebpackStats = stats => config => {
config.stats = stats;
return config;
};