1 | // @remove-on-eject-begin
|
2 | /**
|
3 | * Copyright (c) 2015-present, Facebook, Inc.
|
4 | *
|
5 | * This source code is licensed under the MIT license found in the
|
6 | * LICENSE file in the root directory of this source tree.
|
7 | */
|
8 | // @remove-on-eject-end
|
9 | ;
|
10 |
|
11 | const fs = require('fs');
|
12 | const path = require('path');
|
13 | const webpack = require('webpack');
|
14 | const resolve = require('resolve');
|
15 | const PnpWebpackPlugin = require('pnp-webpack-plugin');
|
16 | const HtmlWebpackPlugin = require('html-webpack-plugin');
|
17 | const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
|
18 | const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
|
19 | const TerserPlugin = require('terser-webpack-plugin');
|
20 | const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
21 | const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
|
22 | const safePostCssParser = require('postcss-safe-parser');
|
23 | const ManifestPlugin = require('webpack-manifest-plugin');
|
24 | const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
|
25 | const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
|
26 | const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
|
27 | const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
|
28 | const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
|
29 | const paths = require('./paths');
|
30 | const getClientEnvironment = require('./env');
|
31 | const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
|
32 | const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin-alt');
|
33 | const typescriptFormatter = require('react-dev-utils/typescriptFormatter');
|
34 | // @remove-on-eject-begin
|
35 | const getCacheIdentifier = require('react-dev-utils/getCacheIdentifier');
|
36 | // @remove-on-eject-end
|
37 |
|
38 | // Source maps are resource heavy and can cause out of memory issue for large source files.
|
39 | const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
|
40 | // Some apps do not need the benefits of saving a web request, so not inlining the chunk
|
41 | // makes for a smoother build process.
|
42 | const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
|
43 |
|
44 | // Check if TypeScript is setup
|
45 | const useTypeScript = fs.existsSync(paths.appTsConfig);
|
46 |
|
47 | // style files regexes
|
48 | const cssRegex = /\.css$/;
|
49 | const cssModuleRegex = /\.module\.css$/;
|
50 | const sassRegex = /\.(scss|sass)$/;
|
51 | const sassModuleRegex = /\.module\.(scss|sass)$/;
|
52 |
|
53 | // This is the production and development configuration.
|
54 | // It is focused on developer experience, fast rebuilds, and a minimal bundle.
|
55 | module.exports = function(webpackEnv) {
|
56 | const isEnvDevelopment = webpackEnv === 'development';
|
57 | const isEnvProduction = webpackEnv === 'production';
|
58 |
|
59 | // Webpack uses `publicPath` to determine where the app is being served from.
|
60 | // It requires a trailing slash, or the file assets will get an incorrect path.
|
61 | // In development, we always serve from the root. This makes config easier.
|
62 | const publicPath = isEnvProduction
|
63 | ? paths.servedPath
|
64 | : isEnvDevelopment && '/';
|
65 | // Some apps do not use client-side routing with pushState.
|
66 | // For these, "homepage" can be set to "." to enable relative asset paths.
|
67 | const shouldUseRelativeAssetPaths = publicPath === './';
|
68 |
|
69 | // `publicUrl` is just like `publicPath`, but we will provide it to our app
|
70 | // as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
|
71 | // Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
|
72 | const publicUrl = isEnvProduction
|
73 | ? publicPath.slice(0, -1)
|
74 | : isEnvDevelopment && '';
|
75 | // Get environment variables to inject into our app.
|
76 | const env = getClientEnvironment(publicUrl);
|
77 |
|
78 | // common function to get style loaders
|
79 | const getStyleLoaders = (cssOptions, preProcessor) => {
|
80 | const loaders = [
|
81 | isEnvDevelopment && require.resolve('style-loader'),
|
82 | isEnvProduction && {
|
83 | loader: MiniCssExtractPlugin.loader,
|
84 | options: Object.assign(
|
85 | {},
|
86 | shouldUseRelativeAssetPaths ? { publicPath: '../../' } : undefined
|
87 | ),
|
88 | },
|
89 | {
|
90 | loader: require.resolve('css-loader'),
|
91 | options: cssOptions,
|
92 | },
|
93 | {
|
94 | // Options for PostCSS as we reference these options twice
|
95 | // Adds vendor prefixing based on your specified browser support in
|
96 | // package.json
|
97 | loader: require.resolve('postcss-loader'),
|
98 | options: {
|
99 | // Necessary for external CSS imports to work
|
100 | // https://github.com/facebook/create-react-app/issues/2677
|
101 | ident: 'postcss',
|
102 | plugins: () => [
|
103 | require('postcss-flexbugs-fixes'),
|
104 | require('postcss-preset-env')({
|
105 | autoprefixer: {
|
106 | flexbox: 'no-2009',
|
107 | },
|
108 | stage: 3,
|
109 | }),
|
110 | ],
|
111 | sourceMap: isEnvProduction && shouldUseSourceMap,
|
112 | },
|
113 | },
|
114 | ].filter(Boolean);
|
115 | if (preProcessor) {
|
116 | loaders.push({
|
117 | loader: require.resolve(preProcessor),
|
118 | options: {
|
119 | sourceMap: isEnvProduction && shouldUseSourceMap,
|
120 | },
|
121 | });
|
122 | }
|
123 | return loaders;
|
124 | };
|
125 |
|
126 | return {
|
127 | mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
|
128 | // Stop compilation early in production
|
129 | bail: isEnvProduction,
|
130 | devtool: isEnvProduction
|
131 | ? shouldUseSourceMap
|
132 | ? 'source-map'
|
133 | : false
|
134 | : isEnvDevelopment && 'cheap-module-source-map',
|
135 | // These are the "entry points" to our application.
|
136 | // This means they will be the "root" imports that are included in JS bundle.
|
137 | entry: [
|
138 | // Include an alternative client for WebpackDevServer. A client's job is to
|
139 | // connect to WebpackDevServer by a socket and get notified about changes.
|
140 | // When you save a file, the client will either apply hot updates (in case
|
141 | // of CSS changes), or refresh the page (in case of JS changes). When you
|
142 | // make a syntax error, this client will display a syntax error overlay.
|
143 | // Note: instead of the default WebpackDevServer client, we use a custom one
|
144 | // to bring better experience for Create React App users. You can replace
|
145 | // the line below with these two lines if you prefer the stock client:
|
146 | // require.resolve('webpack-dev-server/client') + '?/',
|
147 | // require.resolve('webpack/hot/dev-server'),
|
148 | isEnvDevelopment &&
|
149 | require.resolve('react-dev-utils/webpackHotDevClient'),
|
150 | // Finally, this is your app's code:
|
151 | paths.appIndexJs,
|
152 | // We include the app code last so that if there is a runtime error during
|
153 | // initialization, it doesn't blow up the WebpackDevServer client, and
|
154 | // changing JS code would still trigger a refresh.
|
155 | ].filter(Boolean),
|
156 | output: {
|
157 | // The build folder.
|
158 | path: isEnvProduction ? paths.appBuild : undefined,
|
159 | // Add /* filename */ comments to generated require()s in the output.
|
160 | pathinfo: isEnvDevelopment,
|
161 | // There will be one main bundle, and one file per asynchronous chunk.
|
162 | // In development, it does not produce real files.
|
163 | filename: isEnvProduction
|
164 | ? 'static/js/[name].[chunkhash:8].js'
|
165 | : isEnvDevelopment && 'static/js/bundle.js',
|
166 | // There are also additional JS chunk files if you use code splitting.
|
167 | chunkFilename: isEnvProduction
|
168 | ? 'static/js/[name].[chunkhash:8].chunk.js'
|
169 | : isEnvDevelopment && 'static/js/[name].chunk.js',
|
170 | // We inferred the "public path" (such as / or /my-project) from homepage.
|
171 | // We use "/" in development.
|
172 | publicPath: publicPath,
|
173 | // Point sourcemap entries to original disk location (format as URL on Windows)
|
174 | devtoolModuleFilenameTemplate: isEnvProduction
|
175 | ? info =>
|
176 | path
|
177 | .relative(paths.appSrc, info.absoluteResourcePath)
|
178 | .replace(/\\/g, '/')
|
179 | : isEnvDevelopment &&
|
180 | (info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
|
181 | },
|
182 | optimization: {
|
183 | minimize: isEnvProduction,
|
184 | minimizer: [
|
185 | // This is only used in production mode
|
186 | new TerserPlugin({
|
187 | terserOptions: {
|
188 | parse: {
|
189 | // we want terser to parse ecma 8 code. However, we don't want it
|
190 | // to apply any minfication steps that turns valid ecma 5 code
|
191 | // into invalid ecma 5 code. This is why the 'compress' and 'output'
|
192 | // sections only apply transformations that are ecma 5 safe
|
193 | // https://github.com/facebook/create-react-app/pull/4234
|
194 | ecma: 8,
|
195 | },
|
196 | compress: {
|
197 | ecma: 5,
|
198 | warnings: false,
|
199 | // Disabled because of an issue with Uglify breaking seemingly valid code:
|
200 | // https://github.com/facebook/create-react-app/issues/2376
|
201 | // Pending further investigation:
|
202 | // https://github.com/mishoo/UglifyJS2/issues/2011
|
203 | comparisons: false,
|
204 | // Disabled because of an issue with Terser breaking valid code:
|
205 | // https://github.com/facebook/create-react-app/issues/5250
|
206 | // Pending futher investigation:
|
207 | // https://github.com/terser-js/terser/issues/120
|
208 | inline: 2,
|
209 | },
|
210 | mangle: {
|
211 | safari10: true,
|
212 | },
|
213 | output: {
|
214 | ecma: 5,
|
215 | comments: false,
|
216 | // Turned on because emoji and regex is not minified properly using default
|
217 | // https://github.com/facebook/create-react-app/issues/2488
|
218 | ascii_only: true,
|
219 | },
|
220 | },
|
221 | // Use multi-process parallel running to improve the build speed
|
222 | // Default number of concurrent runs: os.cpus().length - 1
|
223 | parallel: true,
|
224 | // Enable file caching
|
225 | cache: true,
|
226 | sourceMap: shouldUseSourceMap,
|
227 | }),
|
228 | // This is only used in production mode
|
229 | new OptimizeCSSAssetsPlugin({
|
230 | cssProcessorOptions: {
|
231 | parser: safePostCssParser,
|
232 | map: shouldUseSourceMap
|
233 | ? {
|
234 | // `inline: false` forces the sourcemap to be output into a
|
235 | // separate file
|
236 | inline: false,
|
237 | // `annotation: true` appends the sourceMappingURL to the end of
|
238 | // the css file, helping the browser find the sourcemap
|
239 | annotation: true,
|
240 | }
|
241 | : false,
|
242 | },
|
243 | }),
|
244 | ],
|
245 | // Automatically split vendor and commons
|
246 | // https://twitter.com/wSokra/status/969633336732905474
|
247 | // https://medium.com/webpack/webpack-4-code-splitting-chunk-graph-and-the-splitchunks-optimization-be739a861366
|
248 | splitChunks: {
|
249 | chunks: 'all',
|
250 | name: false,
|
251 | },
|
252 | // Keep the runtime chunk seperated to enable long term caching
|
253 | // https://twitter.com/wSokra/status/969679223278505985
|
254 | runtimeChunk: true,
|
255 | },
|
256 | resolve: {
|
257 | // This allows you to set a fallback for where Webpack should look for modules.
|
258 | // We placed these paths second because we want `node_modules` to "win"
|
259 | // if there are any conflicts. This matches Node resolution mechanism.
|
260 | // https://github.com/facebook/create-react-app/issues/253
|
261 | modules: ['node_modules'].concat(
|
262 | // It is guaranteed to exist because we tweak it in `env.js`
|
263 | process.env.NODE_PATH.split(path.delimiter).filter(Boolean)
|
264 | ),
|
265 | // These are the reasonable defaults supported by the Node ecosystem.
|
266 | // We also include JSX as a common component filename extension to support
|
267 | // some tools, although we do not recommend using it, see:
|
268 | // https://github.com/facebook/create-react-app/issues/290
|
269 | // `web` extension prefixes have been added for better support
|
270 | // for React Native Web.
|
271 | extensions: paths.moduleFileExtensions
|
272 | .map(ext => `.${ext}`)
|
273 | .filter(ext => useTypeScript || !ext.includes('ts')),
|
274 | alias: {
|
275 | // Support React Native Web
|
276 | // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
|
277 | 'react-native': 'react-native-web',
|
278 | },
|
279 | plugins: [
|
280 | // Adds support for installing with Plug'n'Play, leading to faster installs and adding
|
281 | // guards against forgotten dependencies and such.
|
282 | PnpWebpackPlugin,
|
283 | // Prevents users from importing files from outside of src/ (or node_modules/).
|
284 | // This often causes confusion because we only process files within src/ with babel.
|
285 | // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
|
286 | // please link the files into your node_modules/ and let module-resolution kick in.
|
287 | // Make sure your source files are compiled, as they will not be processed in any way.
|
288 | new ModuleScopePlugin(paths.appSrc, [paths.appPackageJson]),
|
289 | ],
|
290 | },
|
291 | resolveLoader: {
|
292 | plugins: [
|
293 | // Also related to Plug'n'Play, but this time it tells Webpack to load its loaders
|
294 | // from the current package.
|
295 | PnpWebpackPlugin.moduleLoader(module),
|
296 | ],
|
297 | },
|
298 | module: {
|
299 | strictExportPresence: true,
|
300 | rules: [
|
301 | // Disable require.ensure as it's not a standard language feature.
|
302 | { parser: { requireEnsure: false } },
|
303 |
|
304 | // First, run the linter.
|
305 | // It's important to do this before Babel processes the JS.
|
306 | {
|
307 | test: /\.(js|mjs|jsx)$/,
|
308 | enforce: 'pre',
|
309 | use: [
|
310 | {
|
311 | options: {
|
312 | formatter: require.resolve('react-dev-utils/eslintFormatter'),
|
313 | eslintPath: require.resolve('eslint'),
|
314 | // @remove-on-eject-begin
|
315 | baseConfig: {
|
316 | extends: [require.resolve('eslint-config-react-app')],
|
317 | settings: { react: { version: '999.999.999' } },
|
318 | },
|
319 | ignore: false,
|
320 | useEslintrc: false,
|
321 | // @remove-on-eject-end
|
322 | },
|
323 | loader: require.resolve('eslint-loader'),
|
324 | },
|
325 | ],
|
326 | include: paths.appSrc,
|
327 | },
|
328 | {
|
329 | // "oneOf" will traverse all following loaders until one will
|
330 | // match the requirements. When no loader matches it will fall
|
331 | // back to the "file" loader at the end of the loader list.
|
332 | oneOf: [
|
333 | // "url" loader works like "file" loader except that it embeds assets
|
334 | // smaller than specified limit in bytes as data URLs to avoid requests.
|
335 | // A missing `test` is equivalent to a match.
|
336 | {
|
337 | test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
|
338 | loader: require.resolve('url-loader'),
|
339 | options: {
|
340 | limit: 10000,
|
341 | name: 'static/media/[name].[hash:8].[ext]',
|
342 | },
|
343 | },
|
344 | // Process application JS with Babel.
|
345 | // The preset includes JSX, Flow, TypeScript, and some ESnext features.
|
346 | {
|
347 | test: /\.(js|mjs|jsx|ts|tsx)$/,
|
348 | include: paths.appSrc,
|
349 | loader: require.resolve('babel-loader'),
|
350 | options: {
|
351 | customize: require.resolve(
|
352 | 'babel-preset-react-app/webpack-overrides'
|
353 | ),
|
354 | // @remove-on-eject-begin
|
355 | babelrc: false,
|
356 | configFile: false,
|
357 | presets: [require.resolve('babel-preset-react-app')],
|
358 | // Make sure we have a unique cache identifier, erring on the
|
359 | // side of caution.
|
360 | // We remove this when the user ejects because the default
|
361 | // is sane and uses Babel options. Instead of options, we use
|
362 | // the react-scripts and babel-preset-react-app versions.
|
363 | cacheIdentifier: getCacheIdentifier(
|
364 | isEnvProduction
|
365 | ? 'production'
|
366 | : isEnvDevelopment && 'development',
|
367 | [
|
368 | 'babel-plugin-named-asset-import',
|
369 | 'babel-preset-react-app',
|
370 | 'react-dev-utils',
|
371 | 'react-scripts',
|
372 | ]
|
373 | ),
|
374 | // @remove-on-eject-end
|
375 | plugins: [
|
376 | [
|
377 | require.resolve('babel-plugin-named-asset-import'),
|
378 | {
|
379 | loaderMap: {
|
380 | svg: {
|
381 | ReactComponent:
|
382 | '@svgr/webpack?-prettier,-svgo![path]',
|
383 | },
|
384 | },
|
385 | },
|
386 | ],
|
387 | ],
|
388 | // This is a feature of `babel-loader` for webpack (not Babel itself).
|
389 | // It enables caching results in ./node_modules/.cache/babel-loader/
|
390 | // directory for faster rebuilds.
|
391 | cacheDirectory: true,
|
392 | cacheCompression: isEnvProduction,
|
393 | compact: isEnvProduction,
|
394 | },
|
395 | },
|
396 | // Process any JS outside of the app with Babel.
|
397 | // Unlike the application JS, we only compile the standard ES features.
|
398 | {
|
399 | test: /\.(js|mjs)$/,
|
400 | exclude: /@babel(?:\/|\\{1,2})runtime/,
|
401 | loader: require.resolve('babel-loader'),
|
402 | options: {
|
403 | babelrc: false,
|
404 | configFile: false,
|
405 | compact: false,
|
406 | presets: [
|
407 | [
|
408 | require.resolve('babel-preset-react-app/dependencies'),
|
409 | { helpers: true },
|
410 | ],
|
411 | ],
|
412 | cacheDirectory: true,
|
413 | cacheCompression: isEnvProduction,
|
414 | // @remove-on-eject-begin
|
415 | cacheIdentifier: getCacheIdentifier(
|
416 | isEnvProduction
|
417 | ? 'production'
|
418 | : isEnvDevelopment && 'development',
|
419 | [
|
420 | 'babel-plugin-named-asset-import',
|
421 | 'babel-preset-react-app',
|
422 | 'react-dev-utils',
|
423 | 'react-scripts',
|
424 | ]
|
425 | ),
|
426 | // @remove-on-eject-end
|
427 | // If an error happens in a package, it's possible to be
|
428 | // because it was compiled. Thus, we don't want the browser
|
429 | // debugger to show the original code. Instead, the code
|
430 | // being evaluated would be much more helpful.
|
431 | sourceMaps: false,
|
432 | },
|
433 | },
|
434 | // "postcss" loader applies autoprefixer to our CSS.
|
435 | // "css" loader resolves paths in CSS and adds assets as dependencies.
|
436 | // "style" loader turns CSS into JS modules that inject <style> tags.
|
437 | // In production, we use MiniCSSExtractPlugin to extract that CSS
|
438 | // to a file, but in development "style" loader enables hot editing
|
439 | // of CSS.
|
440 | // By default we support CSS Modules with the extension .module.css
|
441 | {
|
442 | test: cssRegex,
|
443 | exclude: cssModuleRegex,
|
444 | use: getStyleLoaders({
|
445 | importLoaders: 1,
|
446 | sourceMap: isEnvProduction && shouldUseSourceMap,
|
447 | }),
|
448 | // Don't consider CSS imports dead code even if the
|
449 | // containing package claims to have no side effects.
|
450 | // Remove this when webpack adds a warning or an error for this.
|
451 | // See https://github.com/webpack/webpack/issues/6571
|
452 | sideEffects: true,
|
453 | },
|
454 | // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
|
455 | // using the extension .module.css
|
456 | {
|
457 | test: cssModuleRegex,
|
458 | use: getStyleLoaders({
|
459 | importLoaders: 1,
|
460 | sourceMap: isEnvProduction && shouldUseSourceMap,
|
461 | modules: true,
|
462 | getLocalIdent: getCSSModuleLocalIdent,
|
463 | }),
|
464 | },
|
465 | // Opt-in support for SASS (using .scss or .sass extensions).
|
466 | // By default we support SASS Modules with the
|
467 | // extensions .module.scss or .module.sass
|
468 | {
|
469 | test: sassRegex,
|
470 | exclude: sassModuleRegex,
|
471 | use: getStyleLoaders(
|
472 | {
|
473 | importLoaders: 2,
|
474 | sourceMap: isEnvProduction && shouldUseSourceMap,
|
475 | },
|
476 | 'sass-loader'
|
477 | ),
|
478 | // Don't consider CSS imports dead code even if the
|
479 | // containing package claims to have no side effects.
|
480 | // Remove this when webpack adds a warning or an error for this.
|
481 | // See https://github.com/webpack/webpack/issues/6571
|
482 | sideEffects: true,
|
483 | },
|
484 | // Adds support for CSS Modules, but using SASS
|
485 | // using the extension .module.scss or .module.sass
|
486 | {
|
487 | test: sassModuleRegex,
|
488 | use: getStyleLoaders(
|
489 | {
|
490 | importLoaders: 2,
|
491 | sourceMap: isEnvProduction && shouldUseSourceMap,
|
492 | modules: true,
|
493 | getLocalIdent: getCSSModuleLocalIdent,
|
494 | },
|
495 | 'sass-loader'
|
496 | ),
|
497 | },
|
498 | // "file" loader makes sure those assets get served by WebpackDevServer.
|
499 | // When you `import` an asset, you get its (virtual) filename.
|
500 | // In production, they would get copied to the `build` folder.
|
501 | // This loader doesn't use a "test" so it will catch all modules
|
502 | // that fall through the other loaders.
|
503 | {
|
504 | loader: require.resolve('file-loader'),
|
505 | // Exclude `js` files to keep "css" loader working as it injects
|
506 | // its runtime that would otherwise be processed through "file" loader.
|
507 | // Also exclude `html` and `json` extensions so they get processed
|
508 | // by webpacks internal loaders.
|
509 | exclude: [/\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
|
510 | options: {
|
511 | name: 'static/media/[name].[hash:8].[ext]',
|
512 | },
|
513 | },
|
514 | // ** STOP ** Are you adding a new loader?
|
515 | // Make sure to add the new loader(s) before the "file" loader.
|
516 | ],
|
517 | },
|
518 | ],
|
519 | },
|
520 | plugins: [
|
521 | // Generates an `index.html` file with the <script> injected.
|
522 | new HtmlWebpackPlugin(
|
523 | Object.assign(
|
524 | {},
|
525 | {
|
526 | inject: true,
|
527 | template: paths.appHtml,
|
528 | },
|
529 | isEnvProduction
|
530 | ? {
|
531 | minify: {
|
532 | removeComments: true,
|
533 | collapseWhitespace: true,
|
534 | removeRedundantAttributes: true,
|
535 | useShortDoctype: true,
|
536 | removeEmptyAttributes: true,
|
537 | removeStyleLinkTypeAttributes: true,
|
538 | keepClosingSlash: true,
|
539 | minifyJS: true,
|
540 | minifyCSS: true,
|
541 | minifyURLs: true,
|
542 | },
|
543 | }
|
544 | : undefined
|
545 | )
|
546 | ),
|
547 | // Inlines the webpack runtime script. This script is too small to warrant
|
548 | // a network request.
|
549 | isEnvProduction &&
|
550 | shouldInlineRuntimeChunk &&
|
551 | new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime~.+[.]js/]),
|
552 | // Makes some environment variables available in index.html.
|
553 | // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
|
554 | // <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
|
555 | // In production, it will be an empty string unless you specify "homepage"
|
556 | // in `package.json`, in which case it will be the pathname of that URL.
|
557 | // In development, this will be an empty string.
|
558 | new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
|
559 | // This gives some necessary context to module not found errors, such as
|
560 | // the requesting resource.
|
561 | new ModuleNotFoundPlugin(paths.appPath),
|
562 | // Makes some environment variables available to the JS code, for example:
|
563 | // if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
|
564 | // It is absolutely essential that NODE_ENV is set to production
|
565 | // during a production build.
|
566 | // Otherwise React will be compiled in the very slow development mode.
|
567 | new webpack.DefinePlugin(env.stringified),
|
568 | // This is necessary to emit hot updates (currently CSS only):
|
569 | isEnvDevelopment && new webpack.HotModuleReplacementPlugin(),
|
570 | // Watcher doesn't work well if you mistype casing in a path so we use
|
571 | // a plugin that prints an error when you attempt to do this.
|
572 | // See https://github.com/facebook/create-react-app/issues/240
|
573 | isEnvDevelopment && new CaseSensitivePathsPlugin(),
|
574 | // If you require a missing module and then `npm install` it, you still have
|
575 | // to restart the development server for Webpack to discover it. This plugin
|
576 | // makes the discovery automatic so you don't have to restart.
|
577 | // See https://github.com/facebook/create-react-app/issues/186
|
578 | isEnvDevelopment &&
|
579 | new WatchMissingNodeModulesPlugin(paths.appNodeModules),
|
580 | isEnvProduction &&
|
581 | new MiniCssExtractPlugin({
|
582 | // Options similar to the same options in webpackOptions.output
|
583 | // both options are optional
|
584 | filename: 'static/css/[name].[contenthash:8].css',
|
585 | chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
|
586 | }),
|
587 | // Generate a manifest file which contains a mapping of all asset filenames
|
588 | // to their corresponding output file so that tools can pick it up without
|
589 | // having to parse `index.html`.
|
590 | new ManifestPlugin({
|
591 | fileName: 'asset-manifest.json',
|
592 | publicPath: publicPath,
|
593 | }),
|
594 | // Moment.js is an extremely popular library that bundles large locale files
|
595 | // by default due to how Webpack interprets its code. This is a practical
|
596 | // solution that requires the user to opt into importing specific locales.
|
597 | // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
|
598 | // You can remove this if you don't use Moment.js:
|
599 | new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
|
600 | // Generate a service worker script that will precache, and keep up to date,
|
601 | // the HTML & assets that are part of the Webpack build.
|
602 | isEnvProduction &&
|
603 | new WorkboxWebpackPlugin.GenerateSW({
|
604 | clientsClaim: true,
|
605 | exclude: [/\.map$/, /asset-manifest\.json$/],
|
606 | importWorkboxFrom: 'cdn',
|
607 | navigateFallback: publicUrl + '/index.html',
|
608 | navigateFallbackBlacklist: [
|
609 | // Exclude URLs starting with /_, as they're likely an API call
|
610 | new RegExp('^/_'),
|
611 | // Exclude URLs containing a dot, as they're likely a resource in
|
612 | // public/ and not a SPA route
|
613 | new RegExp('/[^/]+\\.[^/]+$'),
|
614 | ],
|
615 | }),
|
616 | // TypeScript type checking
|
617 | useTypeScript &&
|
618 | new ForkTsCheckerWebpackPlugin({
|
619 | typescript: resolve.sync('typescript', {
|
620 | basedir: paths.appNodeModules,
|
621 | }),
|
622 | async: false,
|
623 | checkSyntacticErrors: true,
|
624 | tsconfig: paths.appTsConfig,
|
625 | compilerOptions: {
|
626 | module: 'esnext',
|
627 | moduleResolution: 'node',
|
628 | resolveJsonModule: true,
|
629 | isolatedModules: true,
|
630 | noEmit: true,
|
631 | jsx: 'preserve',
|
632 | },
|
633 | reportFiles: [
|
634 | '**',
|
635 | '!**/*.json',
|
636 | '!**/__tests__/**',
|
637 | '!**/?(*.)(spec|test).*',
|
638 | '!**/src/setupProxy.*',
|
639 | '!**/src/setupTests.*',
|
640 | ],
|
641 | watch: paths.appSrc,
|
642 | silent: true,
|
643 | formatter: typescriptFormatter,
|
644 | }),
|
645 | ].filter(Boolean),
|
646 | // Some libraries import Node modules but don't use them in the browser.
|
647 | // Tell Webpack to provide empty mocks for them so importing them works.
|
648 | node: {
|
649 | dgram: 'empty',
|
650 | fs: 'empty',
|
651 | net: 'empty',
|
652 | tls: 'empty',
|
653 | child_process: 'empty',
|
654 | },
|
655 | // Turn off performance processing because we utilize
|
656 | // our own hints via the FileSizeReporter
|
657 | performance: false,
|
658 | };
|
659 | };
|