Unverified Commit eb65ed37 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

avoid retaining webpack config too long (#32053)

avoids keeping the webpack compiler during after webpack build build steps like SSG etc.

(review with ignore whitespace)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
parent 5763a33a
...@@ -571,105 +571,112 @@ export default async function build( ...@@ -571,105 +571,112 @@ export default async function build(
ignore: [] as string[], ignore: [] as string[],
})) }))
const runWebpackSpan = nextBuildSpan.traceChild('run-webpack-compiler') let result: CompilerResult = { warnings: [], errors: [] }
const configs = await runWebpackSpan let webpackBuildStart
.traceChild('generate-webpack-config') let telemetryPlugin
.traceAsyncFn(() => await (async () => {
Promise.all([ // IIFE to isolate locals and avoid retaining memory too long
getBaseWebpackConfig(dir, { const runWebpackSpan = nextBuildSpan.traceChild('run-webpack-compiler')
buildId, const configs = await runWebpackSpan
reactProductionProfiling, .traceChild('generate-webpack-config')
isServer: false, .traceAsyncFn(() =>
config, Promise.all([
target, getBaseWebpackConfig(dir, {
pagesDir, buildId,
entrypoints: entrypoints.client, reactProductionProfiling,
rewrites, isServer: false,
runWebpackSpan, config,
}), target,
getBaseWebpackConfig(dir, { pagesDir,
buildId, entrypoints: entrypoints.client,
reactProductionProfiling, rewrites,
isServer: true, runWebpackSpan,
config, }),
target, getBaseWebpackConfig(dir, {
pagesDir, buildId,
entrypoints: entrypoints.server, reactProductionProfiling,
rewrites, isServer: true,
runWebpackSpan, config,
}), target,
hasConcurrentFeatures pagesDir,
? getBaseWebpackConfig(dir, { entrypoints: entrypoints.server,
buildId, rewrites,
reactProductionProfiling, runWebpackSpan,
isServer: true, }),
webServerRuntime: true, hasConcurrentFeatures
config, ? getBaseWebpackConfig(dir, {
target, buildId,
pagesDir, reactProductionProfiling,
entrypoints: entrypoints.serverWeb, isServer: true,
rewrites, webServerRuntime: true,
runWebpackSpan, config,
}) target,
: null, pagesDir,
]) entrypoints: entrypoints.serverWeb,
) rewrites,
runWebpackSpan,
})
: null,
])
)
const clientConfig = configs[0] const clientConfig = configs[0]
if ( if (
clientConfig.optimization && clientConfig.optimization &&
(clientConfig.optimization.minimize !== true || (clientConfig.optimization.minimize !== true ||
(clientConfig.optimization.minimizer && (clientConfig.optimization.minimizer &&
clientConfig.optimization.minimizer.length === 0)) clientConfig.optimization.minimizer.length === 0))
) { ) {
Log.warn( Log.warn(
`Production code optimization has been disabled in your project. Read more: https://nextjs.org/docs/messages/minification-disabled` `Production code optimization has been disabled in your project. Read more: https://nextjs.org/docs/messages/minification-disabled`
) )
} }
const webpackBuildStart = process.hrtime() webpackBuildStart = process.hrtime()
let result: CompilerResult = { warnings: [], errors: [] } // We run client and server compilation separately to optimize for memory usage
// We run client and server compilation separately to optimize for memory usage await runWebpackSpan.traceAsyncFn(async () => {
await runWebpackSpan.traceAsyncFn(async () => { const clientResult = await runCompiler(clientConfig, { runWebpackSpan })
const clientResult = await runCompiler(clientConfig, { runWebpackSpan }) // Fail build if clientResult contains errors
// Fail build if clientResult contains errors if (clientResult.errors.length > 0) {
if (clientResult.errors.length > 0) { result = {
result = { warnings: [...clientResult.warnings],
warnings: [...clientResult.warnings], errors: [...clientResult.errors],
errors: [...clientResult.errors], }
} } else {
} else { const serverResult = await runCompiler(configs[1], { runWebpackSpan })
const serverResult = await runCompiler(configs[1], { runWebpackSpan }) const serverWebResult = configs[2]
const serverWebResult = configs[2] ? await runCompiler(configs[2], { runWebpackSpan })
? await runCompiler(configs[2], { runWebpackSpan }) : null
: null
result = {
result = { warnings: [
warnings: [ ...clientResult.warnings,
...clientResult.warnings, ...serverResult.warnings,
...serverResult.warnings, ...(serverWebResult?.warnings || []),
...(serverWebResult?.warnings || []), ],
], errors: [
errors: [ ...clientResult.errors,
...clientResult.errors, ...serverResult.errors,
...serverResult.errors, ...(serverWebResult?.errors || []),
...(serverWebResult?.errors || []), ],
], }
} }
} })
}) result = nextBuildSpan
.traceChild('format-webpack-messages')
.traceFn(() => formatWebpackMessages(result, true))
telemetryPlugin = (clientConfig as webpack.Configuration).plugins?.find(
isTelemetryPlugin
)
})()
const webpackBuildEnd = process.hrtime(webpackBuildStart) const webpackBuildEnd = process.hrtime(webpackBuildStart)
if (buildSpinner) { if (buildSpinner) {
buildSpinner.stopAndPersist() buildSpinner.stopAndPersist()
} }
result = nextBuildSpan
.traceChild('format-webpack-messages')
.traceFn(() => formatWebpackMessages(result, true))
if (result.errors.length > 0) { if (result.errors.length > 0) {
// Only keep the first few errors. Others are often indicative // Only keep the first few errors. Others are often indicative
// of the same problem, but confuse the reader with noise. // of the same problem, but confuse the reader with noise.
...@@ -1871,9 +1878,6 @@ export default async function build( ...@@ -1871,9 +1878,6 @@ export default async function build(
}) })
) )
const telemetryPlugin = (
clientConfig as webpack.Configuration
).plugins?.find(isTelemetryPlugin)
if (telemetryPlugin) { if (telemetryPlugin) {
const events = eventBuildFeatureUsage(telemetryPlugin) const events = eventBuildFeatureUsage(telemetryPlugin)
telemetry.record(events) telemetry.record(events)
......
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