|
|
import path from 'path' |
|
|
|
|
|
import { Worker } from '../../lib/worker' |
|
|
import { NextBuildContext } from '../build-context' |
|
|
|
|
|
async function turbopackBuildWithWorker() { |
|
|
try { |
|
|
const worker = new Worker(path.join(__dirname, 'impl.js'), { |
|
|
exposedMethods: ['workerMain', 'waitForShutdown'], |
|
|
debuggerPortOffset: -1, |
|
|
isolatedMemory: false, |
|
|
numWorkers: 1, |
|
|
maxRetries: 0, |
|
|
forkOptions: { |
|
|
env: { |
|
|
NEXT_PRIVATE_BUILD_WORKER: '1', |
|
|
}, |
|
|
}, |
|
|
}) as Worker & typeof import('./impl') |
|
|
const { nextBuildSpan, ...prunedBuildContext } = NextBuildContext |
|
|
const result = await worker.workerMain({ |
|
|
buildContext: prunedBuildContext, |
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
result.shutdownPromise = worker.waitForShutdown().then(() => { |
|
|
worker.end() |
|
|
}) |
|
|
|
|
|
return result |
|
|
} catch (err: any) { |
|
|
|
|
|
|
|
|
if (err.type === 'Error') { |
|
|
const error = new Error(err.message) |
|
|
if (err.name) { |
|
|
error.name = err.name |
|
|
} |
|
|
if (err.cause) { |
|
|
error.cause = err.cause |
|
|
} |
|
|
error.message = err.message |
|
|
error.stack = err.stack |
|
|
throw error |
|
|
} |
|
|
throw err |
|
|
} |
|
|
} |
|
|
|
|
|
export function turbopackBuild( |
|
|
withWorker: boolean |
|
|
): ReturnType<typeof import('./impl').turbopackBuild> { |
|
|
const nextBuildSpan = NextBuildContext.nextBuildSpan! |
|
|
return nextBuildSpan |
|
|
.traceChild('run-turbopack-compiler') |
|
|
.traceAsyncFn(async () => { |
|
|
if (withWorker) { |
|
|
return await turbopackBuildWithWorker() |
|
|
} else { |
|
|
const build = (require('./impl') as typeof import('./impl')) |
|
|
.turbopackBuild |
|
|
return await build() |
|
|
} |
|
|
}) |
|
|
} |
|
|
|