2021-11-25 00:33:34 +01:00
|
|
|
import { platform, arch } from 'os'
|
|
|
|
import { platformArchTriples } from '@napi-rs/triples'
|
|
|
|
import Log from '../output/log'
|
2021-07-27 17:07:28 +02:00
|
|
|
|
2021-10-29 20:56:56 +02:00
|
|
|
const ArchName = arch()
|
|
|
|
const PlatformName = platform()
|
2021-11-25 00:33:34 +01:00
|
|
|
const triples = platformArchTriples[PlatformName][ArchName]
|
2021-10-25 19:23:44 +02:00
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
async function loadBindings() {
|
|
|
|
return (await loadWasm()) || loadNative()
|
|
|
|
}
|
2021-11-17 20:01:02 +01:00
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
async function loadWasm() {
|
2021-11-17 20:01:02 +01:00
|
|
|
// Try to load wasm bindings
|
|
|
|
for (let specifier of ['@next/swc-wasm-web', '@next/swc-wasm-nodejs']) {
|
2021-10-29 20:56:56 +02:00
|
|
|
try {
|
2021-11-25 00:33:34 +01:00
|
|
|
let bindings = await import(specifier)
|
|
|
|
if (specifier === '@next/swc-wasm-web') {
|
|
|
|
bindings = await bindings.default()
|
2021-11-17 20:01:02 +01:00
|
|
|
}
|
2021-11-25 00:33:34 +01:00
|
|
|
return {
|
|
|
|
transform(src, options) {
|
|
|
|
return Promise.resolve(
|
|
|
|
bindings.transformSync(src.toString(), options)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
minify(src, options) {
|
|
|
|
return Promise.resolve(bindings.minifySync(src.toString(), options))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
} catch (e) {}
|
2021-10-29 20:56:56 +02:00
|
|
|
}
|
2021-11-25 00:33:34 +01:00
|
|
|
}
|
2021-10-29 20:56:56 +02:00
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
function loadNative() {
|
|
|
|
let bindings
|
|
|
|
let loadError
|
2021-11-17 20:01:02 +01:00
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
for (const triple of triples) {
|
2021-11-17 20:01:02 +01:00
|
|
|
try {
|
2021-11-25 00:33:34 +01:00
|
|
|
bindings = require(`@next/swc/native/next-swc.${triple.platformArchABI}.node`)
|
|
|
|
Log.info('Using locally built binary of @next/swc')
|
|
|
|
break
|
2021-11-17 20:01:02 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e?.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
loadError = e
|
|
|
|
}
|
2021-10-29 20:56:56 +02:00
|
|
|
}
|
2021-11-25 00:33:34 +01:00
|
|
|
}
|
2021-11-21 12:59:56 +01:00
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
for (const triple of triples) {
|
2021-11-21 12:59:56 +01:00
|
|
|
try {
|
2021-11-25 00:33:34 +01:00
|
|
|
bindings = require(`@next/swc-${triple.platformArchABI}`)
|
|
|
|
break
|
2021-11-21 12:59:56 +01:00
|
|
|
} catch (e) {
|
|
|
|
if (e?.code !== 'MODULE_NOT_FOUND') {
|
|
|
|
loadError = e
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 20:56:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
if (bindings) {
|
|
|
|
return {
|
|
|
|
transform(src, options) {
|
2021-11-25 15:00:14 +01:00
|
|
|
const isModule =
|
|
|
|
typeof src !== undefined &&
|
|
|
|
typeof src !== 'string' &&
|
|
|
|
!Buffer.isBuffer(src)
|
2021-11-25 00:33:34 +01:00
|
|
|
options = options || {}
|
|
|
|
|
|
|
|
if (options?.jsc?.parser) {
|
|
|
|
options.jsc.parser.syntax = options.jsc.parser.syntax ?? 'ecmascript'
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings.transform(
|
|
|
|
isModule ? JSON.stringify(src) : src,
|
|
|
|
isModule,
|
|
|
|
toBuffer(options)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
transformSync(src, options) {
|
2021-11-25 15:00:14 +01:00
|
|
|
if (typeof src === undefined) {
|
|
|
|
throw new Error(
|
|
|
|
"transformSync doesn't implement reading the file from filesystem"
|
|
|
|
)
|
|
|
|
} else if (Buffer.isBuffer(src)) {
|
|
|
|
throw new Error(
|
|
|
|
"transformSync doesn't implement taking the source code as Buffer"
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const isModule = typeof src !== 'string'
|
2021-11-25 00:33:34 +01:00
|
|
|
options = options || {}
|
|
|
|
|
|
|
|
if (options?.jsc?.parser) {
|
|
|
|
options.jsc.parser.syntax = options.jsc.parser.syntax ?? 'ecmascript'
|
|
|
|
}
|
|
|
|
|
|
|
|
return bindings.transformSync(
|
|
|
|
isModule ? JSON.stringify(src) : src,
|
|
|
|
isModule,
|
|
|
|
toBuffer(options)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
|
|
|
|
minify(src, options) {
|
|
|
|
return bindings.minify(toBuffer(src), toBuffer(options ?? {}))
|
|
|
|
},
|
|
|
|
|
|
|
|
minifySync(src, options) {
|
|
|
|
return bindings.minifySync(toBuffer(src), toBuffer(options ?? {}))
|
|
|
|
},
|
|
|
|
|
|
|
|
bundle(options) {
|
|
|
|
return bindings.bundle(toBuffer(options))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 20:56:56 +02:00
|
|
|
if (loadError) {
|
|
|
|
console.error(loadError)
|
|
|
|
}
|
2021-10-25 19:23:44 +02:00
|
|
|
|
|
|
|
Log.error(
|
2021-10-29 20:56:56 +02:00
|
|
|
`Failed to load SWC binary, see more info here: https://nextjs.org/docs/messages/failed-loading-swc`
|
2021-10-25 19:23:44 +02:00
|
|
|
)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
2021-07-27 17:07:28 +02:00
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
function toBuffer(t) {
|
|
|
|
return Buffer.from(JSON.stringify(t))
|
2021-07-27 17:07:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
export async function transform(src, options) {
|
|
|
|
let bindings = await loadBindings()
|
|
|
|
return bindings.transform(src, options)
|
2021-07-27 17:07:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
export function transformSync(src, options) {
|
|
|
|
let bindings = loadNative()
|
|
|
|
return bindings.transformSync(src, options)
|
2021-07-27 17:07:28 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
export async function minify(src, options) {
|
|
|
|
let bindings = await loadBindings()
|
|
|
|
return bindings.minify(src, options)
|
2021-09-08 00:57:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-25 00:33:34 +01:00
|
|
|
export function minifySync(src, options) {
|
|
|
|
let bindings = loadNative()
|
|
|
|
return bindings.minifySync(src, options)
|
2021-09-08 00:57:47 +02:00
|
|
|
}
|
|
|
|
|
2021-11-04 13:23:25 +01:00
|
|
|
export async function bundle(options) {
|
2021-11-25 00:33:34 +01:00
|
|
|
let bindings = loadNative()
|
2021-11-04 13:23:25 +01:00
|
|
|
return bindings.bundle(toBuffer(options))
|
|
|
|
}
|