-
Notifications
You must be signed in to change notification settings - Fork 26k
/
index.ts
116 lines (100 loc) · 3.36 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { COMPILER_INDEXES } from '../../shared/lib/constants'
import * as Log from '../output/log'
import { NextBuildContext } from '../build-context'
import type { TurbotraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin'
import { Worker } from 'next/dist/compiled/jest-worker'
import origDebug from 'next/dist/compiled/debug'
import { ChildProcess } from 'child_process'
import path from 'path'
const debug = origDebug('next:build:webpack-build')
async function webpackBuildWithWorker() {
const {
config,
telemetryPlugin,
buildSpinner,
nextBuildSpan,
...prunedBuildContext
} = NextBuildContext
const getWorker = (compilerName: string) => {
const _worker = new Worker(path.join(__dirname, 'impl.js'), {
exposedMethods: ['workerMain'],
numWorkers: 1,
maxRetries: 0,
forkOptions: {
env: {
...process.env,
NEXT_PRIVATE_BUILD_WORKER: '1',
},
},
}) as Worker & typeof import('./impl')
_worker.getStderr().pipe(process.stderr)
_worker.getStdout().pipe(process.stdout)
for (const worker of ((_worker as any)._workerPool?._workers || []) as {
_child: ChildProcess
}[]) {
worker._child.on('exit', (code, signal) => {
if (code || signal) {
console.error(
`Compiler ${compilerName} unexpectedly exited with code: ${code} and signal: ${signal}`
)
}
})
}
return _worker
}
const combinedResult = {
duration: 0,
turbotraceContext: {} as TurbotraceContext,
}
// order matters here
const ORDERED_COMPILER_NAMES = [
'server',
'edge-server',
'client',
] as (keyof typeof COMPILER_INDEXES)[]
for (const compilerName of ORDERED_COMPILER_NAMES) {
const worker = getWorker(compilerName)
const curResult = await worker.workerMain({
buildContext: prunedBuildContext,
compilerName,
})
// destroy worker so it's not sticking around using memory
await worker.end()
// Update plugin state
prunedBuildContext.pluginState = curResult.pluginState
prunedBuildContext.serializedPagesManifestEntries = {
edgeServerAppPaths:
curResult.serializedPagesManifestEntries?.edgeServerAppPaths,
edgeServerPages:
curResult.serializedPagesManifestEntries?.edgeServerPages,
nodeServerAppPaths:
curResult.serializedPagesManifestEntries?.nodeServerAppPaths,
nodeServerPages:
curResult.serializedPagesManifestEntries?.nodeServerPages,
}
combinedResult.duration += curResult.duration
if (curResult.turbotraceContext?.entriesTrace) {
combinedResult.turbotraceContext = curResult.turbotraceContext
const { entryNameMap } = combinedResult.turbotraceContext.entriesTrace!
if (entryNameMap) {
combinedResult.turbotraceContext.entriesTrace!.entryNameMap = new Map(
entryNameMap
)
}
}
}
buildSpinner?.stopAndPersist()
Log.info('Compiled successfully')
return combinedResult
}
export async function webpackBuild() {
const config = NextBuildContext.config!
if (config.experimental.webpackBuildWorker) {
debug('using separate compiler workers')
return await webpackBuildWithWorker()
} else {
debug('building all compilers in same process')
const webpackBuildImpl = require('./impl').webpackBuildImpl
return await webpackBuildImpl()
}
}