@@ -2,8 +2,9 @@ import type { COMPILER_INDEXES } from '../../shared/lib/constants'
22import * as Log from '../output/log'
33import { NextBuildContext } from '../build-context'
44import type { BuildTraceContext } from '../webpack/plugins/next-trace-entrypoints-plugin'
5- import { Worker } from '../../lib/ worker'
5+ import { Worker } from 'next/dist/compiled/jest- worker'
66import origDebug from 'next/dist/compiled/debug'
7+ import type { ChildProcess } from 'child_process'
78import path from 'path'
89import { exportTraceState , recordTraceEvents } from '../../trace'
910
@@ -37,24 +38,44 @@ async function webpackBuildWithWorker(
3738
3839 prunedBuildContext . pluginState = pluginState
3940
40- const worker = new Worker ( path . join ( __dirname , 'impl.js' ) , {
41- exposedMethods : [ 'workerMain' ] ,
42- numWorkers : 1 ,
43- maxRetries : 0 ,
44- forkOptions : {
45- env : {
46- ...process . env ,
47- NEXT_PRIVATE_BUILD_WORKER : '1' ,
41+ const getWorker = ( compilerName : string ) => {
42+ const _worker = new Worker ( path . join ( __dirname , 'impl.js' ) , {
43+ exposedMethods : [ 'workerMain' ] ,
44+ numWorkers : 1 ,
45+ maxRetries : 0 ,
46+ forkOptions : {
47+ env : {
48+ ...process . env ,
49+ NEXT_PRIVATE_BUILD_WORKER : '1' ,
50+ } ,
4851 } ,
49- } ,
50- } ) as Worker & typeof import ( './impl' )
52+ } ) as Worker & typeof import ( './impl' )
53+ _worker . getStderr ( ) . pipe ( process . stderr )
54+ _worker . getStdout ( ) . pipe ( process . stdout )
55+
56+ for ( const worker of ( ( _worker as any ) . _workerPool ?. _workers || [ ] ) as {
57+ _child : ChildProcess
58+ } [ ] ) {
59+ worker . _child . on ( 'exit' , ( code , signal ) => {
60+ if ( code || ( signal && signal !== 'SIGINT' ) ) {
61+ debug (
62+ `Compiler ${ compilerName } unexpectedly exited with code: ${ code } and signal: ${ signal } `
63+ )
64+ }
65+ } )
66+ }
67+
68+ return _worker
69+ }
5170
5271 const combinedResult = {
5372 duration : 0 ,
5473 buildTraceContext : { } as BuildTraceContext ,
5574 }
5675
5776 for ( const compilerName of compilerNames ) {
77+ const worker = getWorker ( compilerName )
78+
5879 const curResult = await worker . workerMain ( {
5980 buildContext : prunedBuildContext ,
6081 compilerName,
@@ -67,6 +88,8 @@ async function webpackBuildWithWorker(
6788 if ( nextBuildSpan && curResult . debugTraceEvents ) {
6889 recordTraceEvents ( curResult . debugTraceEvents )
6990 }
91+ // destroy worker so it's not sticking around using memory
92+ await worker . end ( )
7093
7194 // Update plugin state
7295 pluginState = deepMerge ( pluginState , curResult . pluginState )
@@ -102,9 +125,6 @@ async function webpackBuildWithWorker(
102125 }
103126 }
104127
105- // destroy worker so it's not sticking around using memory
106- worker . end ( )
107-
108128 if ( compilerNames . length === 3 ) {
109129 Log . event ( 'Compiled successfully' )
110130 }
0 commit comments