Skip to content

Commit

Permalink
Upgrade to Webpack 5
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
mbarrien committed Feb 9, 2021
1 parent fe145e2 commit 15bd5a1
Show file tree
Hide file tree
Showing 5 changed files with 3,921 additions and 3,145 deletions.
14 changes: 14 additions & 0 deletions src/ts/config/mocks/dns.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// See https://github.com/webpack/node-libs-browser/blob/214057f118bad5da8b99db33fd16a5b6ceb42d9b/mock/dns.js

'use strict';

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() {
if (!arguments.length) {
return;
}

var callback = arguments[arguments.length - 1];
if (callback && typeof callback === 'function') {
callback(null, '0.0.0.0');
}
};
3 changes: 3 additions & 0 deletions src/ts/config/mocks/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

module.exports = null;
129 changes: 64 additions & 65 deletions src/ts/config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const TerserPlugin = require("terser-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const safePostCssParser = require("postcss-safe-parser");
const ManifestPlugin = require("webpack-manifest-plugin");
const { WebpackManifestPlugin } = require("webpack-manifest-plugin");
const InterpolateHtmlPlugin = require("react-dev-utils/InterpolateHtmlPlugin");
const WorkboxWebpackPlugin = require("workbox-webpack-plugin");
const WatchMissingNodeModulesPlugin = require("react-dev-utils/WatchMissingNodeModulesPlugin");
Expand Down Expand Up @@ -107,23 +107,25 @@ module.exports = function (webpackEnv) {
// package.json
loader: require.resolve("postcss-loader"),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebook/create-react-app/issues/2677
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
require("postcss-preset-env")({
autoprefixer: {
flexbox: "no-2009",
},
stage: 3,
}),
// Adds PostCSS Normalize as the reset css with default options,
// so that it honors browserslist config in package.json
// which in turn let's users customize the target behavior as per their needs.
postcssNormalize(),
],
sourceMap: isEnvProduction ? shouldUseSourceMap : isEnvDevelopment,
postcssOptions: {
plugins: [
require('postcss-flexbugs-fixes'),
[
require('postcss-preset-env'),
{
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
},
],
// Adds PostCSS Normalize as the reset css with default options,
// so that it honors browserslist config in package.json
// which in turn let's users customize the target behavior as per their needs.
postcssNormalize(),
],
},
sourceMap: isEnvProduction && shouldUseSourceMap,
},
},
].filter(Boolean);
Expand Down Expand Up @@ -167,9 +169,7 @@ module.exports = function (webpackEnv) {
// There will be one main bundle, and one file per asynchronous chunk.
filename: isEnvProduction
? "js/[name].[contenthash:8].js"
: isEnvDevelopment && "js/bundle.js",
// TODO: remove this when upgrading to webpack 5
futureEmitAssets: true,
: isEnvDevelopment && "js/[name].js",
// There are also additional JS chunk files if you use code splitting.
chunkFilename: isEnvProduction
? "js/[name].[contenthash:8].chunk.js"
Expand All @@ -187,9 +187,6 @@ module.exports = function (webpackEnv) {
: isEnvDevelopment &&
((info) =>
path.resolve(info.absoluteResourcePath).replace(/\\/g, "/")),
// Prevents conflicts when multiple webpack runtimes (from different apps)
// are used on the same page.
jsonpFunction: `webpackJsonp${appPackageJson.name}`,
// this defaults to 'window', but by setting it to 'this' then
// module chunks which are built will work in web workers as well.
globalObject: "this",
Expand Down Expand Up @@ -236,7 +233,6 @@ module.exports = function (webpackEnv) {
ascii_only: true,
},
},
sourceMap: shouldUseSourceMap,
}),
// This is only used in production mode
new OptimizeCSSAssetsPlugin({
Expand Down Expand Up @@ -589,7 +585,7 @@ module.exports = function (webpackEnv) {
// `index.html`
// - "entrypoints" key: Array of files which are included in `index.html`,
// can be used to reconstruct the HTML if necessary
new ManifestPlugin({
new WebpackManifestPlugin({
fileName: "asset-manifest.json",
publicPath: paths.publicUrlOrPath,
generate: (seed, files, entrypoints) => {
Expand All @@ -612,7 +608,10 @@ module.exports = function (webpackEnv) {
// solution that requires the user to opt into importing specific locales.
// https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
// You can remove this if you don't use Moment.js:
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.IgnorePlugin({
resourceRegExp: /^\.\/locale$/,
contextRegExp: /moment$/,
}),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the webpack build.
isEnvProduction &&
Expand All @@ -628,34 +627,46 @@ module.exports = function (webpackEnv) {
}),
// TypeScript type checking
useTypeScript &&
new ForkTsCheckerWebpackPlugin({
typescript: resolve.sync("typescript", {
new ForkTsCheckerWebpackPlugin({
typescript: {
typescriptPath: resolve.sync('typescript', {
basedir: paths.appNodeModules,
}),
async: false,
checkSyntacticErrors: true,
resolveModuleNameModule: process.versions.pnp
? `${__dirname}/pnpTs.js`
: undefined,
resolveTypeReferenceDirectiveModule: process.versions.pnp
? `${__dirname}/pnpTs.js`
: undefined,
tsconfig: paths.appTsConfig,
reportFiles: [
// This one is specifically to match during CI tests,
// as micromatch doesn't match
// '../cra-template-typescript/template/src/App.tsx'
// otherwise.
"../**/src/**/*.{ts,tsx}",
"**/src/**/*.{ts,tsx}",
"!**/src/**/__tests__/**",
"!**/src/**/?(*.)(spec|test).*",
"!**/src/setupProxy.*",
"!**/src/setupTests.*",
],
silent: true,
formatter: typescriptFormatter,
}),
configOverwrite: {
compilerOptions: {
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
skipLibCheck: true,
inlineSourceMap: false,
declarationMap: false,
noEmit: true,
incremental: true,
},
},
context: paths.appPath,
diagnosticOptions: {
syntactic: true,
},
mode: 'write-references',
// profile: true,
},
issue: {
include: [
'../**/src/**/*.{ts,tsx}',
'**/src/**/*.{ts,tsx}',
].map(file => ({ file })),
exclude: [
'**/src/**/__tests__/**',
'**/src/**/?(*.)(spec|test).*',
'**/src/setupProxy.*',
'**/src/setupTests.*',
].map(file => ({ file })),
},
logger: {
infrastructure: 'silent',
},
}),
// new ESLintPlugin({
// // Plugin options
// extensions: ['js', 'mjs', 'jsx', 'ts', 'tsx'],
Expand All @@ -676,18 +687,6 @@ module.exports = function (webpackEnv) {
// },
// }),
].filter(Boolean),
// Some libraries import Node modules but don't use them in the browser.
// Tell webpack to provide empty mocks for them so importing them works.
node: {
module: "empty",
dgram: "empty",
dns: "mock",
fs: "empty",
http2: "empty",
net: "empty",
tls: "empty",
child_process: "empty",
},
// Turn off performance processing because we utilize
// our own hints via the FileSizeReporter
performance: false,
Expand Down
Loading

0 comments on commit 15bd5a1

Please sign in to comment.