Commit 2fbb3893 authored by Luke Sandberg's avatar Luke Sandberg

Add NAPI-based WASM runtime for SWC plugins

Replace wasmtime with a NAPI backend that delegates WASM compilation
and execution to Node.js's V8 engine. This eliminates ~20MB of
wasmtime/Cranelift from the native binary.

Architecture:
- New crate `swc_plugin_backend_napi` implements the
  `swc_plugin_runner::runtime` traits (Runtime, Instance, Caller)
- A global ThreadsafeFunction bridges background SWC transform threads
  to the JS main thread where V8's WebAssembly APIs are available
- Two Caller types: TsfnCaller (background thread, dispatches via TSFN)
  and DirectCaller (JS thread, direct memory access during host
  function callbacks)
- JS-side wasm-manager.ts handles WebAssembly.Module/Instance lifecycle

Files added:
- crates/swc-plugin-backend-napi/ (Cargo.toml + lib.rs)
- packages/next/src/build/swc/wasm-manager.ts

Files modified:
- Cargo.toml: add napi backend to workspace
- crates/next-napi-bindings: switch plugin feature from wasmtime to napi
- turbopack-ecmascript-plugins: switch from WasmtimeRuntime to NapiRuntime
- packages/next/src/build/swc/index.ts: register WASM runtime at load
parent e5b08416
...@@ -4916,7 +4916,7 @@ dependencies = [ ...@@ -4916,7 +4916,7 @@ dependencies = [
"serde_json", "serde_json",
"supports-hyperlinks", "supports-hyperlinks",
"swc_core 58.0.4", "swc_core 58.0.4",
"swc_plugin_backend_wasmtime", "swc_plugin_backend_napi",
"terminal_hyperlink", "terminal_hyperlink",
"terminal_size", "terminal_size",
"tokio", "tokio",
...@@ -8927,6 +8927,17 @@ dependencies = [ ...@@ -8927,6 +8927,17 @@ dependencies = [
"swc_common 19.0.0", "swc_common 19.0.0",
] ]
[[package]]
name = "swc_plugin_backend_napi"
version = "0.0.0"
dependencies = [
"anyhow",
"napi",
"napi-derive",
"once_cell",
"swc_plugin_runner",
]
[[package]] [[package]]
name = "swc_plugin_backend_wasmer" name = "swc_plugin_backend_wasmer"
version = "8.0.0" version = "8.0.0"
...@@ -10731,7 +10742,7 @@ dependencies = [ ...@@ -10731,7 +10742,7 @@ dependencies = [
"styled_jsx", "styled_jsx",
"swc_core 58.0.4", "swc_core 58.0.4",
"swc_emotion", "swc_emotion",
"swc_plugin_backend_wasmtime", "swc_plugin_backend_napi",
"swc_relay", "swc_relay",
"tracing", "tracing",
"turbo-rcstr", "turbo-rcstr",
......
...@@ -12,6 +12,7 @@ members = [ ...@@ -12,6 +12,7 @@ members = [
"crates/next-core", "crates/next-core",
"crates/next-custom-transforms", "crates/next-custom-transforms",
"crates/next-taskless", "crates/next-taskless",
"crates/swc-plugin-backend-napi",
"crates/swc-plugin-backend-wasmtime", "crates/swc-plugin-backend-wasmtime",
"turbopack/crates/*", "turbopack/crates/*",
"turbopack/crates/*/fuzz", "turbopack/crates/*/fuzz",
...@@ -322,6 +323,7 @@ swc_core = { version = "58.0.4", default-features = false, features = [ ...@@ -322,6 +323,7 @@ swc_core = { version = "58.0.4", default-features = false, features = [
"ecma_loader_parking_lot", "ecma_loader_parking_lot",
"parallel_rayon", "parallel_rayon",
] } ] }
swc_plugin_backend_napi = { path = "crates/swc-plugin-backend-napi" }
swc_plugin_backend_wasmtime = { path = "crates/swc-plugin-backend-wasmtime" } swc_plugin_backend_wasmtime = { path = "crates/swc-plugin-backend-wasmtime" }
testing = "19.0.0" testing = "19.0.0"
wasmtime = { version = "29", default-features = false, features = ["cranelift", "runtime"] } wasmtime = { version = "29", default-features = false, features = ["cranelift", "runtime"] }
......
...@@ -18,7 +18,7 @@ plugin = [ ...@@ -18,7 +18,7 @@ plugin = [
"swc_core/__plugin_transform_host", "swc_core/__plugin_transform_host",
"swc_core/__plugin_transform_host_schema_v1", "swc_core/__plugin_transform_host_schema_v1",
"swc_core/plugin_transform_host_native_filesystem_cache", "swc_core/plugin_transform_host_native_filesystem_cache",
"dep:swc_plugin_backend_wasmtime", "dep:swc_plugin_backend_napi",
"next-custom-transforms/plugin", "next-custom-transforms/plugin",
"next-core/plugin", "next-core/plugin",
"turbopack-ecmascript-plugins", "turbopack-ecmascript-plugins",
...@@ -54,7 +54,7 @@ ignored = [ ...@@ -54,7 +54,7 @@ ignored = [
# directly import it # directly import it
"turbopack-ecmascript-plugins", "turbopack-ecmascript-plugins",
# used conditionally behind the `plugin` feature flag # used conditionally behind the `plugin` feature flag
"swc_plugin_backend_wasmtime", "swc_plugin_backend_napi",
] ]
[dependencies] [dependencies]
...@@ -110,7 +110,7 @@ swc_core = { workspace = true, features = [ ...@@ -110,7 +110,7 @@ swc_core = { workspace = true, features = [
# Dependencies for the native, non-wasm32 build. # Dependencies for the native, non-wasm32 build.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies] [target.'cfg(not(target_arch = "wasm32"))'.dependencies]
lightningcss-napi = { workspace = true } lightningcss-napi = { workspace = true }
swc_plugin_backend_wasmtime = { workspace = true, optional = true } swc_plugin_backend_napi = { workspace = true, optional = true }
lightningcss = { workspace = true } lightningcss = { workspace = true }
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
turbo-rcstr = { workspace = true, features = ["napi"] } turbo-rcstr = { workspace = true, features = ["napi"] }
......
...@@ -34,6 +34,8 @@ use std::{ ...@@ -34,6 +34,8 @@ use std::{
}; };
use anyhow::{Context as _, anyhow, bail}; use anyhow::{Context as _, anyhow, bail};
#[cfg(feature = "plugin")]
use napi::JsObject;
use napi::bindgen_prelude::*; use napi::bindgen_prelude::*;
use napi_derive::napi; use napi_derive::napi;
use next_custom_transforms::chain_transforms::{TransformOptions, custom_before_pass}; use next_custom_transforms::chain_transforms::{TransformOptions, custom_before_pass};
...@@ -134,7 +136,7 @@ impl Task for TransformTask { ...@@ -134,7 +136,7 @@ impl Task for TransformTask {
options.swc.runtime_options = options.swc.runtime_options =
swc_core::base::config::RuntimeOptions::default() swc_core::base::config::RuntimeOptions::default()
.plugin_runtime(std::sync::Arc::new( .plugin_runtime(std::sync::Arc::new(
swc_plugin_backend_wasmtime::WasmtimeRuntime, swc_plugin_backend_napi::NapiRuntime,
)); ));
} }
...@@ -259,6 +261,14 @@ fn test_deser() { ...@@ -259,6 +261,14 @@ fn test_deser() {
println!("{tr:#?}"); println!("{tr:#?}");
} }
/// Register the NAPI-based WASM plugin runtime.
/// Must be called from JS before any SWC transforms that use plugins.
#[cfg(feature = "plugin")]
#[napi]
pub fn register_wasm_plugin_runtime(env: Env, js_manager: JsObject) -> napi::Result<()> {
swc_plugin_backend_napi::register_wasm_runtime(&env, &js_manager)
}
#[test] #[test]
fn test_deserialize_transform_regenerator() { fn test_deserialize_transform_regenerator() {
const JSON_STR: &str = r#"{"jsc":{"parser":{"syntax":"ecmascript","dynamicImport":true,"jsx":true},"transform":{ "regenerator": { "importPath": "foo" }, "react":{"runtime":"automatic","pragma":"React.createElement","pragmaFrag":"React.Fragment","throwIfNamespace":true,"development":false,"useBuiltins":true}},"target":"es5"},"filename":"/Users/timneutkens/projects/next.js/packages/next/dist/client/next.js","sourceMaps":false,"sourceFileName":"/Users/timneutkens/projects/next.js/packages/next/dist/client/next.js"}"#; const JSON_STR: &str = r#"{"jsc":{"parser":{"syntax":"ecmascript","dynamicImport":true,"jsx":true},"transform":{ "regenerator": { "importPath": "foo" }, "react":{"runtime":"automatic","pragma":"React.createElement","pragmaFrag":"React.Fragment","throwIfNamespace":true,"development":false,"useBuiltins":true}},"target":"es5"},"filename":"/Users/timneutkens/projects/next.js/packages/next/dist/client/next.js","sourceMaps":false,"sourceFileName":"/Users/timneutkens/projects/next.js/packages/next/dist/client/next.js"}"#;
......
[package]
name = "swc_plugin_backend_napi"
edition = "2024"
version = "0.0.0"
publish = false
[lints]
workspace = true
[dependencies]
anyhow = { workspace = true }
once_cell = { workspace = true }
napi = { workspace = true }
napi-derive = { workspace = true }
swc_plugin_runner = { version = "25.0.0", default-features = false }
This diff is collapsed.
...@@ -1714,6 +1714,14 @@ function loadNative(importPath?: string): Binding { ...@@ -1714,6 +1714,14 @@ function loadNative(importPath?: string): Binding {
return bindings.codeFrameColumns(source, location, options) ?? undefined return bindings.codeFrameColumns(source, location, options) ?? undefined
}, },
} }
// Register the NAPI-based WASM plugin runtime so SWC plugins run via
// Node's V8 WebAssembly engine instead of wasmtime.
if (typeof bindings.registerWasmPluginRuntime === 'function') {
const { wasmManager } = (require('./wasm-manager') as typeof import('./wasm-manager'))
bindings.registerWasmPluginRuntime(wasmManager)
}
return loadedBindings! return loadedBindings!
} }
......
/**
* JS-side WASM module and instance manager for the NAPI-based SWC plugin
* runtime.
*
* This module is called from Rust via a ThreadsafeFunction. The Rust side
* dispatches WASM operations (compile, instantiate, transform, memory
* read/write, alloc/free) to the JS main thread, where V8's WebAssembly APIs
* are available.
*
* Host function callbacks during WASM execution are handled by a Rust
* `dispatch_fn` that runs synchronously on the JS thread with direct memory
* access (no cross-thread overhead).
*/
interface WasmModuleEntry {
module: WebAssembly.Module
}
interface WasmInstanceEntry {
instance: WebAssembly.Instance
memory: WebAssembly.Memory
}
interface HostFnDescriptor {
name: string
paramCount: number
resultCount: number
index: number
}
type DispatchFn = (
index: number,
args: number[],
memory: WebAssembly.Memory,
allocFn: Function,
freeFn: Function
) => number[] | undefined
let nextId = 1
const modules = new Map<number, WasmModuleEntry>()
const instances = new Map<number, WasmInstanceEntry>()
export const wasmManager = {
compileModule(wasmBytes: Buffer): number {
const id = nextId++
// WebAssembly.Module() is synchronous in Node.js (no size limit unlike
// browsers).
const module = new WebAssembly.Module(wasmBytes)
modules.set(id, { module })
return id
},
instantiateModule(
moduleId: number,
descriptors: HostFnDescriptor[],
dispatchFn: DispatchFn
): number {
const entry = modules.get(moduleId)
if (!entry) {
throw new Error(`WASM module ${moduleId} not found`)
}
const envImports: Record<string, Function> = {}
// Mutable context set after instantiation (circular: imports reference
// exports). Wrapped in an object so loop closures capture a stable ref.
const ctx: {
memory: WebAssembly.Memory | null
allocFn: Function | null
freeFn: Function | null
} = { memory: null, allocFn: null, freeFn: null }
for (const desc of descriptors) {
const { index, paramCount, resultCount } = desc
envImports[desc.name] = (...args: number[]) => {
const results = dispatchFn(
index,
args.slice(0, paramCount),
ctx.memory!,
ctx.allocFn!,
ctx.freeFn!
)
if (resultCount === 0) return undefined
if (results && resultCount === 1) return results[0]
return results?.[0]
}
}
const instance = new WebAssembly.Instance(entry.module, { env: envImports })
ctx.memory = instance.exports.memory as WebAssembly.Memory
ctx.allocFn = instance.exports.__alloc as Function
ctx.freeFn = instance.exports.__free as Function
const memory = ctx.memory
const id = nextId++
instances.set(id, { instance, memory })
return id
},
callTransform(
instanceId: number,
programPtr: number,
programLen: number,
unresolvedMark: number,
shouldEnableCommentsProxy: number
): number {
const entry = instances.get(instanceId)
if (!entry) {
throw new Error(`WASM instance ${instanceId} not found`)
}
const fn_ = entry.instance.exports
.__transform_plugin_process_impl as Function
return fn_(
programPtr,
programLen,
unresolvedMark,
shouldEnableCommentsProxy
)
},
readMemory(instanceId: number, ptr: number, len: number): Buffer {
const entry = instances.get(instanceId)
if (!entry) {
throw new Error(`WASM instance ${instanceId} not found`)
}
const view = new Uint8Array(entry.memory.buffer, ptr, len)
// Copy the data out (the buffer may be detached on memory growth)
return Buffer.from(view)
},
writeMemory(instanceId: number, ptr: number, data: Buffer): void {
const entry = instances.get(instanceId)
if (!entry) {
throw new Error(`WASM instance ${instanceId} not found`)
}
new Uint8Array(entry.memory.buffer).set(data, ptr)
},
callAlloc(instanceId: number, size: number): number {
const entry = instances.get(instanceId)
if (!entry) {
throw new Error(`WASM instance ${instanceId} not found`)
}
return (entry.instance.exports.__alloc as Function)(size)
},
callFree(instanceId: number, ptr: number, size: number): number {
const entry = instances.get(instanceId)
if (!entry) {
throw new Error(`WASM instance ${instanceId} not found`)
}
return (entry.instance.exports.__free as Function)(ptr, size)
},
callGetDiag(instanceId: number): number {
const entry = instances.get(instanceId)
if (!entry) {
throw new Error(`WASM instance ${instanceId} not found`)
}
return (
entry.instance.exports.__get_transform_plugin_core_pkg_diag as Function
)()
},
cloneModule(moduleId: number): number {
const entry = modules.get(moduleId)
if (!entry) {
throw new Error(`WASM module ${moduleId} not found`)
}
// WebAssembly.Module is internally reference-counted; sharing is cheap
const id = nextId++
modules.set(id, { module: entry.module })
return id
},
dropModule(moduleId: number): void {
modules.delete(moduleId)
},
dropInstance(instanceId: number): void {
instances.delete(instanceId)
},
}
...@@ -17,10 +17,10 @@ swc_ecma_transform_plugin = [ ...@@ -17,10 +17,10 @@ swc_ecma_transform_plugin = [
# Use granular plugin features to avoid pulling in wasmer via __plugin_transform_env_native. # Use granular plugin features to avoid pulling in wasmer via __plugin_transform_env_native.
# __plugin_transform_host provides the plugin runner types and proxy re-exports. # __plugin_transform_host provides the plugin runner types and proxy re-exports.
# __plugin_transform_host_schema_v1 provides the schema version support. # __plugin_transform_host_schema_v1 provides the schema version support.
# We provide WasmtimeRuntime explicitly, so we don't need swc's env-native backend wiring. # We provide NapiRuntime explicitly, so we don't need swc's env-native backend wiring.
"swc_core/__plugin_transform_host", "swc_core/__plugin_transform_host",
"swc_core/__plugin_transform_host_schema_v1", "swc_core/__plugin_transform_host_schema_v1",
"swc_plugin_backend_wasmtime" "swc_plugin_backend_napi"
] ]
[lints] [lints]
...@@ -46,7 +46,7 @@ turbopack-ecmascript = { workspace = true } ...@@ -46,7 +46,7 @@ turbopack-ecmascript = { workspace = true }
styled_components = { workspace = true } styled_components = { workspace = true }
styled_jsx = { workspace = true } styled_jsx = { workspace = true }
swc_core = { workspace = true, features = ["ecma_ast", "ecma_visit", "common"] } swc_core = { workspace = true, features = ["ecma_ast", "ecma_visit", "common"] }
swc_plugin_backend_wasmtime = { workspace = true, optional = true } swc_plugin_backend_napi = { workspace = true, optional = true }
swc_emotion = { workspace = true } swc_emotion = { workspace = true }
swc_relay = { workspace = true } swc_relay = { workspace = true }
...@@ -28,11 +28,11 @@ impl SwcPluginModule { ...@@ -28,11 +28,11 @@ impl SwcPluginModule {
use swc_core::plugin_runner::plugin_module_bytes::{ use swc_core::plugin_runner::plugin_module_bytes::{
CompiledPluginModuleBytes, RawPluginModuleBytes, CompiledPluginModuleBytes, RawPluginModuleBytes,
}; };
use swc_plugin_backend_wasmtime::WasmtimeRuntime; use swc_plugin_backend_napi::NapiRuntime;
Self { Self {
plugin: CompiledPluginModuleBytes::from_raw_module( plugin: CompiledPluginModuleBytes::from_raw_module(
&WasmtimeRuntime, &NapiRuntime,
RawPluginModuleBytes::new(plugin_name.to_string(), plugin_bytes), RawPluginModuleBytes::new(plugin_name.to_string(), plugin_bytes),
), ),
name: plugin_name, name: plugin_name,
...@@ -181,7 +181,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -181,7 +181,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
plugin::proxies::{COMMENTS, HostCommentsStorage}, plugin::proxies::{COMMENTS, HostCommentsStorage},
plugin_runner::plugin_module_bytes::CompiledPluginModuleBytes, plugin_runner::plugin_module_bytes::CompiledPluginModuleBytes,
}; };
use swc_plugin_backend_wasmtime::WasmtimeRuntime; use swc_plugin_backend_napi::NapiRuntime;
use turbo_tasks::TryJoinIterExt; use turbo_tasks::TryJoinIterExt;
let plugins = self let plugins = self
...@@ -192,7 +192,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -192,7 +192,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
Ok(( Ok((
plugin_module.name.clone(), plugin_module.name.clone(),
config.clone(), config.clone(),
Box::new(plugin_module.plugin.clone_module(&WasmtimeRuntime)), Box::new(plugin_module.plugin.clone_module(&NapiRuntime)),
)) ))
}) })
.try_join() .try_join()
...@@ -259,7 +259,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -259,7 +259,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
None, None,
plugin_module, plugin_module,
Some(plugin_config), Some(plugin_config),
Arc::new(WasmtimeRuntime), Arc::new(NapiRuntime),
); );
serialized_program = Either::Right( serialized_program = Either::Right(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment