Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Start the modern Child Compiler earlier during builds #151

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 additions & 19 deletions packages/next/build/webpack/plugins/next-esm-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,24 +254,36 @@ export default class NextEsmPlugin implements Plugin {
}
}

compilation.hooks.additionalAssets.tapAsync(
PLUGIN_NAME,
childProcessDone => {
this.updateOptions(childCompiler)

childCompiler.runAsChild((err, entries, childCompilation) => {
if (err) {
return childProcessDone(err)
}

if (childCompilation.errors.length > 0) {
return childProcessDone(childCompilation.errors[0])
}

this.updateAssets(compilation, childCompilation)
childProcessDone()
})
}
)
// Hold back the main compilation until our Child Compiler has completed so its assets get optimized
const child = new Promise((resolve, reject) => {
// Defer the child compiler until known main thread "dead time" (while Terser is doing minification in the background)
let started = false
compilation.hooks.optimizeChunkAssets.intercept({
call: () => {
// only run the first time optimizeChunkAssets is called
if (started) return
started = true

// Delay the Child Compiler until optimizeChunkAssets has had time to send work to the Terser pool
setTimeout(() => {
this.updateOptions(childCompiler)

childCompiler.runAsChild((err, entries, childCompilation) => {
if (err) {
return reject(err)
}

if (childCompilation.errors.length > 0) {
return reject(childCompilation.errors[0])
}

this.updateAssets(compilation, childCompilation)
resolve()
})
}, 500)
},
})
})
compilation.hooks.optimizeAssets.tapPromise(PLUGIN_NAME, () => child)
}
}