Unverified Commit 95c3522b authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

refactor(webpack-config): Disable some options based on bundler version (#75888)

Extracted from the try-ci-test work: #75443

Includes some minor cleanup, deduping some use of the environment variable.

<!--
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent c3543f8a
...@@ -1015,7 +1015,8 @@ export default async function getBaseWebpackConfig( ...@@ -1015,7 +1015,8 @@ export default async function getBaseWebpackConfig(
}): boolean { }): boolean {
return ( return (
!module.type?.startsWith('css') && !module.type?.startsWith('css') &&
module.size() > 160000 && // rspack doesn't support module.size
(isRspack || module.size() > 160000) &&
/node_modules[/\\]/.test(module.nameForCondition() || '') /node_modules[/\\]/.test(module.nameForCondition() || '')
) )
}, },
...@@ -1029,12 +1030,15 @@ export default async function getBaseWebpackConfig( ...@@ -1029,12 +1030,15 @@ export default async function getBaseWebpackConfig(
if (isModuleCSS(module)) { if (isModuleCSS(module)) {
module.updateHash(hash) module.updateHash(hash)
} else { } else {
if (!module.libIdent) { // rspack doesn't support this
throw new Error( if (!isRspack) {
`Encountered unknown module type: ${module.type}. Please open an issue.` if (!module.libIdent) {
) throw new Error(
`Encountered unknown module type: ${module.type}. Please open an issue.`
)
}
hash.update(module.libIdent({ context: dir }))
} }
hash.update(module.libIdent({ context: dir }))
} }
// Ensures the name of the chunk is not the same between two modules in different layers // Ensures the name of the chunk is not the same between two modules in different layers
...@@ -1057,12 +1061,20 @@ export default async function getBaseWebpackConfig( ...@@ -1057,12 +1061,20 @@ export default async function getBaseWebpackConfig(
// as we don't need a separate vendor chunk from that // as we don't need a separate vendor chunk from that
// and all other chunk depend on them so there is no // and all other chunk depend on them so there is no
// duplication that need to be pulled out. // duplication that need to be pulled out.
chunks: (chunk: any) => chunks: isRspack
!/^(polyfills|main|pages\/_app)$/.test(chunk.name), ? // using a function here causes noticable slowdown
cacheGroups: { // in rspack
framework: frameworkCacheGroup, /(?!polyfills|main|pages\/_app)/
lib: libCacheGroup, : (chunk: any) =>
}, !/^(polyfills|main|pages\/_app)$/.test(chunk.name),
// TODO: investigate these cache groups with rspack
cacheGroups: isRspack
? {}
: {
framework: frameworkCacheGroup,
lib: libCacheGroup,
},
maxInitialRequests: 25, maxInitialRequests: 25,
minSize: 20000, minSize: 20000,
} }
...@@ -1076,33 +1088,44 @@ export default async function getBaseWebpackConfig( ...@@ -1076,33 +1088,44 @@ export default async function getBaseWebpackConfig(
(isClient || (isClient ||
isEdgeServer || isEdgeServer ||
(isNodeServer && config.experimental.serverMinification)), (isNodeServer && config.experimental.serverMinification)),
minimizer: [ minimizer: isRspack
// Minify JavaScript ? [
(compiler: webpack.Compiler) => { // @ts-expect-error
// @ts-ignore No typings yet new webpack.SwcJsMinimizerRspackPlugin({
const { MinifyPlugin } = // JS minimizer configuration
require('./webpack/plugins/minify-webpack-plugin/src/index.js') as typeof import('./webpack/plugins/minify-webpack-plugin/src') }),
new MinifyPlugin().apply(compiler) // @ts-expect-error
}, new webpack.LightningCssMinimizerRspackPlugin({
// Minify CSS // CSS minimizer configuration
(compiler: webpack.Compiler) => { }),
const { ]
CssMinimizerPlugin, : [
} = require('./webpack/plugins/css-minimizer-plugin') // Minify JavaScript
new CssMinimizerPlugin({ (compiler: webpack.Compiler) => {
postcssOptions: { // @ts-ignore No typings yet
map: { const { MinifyPlugin } =
// `inline: false` generates the source map in a separate file. require('./webpack/plugins/minify-webpack-plugin/src/index.js') as typeof import('./webpack/plugins/minify-webpack-plugin/src')
// Otherwise, the CSS file is needlessly large. new MinifyPlugin().apply(compiler)
inline: false,
// `annotation: false` skips appending the `sourceMappingURL`
// to the end of the CSS file. Webpack already handles this.
annotation: false,
},
}, },
}).apply(compiler) // Minify CSS
}, (compiler: webpack.Compiler) => {
], const {
CssMinimizerPlugin,
} = require('./webpack/plugins/css-minimizer-plugin')
new CssMinimizerPlugin({
postcssOptions: {
map: {
// `inline: false` generates the source map in a separate file.
// Otherwise, the CSS file is needlessly large.
inline: false,
// `annotation: false` skips appending the `sourceMappingURL`
// to the end of the CSS file. Webpack already handles this.
annotation: false,
},
},
}).apply(compiler)
},
],
}, },
context: dir, context: dir,
// Kept as function to be backwards compatible // Kept as function to be backwards compatible
...@@ -1774,7 +1797,12 @@ export default async function getBaseWebpackConfig( ...@@ -1774,7 +1797,12 @@ export default async function getBaseWebpackConfig(
} }
), ),
dev && new MemoryWithGcCachePlugin({ maxGenerations: 5 }), dev && new MemoryWithGcCachePlugin({ maxGenerations: 5 }),
dev && isClient && new ReactRefreshWebpackPlugin(webpack), dev &&
isClient &&
(isRspack
? // eslint-disable-next-line
new (require('@rspack/plugin-react-refresh') as any)()
: new ReactRefreshWebpackPlugin(webpack)),
// Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4) // Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4)
(isClient || isEdgeServer) && (isClient || isEdgeServer) &&
new webpack.ProvidePlugin({ new webpack.ProvidePlugin({
...@@ -1804,7 +1832,8 @@ export default async function getBaseWebpackConfig( ...@@ -1804,7 +1832,8 @@ export default async function getBaseWebpackConfig(
runtimeAsset: `server/${MIDDLEWARE_REACT_LOADABLE_MANIFEST}.js`, runtimeAsset: `server/${MIDDLEWARE_REACT_LOADABLE_MANIFEST}.js`,
dev, dev,
}), }),
(isClient || isEdgeServer) && new DropClientPage(), // rspack doesn't support the parser hooks used here
!isRspack && (isClient || isEdgeServer) && new DropClientPage(),
isNodeServer && isNodeServer &&
!dev && !dev &&
new (require('./webpack/plugins/next-trace-entrypoints-plugin') new (require('./webpack/plugins/next-trace-entrypoints-plugin')
...@@ -1882,7 +1911,7 @@ export default async function getBaseWebpackConfig( ...@@ -1882,7 +1911,7 @@ export default async function getBaseWebpackConfig(
appDirEnabled: hasAppDir, appDirEnabled: hasAppDir,
clientRouterFilters, clientRouterFilters,
}), }),
new ProfilingPlugin({ runWebpackSpan, rootDir: dir }), !isRspack && new ProfilingPlugin({ runWebpackSpan, rootDir: dir }),
new WellKnownErrorsPlugin(), new WellKnownErrorsPlugin(),
isClient && isClient &&
new CopyFilePlugin({ new CopyFilePlugin({
...@@ -1933,11 +1962,13 @@ export default async function getBaseWebpackConfig( ...@@ -1933,11 +1962,13 @@ export default async function getBaseWebpackConfig(
new NextFontManifestPlugin({ new NextFontManifestPlugin({
appDir, appDir,
}), }),
!dev && !isRspack &&
!dev &&
isClient && isClient &&
config.experimental.cssChunking && config.experimental.cssChunking &&
new CssChunkingPlugin(config.experimental.cssChunking === 'strict'), new CssChunkingPlugin(config.experimental.cssChunking === 'strict'),
!dev && !isRspack &&
!dev &&
isClient && isClient &&
new ( new (
require('./webpack/plugins/telemetry-plugin/telemetry-plugin') as typeof import('./webpack/plugins/telemetry-plugin/telemetry-plugin') require('./webpack/plugins/telemetry-plugin/telemetry-plugin') as typeof import('./webpack/plugins/telemetry-plugin/telemetry-plugin')
...@@ -1971,7 +2002,8 @@ export default async function getBaseWebpackConfig( ...@@ -1971,7 +2002,8 @@ export default async function getBaseWebpackConfig(
].filter<[Feature, boolean]>(Boolean as any) ].filter<[Feature, boolean]>(Boolean as any)
) )
), ),
!dev && !isRspack &&
!dev &&
isNodeServer && isNodeServer &&
new ( new (
require('./webpack/plugins/telemetry-plugin/telemetry-plugin') as typeof import('./webpack/plugins/telemetry-plugin/telemetry-plugin') require('./webpack/plugins/telemetry-plugin/telemetry-plugin') as typeof import('./webpack/plugins/telemetry-plugin/telemetry-plugin')
......
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