Unverified Commit 8545d666 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Use separate workers for webpackBuildWorker (#46666)

This updates our experimental `webpackBuildWorker` config to use
separate process for each compilation to reduce memory usage building up
from all compilers sharing same process.

x-ref: [slack thread](https://vercel.slack.com/archives/C04S835KUC9)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent 2e23cd75
......@@ -5,6 +5,7 @@ import type { __ApiPreviewProps } from '../server/api-utils'
import type { NextConfigComplete } from '../server/config-shared'
import type { Span } from '../trace'
import type getBaseWebpackConfig from './webpack-config'
import { PagesManifest } from './webpack/plugins/pages-manifest-plugin'
import type { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
// a global object to store context for the current build
......@@ -12,6 +13,22 @@ import type { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'
// to pass it through function arguments.
// Not exhaustive, but should be extended to as needed whilst refactoring
export const NextBuildContext: Partial<{
compilerIdx?: number
serializedFlightMaps?: {
injectedClientEntries?: any
serverModuleIds?: any
edgeServerModuleIds?: any
asyncClientModules?: any
serverActions?: any
serverCSSManifest?: any
edgeServerCSSManifest?: any
}
serializedPagesManifestEntries: {
edgeServerPages?: PagesManifest
nodeServerPages?: PagesManifest
edgeServerAppPaths?: PagesManifest
nodeServerAppPaths?: PagesManifest
}
// core fields
dir: string
buildId: string
......
......@@ -1068,7 +1068,9 @@ export default async function build(
if (filesTracedFromEntries.length) {
// The turbo trace doesn't provide the traced file type and reason at present
// let's write the traced files into the first [entry].nft.json
// @ts-expect-error types
const [[, entryName]] = Array.from(entryNameMap.entries()).filter(
// @ts-expect-error types
([k]) => k.startsWith(turbotraceContextAppDir)
)
const traceOutputPath = path.join(
......@@ -1085,7 +1087,7 @@ export default async function build(
}
if (chunksTrace) {
const { action, outputPath } = chunksTrace
action.input = action.input.filter((f) => {
action.input = action.input.filter((f: any) => {
const outputPagesPath = path.join(outputPath, '..', 'pages')
return (
!f.startsWith(outputPagesPath) ||
......
......@@ -16,6 +16,13 @@ const ArchName = arch()
const PlatformName = platform()
const triples = platformArchTriples[PlatformName][ArchName] || []
const infoLog = (...args: any[]) => {
if (process.env.NEXT_PRIVATE_BUILD_WORKER) {
return
}
Log.info(...args)
}
// Allow to specify an absolute path to the custom turbopack binary to load.
// If one of env variables is set, `loadNative` will try to use any turbo-* interfaces from specified
// binary instead. This will not affect existing swc's transform, or other interfaces. This is thin,
......@@ -213,7 +220,7 @@ async function loadWasm(importPath = '') {
if (pkg === '@next/swc-wasm-web') {
bindings = await bindings.default()
}
Log.info('Using wasm build of next-swc')
infoLog('Using wasm build of next-swc')
// Note wasm binary does not support async intefaces yet, all async
// interface coereces to sync interfaces.
......@@ -292,7 +299,7 @@ function loadNative(isCustomTurbopack = false) {
for (const triple of triples) {
try {
bindings = require(`@next/swc/native/next-swc.${triple.platformArchABI}.node`)
Log.info('Using locally built binary of @next/swc')
infoLog('Using locally built binary of @next/swc')
break
} catch (e) {}
}
......
This diff is collapsed.
......@@ -41,7 +41,7 @@ interface Options {
const PLUGIN_NAME = 'ClientEntryPlugin'
export const injectedClientEntries = new Map()
export const injectedClientEntries = new Map<string, string>()
export const serverModuleIds = new Map<string, string | number>()
export const edgeServerModuleIds = new Map<string, string | number>()
......@@ -55,9 +55,9 @@ export type ActionManifest = {
}
// A map to track "action" -> "list of bundles".
let serverActions: ActionManifest = {}
let serverCSSManifest: FlightCSSManifest = {}
let edgeServerCSSManifest: FlightCSSManifest = {}
export const serverActions: ActionManifest = {}
export const serverCSSManifest: FlightCSSManifest = {}
export const edgeServerCSSManifest: FlightCSSManifest = {}
export class FlightClientEntryPlugin {
dev: boolean
......@@ -274,7 +274,6 @@ export class FlightClientEntryPlugin {
)
// Create action entry
serverActions = {}
if (actionEntryImports.size > 0) {
addActionEntryList.push(
this.injectActionEntry({
......@@ -292,11 +291,6 @@ export class FlightClientEntryPlugin {
// by the certain chunk.
compilation.hooks.afterOptimizeModules.tap(PLUGIN_NAME, () => {
const cssImportsForChunk: Record<string, Set<string>> = {}
if (this.isEdgeServer) {
edgeServerCSSManifest = {}
} else {
serverCSSManifest = {}
}
let cssManifest = this.isEdgeServer
? edgeServerCSSManifest
......
......@@ -8,10 +8,10 @@ import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-s
export type PagesManifest = { [page: string]: string }
let edgeServerPages = {}
let nodeServerPages = {}
let edgeServerAppPaths = {}
let nodeServerAppPaths = {}
export let edgeServerPages = {}
export let nodeServerPages = {}
export let edgeServerAppPaths = {}
export let nodeServerAppPaths = {}
// This plugin creates a pages-manifest.json from page entrypoints.
// This is used for mapping paths like `/` to `.next/server/static/<buildid>/pages/index.js` when doing SSR
......
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