4172a4c0a6
* Set up next-swc package with no custom transforms * Add hook_optimizer transform * Fix lint * Build next-swc on CI * Add toolchain in rust action * Build binaries in manual workflow * Commit from GitHub Actions (Build next-swc native binaries) * Update dependencies * Update swc and use stable rust * Build next-swc binaries * Test compiled code * Dedupe @node-rs/helper * Add workflow to check next-swc binaries * Fix check native * PR feedback * PR feedback * Pr feedback * Build next-swc binaries * Combine native workflows * Add docs for contributors on less common platforms * PR feedback * Compare JsWord instead of converting to string * Fix workflow formatting * Add docs for building binaries for CI * Build next-swc binaries * Fix workflow if syntax * Add license info to copied code Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
const { loadBinding } = require('@node-rs/helper')
|
|
const path = require('path')
|
|
|
|
/**
|
|
* __dirname means load native addon from current dir
|
|
* 'next-swc' is the name of native addon
|
|
* the second arguments was decided by `napi.name` field in `package.json`
|
|
* the third arguments was decided by `name` field in `package.json`
|
|
* `loadBinding` helper will load `next-swc.[PLATFORM].node` from `__dirname` first
|
|
* If failed to load addon, it will fallback to load from `next-swc-[PLATFORM]`
|
|
*/
|
|
const bindings = loadBinding(
|
|
path.join(__dirname, '../../../native'),
|
|
'next-swc',
|
|
'next-swc'
|
|
)
|
|
|
|
async function transform(src, options) {
|
|
const isModule = typeof src !== 'string'
|
|
options = options || {}
|
|
|
|
if (options?.jsc?.parser) {
|
|
options.jsc.parser.syntax = options.jsc.parser.syntax ?? 'ecmascript'
|
|
}
|
|
|
|
const { plugin, ...newOptions } = options
|
|
|
|
if (plugin) {
|
|
const m =
|
|
typeof src === 'string'
|
|
? await this.parse(src, options?.jsc?.parser)
|
|
: src
|
|
return this.transform(plugin(m), newOptions)
|
|
}
|
|
|
|
return bindings.transform(
|
|
isModule ? JSON.stringify(src) : src,
|
|
isModule,
|
|
toBuffer(newOptions)
|
|
)
|
|
}
|
|
|
|
function transformSync(src, options) {
|
|
const isModule = typeof src !== 'string'
|
|
options = options || {}
|
|
|
|
if (options?.jsc?.parser) {
|
|
options.jsc.parser.syntax = options.jsc.parser.syntax ?? 'ecmascript'
|
|
}
|
|
|
|
const { plugin, ...newOptions } = options
|
|
|
|
if (plugin) {
|
|
const m =
|
|
typeof src === 'string' ? this.parseSync(src, options?.jsc?.parser) : src
|
|
return this.transformSync(plugin(m), newOptions)
|
|
}
|
|
|
|
return bindings.transformSync(
|
|
isModule ? JSON.stringify(src) : src,
|
|
isModule,
|
|
toBuffer(newOptions)
|
|
)
|
|
}
|
|
|
|
function toBuffer(t) {
|
|
return Buffer.from(JSON.stringify(t))
|
|
}
|
|
|
|
module.exports.transform = transform
|
|
module.exports.transformSync = transformSync
|