Skip to content

Commit

Permalink
feat(next/swc): run custom turbopack binary
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Nov 8, 2022
1 parent ca923bc commit 2fed6a3
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions packages/next/build/swc/index.js
Expand Up @@ -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
Expand All @@ -86,7 +87,7 @@ export async function loadBindings() {
}

try {
return resolve(loadNative())
return resolve(loadNative(isCustomTurbopack))
} catch (a) {
attempts = attempts.concat(a)
}
Expand Down Expand Up @@ -262,7 +263,7 @@ async function loadWasm(importPath = '') {
throw attempts
}

function loadNative() {
function loadNative(isCustomTurbopack = false) {
if (nativeBindings) {
return nativeBindings
}
Expand Down Expand Up @@ -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 })),
Expand Down

0 comments on commit 2fed6a3

Please sign in to comment.