Revert "perf: enable webpack build worker (#57346)" (#57854)

This reverts commit 907f37978e.

x-ref: https://github.com/vercel/next.js/pull/57346
This commit is contained in:
JJ Kasper 2023-10-31 19:57:37 -07:00 committed by GitHub
parent c0baa0ac23
commit f2efb502ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 61 deletions

View file

@ -1,15 +0,0 @@
---
title: Webpack Build Worker automatic opt-out
---
## Webpack Build Worker Opt-out
#### Why This Error Occurred
The Next.js config contains custom webpack configuration, as a result, the webpack build worker optimization was disabled.
The webpack build worker optimization helps alleviate memory stress during builds and reduce out-of-memory errors although some custom configurations may not be compatible.
#### Possible Ways to Fix It
You can force enable the option by setting `config.experimental.webpackBuildWorker: true` in the config.

View file

@ -1061,35 +1061,16 @@ export default async function build(
})
const [duration] = process.hrtime(turboNextBuildStart)
return { duration, buildTraceContext: undefined }
return { duration, buildTraceContext: null }
}
let buildTraceContext: undefined | BuildTraceContext
let buildTracesPromise: Promise<any> | undefined = undefined
// if the option is set, we respect it, otherwise we check if the user
// has a custom webpack config and disable the build worker by default.
const useBuildWorker =
config.experimental.webpackBuildWorker || !config.webpack
nextBuildSpan.setAttribute(
'has-custom-webpack-config',
String(!!config.webpack)
)
nextBuildSpan.setAttribute('use-build-worker', String(useBuildWorker))
if (
config.webpack &&
config.experimental.webpackBuildWorker === undefined
) {
Log.warn(
'Custom webpack configuration is detected. When using a custom webpack configuration, the Webpack build worker is disabled by default. To force enable it, set the "experimental.webpackBuildWorker" option to "true". Read more: https://nextjs.org/docs/messages/webpack-build-worker-opt-out'
)
}
if (!isGenerate) {
if (isCompile && useBuildWorker) {
if (isCompile && config.experimental.webpackBuildWorker) {
let durationInSeconds = 0
await webpackBuild(true, ['server']).then((res) => {
await webpackBuild(['server']).then((res) => {
buildTraceContext = res.buildTraceContext
durationInSeconds += res.duration
const buildTraceWorker = new Worker(
@ -1118,11 +1099,11 @@ export default async function build(
})
})
await webpackBuild(true, ['edge-server']).then((res) => {
await webpackBuild(['edge-server']).then((res) => {
durationInSeconds += res.duration
})
await webpackBuild(true, ['client']).then((res) => {
await webpackBuild(['client']).then((res) => {
durationInSeconds += res.duration
})
@ -1138,7 +1119,7 @@ export default async function build(
} else {
const { duration: webpackBuildDuration, ...rest } = turboNextBuild
? await turbopackBuild()
: await webpackBuild(false)
: await webpackBuild()
buildTraceContext = rest.buildTraceContext

View file

@ -134,16 +134,17 @@ async function webpackBuildWithWorker(
return combinedResult
}
export function webpackBuild(
withWorker: boolean,
export async function webpackBuild(
compilerNames?: typeof ORDERED_COMPILER_NAMES
): ReturnType<typeof webpackBuildWithWorker> {
if (withWorker) {
) {
const config = NextBuildContext.config!
if (config.experimental.webpackBuildWorker) {
debug('using separate compiler workers')
return webpackBuildWithWorker(compilerNames)
return await webpackBuildWithWorker(compilerNames)
} else {
debug('building all compilers in same process')
const webpackBuildImpl = require('./impl').webpackBuildImpl
return webpackBuildImpl()
return await webpackBuildImpl()
}
}

View file

@ -282,11 +282,7 @@ export interface ExperimentalConfig {
typedRoutes?: boolean
/**
* Run the Webpack build in a separate process to optimize memory usage during build.
* Valid values are:
* - `false`: Disable the Webpack build worker
* - `true`: Enable the Webpack build worker
* - `undefined`: Enable the Webpack build worker only if the webpack config is not customized
* This option is to enable running the Webpack build in a worker thread.
*/
webpackBuildWorker?: boolean
@ -780,7 +776,6 @@ export const defaultConfig: NextConfig = {
typedRoutes: false,
instrumentationHook: false,
bundlePagesExternals: false,
webpackBuildWorker: undefined,
},
}

View file

@ -23,10 +23,7 @@ let devOutput
() => {
it('should not fail to build invalid usage of the Image component', async () => {
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
const errors = stderr
.split('\n')
.filter((line: string) => line && !line.trim().startsWith('⚠'))
expect(errors).toEqual([])
expect(stderr).toBeFalsy()
expect(code).toBe(0)
})
}

View file

@ -25,7 +25,7 @@ let devOutput
const { stderr, code } = await nextBuild(appDir, [], { stderr: true })
const errors = stderr
.split('\n')
.filter((line: string) => line && !line.trim().startsWith('⚠'))
.filter((line) => line && !line.trim().startsWith('⚠'))
expect(errors).toEqual([])
expect(code).toBe(0)
})

View file

@ -22,10 +22,8 @@ const appDir = path.join(__dirname, '..')
stdout: true,
stderr: true,
})
const errors = stderr
.split('\n')
.filter((line) => line && !line.trim().startsWith('⚠'))
expect(errors).toEqual([])
console.log(stderr)
expect(stderr.length).toStrictEqual(0)
expect(stdout).toMatch(/Initialized config/)
})
})