File size: 4,865 Bytes
1e92f2d |
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
import type { COMPILER_INDEXES } from '../../shared/lib/constants'
import * as Log from '../output/log'
import { NextBuildContext } from '../build-context'
import type { BuildTraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin'
import { Worker } from '../../lib/worker'
import origDebug from 'next/dist/compiled/debug'
import path from 'path'
import { exportTraceState, recordTraceEvents } from '../../trace'
import { mergeUseCacheTrackers } from '../webpack/plugins/telemetry-plugin/use-cache-tracker-utils'
import { durationToString } from '../duration-to-string'
const debug = origDebug('next:build:webpack-build')
const ORDERED_COMPILER_NAMES = [
'server',
'edge-server',
'client',
] as (keyof typeof COMPILER_INDEXES)[]
let pluginState: Record<any, any> = {}
function deepMerge(target: any, source: any) {
const result = { ...target, ...source }
for (const key of Object.keys(result)) {
result[key] = Array.isArray(target[key])
? (target[key] = [...target[key], ...(source[key] || [])])
: typeof target[key] == 'object' && typeof source[key] == 'object'
? deepMerge(target[key], source[key])
: result[key]
}
return result
}
async function webpackBuildWithWorker(
compilerNamesArg: typeof ORDERED_COMPILER_NAMES | null
) {
const compilerNames = compilerNamesArg || ORDERED_COMPILER_NAMES
const { nextBuildSpan, ...prunedBuildContext } = NextBuildContext
prunedBuildContext.pluginState = pluginState
const combinedResult = {
duration: 0,
buildTraceContext: {} as BuildTraceContext,
}
for (const compilerName of compilerNames) {
const worker = new Worker(path.join(__dirname, 'impl.js'), {
exposedMethods: ['workerMain'],
debuggerPortOffset: -1,
isolatedMemory: false,
numWorkers: 1,
maxRetries: 0,
forkOptions: {
env: {
NEXT_PRIVATE_BUILD_WORKER: '1',
},
},
}) as Worker & typeof import('./impl')
const curResult = await worker.workerMain({
buildContext: prunedBuildContext,
compilerName,
traceState: {
...exportTraceState(),
defaultParentSpanId: nextBuildSpan?.getId(),
shouldSaveTraceEvents: true,
},
})
if (nextBuildSpan && curResult.debugTraceEvents) {
recordTraceEvents(curResult.debugTraceEvents)
}
// destroy worker so it's not sticking around using memory
await worker.end()
// Update plugin state
pluginState = deepMerge(pluginState, curResult.pluginState)
prunedBuildContext.pluginState = pluginState
if (curResult.telemetryState) {
NextBuildContext.telemetryState = {
...curResult.telemetryState,
useCacheTracker: mergeUseCacheTrackers(
NextBuildContext.telemetryState?.useCacheTracker,
curResult.telemetryState.useCacheTracker
),
}
}
combinedResult.duration += curResult.duration
if (curResult.buildTraceContext?.entriesTrace) {
const { entryNameMap } = curResult.buildTraceContext.entriesTrace!
if (entryNameMap) {
combinedResult.buildTraceContext.entriesTrace =
curResult.buildTraceContext.entriesTrace
combinedResult.buildTraceContext.entriesTrace!.entryNameMap =
entryNameMap
}
if (curResult.buildTraceContext?.chunksTrace) {
const { entryNameFilesMap } = curResult.buildTraceContext.chunksTrace!
if (entryNameFilesMap) {
combinedResult.buildTraceContext.chunksTrace =
curResult.buildTraceContext.chunksTrace!
combinedResult.buildTraceContext.chunksTrace!.entryNameFilesMap =
entryNameFilesMap
}
}
}
}
if (compilerNames.length === 3) {
const durationString = durationToString(combinedResult.duration)
Log.event(`Compiled successfully in ${durationString}`)
}
return combinedResult
}
export async function webpackBuild(
withWorker: boolean,
compilerNames: typeof ORDERED_COMPILER_NAMES | null
): Promise<
| Awaited<ReturnType<typeof webpackBuildWithWorker>>
| Awaited<ReturnType<typeof import('./impl').webpackBuildImpl>>
> {
if (withWorker) {
debug('using separate compiler workers')
return await webpackBuildWithWorker(compilerNames)
} else {
debug('building all compilers in same process')
const webpackBuildImpl = (require('./impl') as typeof import('./impl'))
.webpackBuildImpl
const curResult = await webpackBuildImpl(null)
// Mirror what happens in webpackBuildWithWorker
if (curResult.telemetryState) {
NextBuildContext.telemetryState = {
...curResult.telemetryState,
useCacheTracker: mergeUseCacheTrackers(
NextBuildContext.telemetryState?.useCacheTracker,
curResult.telemetryState.useCacheTracker
),
}
}
return curResult
}
}
|