@@ -2,9 +2,8 @@ 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 'next/dist/compiled/jest- worker'
5+ import { Worker } from '../../lib/ worker'
66import origDebug from 'next/dist/compiled/debug'
7- import type { ChildProcess } from 'child_process'
87import path from 'path'
98import { exportTraceState , recordTraceEvents } from '../../trace'
109
@@ -38,44 +37,24 @@ async function webpackBuildWithWorker(
3837
3938 prunedBuildContext . pluginState = pluginState
4039
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- } ,
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' ,
5148 } ,
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- }
49+ } ,
50+ } ) as Worker & typeof import ( './impl' )
7051
7152 const combinedResult = {
7253 duration : 0 ,
7354 buildTraceContext : { } as BuildTraceContext ,
7455 }
7556
7657 for ( const compilerName of compilerNames ) {
77- const worker = getWorker ( compilerName )
78-
7958 const curResult = await worker . workerMain ( {
8059 buildContext : prunedBuildContext ,
8160 compilerName,
@@ -88,8 +67,6 @@ async function webpackBuildWithWorker(
8867 if ( nextBuildSpan && curResult . debugTraceEvents ) {
8968 recordTraceEvents ( curResult . debugTraceEvents )
9069 }
91- // destroy worker so it's not sticking around using memory
92- await worker . end ( )
9370
9471 // Update plugin state
9572 pluginState = deepMerge ( pluginState , curResult . pluginState )
@@ -125,6 +102,9 @@ async function webpackBuildWithWorker(
125102 }
126103 }
127104
105+ // destroy worker so it's not sticking around using memory
106+ worker . end ( )
107+
128108 if ( compilerNames . length === 3 ) {
129109 Log . event ( 'Compiled successfully' )
130110 }
0 commit comments