Commit 1e7b3e3f authored by Luke Sandberg's avatar Luke Sandberg

Replace wasmer with wasmtime for SWC plugin WASM engine

Swap out swc_plugin_backend_wasmer (wasmer 6.1.0-rc) with a new
swc_plugin_backend_wasmtime crate to reduce native binary size.

- Create crates/swc-plugin-backend-wasmtime implementing the
  swc_plugin_runner Runtime/Instance/Caller traits via wasmtime
- Update workspace Cargo.toml: add wasmtime deps, remove wasmer deps
  and release profiles
- Wire WasmtimeRuntime into both code paths:
  - Turbopack: turbopack-ecmascript-plugins
  - Webpack/SWC NAPI: next-napi-bindings transform.rs
- Remove swc_core/plugin_backend_wasmer feature flag usage
- Integration points designed for future swappability (e.g. Node NAPI
  WASM engine)
parent cd49df2d
......@@ -12,6 +12,7 @@ members = [
"crates/next-core",
"crates/next-custom-transforms",
"crates/next-taskless",
"crates/swc-plugin-backend-wasmtime",
"turbopack/crates/*",
"turbopack/crates/*/fuzz",
"turbopack/xtask",
......@@ -69,7 +70,7 @@ opt-level = 3
[profile.release.package.next-api]
opt-level = 3
[profile.release.package.wasmer-wasix]
[profile.release.package.swc_plugin_backend_wasmtime]
opt-level = "s"
[profile.release.package.cranelift-codegen]
......@@ -150,9 +151,6 @@ opt-level = 3
[profile.release.package.auto-hash-map]
opt-level = 3
[profile.release.package.webc]
opt-level = "s"
[profile.release.package.turbopack-nodejs]
opt-level = 3
......@@ -192,12 +190,6 @@ opt-level = "s"
[profile.release.package.swc_ecma_compat_es2022]
opt-level = "s"
[profile.release.package.wasmer-config]
opt-level = "s"
[profile.release.package.wasmer-compiler]
opt-level = "s"
[profile.release.package.swc_ecma_utils]
opt-level = 3
......@@ -246,21 +238,9 @@ opt-level = 3
[profile.release.package.serde]
opt-level = 3
[profile.release.package.wasmer]
opt-level = "s"
[profile.release.package.wasmer-vm]
opt-level = "s"
[profile.release.package.wasmer-compiler-cranelift]
opt-level = "s"
[profile.release.package.regalloc2]
opt-level = "s"
[profile.release.package.swc_plugin_backend_wasmer]
opt-level = "s"
[profile.release.package.globset]
opt-level = "s"
......@@ -342,8 +322,10 @@ swc_core = { version = "57.0.0", features = [
"ecma_loader_parking_lot",
"parallel_rayon",
] }
swc_plugin_backend_wasmer = { version = "7.0.0" }
swc_plugin_backend_wasmtime = { path = "crates/swc-plugin-backend-wasmtime" }
testing = "19.0.0"
wasmtime = { version = "29", default-features = false, features = ["cranelift", "runtime"] }
wasmtime-wasi = { version = "29", default-features = false, features = ["preview1"] }
# Keep consistent with preset_env_base through swc_core
browserslist-rs = "0.19.0"
......
......@@ -74,6 +74,6 @@ preset_env_base = { workspace = true }
[dev-dependencies]
testing = { workspace = true }
# effectively enable the "plugin" feature for tests, plus swc_core/plugin_backend_wasmer
swc_core = { workspace = true, features = ["testing_transform", "plugin_transform_host_native", "plugin_backend_wasmer"] }
# effectively enable the "plugin" feature for tests
swc_core = { workspace = true, features = ["testing_transform", "plugin_transform_host_native"] }
turbopack-ecmascript-plugins = { workspace = true, features = ["swc_ecma_transform_plugin"] }
......@@ -17,7 +17,7 @@ plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/plugin_transform_host_native_filesystem_cache",
"swc_core/plugin_transform_host_native_shared_runtime",
"swc_core/plugin_backend_wasmer",
"dep:swc_plugin_backend_wasmtime",
"next-custom-transforms/plugin",
"next-core/plugin",
"turbopack-ecmascript-plugins",
......@@ -52,6 +52,8 @@ ignored = [
# the plugins feature needs to set a feature on this transitively depended-on package, we never
# directly import it
"turbopack-ecmascript-plugins",
# used conditionally behind the `plugin` feature flag
"swc_plugin_backend_wasmtime",
]
[dependencies]
......@@ -107,6 +109,7 @@ swc_core = { workspace = true, features = [
# Dependencies for the native, non-wasm32 build.
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
lightningcss-napi = { workspace = true }
swc_plugin_backend_wasmtime = { workspace = true, optional = true }
lightningcss = { workspace = true }
tokio = { workspace = true, features = ["full"] }
turbo-rcstr = { workspace = true, features = ["napi"] }
......
......@@ -129,6 +129,15 @@ impl Task for TransformTask {
let mut options = options.patch(&fm);
options.swc.unresolved_mark = Some(unresolved_mark);
#[cfg(feature = "plugin")]
{
options.swc.runtime_options =
swc_core::base::config::RuntimeOptions::default()
.plugin_runtime(std::sync::Arc::new(
swc_plugin_backend_wasmtime::WasmtimeRuntime,
));
}
let cm = self.c.cm.clone();
let file = fm.clone();
......
[package]
name = "swc_plugin_backend_wasmtime"
edition = "2024"
version = "0.0.0"
publish = false
[lints]
workspace = true
[dependencies]
anyhow = { workspace = true }
once_cell = { workspace = true }
swc_plugin_runner = { version = "24.0.0", default-features = false }
wasmtime = { workspace = true }
wasmtime-wasi = { workspace = true }
This diff is collapsed.
......@@ -16,7 +16,7 @@ transform_emotion = []
swc_ecma_transform_plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/plugin_transform_host_native_shared_runtime",
"swc_plugin_backend_wasmer"
"swc_plugin_backend_wasmtime"
]
[lints]
......@@ -42,7 +42,7 @@ turbopack-ecmascript = { workspace = true }
styled_components = { workspace = true }
styled_jsx = { workspace = true }
swc_core = { workspace = true, features = ["ecma_ast", "ecma_visit", "common"] }
swc_plugin_backend_wasmer = { workspace = true, optional = true }
swc_plugin_backend_wasmtime = { workspace = true, optional = true }
swc_emotion = { workspace = true }
swc_relay = { workspace = true }
......@@ -11,7 +11,7 @@ use turbopack_ecmascript::{CustomTransformer, TransformContext};
/// it to operate with the turbo_tasks caching requirements.
///
/// Internally this contains a `CompiledPluginModuleBytes`, which points to the
/// compiled, serialized wasmer::Module instead of raw file bytes to reduce the
/// compiled, serialized WASM module instead of raw file bytes to reduce the
/// cost of the compilation.
#[turbo_tasks::value(serialization = "none", eq = "manual", cell = "new", shared)]
pub struct SwcPluginModule {
......@@ -28,11 +28,11 @@ impl SwcPluginModule {
use swc_core::plugin_runner::plugin_module_bytes::{
CompiledPluginModuleBytes, RawPluginModuleBytes,
};
use swc_plugin_backend_wasmer::WasmerRuntime;
use swc_plugin_backend_wasmtime::WasmtimeRuntime;
Self {
plugin: CompiledPluginModuleBytes::from_raw_module(
&WasmerRuntime,
&WasmtimeRuntime,
RawPluginModuleBytes::new(plugin_name.to_string(), plugin_bytes),
),
name: plugin_name,
......@@ -181,7 +181,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
plugin::proxies::{COMMENTS, HostCommentsStorage},
plugin_runner::plugin_module_bytes::CompiledPluginModuleBytes,
};
use swc_plugin_backend_wasmer::WasmerRuntime;
use swc_plugin_backend_wasmtime::WasmtimeRuntime;
use turbo_tasks::TryJoinIterExt;
let plugins = self
......@@ -192,7 +192,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
Ok((
plugin_module.name.clone(),
config.clone(),
Box::new(plugin_module.plugin.clone_module(&WasmerRuntime)),
Box::new(plugin_module.plugin.clone_module(&WasmtimeRuntime)),
))
})
.try_join()
......@@ -259,7 +259,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
None,
plugin_module,
Some(plugin_config),
Arc::new(WasmerRuntime),
Arc::new(WasmtimeRuntime),
);
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