diff --git a/packages/next/build/swc/index.js b/packages/next/build/swc/index.js index bd520574fb27212..0af42f4dc0ae743 100644 --- a/packages/next/build/swc/index.js +++ b/packages/next/build/swc/index.js @@ -63,6 +63,7 @@ export async function loadBindings() { if (pendingBindings) { return pendingBindings } + const isCustomTurbopack = await __isCustomTurbopackBinary() pendingBindings = new Promise(async (resolve, reject) => { if (!lockfilePatchPromise.cur) { // always run lockfile check once so that it gets patched @@ -86,7 +87,7 @@ export async function loadBindings() { } try { - return resolve(loadNative()) + return resolve(loadNative(isCustomTurbopack)) } catch (a) { attempts = attempts.concat(a) } @@ -262,7 +263,7 @@ async function loadWasm(importPath = '') { throw attempts } -function loadNative() { +function loadNative(isCustomTurbopack = false) { if (nativeBindings) { return nativeBindings } @@ -375,7 +376,60 @@ function loadNative() { ...options, noOpen: options.noOpen ?? true, } - bindings.startTurboDev(toBuffer(devOptions)) + + if (!isCustomTurbopack) { + bindings.startTurboDev(toBuffer(devOptions)) + } else if (!!__INTERNAL_CUSTOM_TURBOPACK_BINARY) { + console.warn( + `Loading custom turbopack binary from ${__INTERNAL_CUSTOM_TURBOPACK_BINARY}` + ) + + return new Promise((resolve, reject) => { + const spawn = require('next/dist/compiled/cross-spawn') + const args = [] + + Object.entries(devOptions).forEach(([key, value]) => { + let cli_key = `--${key.replace( + /[A-Z]/g, + (m) => '-' + m.toLowerCase() + )}` + if (key === 'dir') { + args.push(value) + } else if (typeof value == 'boolean' && value === true) { + args.push(cli_key) + } else if (typeof value != 'boolean' && !!value) { + args.push(cli_key, value) + } + }) + + console.warn(`Running turbopack with args: [${args.join(' ')}]`) + + const child = spawn(__INTERNAL_CUSTOM_TURBOPACK_BINARY, args, { + stdio: 'inherit', + env: { + ...process.env, + }, + }) + child.on('close', (code) => { + if (code !== 0) { + reject({ + command: `${__INTERNAL_CUSTOM_TURBOPACK_BINARY} ${args.join( + ' ' + )}`, + }) + return + } + resolve(0) + }) + }) + } else if (!!__INTERNAL_CUSTOM_TURBOPACK_BINDINGS) { + console.warn( + `Loading custom turbopack bindings from ${__INTERNAL_CUSTOM_TURBOPACK_BINARY}` + ) + console.warn(`Running turbopack with args: `, devOptions) + + require(__INTERNAL_CUSTOM_TURBOPACK_BINDINGS).startDev(devOptions) + } }, startTrace: (options = {}) => bindings.runTurboTracing(toBuffer({ exact: true, ...options })),