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(
}): boolean {
return (
!module.type?.startsWith('css') &&
module.size() > 160000 &&
// rspack doesn't support module.size
(isRspack || module.size() > 160000) &&
/node_modules[/\\]/.test(module.nameForCondition() || '')
)
},
......@@ -1029,12 +1030,15 @@ export default async function getBaseWebpackConfig(
if (isModuleCSS(module)) {
module.updateHash(hash)
} else {
if (!module.libIdent) {
throw new Error(
`Encountered unknown module type: ${module.type}. Please open an issue.`
)
// rspack doesn't support this
if (!isRspack) {
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
......@@ -1057,12 +1061,20 @@ export default async function getBaseWebpackConfig(
// as we don't need a separate vendor chunk from that
// and all other chunk depend on them so there is no
// duplication that need to be pulled out.
chunks: (chunk: any) =>
!/^(polyfills|main|pages\/_app)$/.test(chunk.name),
cacheGroups: {
framework: frameworkCacheGroup,
lib: libCacheGroup,
},
chunks: isRspack
? // using a function here causes noticable slowdown
// in rspack
/(?!polyfills|main|pages\/_app)/
: (chunk: any) =>
!/^(polyfills|main|pages\/_app)$/.test(chunk.name),
// TODO: investigate these cache groups with rspack
cacheGroups: isRspack
? {}
: {
framework: frameworkCacheGroup,
lib: libCacheGroup,
},
maxInitialRequests: 25,
minSize: 20000,
}
......@@ -1076,33 +1088,44 @@ export default async function getBaseWebpackConfig(
(isClient ||
isEdgeServer ||
(isNodeServer && config.experimental.serverMinification)),
minimizer: [
// Minify JavaScript
(compiler: webpack.Compiler) => {
// @ts-ignore No typings yet
const { MinifyPlugin } =
require('./webpack/plugins/minify-webpack-plugin/src/index.js') as typeof import('./webpack/plugins/minify-webpack-plugin/src')
new MinifyPlugin().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,
},
minimizer: isRspack
? [
// @ts-expect-error
new webpack.SwcJsMinimizerRspackPlugin({
// JS minimizer configuration
}),
// @ts-expect-error
new webpack.LightningCssMinimizerRspackPlugin({
// CSS minimizer configuration
}),
]
: [
// Minify JavaScript
(compiler: webpack.Compiler) => {
// @ts-ignore No typings yet
const { MinifyPlugin } =
require('./webpack/plugins/minify-webpack-plugin/src/index.js') as typeof import('./webpack/plugins/minify-webpack-plugin/src')
new MinifyPlugin().apply(compiler)
},
}).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,
// Kept as function to be backwards compatible
......@@ -1774,7 +1797,12 @@ export default async function getBaseWebpackConfig(
}
),
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)
(isClient || isEdgeServer) &&
new webpack.ProvidePlugin({
......@@ -1804,7 +1832,8 @@ export default async function getBaseWebpackConfig(
runtimeAsset: `server/${MIDDLEWARE_REACT_LOADABLE_MANIFEST}.js`,
dev,
}),
(isClient || isEdgeServer) && new DropClientPage(),
// rspack doesn't support the parser hooks used here
!isRspack && (isClient || isEdgeServer) && new DropClientPage(),
isNodeServer &&
!dev &&
new (require('./webpack/plugins/next-trace-entrypoints-plugin')
......@@ -1882,7 +1911,7 @@ export default async function getBaseWebpackConfig(
appDirEnabled: hasAppDir,
clientRouterFilters,
}),
new ProfilingPlugin({ runWebpackSpan, rootDir: dir }),
!isRspack && new ProfilingPlugin({ runWebpackSpan, rootDir: dir }),
new WellKnownErrorsPlugin(),
isClient &&
new CopyFilePlugin({
......@@ -1933,11 +1962,13 @@ export default async function getBaseWebpackConfig(
new NextFontManifestPlugin({
appDir,
}),
!dev &&
!isRspack &&
!dev &&
isClient &&
config.experimental.cssChunking &&
new CssChunkingPlugin(config.experimental.cssChunking === 'strict'),
!dev &&
!isRspack &&
!dev &&
isClient &&
new (
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(
].filter<[Feature, boolean]>(Boolean as any)
)
),
!dev &&
!isRspack &&
!dev &&
isNodeServer &&
new (
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