Skip to content

Commit eb65ed3

Browse files
authored
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`
1 parent 5763a33 commit eb65ed3

File tree

1 file changed

+95
-91
lines changed

1 file changed

+95
-91
lines changed

packages/next/build/index.ts

+95-91
Original file line numberDiff line numberDiff line change
@@ -571,105 +571,112 @@ export default async function build(
571571
ignore: [] as string[],
572572
}))
573573

574-
const runWebpackSpan = nextBuildSpan.traceChild('run-webpack-compiler')
575-
const configs = await runWebpackSpan
576-
.traceChild('generate-webpack-config')
577-
.traceAsyncFn(() =>
578-
Promise.all([
579-
getBaseWebpackConfig(dir, {
580-
buildId,
581-
reactProductionProfiling,
582-
isServer: false,
583-
config,
584-
target,
585-
pagesDir,
586-
entrypoints: entrypoints.client,
587-
rewrites,
588-
runWebpackSpan,
589-
}),
590-
getBaseWebpackConfig(dir, {
591-
buildId,
592-
reactProductionProfiling,
593-
isServer: true,
594-
config,
595-
target,
596-
pagesDir,
597-
entrypoints: entrypoints.server,
598-
rewrites,
599-
runWebpackSpan,
600-
}),
601-
hasConcurrentFeatures
602-
? getBaseWebpackConfig(dir, {
603-
buildId,
604-
reactProductionProfiling,
605-
isServer: true,
606-
webServerRuntime: true,
607-
config,
608-
target,
609-
pagesDir,
610-
entrypoints: entrypoints.serverWeb,
611-
rewrites,
612-
runWebpackSpan,
613-
})
614-
: null,
615-
])
616-
)
574+
let result: CompilerResult = { warnings: [], errors: [] }
575+
let webpackBuildStart
576+
let telemetryPlugin
577+
await (async () => {
578+
// IIFE to isolate locals and avoid retaining memory too long
579+
const runWebpackSpan = nextBuildSpan.traceChild('run-webpack-compiler')
580+
const configs = await runWebpackSpan
581+
.traceChild('generate-webpack-config')
582+
.traceAsyncFn(() =>
583+
Promise.all([
584+
getBaseWebpackConfig(dir, {
585+
buildId,
586+
reactProductionProfiling,
587+
isServer: false,
588+
config,
589+
target,
590+
pagesDir,
591+
entrypoints: entrypoints.client,
592+
rewrites,
593+
runWebpackSpan,
594+
}),
595+
getBaseWebpackConfig(dir, {
596+
buildId,
597+
reactProductionProfiling,
598+
isServer: true,
599+
config,
600+
target,
601+
pagesDir,
602+
entrypoints: entrypoints.server,
603+
rewrites,
604+
runWebpackSpan,
605+
}),
606+
hasConcurrentFeatures
607+
? getBaseWebpackConfig(dir, {
608+
buildId,
609+
reactProductionProfiling,
610+
isServer: true,
611+
webServerRuntime: true,
612+
config,
613+
target,
614+
pagesDir,
615+
entrypoints: entrypoints.serverWeb,
616+
rewrites,
617+
runWebpackSpan,
618+
})
619+
: null,
620+
])
621+
)
617622

618-
const clientConfig = configs[0]
623+
const clientConfig = configs[0]
619624

620-
if (
621-
clientConfig.optimization &&
622-
(clientConfig.optimization.minimize !== true ||
623-
(clientConfig.optimization.minimizer &&
624-
clientConfig.optimization.minimizer.length === 0))
625-
) {
626-
Log.warn(
627-
`Production code optimization has been disabled in your project. Read more: https://nextjs.org/docs/messages/minification-disabled`
628-
)
629-
}
625+
if (
626+
clientConfig.optimization &&
627+
(clientConfig.optimization.minimize !== true ||
628+
(clientConfig.optimization.minimizer &&
629+
clientConfig.optimization.minimizer.length === 0))
630+
) {
631+
Log.warn(
632+
`Production code optimization has been disabled in your project. Read more: https://nextjs.org/docs/messages/minification-disabled`
633+
)
634+
}
630635

631-
const webpackBuildStart = process.hrtime()
636+
webpackBuildStart = process.hrtime()
632637

633-
let result: CompilerResult = { warnings: [], errors: [] }
634-
// We run client and server compilation separately to optimize for memory usage
635-
await runWebpackSpan.traceAsyncFn(async () => {
636-
const clientResult = await runCompiler(clientConfig, { runWebpackSpan })
637-
// Fail build if clientResult contains errors
638-
if (clientResult.errors.length > 0) {
639-
result = {
640-
warnings: [...clientResult.warnings],
641-
errors: [...clientResult.errors],
642-
}
643-
} else {
644-
const serverResult = await runCompiler(configs[1], { runWebpackSpan })
645-
const serverWebResult = configs[2]
646-
? await runCompiler(configs[2], { runWebpackSpan })
647-
: null
648-
649-
result = {
650-
warnings: [
651-
...clientResult.warnings,
652-
...serverResult.warnings,
653-
...(serverWebResult?.warnings || []),
654-
],
655-
errors: [
656-
...clientResult.errors,
657-
...serverResult.errors,
658-
...(serverWebResult?.errors || []),
659-
],
638+
// We run client and server compilation separately to optimize for memory usage
639+
await runWebpackSpan.traceAsyncFn(async () => {
640+
const clientResult = await runCompiler(clientConfig, { runWebpackSpan })
641+
// Fail build if clientResult contains errors
642+
if (clientResult.errors.length > 0) {
643+
result = {
644+
warnings: [...clientResult.warnings],
645+
errors: [...clientResult.errors],
646+
}
647+
} else {
648+
const serverResult = await runCompiler(configs[1], { runWebpackSpan })
649+
const serverWebResult = configs[2]
650+
? await runCompiler(configs[2], { runWebpackSpan })
651+
: null
652+
653+
result = {
654+
warnings: [
655+
...clientResult.warnings,
656+
...serverResult.warnings,
657+
...(serverWebResult?.warnings || []),
658+
],
659+
errors: [
660+
...clientResult.errors,
661+
...serverResult.errors,
662+
...(serverWebResult?.errors || []),
663+
],
664+
}
660665
}
661-
}
662-
})
666+
})
667+
result = nextBuildSpan
668+
.traceChild('format-webpack-messages')
669+
.traceFn(() => formatWebpackMessages(result, true))
663670

671+
telemetryPlugin = (clientConfig as webpack.Configuration).plugins?.find(
672+
isTelemetryPlugin
673+
)
674+
})()
664675
const webpackBuildEnd = process.hrtime(webpackBuildStart)
665676
if (buildSpinner) {
666677
buildSpinner.stopAndPersist()
667678
}
668679

669-
result = nextBuildSpan
670-
.traceChild('format-webpack-messages')
671-
.traceFn(() => formatWebpackMessages(result, true))
672-
673680
if (result.errors.length > 0) {
674681
// Only keep the first few errors. Others are often indicative
675682
// of the same problem, but confuse the reader with noise.
@@ -1871,9 +1878,6 @@ export default async function build(
18711878
})
18721879
)
18731880

1874-
const telemetryPlugin = (
1875-
clientConfig as webpack.Configuration
1876-
).plugins?.find(isTelemetryPlugin)
18771881
if (telemetryPlugin) {
18781882
const events = eventBuildFeatureUsage(telemetryPlugin)
18791883
telemetry.record(events)

0 commit comments

Comments
 (0)