1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 const fs = require('fs');
8 const path = require('path');
9 const webpack = require('webpack');
10 const resolve = require('resolve');
11 const HtmlWebpackPlugin = require('html-webpack-plugin');
12 const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
13 const InlineChunkHtmlPlugin = require('react-dev-utils/InlineChunkHtmlPlugin');
14 const TerserPlugin = require('terser-webpack-plugin');
15 const MiniCssExtractPlugin = require('mini-css-extract-plugin');
16 const CssMinimizerPlugin = require('css-minimizer-webpack-plugin');
17 const { WebpackManifestPlugin } = require('webpack-manifest-plugin');
18 const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
19 const WorkboxWebpackPlugin = require('workbox-webpack-plugin');
20 const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
21 const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent');
22 const ESLintPlugin = require('eslint-webpack-plugin');
23 const paths = require('./paths');
24 const modules = require('./modules');
25 const getClientEnvironment = require('./env');
26 const ModuleNotFoundPlugin = require('react-dev-utils/ModuleNotFoundPlugin');
27 const ForkTsCheckerWebpackPlugin =
28 process.env.TSC_COMPILE_ON_ERROR === 'true'
29 ? require('react-dev-utils/ForkTsCheckerWarningWebpackPlugin')
30 : require('react-dev-utils/ForkTsCheckerWebpackPlugin');
31 const ReactRefreshWebpackPlugin = require('@pmmmwh/react-refresh-webpack-plugin');
33 const createEnvironmentHash = require('./webpack/persistentCache/createEnvironmentHash');
35 // Source maps are resource heavy and can cause out of memory issue for large source files.
36 const shouldUseSourceMap = process.env.GENERATE_SOURCEMAP !== 'false';
38 const reactRefreshRuntimeEntry = require.resolve('react-refresh/runtime');
39 const reactRefreshWebpackPluginRuntimeEntry = require.resolve(
40 '@pmmmwh/react-refresh-webpack-plugin'
42 const babelRuntimeEntry = require.resolve('babel-preset-react-app');
43 const babelRuntimeEntryHelpers = require.resolve(
44 '@babel/runtime/helpers/esm/assertThisInitialized',
45 { paths: [babelRuntimeEntry] }
47 const babelRuntimeRegenerator = require.resolve('@babel/runtime/regenerator', {
48 paths: [babelRuntimeEntry],
51 // Some apps do not need the benefits of saving a web request, so not inlining the chunk
52 // makes for a smoother build process.
53 const shouldInlineRuntimeChunk = process.env.INLINE_RUNTIME_CHUNK !== 'false';
55 const emitErrorsAsWarnings = process.env.ESLINT_NO_DEV_ERRORS === 'true';
56 const disableESLintPlugin = process.env.DISABLE_ESLINT_PLUGIN === 'true';
58 const imageInlineSizeLimit = parseInt(
59 process.env.IMAGE_INLINE_SIZE_LIMIT || '10000'
62 // Check if TypeScript is setup
63 const useTypeScript = fs.existsSync(paths.appTsConfig);
65 // Check if Tailwind config exists
66 const useTailwind = fs.existsSync(
67 path.join(paths.appPath, 'tailwind.config.js')
70 // Get the path to the uncompiled service worker (if it exists).
71 const swSrc = paths.swSrc;
73 // style files regexes
74 const cssRegex = /\.css$/;
75 const cssModuleRegex = /\.module\.css$/;
76 const sassRegex = /\.(scss|sass)$/;
77 const sassModuleRegex = /\.module\.(scss|sass)$/;
79 const hasJsxRuntime = (() => {
80 if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
85 require.resolve('react/jsx-runtime');
92 // This is the production and development configuration.
93 // It is focused on developer experience, fast rebuilds, and a minimal bundle.
94 module.exports = function (webpackEnv) {
95 const isEnvDevelopment = webpackEnv === 'development';
96 const isEnvProduction = webpackEnv === 'production';
98 // Variable used for enabling profiling in Production
99 // passed into alias object. Uses a flag if passed into the build command
100 const isEnvProductionProfile =
101 isEnvProduction && process.argv.includes('--profile');
103 // We will provide `paths.publicUrlOrPath` to our app
104 // as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
105 // Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
106 // Get environment variables to inject into our app.
107 const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
109 const shouldUseReactRefresh = env.raw.FAST_REFRESH;
111 // common function to get style loaders
112 const getStyleLoaders = (cssOptions, preProcessor) => {
114 isEnvDevelopment && require.resolve('style-loader'),
116 loader: MiniCssExtractPlugin.loader,
117 // css is located in `static/css`, use '../../' to locate index.html folder
118 // in production `paths.publicUrlOrPath` can be a relative path
119 options: paths.publicUrlOrPath.startsWith('.')
120 ? { publicPath: '../../' }
124 loader: require.resolve('css-loader'),
128 // Options for PostCSS as we reference these options twice
129 // Adds vendor prefixing based on your specified browser support in
131 loader: require.resolve('postcss-loader'),
134 // Necessary for external CSS imports to work
135 // https://github.com/facebook/create-react-app/issues/2677
138 plugins: !useTailwind
140 'postcss-flexbugs-fixes',
142 'postcss-preset-env',
150 // Adds PostCSS Normalize as the reset css with default options,
151 // so that it honors browserslist config in package.json
152 // which in turn let's users customize the target behavior as per their needs.
157 'postcss-flexbugs-fixes',
159 'postcss-preset-env',
169 sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
176 loader: require.resolve('resolve-url-loader'),
178 sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
183 loader: require.resolve(preProcessor),
194 target: ['browserslist'],
195 mode: isEnvProduction ? 'production' : isEnvDevelopment && 'development',
196 // Stop compilation early in production
197 bail: isEnvProduction,
198 devtool: isEnvProduction
202 : isEnvDevelopment && 'cheap-module-source-map',
203 // These are the "entry points" to our application.
204 // This means they will be the "root" imports that are included in JS bundle.
205 entry: paths.appIndexJs,
208 path: paths.appBuild,
209 // Add /* filename */ comments to generated require()s in the output.
210 pathinfo: isEnvDevelopment,
211 // There will be one main bundle, and one file per asynchronous chunk.
212 // In development, it does not produce real files.
213 filename: isEnvProduction
214 ? 'static/js/[name].[contenthash:8].js'
215 : isEnvDevelopment && 'static/js/bundle.js',
216 // There are also additional JS chunk files if you use code splitting.
217 chunkFilename: isEnvProduction
218 ? 'static/js/[name].[contenthash:8].chunk.js'
219 : isEnvDevelopment && 'static/js/[name].chunk.js',
220 assetModuleFilename: 'static/media/[name].[hash][ext]',
221 // webpack uses `publicPath` to determine where the app is being served from.
222 // It requires a trailing slash, or the file assets will get an incorrect path.
223 // We inferred the "public path" (such as / or /my-project) from homepage.
224 publicPath: paths.publicUrlOrPath,
225 // Point sourcemap entries to original disk location (format as URL on Windows)
226 devtoolModuleFilenameTemplate: isEnvProduction
229 .relative(paths.appSrc, info.absoluteResourcePath)
231 : isEnvDevelopment &&
232 (info => path.resolve(info.absoluteResourcePath).replace(/\\/g, '/')),
236 version: createEnvironmentHash(env.raw),
237 cacheDirectory: paths.appWebpackCache,
240 defaultWebpack: ['webpack/lib/'],
241 config: [__filename],
242 tsconfig: [paths.appTsConfig, paths.appJsConfig].filter(f =>
247 infrastructureLogging: {
251 minimize: isEnvProduction,
253 // This is only used in production mode
257 // We want terser to parse ecma 8 code. However, we don't want it
258 // to apply any minification steps that turns valid ecma 5 code
259 // into invalid ecma 5 code. This is why the 'compress' and 'output'
260 // sections only apply transformations that are ecma 5 safe
261 // https://github.com/facebook/create-react-app/pull/4234
267 // Disabled because of an issue with Uglify breaking seemingly valid code:
268 // https://github.com/facebook/create-react-app/issues/2376
269 // Pending further investigation:
270 // https://github.com/mishoo/UglifyJS2/issues/2011
272 // Disabled because of an issue with Terser breaking valid code:
273 // https://github.com/facebook/create-react-app/issues/5250
274 // Pending further investigation:
275 // https://github.com/terser-js/terser/issues/120
281 // Added for profiling in devtools
282 keep_classnames: isEnvProductionProfile,
283 keep_fnames: isEnvProductionProfile,
287 // Turned on because emoji and regex is not minified properly using default
288 // https://github.com/facebook/create-react-app/issues/2488
293 // This is only used in production mode
294 new CssMinimizerPlugin(),
298 // This allows you to set a fallback for where webpack should look for modules.
299 // We placed these paths second because we want `node_modules` to "win"
300 // if there are any conflicts. This matches Node resolution mechanism.
301 // https://github.com/facebook/create-react-app/issues/253
302 modules: ['node_modules', paths.appNodeModules].concat(
303 modules.additionalModulePaths || []
305 fallback: { "path": require.resolve("path-browserify") },
306 // These are the reasonable defaults supported by the Node ecosystem.
307 // We also include JSX as a common component filename extension to support
308 // some tools, although we do not recommend using it, see:
309 // https://github.com/facebook/create-react-app/issues/290
310 // `web` extension prefixes have been added for better support
311 // for React Native Web.
312 extensions: paths.moduleFileExtensions
313 .map(ext => `.${ext}`)
314 .filter(ext => useTypeScript || !ext.includes('ts')),
316 // Support React Native Web
317 // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
318 'react-native': 'react-native-web',
319 // Allows for better profiling with ReactDevTools
320 ...(isEnvProductionProfile && {
321 'react-dom$': 'react-dom/profiling',
322 'scheduler/tracing': 'scheduler/tracing-profiling',
324 ...(modules.webpackAliases || {}),
327 // Prevents users from importing files from outside of src/ (or node_modules/).
328 // This often causes confusion because we only process files within src/ with babel.
329 // To fix this, we prevent you from importing files out of src/ -- if you'd like to,
330 // please link the files into your node_modules/ and let module-resolution kick in.
331 // Make sure your source files are compiled, as they will not be processed in any way.
332 new ModuleScopePlugin(paths.appSrc, [
333 paths.appPackageJson,
334 reactRefreshRuntimeEntry,
335 reactRefreshWebpackPluginRuntimeEntry,
337 babelRuntimeEntryHelpers,
338 babelRuntimeRegenerator,
343 strictExportPresence: true,
345 // Handle node_modules packages that contain sourcemaps
346 shouldUseSourceMap && {
348 exclude: /@babel(?:\/|\\{1,2})runtime/,
349 test: /\.(js|mjs|jsx|ts|tsx|css)$/,
350 loader: require.resolve('source-map-loader'),
353 // "oneOf" will traverse all following loaders until one will
354 // match the requirements. When no loader matches it will fall
355 // back to the "file" loader at the end of the loader list.
357 // TODO: Merge this config once `image/avif` is in the mime-db
358 // https://github.com/jshttp/mime-db
362 mimetype: 'image/avif',
365 maxSize: imageInlineSizeLimit,
369 // "url" loader works like "file" loader except that it embeds assets
370 // smaller than specified limit in bytes as data URLs to avoid requests.
371 // A missing `test` is equivalent to a match.
373 test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
377 maxSize: imageInlineSizeLimit,
385 loader: require.resolve('@svgr/webpack'),
390 plugins: [{ removeViewBox: false }],
397 loader: require.resolve('file-loader'),
399 name: 'static/media/[name].[hash].[ext]',
404 and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
407 // Process application JS with Babel.
408 // The preset includes JSX, Flow, TypeScript, and some ESnext features.
410 test: /\.(js|mjs|jsx|ts|tsx)$/,
411 include: paths.appSrc,
412 loader: require.resolve('babel-loader'),
414 customize: require.resolve(
415 'babel-preset-react-app/webpack-overrides'
419 require.resolve('babel-preset-react-app'),
421 runtime: hasJsxRuntime ? 'automatic' : 'classic',
428 shouldUseReactRefresh &&
429 require.resolve('react-refresh/babel'),
431 // This is a feature of `babel-loader` for webpack (not Babel itself).
432 // It enables caching results in ./node_modules/.cache/babel-loader/
433 // directory for faster rebuilds.
434 cacheDirectory: true,
435 // See #6846 for context on why cacheCompression is disabled
436 cacheCompression: false,
437 compact: isEnvProduction,
440 // Process any JS outside of the app with Babel.
441 // Unlike the application JS, we only compile the standard ES features.
444 exclude: /@babel(?:\/|\\{1,2})runtime/,
445 loader: require.resolve('babel-loader'),
452 require.resolve('babel-preset-react-app/dependencies'),
456 cacheDirectory: true,
457 // See #6846 for context on why cacheCompression is disabled
458 cacheCompression: false,
460 // Babel sourcemaps are needed for debugging into node_modules
461 // code. Without the options below, debuggers like VSCode
462 // show incorrect code and set breakpoints on the wrong lines.
463 sourceMaps: shouldUseSourceMap,
464 inputSourceMap: shouldUseSourceMap,
467 // "postcss" loader applies autoprefixer to our CSS.
468 // "css" loader resolves paths in CSS and adds assets as dependencies.
469 // "style" loader turns CSS into JS modules that inject <style> tags.
470 // In production, we use MiniCSSExtractPlugin to extract that CSS
471 // to a file, but in development "style" loader enables hot editing
473 // By default we support CSS Modules with the extension .module.css
476 exclude: cssModuleRegex,
477 use: getStyleLoaders({
479 sourceMap: isEnvProduction
486 // Don't consider CSS imports dead code even if the
487 // containing package claims to have no side effects.
488 // Remove this when webpack adds a warning or an error for this.
489 // See https://github.com/webpack/webpack/issues/6571
492 // Adds support for CSS Modules (https://github.com/css-modules/css-modules)
493 // using the extension .module.css
495 test: cssModuleRegex,
496 use: getStyleLoaders({
498 sourceMap: isEnvProduction
503 getLocalIdent: getCSSModuleLocalIdent,
507 // Opt-in support for SASS (using .scss or .sass extensions).
508 // By default we support SASS Modules with the
509 // extensions .module.scss or .module.sass
512 exclude: sassModuleRegex,
513 use: getStyleLoaders(
516 sourceMap: isEnvProduction
525 // Don't consider CSS imports dead code even if the
526 // containing package claims to have no side effects.
527 // Remove this when webpack adds a warning or an error for this.
528 // See https://github.com/webpack/webpack/issues/6571
531 // Adds support for CSS Modules, but using SASS
532 // using the extension .module.scss or .module.sass
534 test: sassModuleRegex,
535 use: getStyleLoaders(
538 sourceMap: isEnvProduction
543 getLocalIdent: getCSSModuleLocalIdent,
549 // "file" loader makes sure those assets get served by WebpackDevServer.
550 // When you `import` an asset, you get its (virtual) filename.
551 // In production, they would get copied to the `build` folder.
552 // This loader doesn't use a "test" so it will catch all modules
553 // that fall through the other loaders.
555 // Exclude `js` files to keep "css" loader working as it injects
556 // its runtime that would otherwise be processed through "file" loader.
557 // Also exclude `html` and `json` extensions so they get processed
558 // by webpacks internal loaders.
559 exclude: [/^$/, /\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
560 type: 'asset/resource',
562 // ** STOP ** Are you adding a new loader?
563 // Make sure to add the new loader(s) before the "file" loader.
569 // Generates an `index.html` file with the <script> injected.
570 new HtmlWebpackPlugin(
575 template: paths.appHtml,
580 removeComments: true,
581 collapseWhitespace: true,
582 removeRedundantAttributes: true,
583 useShortDoctype: true,
584 removeEmptyAttributes: true,
585 removeStyleLinkTypeAttributes: true,
586 keepClosingSlash: true,
595 // Inlines the webpack runtime script. This script is too small to warrant
596 // a network request.
597 // https://github.com/facebook/create-react-app/issues/5358
599 shouldInlineRuntimeChunk &&
600 new InlineChunkHtmlPlugin(HtmlWebpackPlugin, [/runtime-.+[.]js/]),
601 // Makes some environment variables available in index.html.
602 // The public URL is available as %PUBLIC_URL% in index.html, e.g.:
603 // <link rel="icon" href="%PUBLIC_URL%/favicon.ico">
604 // It will be an empty string unless you specify "homepage"
605 // in `package.json`, in which case it will be the pathname of that URL.
606 new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
607 // This gives some necessary context to module not found errors, such as
608 // the requesting resource.
609 new ModuleNotFoundPlugin(paths.appPath),
610 // Makes some environment variables available to the JS code, for example:
611 // if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
612 // It is absolutely essential that NODE_ENV is set to production
613 // during a production build.
614 // Otherwise React will be compiled in the very slow development mode.
615 new webpack.DefinePlugin(env.stringified),
616 // Experimental hot reloading for React .
617 // https://github.com/facebook/react/tree/main/packages/react-refresh
619 shouldUseReactRefresh &&
620 new ReactRefreshWebpackPlugin({
623 // Watcher doesn't work well if you mistype casing in a path so we use
624 // a plugin that prints an error when you attempt to do this.
625 // See https://github.com/facebook/create-react-app/issues/240
626 isEnvDevelopment && new CaseSensitivePathsPlugin(),
628 new MiniCssExtractPlugin({
629 // Options similar to the same options in webpackOptions.output
630 // both options are optional
631 filename: 'static/css/[name].[contenthash:8].css',
632 chunkFilename: 'static/css/[name].[contenthash:8].chunk.css',
634 // Generate an asset manifest file with the following content:
635 // - "files" key: Mapping of all asset filenames to their corresponding
636 // output file so that tools can pick it up without having to parse
638 // - "entrypoints" key: Array of files which are included in `index.html`,
639 // can be used to reconstruct the HTML if necessary
640 new WebpackManifestPlugin({
641 fileName: 'asset-manifest.json',
642 publicPath: paths.publicUrlOrPath,
643 generate: (seed, files, entrypoints) => {
644 const manifestFiles = files.reduce((manifest, file) => {
645 manifest[file.name] = file.path;
648 const entrypointFiles = entrypoints.main.filter(
649 fileName => !fileName.endsWith('.map')
653 files: manifestFiles,
654 entrypoints: entrypointFiles,
658 // Moment.js is an extremely popular library that bundles large locale files
659 // by default due to how webpack interprets its code. This is a practical
660 // solution that requires the user to opt into importing specific locales.
661 // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
662 // You can remove this if you don't use Moment.js:
663 new webpack.IgnorePlugin({
664 resourceRegExp: /^\.\/locale$/,
665 contextRegExp: /moment$/,
667 // Generate a service worker script that will precache, and keep up to date,
668 // the HTML & assets that are part of the webpack build.
670 fs.existsSync(swSrc) &&
671 new WorkboxWebpackPlugin.InjectManifest({
673 dontCacheBustURLsMatching: /\.[0-9a-f]{8}\./,
674 exclude: [/\.map$/, /asset-manifest\.json$/, /LICENSE/],
675 // Bump up the default maximum size (2mb) that's precached,
676 // to make lazy-loading failure scenarios less likely.
677 // See https://github.com/cra-template/pwa/issues/13#issuecomment-722667270
678 maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
680 // TypeScript type checking
682 new ForkTsCheckerWebpackPlugin({
683 async: isEnvDevelopment,
685 typescriptPath: resolve.sync('typescript', {
686 basedir: paths.appNodeModules,
690 sourceMap: isEnvProduction
694 inlineSourceMap: false,
695 declarationMap: false,
698 tsBuildInfoFile: paths.appTsBuildInfoFile,
701 context: paths.appPath,
705 mode: 'write-references',
709 // This one is specifically to match during CI tests,
710 // as micromatch doesn't match
711 // '../cra-template-typescript/template/src/App.tsx'
714 { file: '../**/src/**/*.{ts,tsx}' },
715 { file: '**/src/**/*.{ts,tsx}' },
718 { file: '**/src/**/__tests__/**' },
719 { file: '**/src/**/?(*.){spec|test}.*' },
720 { file: '**/src/setupProxy.*' },
721 { file: '**/src/setupTests.*' },
725 infrastructure: 'silent',
728 !disableESLintPlugin &&
731 extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
732 formatter: require.resolve('react-dev-utils/eslintFormatter'),
733 eslintPath: require.resolve('eslint'),
734 failOnError: !(isEnvDevelopment && emitErrorsAsWarnings),
735 context: paths.appSrc,
737 cacheLocation: path.resolve(
738 paths.appNodeModules,
739 '.cache/.eslintcache'
741 // ESLint class options
743 resolvePluginsRelativeTo: __dirname,
745 extends: [require.resolve('eslint-config-react-app/base')],
747 ...(!hasJsxRuntime && {
748 'react/react-in-jsx-scope': 'error',
754 // Turn off performance processing because we utilize
755 // our own hints via the FileSizeReporter