Unverified Commit 29c697b3 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Add initial configuration changes for webpack 5 (#11917)

* Disable PnP plugin in webpack 5
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Use full hashes for hot update names
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Disable autodll in webpack 5
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Disable HashedModuleIdsPlugin in webpack 5
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Use regexes for ignoreplugin as it only supports regexes in webpack 5
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* webpack 4 supports resourceRegExp and contextRegExp
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Remove webpack 4 specific properties from the webpack 5 config
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Add library option for webpack 5 support
Co-Authored-By: 's avatarZack Jackson <zackary.l.jackson@gmail.com>

* Fix TypeScript Errors
Co-authored-by: 's avatarZack Jackson <zackary.l.jackson@gmail.com>
Co-authored-by: 's avatarJoe Haddad <joe.haddad@zeit.co>
parent fe09c103
...@@ -53,6 +53,8 @@ import WebpackConformancePlugin, { ...@@ -53,6 +53,8 @@ import WebpackConformancePlugin, {
type ExcludesFalse = <T>(x: T | false) => x is T type ExcludesFalse = <T>(x: T | false) => x is T
const isWebpack5 = parseInt(webpack.version!) === 5
const escapePathVariables = (value: any) => { const escapePathVariables = (value: any) => {
return typeof value === 'string' return typeof value === 'string'
? value.replace(/\[(\\*[\w:]+\\*)\]/gi, '[\\$1\\]') ? value.replace(/\[(\\*[\w:]+\\*)\]/gi, '[\\$1\\]')
...@@ -277,7 +279,10 @@ export default async function getBaseWebpackConfig( ...@@ -277,7 +279,10 @@ export default async function getBaseWebpackConfig(
...getOptimizedAliases(isServer), ...getOptimizedAliases(isServer),
}, },
mainFields: isServer ? ['main', 'module'] : ['browser', 'module', 'main'], mainFields: isServer ? ['main', 'module'] : ['browser', 'module', 'main'],
plugins: [PnpWebpackPlugin], plugins: isWebpack5
? // webpack 5+ has the PnP resolver built-in by default:
[]
: [PnpWebpackPlugin],
} }
const webpackMode = dev ? 'development' : 'production' const webpackMode = dev ? 'development' : 'production'
...@@ -681,8 +686,12 @@ export default async function getBaseWebpackConfig( ...@@ -681,8 +686,12 @@ export default async function getBaseWebpackConfig(
return '[name]' return '[name]'
}, },
libraryTarget: isServer ? 'commonjs2' : 'var', libraryTarget: isServer ? 'commonjs2' : 'var',
hotUpdateChunkFilename: 'static/webpack/[id].[hash].hot-update.js', hotUpdateChunkFilename: isWebpack5
hotUpdateMainFilename: 'static/webpack/[hash].hot-update.json', ? 'static/webpack/[id].[fullhash].hot-update.js'
: 'static/webpack/[id].[hash].hot-update.js',
hotUpdateMainFilename: isWebpack5
? 'static/webpack/[fullhash].hot-update.json'
: 'static/webpack/[hash].hot-update.json',
// This saves chunks with the name given via `import()` // This saves chunks with the name given via `import()`
chunkFilename: isServer chunkFilename: isServer
? `${dev ? '[name]' : '[name].[contenthash]'}.js` ? `${dev ? '[name]' : '[name].[contenthash]'}.js`
...@@ -881,7 +890,9 @@ export default async function getBaseWebpackConfig( ...@@ -881,7 +890,9 @@ export default async function getBaseWebpackConfig(
new NextJsRequireCacheHotReloader(), new NextJsRequireCacheHotReloader(),
] ]
if (!isServer) { // Webpack 5 has the ability to cache packages persistently, so we
// do not need this DLL plugin:
if (!isServer && !isWebpack5) {
const AutoDllPlugin = require('next/dist/compiled/autodll-webpack-plugin')( const AutoDllPlugin = require('next/dist/compiled/autodll-webpack-plugin')(
distDir distDir
) )
...@@ -906,18 +917,12 @@ export default async function getBaseWebpackConfig( ...@@ -906,18 +917,12 @@ export default async function getBaseWebpackConfig(
return devPlugins return devPlugins
})() })()
: []), : []),
!dev && new webpack.HashedModuleIdsPlugin(), // Webpack 5 no longer requires this plugin in production:
!isWebpack5 && !dev && new webpack.HashedModuleIdsPlugin(),
!dev && !dev &&
new webpack.IgnorePlugin({ new webpack.IgnorePlugin({
checkResource: (resource: string) => { resourceRegExp: /react-is/,
return /react-is/.test(resource) contextRegExp: /(next-server|next)[\\/]dist[\\/]/,
},
checkContext: (context: string) => {
return (
/next-server[\\/]dist[\\/]/.test(context) ||
/next[\\/]dist[\\/]/.test(context)
)
},
}), }),
isServerless && isServer && new ServerlessPlugin(), isServerless && isServer && new ServerlessPlugin(),
isServer && new PagesManifestPlugin(isLikeServerless), isServer && new PagesManifestPlugin(isLikeServerless),
...@@ -1010,6 +1015,13 @@ export default async function getBaseWebpackConfig( ...@@ -1010,6 +1015,13 @@ export default async function getBaseWebpackConfig(
) )
} }
if (isWebpack5) {
// On by default:
delete webpackConfig.output?.futureEmitAssets
// No longer polyfills Node.js modules:
if (webpackConfig.node) delete webpackConfig.node.setImmediate
}
webpackConfig = await buildConfiguration(webpackConfig, { webpackConfig = await buildConfiguration(webpackConfig, {
rootDirectory: dir, rootDirectory: dir,
customAppFile, customAppFile,
...@@ -1266,5 +1278,16 @@ export default async function getBaseWebpackConfig( ...@@ -1266,5 +1278,16 @@ export default async function getBaseWebpackConfig(
webpackConfig.entry = await (webpackConfig.entry as webpack.EntryFunc)() webpackConfig.entry = await (webpackConfig.entry as webpack.EntryFunc)()
} }
// In webpack 5, the 'var' libraryTarget output requires a name.
// TODO: this should be revisited as 'var' was only used to not have the
// initial variable exposed. In webpack 4, not setting the library option
// would result in the bundle being a self-executing function without the
// variable.
if (isWebpack5 && !isServer) {
webpackConfig.output!.library = webpackConfig.output?.library
? webpackConfig.output.library
: 'INTERNAL_NEXT_APP'
}
return webpackConfig return webpackConfig
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment