Skip to content

Commit b2002d3

Browse files
committed
Upgrade to Webpack 5
Needed for future Docker compatibility. Manually applied many of the PRs from facebook/create-react-app#9994 and facebook/create-react-app#7929 (including comments)
1 parent a5c4b13 commit b2002d3

File tree

5 files changed

+3921
-3145
lines changed

5 files changed

+3921
-3145
lines changed

src/ts/config/mocks/dns.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// See https://github.com/webpack/node-libs-browser/blob/214057f118bad5da8b99db33fd16a5b6ceb42d9b/mock/dns.js
2+
3+
'use strict';
4+
5+
module.exports.lookup = module.exports.resolve4 = module.exports.resolve6 = module.exports.resolveCname = module.exports.resolveMx = module.exports.resolveNs = module.exports.resolveTxt = module.exports.resolveSrv = module.exports.resolveNaptr = module.exports.reverse = module.exports.resolve = function() {
6+
if (!arguments.length) {
7+
return;
8+
}
9+
10+
var callback = arguments[arguments.length - 1];
11+
if (callback && typeof callback === 'function') {
12+
callback(null, '0.0.0.0');
13+
}
14+
};

src/ts/config/mocks/empty.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
module.exports = null;

src/ts/config/webpack.config.js

+64-65
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const TerserPlugin = require("terser-webpack-plugin");
1212
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
1313
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
1414
const safePostCssParser = require("postcss-safe-parser");
15-
const ManifestPlugin = require("webpack-manifest-plugin");
15+
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
1616
const InterpolateHtmlPlugin = require("react-dev-utils/InterpolateHtmlPlugin");
1717
const WorkboxWebpackPlugin = require("workbox-webpack-plugin");
1818
const WatchMissingNodeModulesPlugin = require("react-dev-utils/WatchMissingNodeModulesPlugin");
@@ -107,23 +107,25 @@ module.exports = function (webpackEnv) {
107107
// package.json
108108
loader: require.resolve("postcss-loader"),
109109
options: {
110-
// Necessary for external CSS imports to work
111-
// https://github.com/facebook/create-react-app/issues/2677
112-
ident: "postcss",
113-
plugins: () => [
114-
require("postcss-flexbugs-fixes"),
115-
require("postcss-preset-env")({
116-
autoprefixer: {
117-
flexbox: "no-2009",
118-
},
119-
stage: 3,
120-
}),
121-
// Adds PostCSS Normalize as the reset css with default options,
122-
// so that it honors browserslist config in package.json
123-
// which in turn let's users customize the target behavior as per their needs.
124-
postcssNormalize(),
125-
],
126-
sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
110+
postcssOptions: {
111+
plugins: [
112+
require('postcss-flexbugs-fixes'),
113+
[
114+
require('postcss-preset-env'),
115+
{
116+
autoprefixer: {
117+
flexbox: 'no-2009',
118+
},
119+
stage: 3,
120+
},
121+
],
122+
// Adds PostCSS Normalize as the reset css with default options,
123+
// so that it honors browserslist config in package.json
124+
// which in turn let's users customize the target behavior as per their needs.
125+
postcssNormalize(),
126+
],
127+
},
128+
sourceMap: isEnvProduction && shouldUseSourceMap,
127129
},
128130
},
129131
].filter(Boolean);
@@ -167,9 +169,7 @@ module.exports = function (webpackEnv) {
167169
// There will be one main bundle, and one file per asynchronous chunk.
168170
filename: isEnvProduction
169171
? "js/[name].[contenthash:8].js"
170-
: isEnvDevelopment && "js/bundle.js",
171-
// TODO: remove this when upgrading to webpack 5
172-
futureEmitAssets: true,
172+
: isEnvDevelopment && "js/[name].js",
173173
// There are also additional JS chunk files if you use code splitting.
174174
chunkFilename: isEnvProduction
175175
? "js/[name].[contenthash:8].chunk.js"
@@ -187,9 +187,6 @@ module.exports = function (webpackEnv) {
187187
: isEnvDevelopment &&
188188
((info) =>
189189
path.resolve(info.absoluteResourcePath).replace(/\\/g, "/")),
190-
// Prevents conflicts when multiple webpack runtimes (from different apps)
191-
// are used on the same page.
192-
jsonpFunction: `webpackJsonp${appPackageJson.name}`,
193190
// this defaults to 'window', but by setting it to 'this' then
194191
// module chunks which are built will work in web workers as well.
195192
globalObject: "this",
@@ -236,7 +233,6 @@ module.exports = function (webpackEnv) {
236233
ascii_only: true,
237234
},
238235
},
239-
sourceMap: shouldUseSourceMap,
240236
}),
241237
// This is only used in production mode
242238
new OptimizeCSSAssetsPlugin({
@@ -589,7 +585,7 @@ module.exports = function (webpackEnv) {
589585
// `index.html`
590586
// - "entrypoints" key: Array of files which are included in `index.html`,
591587
// can be used to reconstruct the HTML if necessary
592-
new ManifestPlugin({
588+
new WebpackManifestPlugin({
593589
fileName: "asset-manifest.json",
594590
publicPath: paths.publicUrlOrPath,
595591
generate: (seed, files, entrypoints) => {
@@ -612,7 +608,10 @@ module.exports = function (webpackEnv) {
612608
// solution that requires the user to opt into importing specific locales.
613609
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
614610
// You can remove this if you don't use Moment.js:
615-
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
611+
new webpack.IgnorePlugin({
612+
resourceRegExp: /^\.\/locale$/,
613+
contextRegExp: /moment$/,
614+
}),
616615
// Generate a service worker script that will precache, and keep up to date,
617616
// the HTML & assets that are part of the webpack build.
618617
isEnvProduction &&
@@ -628,34 +627,46 @@ module.exports = function (webpackEnv) {
628627
}),
629628
// TypeScript type checking
630629
useTypeScript &&
631-
new ForkTsCheckerWebpackPlugin({
632-
typescript: resolve.sync("typescript", {
630+
new ForkTsCheckerWebpackPlugin({
631+
typescript: {
632+
typescriptPath: resolve.sync('typescript', {
633633
basedir: paths.appNodeModules,
634634
}),
635-
async: false,
636-
checkSyntacticErrors: true,
637-
resolveModuleNameModule: process.versions.pnp
638-
? `${__dirname}/pnpTs.js`
639-
: undefined,
640-
resolveTypeReferenceDirectiveModule: process.versions.pnp
641-
? `${__dirname}/pnpTs.js`
642-
: undefined,
643-
tsconfig: paths.appTsConfig,
644-
reportFiles: [
645-
// This one is specifically to match during CI tests,
646-
// as micromatch doesn't match
647-
// '../cra-template-typescript/template/src/App.tsx'
648-
// otherwise.
649-
"../**/src/**/*.{ts,tsx}",
650-
"**/src/**/*.{ts,tsx}",
651-
"!**/src/**/__tests__/**",
652-
"!**/src/**/?(*.)(spec|test).*",
653-
"!**/src/setupProxy.*",
654-
"!**/src/setupTests.*",
655-
],
656-
silent: true,
657-
formatter: typescriptFormatter,
658-
}),
635+
configOverwrite: {
636+
compilerOptions: {
637+
sourceMap: isEnvProduction
638+
? shouldUseSourceMap
639+
: isEnvDevelopment,
640+
skipLibCheck: true,
641+
inlineSourceMap: false,
642+
declarationMap: false,
643+
noEmit: true,
644+
incremental: true,
645+
},
646+
},
647+
context: paths.appPath,
648+
diagnosticOptions: {
649+
syntactic: true,
650+
},
651+
mode: 'write-references',
652+
// profile: true,
653+
},
654+
issue: {
655+
include: [
656+
'../**/src/**/*.{ts,tsx}',
657+
'**/src/**/*.{ts,tsx}',
658+
].map(file => ({ file })),
659+
exclude: [
660+
'**/src/**/__tests__/**',
661+
'**/src/**/?(*.)(spec|test).*',
662+
'**/src/setupProxy.*',
663+
'**/src/setupTests.*',
664+
].map(file => ({ file })),
665+
},
666+
logger: {
667+
infrastructure: 'silent',
668+
},
669+
}),
659670
// new ESLintPlugin({
660671
// // Plugin options
661672
// extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
@@ -676,18 +687,6 @@ module.exports = function (webpackEnv) {
676687
// },
677688
// }),
678689
].filter(Boolean),
679-
// Some libraries import Node modules but don't use them in the browser.
680-
// Tell webpack to provide empty mocks for them so importing them works.
681-
node: {
682-
module: "empty",
683-
dgram: "empty",
684-
dns: "mock",
685-
fs: "empty",
686-
http2: "empty",
687-
net: "empty",
688-
tls: "empty",
689-
child_process: "empty",
690-
},
691690
// Turn off performance processing because we utilize
692691
// our own hints via the FileSizeReporter
693692
performance: false,

0 commit comments

Comments
 (0)