-
Notifications
You must be signed in to change notification settings - Fork 331
/
createWebpackConfig.js
759 lines (704 loc) · 23.3 KB
/
createWebpackConfig.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
// @flow
import path from 'path'
import autoprefixer from 'autoprefixer'
import CaseSensitivePathsPlugin from 'case-sensitive-paths-webpack-plugin'
import CopyPlugin from 'copy-webpack-plugin'
import HtmlPlugin from 'html-webpack-plugin'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import NpmInstallPlugin from '@insin/npm-install-webpack-plugin'
import ReactRefreshPlugin from '@pmmmwh/react-refresh-webpack-plugin'
import webpack from 'webpack'
import merge from 'webpack-merge'
import createBabelConfig from './createBabelConfig'
import {DEFAULT_BROWSERS_DEV, DEFAULT_BROWSERS_PROD} from './constants'
import debug from './debug'
import {UserError} from './errors'
import {deepToString, replaceArrayMerge, typeOf} from './utils'
import StatusPlugin from './WebpackStatusPlugin'
type LoaderConfig = {
loader?: string,
options?: Object
};
type LoaderConfigFactory = (id: ?string, defaultConfig: LoaderConfig) => LoaderConfig;
type UseConfig = Array<string | LoaderConfig>;
type RuleConfig = {
test?: RegExp,
include?: RegExp,
exclude?: RegExp,
loader?: string,
options?: Object,
use?: UseConfig
};
type RuleConfigFactory = (?string, RuleConfig) => ?RuleConfig;
type ServerConfig = boolean | Object;
const DEFAULT_TERSER_CONFIG = {
extractComments: false,
}
function createTerserConfig(userWebpackConfig) {
if (userWebpackConfig.debug) {
return merge(
DEFAULT_TERSER_CONFIG,
{
terserOptions: {
output: {
beautify: true,
},
mangle: false,
}
},
// Preserve user 'compress' config if present, as it affects what gets
// removed from the production build.
typeof userWebpackConfig.terser === 'object' &&
typeof userWebpackConfig.terser.terserConfig === 'object' &&
'compress' in userWebpackConfig.terser.terserConfig
? {terserOptions: {compress: userWebpackConfig.terser.terserConfig.compress}}
: {}
)
}
return merge(
DEFAULT_TERSER_CONFIG,
typeof userWebpackConfig.terser === 'object' ? userWebpackConfig.terser : {}
)
}
/**
* Merge webpack rule config objects.
*/
export function mergeRuleConfig(
defaultConfig: RuleConfig,
buildConfig: RuleConfig = {},
userConfig: RuleConfig = {}
): RuleConfig {
let rule
// Omit the default loader and options if the user is configuring their own
if (defaultConfig.loader && (userConfig.loader || userConfig.use)) {
let {
loader: defaultLoader, options: defaultOptions, // eslint-disable-line no-unused-vars
...defaultRuleConfig
} = defaultConfig
rule = merge(defaultRuleConfig, userConfig)
}
else {
rule = replaceArrayMerge(defaultConfig, buildConfig, userConfig)
}
if (rule.options && Object.keys(rule.options).length === 0) {
delete rule.options
}
return rule
}
/**
* Merge webpack loader config objects.
*/
export function mergeLoaderConfig(
defaultConfig: LoaderConfig,
buildConfig: LoaderConfig = {},
userConfig: LoaderConfig = {}
): RuleConfig {
let loader
// If the loader is being changed, only use the provided config
if (userConfig.loader) {
loader = {...userConfig}
}
else {
// The only arrays used in default options are for PostCSS plugins, which we
// want the user to be able to completely override.
loader = replaceArrayMerge(defaultConfig, buildConfig, userConfig)
}
if (loader.options && Object.keys(loader.options).length === 0) {
delete loader.options
}
return loader
}
/**
* Create a function which configures a rule identified by a unique id, with
* the option to override defaults with build-specific and user config.
*/
export function createRuleConfigFactory(
buildConfig: {[key: string]: RuleConfig} = {},
userConfig: {[key: string]: RuleConfig} = {}
): RuleConfigFactory {
return function(id: ?string, defaultConfig: RuleConfig): ?RuleConfig {
if (id) {
// Allow the user to turn off rules by configuring them with false
if (userConfig[id] === false) {
return null
}
let rule = mergeRuleConfig(defaultConfig, buildConfig[id], userConfig[id])
return rule
}
return defaultConfig
}
}
/**
* Create a function which configures a loader identified by a unique id, with
* the option to override defaults with build-specific and user config.
*/
export function createLoaderConfigFactory(
buildConfig: {[key: string]: LoaderConfig} = {},
userConfig: {[key: string]: LoaderConfig} = {}
): LoaderConfigFactory {
return function (id: ?string, defaultConfig: LoaderConfig): LoaderConfig {
if (id) {
let loader = mergeLoaderConfig(defaultConfig, buildConfig[id], userConfig[id])
return loader
}
return defaultConfig
}
}
/**
* Create a function which applies a prefix to a name when a prefix is given,
* unless the prefix ends with the name, in which case the prefix itself is
* returned.
* The latter rule is to allow rules created for CSS preprocessor plugins to
* be given unique ids for user configuration without duplicating the name of
* the rule.
* e.g.: loaderConfigName('sass')('css') => 'sass-css'
* loaderConfigName('sass')('sass') => 'sass' (as opposed to 'sass-sass')
*/
export let loaderConfigName = (prefix: ?string) =>
(name: string): string => {
if (prefix && prefix.endsWith(name)) {
return prefix
}
return prefix ? `${prefix}-${name}` : name
}
/**
* Create a list of chained loader config objects for a static build (default)
* or serving.
*/
export function createStyleLoaders(
createLoader: (?string, LoaderConfig) => LoaderConfig,
userWebpackConfig: Object,
options: {
preprocessor?: ?Object,
prefix?: ?string,
server?: ?ServerConfig
} = {},
): UseConfig {
let {
preprocessor = null,
prefix = null,
server = false,
} = options
let name = loaderConfigName(prefix)
let styleLoader = createLoader(name('style'), {
loader: require.resolve('style-loader'),
})
let loaders = [
createLoader(name('css'), {
loader: require.resolve('css-loader'),
options: {
// Apply postcss-loader to @imports
importLoaders: 1,
},
}),
createLoader(name('postcss'), {
loader: require.resolve('postcss-loader'),
options: {
ident: name('postcss'),
plugins: createDefaultPostCSSPlugins(userWebpackConfig),
}
})
]
if (preprocessor) {
loaders.push(createLoader(
preprocessor.id ? name(preprocessor.id) : null,
preprocessor.config
))
}
if (server || userWebpackConfig.extractCSS === false) {
loaders.unshift(styleLoader)
return loaders
}
else {
loaders.unshift(createLoader(name('extract-css'), {
loader: MiniCssExtractPlugin.loader,
}))
return loaders
}
}
/**
* Create style rules. By default, creates a single rule for .css files and for
* any style preprocessor plugins present. The user can configure this to create
* multiple rules if needed.
*/
function createStyleRules(
server: ServerConfig,
userWebpackConfig: Object,
pluginConfig: Object,
createRule: RuleConfigFactory,
createLoader: LoaderConfigFactory
): Array<?RuleConfig> {
let styleConfig = userWebpackConfig.styles || {}
let styleRules = []
// Configured styles rules, with individual loader configuration as part of
// the definition.
Object.keys(styleConfig).forEach(type => {
let test, preprocessor
if (type === 'css') {
test = /\.css$/
}
else {
let preprocessorConfig = pluginConfig.cssPreprocessors[type]
test = preprocessorConfig.test
preprocessor = {id: null, config: {loader: preprocessorConfig.loader}}
}
let ruleConfigs = [].concat(...styleConfig[type])
ruleConfigs.forEach(ruleConfig => {
let {loaders: loaderConfig, ...topLevelRuleConfig} = ruleConfig
// Empty build config, as all loader config for custom style rules will be
// provided by the user.
let styleRuleLoader = createLoaderConfigFactory({}, loaderConfig)
styleRules.push({
test,
use: createStyleLoaders(styleRuleLoader, userWebpackConfig, {preprocessor, server}),
...topLevelRuleConfig,
})
})
})
// Default CSS rule when nothing is configured, tweakable via webpack.rules by
// unique id.
if (!('css' in styleConfig)) {
styleRules.push(
createRule('css-rule', {
test: /\.css$/,
use: createStyleLoaders(createLoader, userWebpackConfig, {server}),
})
)
}
// Default rule for each CSS preprocessor plugin when nothing is configured,
// tweakable via webpack.rules by unique id.
if (pluginConfig.cssPreprocessors) {
Object.keys(pluginConfig.cssPreprocessors).forEach(id => {
if (id in styleConfig) return
let {test, loader: preprocessorLoader} = pluginConfig.cssPreprocessors[id]
styleRules.push(
createRule(`${id}-rule`, {
test,
use: createStyleLoaders(createLoader, userWebpackConfig, {
prefix: id,
preprocessor: {id, config: {loader: preprocessorLoader}},
server,
})
})
)
})
}
return styleRules
}
/**
* Final webpack rules config consists of:
* - the default set of rules created in this function, with build and user
* config tweaks based on rule id.
* - extra rules defined in build config, with user config tweaks based
* on rule id.
* - extra rules created for CSS preprocessor plugins, with user config
* tweaks based on loader id.
* - extra rules defined in user config.
*/
export function createRules(
server: ServerConfig,
buildConfig: Object = {},
userWebpackConfig: Object = {},
pluginConfig: Object = {}
): RuleConfig[] {
let createRule = createRuleConfigFactory(buildConfig, userWebpackConfig.rules)
let createLoader = createLoaderConfigFactory(buildConfig, userWebpackConfig.rules)
// Default options for url-loader
let urlLoaderOptions = {
// Don't inline anything by default
limit: 1,
// Always use a hash to prevent files with the same name causing issues
name: '[name].[hash:8].[ext]',
}
let rules = [
createRule('babel', {
test: /\.js$/,
loader: require.resolve('babel-loader'),
exclude: /node_modules[\\/](?!react-app-polyfill)/,
options: {
// Don't look for .babelrc files
babelrc: false,
// Cache transformations to the filesystem (in default temp dir)
cacheDirectory: true,
}
}),
createRule('graphics', {
test: /\.(gif|png|webp)$/,
loader: require.resolve('url-loader'),
options: {...urlLoaderOptions},
}),
createRule('svg', {
test: /\.svg$/,
loader: require.resolve('url-loader'),
options: {...urlLoaderOptions},
}),
createRule('jpeg', {
test: /\.jpe?g$/,
loader: require.resolve('url-loader'),
options: {...urlLoaderOptions},
}),
createRule('fonts', {
test: /\.(eot|otf|ttf|woff|woff2)$/,
loader: require.resolve('url-loader'),
options: {...urlLoaderOptions},
}),
createRule('video', {
test: /\.(mp4|ogg|webm)$/,
loader: require.resolve('url-loader'),
options: {...urlLoaderOptions},
}),
createRule('audio', {
test: /\.(wav|mp3|m4a|aac|oga)$/,
loader: require.resolve('url-loader'),
options: {...urlLoaderOptions},
}),
// Extra rules from build config, still configurable via user config when
// the rules specify an id.
...createExtraRules(buildConfig.extra, userWebpackConfig.rules),
]
// Add rules with chained style loaders, using MiniCssExtractPlugin for builds
if (userWebpackConfig.styles !== false) {
rules = rules.concat(createStyleRules(
server, userWebpackConfig, pluginConfig, createRule, createLoader
))
}
return rules.filter(Boolean)
}
/**
* Create rules from rule definitions which may include an id attribute for
* user customisation. It's assumed these are being created from build config.
*/
export function createExtraRules(
extraRules: Object[] = [],
userConfig: Object = {}
): Array<?RuleConfig> {
let createRule = createRuleConfigFactory({}, userConfig)
return extraRules.map(extraRule => {
let {id, ...ruleConfig} = extraRule
return createRule(id, ruleConfig)
})
}
/**
* Plugin for HtmlPlugin which inlines the Webpack runtime code and chunk
* manifest into its <script> tag.
*/
function inlineRuntimePlugin() {
this.hooks.compilation.tap('inlineRuntimePlugin', compilation => {
HtmlPlugin.getHooks(compilation).alterAssetTags.tapAsync('inlineRuntimePlugin', (data, cb) => {
Object.keys(compilation.assets).forEach(assetName => {
if (!/^runtime\.[a-z\d]+\.js$/.test(assetName)) return
let {children} = compilation.assets[assetName]
if (children && children[0]) {
let tag = data.assetTags.scripts.find(tag => tag.attributes.src.endsWith(assetName))
delete tag.attributes.src
tag.innerHTML = children[0]._value
}
})
cb(null, data)
})
})
}
function getCopyPluginArgs(buildConfig, userConfig) {
let patterns = []
let options = {}
if (buildConfig) {
patterns = patterns.concat(buildConfig)
}
if (userConfig) {
patterns = patterns.concat(userConfig.patterns || [])
options = userConfig.options || {}
}
return [patterns, options]
}
/**
* Final webpack plugin config consists of:
* - the default set of plugins created by this function based on whether or not
* a server build is being configured, whether or not the build is for an
* app (for which HTML will be generated), plus environment variables.
* - extra plugins managed by this function, whose inclusion is triggered by
* build config, which provides default configuration for them which can be
* tweaked by user plugin config when appropriate.
* - any extra plugins defined in build and user config (extra user plugins are
* not handled here, but by the final merge of webpack.extra config).
*/
export function createPlugins(
server: ServerConfig,
buildConfig: Object = {},
userConfig: Object = {}
): {optimization: Object, plugins: Object[]} {
let production = process.env.NODE_ENV === 'production'
let optimization = {}
let plugins = [
// Enforce case-sensitive import paths
new CaseSensitivePathsPlugin(),
// Replace specified expressions with values
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
...buildConfig.define,
...userConfig.define,
}),
// XXX Workaround until loaders migrate away from using this.options
new webpack.LoaderOptionsPlugin({
options: {
context: process.cwd()
}
})
]
if (server) {
// HMR is enabled by default but can be explicitly disabled
if (server.hot !== false) {
plugins.push(new webpack.HotModuleReplacementPlugin())
optimization.noEmitOnErrors = true
}
if (buildConfig.reactRefresh) {
plugins.push(new ReactRefreshPlugin())
}
if (buildConfig.status) {
plugins.push(new StatusPlugin(buildConfig.status))
}
}
// If we're not serving, we're creating a static build
else {
if (userConfig.extractCSS !== false) {
// Extract imported stylesheets out into .css files
plugins.push(new MiniCssExtractPlugin({
filename: production ? `[name].[contenthash:8].css` : '[name].css',
...userConfig.extractCSS,
}))
}
// Move modules imported from node_modules/ into a vendor chunk when enabled
if (buildConfig.vendor) {
optimization.splitChunks = {
// Split the entry chunk too
chunks: 'all',
// A 'vendors' cacheGroup will get defaulted if it doesn't exist, so
// we override it to explicitly set the chunk name.
cacheGroups: {
vendors: {
name: 'vendor',
priority: -10,
test: /[\\/]node_modules[\\/]/,
}
}
}
}
}
if (production) {
plugins.push(new webpack.LoaderOptionsPlugin({
debug: false,
minimize: true,
}))
optimization.minimize = buildConfig.terser !== false && userConfig.terser !== false
if (buildConfig.terser !== false && userConfig.terser !== false) {
optimization.minimizer = [
(compiler: any) => {
let TerserPlugin = require('terser-webpack-plugin')
let OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
new TerserPlugin(createTerserConfig(userConfig)).apply(compiler)
new OptimizeCSSAssetsPlugin({
cssProcessorOptions: {
map: {
inline: false,
annotation: true,
}
}
}).apply(compiler)
}
]
}
}
// Generate an HTML file for web apps which pulls in generated resources
if (buildConfig.html) {
plugins.push(new HtmlPlugin({
template: path.join(__dirname, '../templates/webpack-template.html'),
...buildConfig.html,
...userConfig.html,
}))
// Extract the Webpack runtime and manifest into its own chunk
// The default runtime chunk name is 'runtime' with this configuration
optimization.runtimeChunk = 'single'
// Inline the runtime and manifest
plugins.push(inlineRuntimePlugin)
}
// Copy static resources
if (buildConfig.copy || userConfig.copy) {
const [patterns, options] = getCopyPluginArgs(buildConfig.copy, userConfig.copy)
if (patterns.length > 0) {
plugins.push(new CopyPlugin({patterns, options}))
}
}
// Automatically install missing npm dependencies and add them to package.json
// if present.
// Must be enabled with an --install or --auto-install flag.
if (buildConfig.autoInstall) {
plugins.push(new NpmInstallPlugin({
peerDependencies: false,
quiet: true,
...userConfig.install,
}))
}
// Insert a banner comment at the top of generated files - used for UMD builds
if (buildConfig.banner) {
plugins.push(new webpack.BannerPlugin({banner: buildConfig.banner}))
}
// Escape hatch for any extra plugins a particular build ever needs to add
if (buildConfig.extra) {
plugins = plugins.concat(buildConfig.extra)
}
return {optimization, plugins}
}
function createDefaultPostCSSPlugins(userWebpackConfig) {
// Users can override browser versions for Autoprefixer using `browsers` or
// `webpack.autoprefixer.overrideBrowserslist` config.
let overrideBrowserslist = process.env.NODE_ENV === 'production'
? (userWebpackConfig.browsers && userWebpackConfig.browsers.production) ||
DEFAULT_BROWSERS_PROD
: (userWebpackConfig.browsers && userWebpackConfig.browsers.development) ||
DEFAULT_BROWSERS_DEV
return [
autoprefixer({
overrideBrowserslist,
...userWebpackConfig.autoprefixer
})
]
}
export const COMPAT_CONFIGS = {
intl(options: {locales: string[]}) {
return {
plugins: [
new webpack.ContextReplacementPlugin(
/intl[/\\]locale-data[/\\]jsonp$/,
new RegExp(`^\\.\\/(${options.locales.join('|')})$`)
)
]
}
},
moment(options: {locales: string[]}) {
return {
plugins: [
new webpack.ContextReplacementPlugin(
/moment[/\\]locale$/,
new RegExp(`^\\.\\/(${options.locales.join('|')})$`)
)
]
}
},
'react-intl'(options: {locales: string[]}) {
return {
plugins: [
new webpack.ContextReplacementPlugin(
/react-intl[/\\]locale-data$/,
new RegExp(`^\\.\\/(${options.locales.join('|')})$`)
)
]
}
},
}
/**
* Create a chunk of webpack config containing compatibility tweaks for
* libraries which are known to cause issues, to be merged into the generated
* config.
* Returns null if there's nothing to merge based on user config.
*/
export function getCompatConfig(userCompatConfig: Object = {}): ?Object {
let configs = []
Object.keys(userCompatConfig).map(lib => {
if (!userCompatConfig[lib]) return
let compatConfig = COMPAT_CONFIGS[lib]
if (typeOf(compatConfig) === 'function') {
compatConfig = compatConfig(userCompatConfig[lib])
if (!compatConfig) return
}
configs.push(compatConfig)
})
if (configs.length === 0) return null
if (configs.length === 1) return {...configs[0]}
return merge(...configs)
}
/**
* Create a webpack config with a curated set of default rules suitable for
* creating a static build (default) or serving an app with hot reloading.
*/
export default function createWebpackConfig(
buildConfig: Object,
pluginConfig: Object = {},
userConfig: Object = {}
): Object {
debug('createWebpackConfig buildConfig: %s', deepToString(buildConfig))
// Final webpack config is primarily driven by build configuration for the nwb
// command being run. Each command configures a default, working webpack
// configuration for the task it needs to perform.
let {
// These build config items are used to create chunks of webpack config,
// rather than being included as-is.
babel: buildBabelConfig = {},
entry,
output: buildOutputConfig,
plugins: buildPluginConfig = {},
resolve: buildResolveConfig = {},
rules: buildRulesConfig = {},
server = false,
// Any other build config provided is merged directly into the final webpack
// config to provide the rest of the default config.
...otherBuildConfig
} = buildConfig
let userWebpackConfig = userConfig.webpack || {}
let userOutputConfig = {}
if ('publicPath' in userWebpackConfig) {
userOutputConfig.publicPath = userWebpackConfig.publicPath
}
let userResolveConfig = {}
if (userWebpackConfig.aliases) {
userResolveConfig.alias = userWebpackConfig.aliases
}
// Generate config for babel-loader and set it as loader config for the build
buildRulesConfig.babel = {
options: createBabelConfig(buildBabelConfig, userConfig.babel, userConfig.path, userConfig.browsers)
}
let webpackConfig = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
module: {
rules: createRules(server, buildRulesConfig, userWebpackConfig, pluginConfig),
strictExportPresence: true,
},
output: {
...buildOutputConfig,
...userOutputConfig,
},
performance: {
hints: false
},
// Plugins are configured via a 'plugins' list and 'optimization' config
...createPlugins(server, buildPluginConfig, userWebpackConfig),
resolve: merge(buildResolveConfig, userResolveConfig),
resolveLoader: {
modules: ['node_modules', path.join(__dirname, '../node_modules')],
},
...otherBuildConfig,
}
if (entry) {
webpackConfig.entry = entry
}
// Create and merge compatibility configuration into the generated config if
// specified.
if (userWebpackConfig.compat) {
let compatConfig = getCompatConfig(userWebpackConfig.compat)
if (compatConfig) {
webpackConfig = merge(webpackConfig, compatConfig)
}
}
// Any extra user webpack config is merged into the generated config to give
// them even more control.
if (userWebpackConfig.extra) {
webpackConfig = merge(webpackConfig, userWebpackConfig.extra)
}
// Finally, give them a chance to do whatever they want with the generated
// config.
if (typeOf(userWebpackConfig.config) === 'function') {
webpackConfig = userWebpackConfig.config(webpackConfig)
if (!webpackConfig) {
throw new UserError(`webpack.config() in ${userConfig.path} didn't return anything - it must return the Webpack config object.`)
}
}
return webpackConfig
}