-
-
Notifications
You must be signed in to change notification settings - Fork 874
/
createConfig.js
623 lines (589 loc) · 20.5 KB
/
createConfig.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
'use strict';
const fs = require('fs-extra');
const path = require('path');
const webpack = require('webpack');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const nodeExternals = require('webpack-node-externals');
const AssetsPlugin = require('assets-webpack-plugin');
const StartServerPlugin = require('start-server-webpack-plugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const autoprefixer = require('autoprefixer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const paths = require('./paths');
const runPlugin = require('./runPlugin');
const getClientEnv = require('./env').getClientEnv;
const nodePath = require('./env').nodePath;
const errorOverlayMiddleware = require('react-dev-utils/errorOverlayMiddleware');
const WebpackBar = require('webpackbar');
const postCssOptions = {
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
};
// This is the Webpack configuration factory. It's the juice!
module.exports = (
target = 'web',
env = 'dev',
{
clearConsole = true,
host = 'localhost',
port = 3000,
modify,
plugins,
modifyBabelOptions,
},
webpackObject
) => {
// First we check to see if the user has a custom .babelrc file, otherwise
// we just use babel-preset-razzle.
const hasBabelRc = fs.existsSync(paths.appBabelRc);
const mainBabelOptions = {
babelrc: true,
cacheDirectory: true,
presets: [],
};
const hasEslintRc = fs.existsSync(paths.appEslintRc);
const mainEslintOptions = {
formatter: eslintFormatter,
eslintPath: require.resolve('eslint'),
ignore: false,
useEslintrc: true,
};
if (!hasBabelRc) {
mainBabelOptions.presets.push(require.resolve('../babel'));
}
// Allow app to override babel options
const babelOptions = modifyBabelOptions
? modifyBabelOptions(mainBabelOptions)
: mainBabelOptions;
if (hasBabelRc && babelOptions.babelrc) {
console.log('Using .babelrc defined in your app root');
}
if (hasEslintRc) {
console.log('Using .eslintrc defined in your app root');
} else {
mainEslintOptions.baseConfig = {
extends: [require.resolve('eslint-config-react-app')],
};
mainEslintOptions.useEslintrc = false;
}
// Define some useful shorthands.
const IS_NODE = target === 'node';
const IS_WEB = target === 'web';
const IS_PROD = env === 'prod';
const IS_DEV = env === 'dev';
process.env.NODE_ENV = IS_PROD ? 'production' : 'development';
const dotenv = getClientEnv(target, { clearConsole, host, port });
const devServerPort = parseInt(dotenv.raw.PORT, 10) + 1;
// VMs, Docker containers might not be available at localhost:3001. CLIENT_PUBLIC_PATH can override.
const clientPublicPath =
dotenv.raw.CLIENT_PUBLIC_PATH ||
(IS_DEV ? `http://${dotenv.raw.HOST}:${devServerPort}/` : '/');
// This is our base webpack config.
let config = {
// Set webpack mode:
mode: IS_DEV ? 'development' : 'production',
// Set webpack context to the current command's directory
context: process.cwd(),
// Specify target (either 'node' or 'web')
target: target,
// Controversially, decide on sourcemaps.
devtool: IS_DEV ? 'cheap-module-source-map' : 'source-map',
// We need to tell webpack how to resolve both Razzle's node_modules and
// the users', so we use resolve and resolveLoader.
resolve: {
modules: ['node_modules', paths.appNodeModules].concat(
// It is guaranteed to exist because we tweak it in `env.js`
nodePath.split(path.delimiter).filter(Boolean)
),
extensions: ['.mjs', '.jsx', '.js', '.json'],
alias: {
// This is required so symlinks work during development.
'webpack/hot/poll': require.resolve('webpack/hot/poll'),
// Support React Native Web
// https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
'react-native': 'react-native-web',
},
},
resolveLoader: {
modules: [paths.appNodeModules, paths.ownNodeModules],
},
module: {
strictExportPresence: true,
rules: [
// Disable require.ensure as it's not a standard language feature.
// { parser: { requireEnsure: false } },
{
test: /\.(js|jsx|mjs)$/,
enforce: 'pre',
use: [
{
options: mainEslintOptions,
loader: require.resolve('eslint-loader'),
},
],
include: paths.appSrc,
},
// Avoid "require is not defined" errors
{
test: /\.mjs$/,
include: /node_modules/,
type: 'javascript/auto',
},
// Transform ES6 with Babel
{
test: /\.(js|jsx|mjs)$/,
include: [paths.appSrc],
use: [
{
loader: require.resolve('babel-loader'),
options: babelOptions,
},
],
},
{
exclude: [
/\.html$/,
/\.(js|jsx|mjs)$/,
/\.(ts|tsx)$/,
/\.(vue)$/,
/\.(less)$/,
/\.(re)$/,
/\.(s?css|sass)$/,
/\.json$/,
/\.bmp$/,
/\.gif$/,
/\.jpe?g$/,
/\.png$/,
],
loader: require.resolve('file-loader'),
options: {
name: 'static/media/[name].[hash:8].[ext]',
emitFile: true,
},
},
// "url" loader works like "file" loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'static/media/[name].[hash:8].[ext]',
emitFile: true,
},
},
// "postcss" loader applies autoprefixer to our CSS.
// "css" loader resolves paths in CSS and adds assets as dependencies.
// "style" loader turns CSS into JS modules that inject <style> tags.
// In production, we use a plugin to extract that CSS to a file, but
// in development "style" loader enables hot editing of CSS.
//
// Note: this yields the exact same CSS config as create-react-app.
{
test: /\.css$/,
exclude: [paths.appBuild, /\.module\.css$/],
use: IS_NODE
? // Style-loader does not work in Node.js without some crazy
// magic. Luckily we just need css-loader.
[
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
]
: IS_DEV
? [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
},
},
{
loader: require.resolve('postcss-loader'),
options: postCssOptions,
},
]
: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
modules: false,
minimize: true,
},
},
{
loader: require.resolve('postcss-loader'),
options: postCssOptions,
},
],
},
// Adds support for CSS Modules (https://github.com/css-modules/css-modules)
// using the extension .module.css
{
test: /\.module\.css$/,
exclude: [paths.appBuild],
use: IS_NODE
? [
{
// on the server we do not need to embed the css and just want the identifier mappings
// https://github.com/webpack-contrib/css-loader#scope
loader: require.resolve('css-loader/locals'),
options: {
modules: true,
importLoaders: 1,
localIdentName: '[path]__[name]___[local]',
},
},
]
: IS_DEV
? [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
modules: true,
importLoaders: 1,
localIdentName: '[path]__[name]___[local]',
},
},
{
loader: require.resolve('postcss-loader'),
options: postCssOptions,
},
]
: [
MiniCssExtractPlugin.loader,
{
loader: require.resolve('css-loader'),
options: {
modules: true,
importLoaders: 1,
minimize: true,
localIdentName: '[path]__[name]___[local]',
},
},
{
loader: require.resolve('postcss-loader'),
options: postCssOptions,
},
],
},
],
},
};
if (IS_NODE) {
// We want to uphold node's __filename, and __dirname.
config.node = {
__console: false,
__dirname: false,
__filename: false,
};
// We need to tell webpack what to bundle into our Node bundle.
config.externals = [
nodeExternals({
whitelist: [
IS_DEV ? 'webpack/hot/poll?300' : null,
/\.(eot|woff|woff2|ttf|otf)$/,
/\.(svg|png|jpg|jpeg|gif|ico)$/,
/\.(mp4|mp3|ogg|swf|webp)$/,
/\.(css|scss|sass|sss|less)$/,
].filter(x => x),
}),
];
// Specify webpack Node.js output path and filename
config.output = {
path: paths.appBuild,
publicPath: clientPublicPath,
filename: 'server.js',
libraryTarget: 'commonjs2',
};
// Add some plugins...
config.plugins = [
// We define environment variables that can be accessed globally in our
new webpack.DefinePlugin(dotenv.stringified),
// Prevent creating multiple chunks for the server
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1,
}),
];
config.entry = [paths.appServerIndexJs];
if (IS_DEV) {
// Use watch mode
config.watch = true;
config.entry.unshift('webpack/hot/poll?300');
// Pretty format server errors
config.entry.unshift('razzle-dev-utils/prettyNodeErrors');
const nodeArgs = ['-r', 'source-map-support/register'];
// Passthrough --inspect and --inspect-brk flags (with optional [host:port] value) to node
if (process.env.INSPECT_BRK) {
nodeArgs.push(process.env.INSPECT_BRK);
} else if (process.env.INSPECT) {
nodeArgs.push(process.env.INSPECT);
}
config.plugins = [
...config.plugins,
// Add hot module replacement
new webpack.HotModuleReplacementPlugin(),
// Supress errors to console (we use our own logger)
new StartServerPlugin({
name: 'server.js',
nodeArgs,
}),
// Ignore assets.json to avoid infinite recompile bug
new webpack.WatchIgnorePlugin([paths.appManifest]),
];
}
}
if (IS_WEB) {
config.plugins = [
// Output our JS and CSS files in a manifest file called assets.json
// in the build directory.
new AssetsPlugin({
path: paths.appBuild,
filename: 'assets.json',
}),
// Maybe we should move to this???
// new ManifestPlugin({
// path: paths.appBuild,
// writeToFileEmit: true,
// filename: 'manifest.json',
// }),
];
if (IS_DEV) {
// Setup Webpack Dev Server on port 3001 and
// specify our client entry point /client/index.js
config.entry = {
client: [
// We ship a few polyfills by default but only include them if React is being placed in
// the default path. If you are doing some vendor bundling, you'll need to require the razzle/polyfills
// on your own.
!dotenv.raw.REACT_BUNDLE_PATH && require.resolve('./polyfills'),
require.resolve('razzle-dev-utils/webpackHotDevClient'),
paths.appClientIndexJs,
].filter(Boolean),
};
// Configure our client bundles output. Not the public path is to 3001.
config.output = {
path: paths.appBuildPublic,
publicPath: clientPublicPath,
pathinfo: true,
libraryTarget: 'var',
filename: 'static/js/bundle.js',
chunkFilename: 'static/js/[name].chunk.js',
devtoolModuleFilenameTemplate: info =>
path.resolve(info.resourcePath).replace(/\\/g, '/'),
};
// Configure webpack-dev-server to serve our client-side bundle from
// http://${dotenv.raw.HOST}:3001
config.devServer = {
disableHostCheck: true,
clientLogLevel: 'none',
// Enable gzip compression of generated files.
compress: true,
// watchContentBase: true,
headers: {
'Access-Control-Allow-Origin': '*',
},
historyApiFallback: {
// Paths with dots should still use the history fallback.
// See https://github.com/facebookincubator/create-react-app/issues/387.
disableDotRule: true,
},
host: dotenv.raw.HOST,
hot: true,
noInfo: true,
overlay: false,
port: devServerPort,
quiet: true,
// By default files from `contentBase` will not trigger a page reload.
// Reportedly, this avoids CPU overload on some systems.
// https://github.com/facebookincubator/create-react-app/issues/293
watchOptions: {
ignored: /node_modules/,
},
before(app) {
// This lets us open files from the runtime error overlay.
app.use(errorOverlayMiddleware());
},
};
// Add client-only development plugins
config.plugins = [
...config.plugins,
new webpack.HotModuleReplacementPlugin({
multiStep: true,
}),
new webpack.DefinePlugin(dotenv.stringified),
];
config.optimization = {
// @todo automatic vendor bundle
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
// splitChunks: {
// chunks: 'all',
// },
// Keep the runtime chunk seperated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
// runtimeChunk: true,
};
} else {
// Specify production entry point (/client/index.js)
config.entry = {
client: [
// We ship a few polyfills by default but only include them if React is being placed in
// the default path. If you are doing some vendor bundling, you'll need to require the razzle/polyfills
// on your own.
!dotenv.raw.REACT_BUNDLE_PATH && require.resolve('./polyfills'),
paths.appClientIndexJs,
].filter(Boolean),
};
// Specify the client output directory and paths. Notice that we have
// changed the publiPath to just '/' from http://localhost:3001. This is because
// we will only be using one port in production.
config.output = {
path: paths.appBuildPublic,
publicPath: dotenv.raw.PUBLIC_PATH || '/',
filename: 'static/js/bundle.[chunkhash:8].js',
chunkFilename: 'static/js/[name].[chunkhash:8].chunk.js',
libraryTarget: 'var',
};
config.plugins = [
...config.plugins,
// Define production environment vars
new webpack.DefinePlugin(dotenv.stringified),
// Extract our CSS into a files.
new MiniCssExtractPlugin({
filename: 'static/css/bundle.[contenthash:8].css',
chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
// allChunks: true because we want all css to be included in the main
// css bundle when doing code splitting to avoid FOUC:
// https://github.com/facebook/create-react-app/issues/2415
allChunks: true,
}),
new webpack.HashedModuleIdsPlugin(),
new webpack.optimize.AggressiveMergingPlugin(),
];
config.optimization = {
minimize: true,
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
parse: {
// we want uglify-js to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8,
},
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false,
},
mangle: {
safari10: true,
},
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true,
},
},
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
parallel: true,
// Enable file caching
cache: true,
// @todo add flag for sourcemaps
sourceMap: true,
}),
],
// @todo automatic vendor bundle
// Automatically split vendor and commons
// https://twitter.com/wSokra/status/969633336732905474
// splitChunks: {
// chunks: 'all',
// minSize: 30000,
// minChunks: 1,
// maxAsyncRequests: 5,
// maxInitialRequests: 3,
// name: true,
// cacheGroups: {
// commons: {
// test: /[\\/]node_modules[\\/]/,
// name: 'vendor',
// chunks: 'all',
// },
// main: {
// chunks: 'all',
// minChunks: 2,
// reuseExistingChunk: true,
// enforce: true,
// },
// },
// },
// Keep the runtime chunk seperated to enable long term caching
// https://twitter.com/wSokra/status/969679223278505985
// runtimeChunk: true,
};
}
}
if (IS_DEV) {
config.plugins = [
...config.plugins,
// Use our own FriendlyErrorsPlugin during development.
// new FriendlyErrorsPlugin({
// verbose: dotenv.raw.VERBOSE,
// target,
// onSuccessMessage: `Your application is running at http://${
// dotenv.raw.HOST
// }:${dotenv.raw.PORT}`,
// }),
new WebpackBar({
color: target === 'web' ? '#f56be2' : '#c065f4',
name: target === 'web' ? 'client' : 'server',
}),
];
}
// Apply razzle plugins, if they are present in razzle.config.js
if (Array.isArray(plugins)) {
plugins.forEach(plugin => {
config = runPlugin(
plugin,
config,
{ target, dev: IS_DEV },
webpackObject
);
});
}
// Check if razzle.config has a modify function. If it does, call it on the
// configs we created.
if (modify) {
config = modify(config, { target, dev: IS_DEV }, webpackObject);
}
return config;
};