Unverified Commit a24462d9 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

Replace wasmer with wasmtime for SWC plugin WASM engine (#91533)

## What?

Replace wasmer with wasmtime as the WASM runtime for SWC plugins.

## Why?

- Wasmer has been a source of build complexity due to conflicting feature flags (`wasmer/default` vs `wasmer/js-default`) and large binary size
- Wasmtime is actively maintained by the Bytecode Alliance with better performance and smaller footprint
- The upstream SWC project already provides `swc_plugin_backend_wasmtime` on crates.io, so we can use it directly instead of maintaining our own implementation

## How?

- Use the upstream `swc_plugin_backend_wasmtime` v9.0.0 crate from crates.io instead of a custom local implementation
- Replace `swc_core/plugin_transform_host_native` (which pulls in wasmer via `__plugin_transform_env_native`) with granular features: `__plugin_transform_host` + `__plugin_transform_host_schema_v1`
- Provide `WasmtimeRuntime` explicitly in both the NAPI bindings and turbopack plugin paths
- Remove all direct `wasmtime`/`wasmtime-wasi` workspace dependencies — the upstream crate manages its own wasmtime version
- Replace wasmer-related release profile size optimizations with equivalent wasmtime ones (`swc_plugin_backend_wasmtime`, `swc_plugin_runner`, `wasmtime`, `wasmtime-internal-cranelift`)
- Pin `vergen = "=9.0.6"` to avoid a `vergen-lib` version conflict with `vergen-gitcl` 1.0.8

## Binary size impact (darwin-arm64, compared against current canary)

| Metric | Canary | Wasmtime (this PR) | Delta |
|---|---|---|---|
| Unstripped dylib | 133.1 MB | 128.2 MB | **-5.0 MB (-3.7%)** |
| Stripped | 91.2 MB | 86.9 MB | **-4.3 MB (-4.8%)** |
| Stripped + gzip (npm) | 30.9 MB | 29.4 MB | **-1.5 MB (-5.0%)** |

<!-- NEXT_JS_LLM_PR -->
parent 9cf9a632
This diff is collapsed.
......@@ -87,7 +87,7 @@ inherits = "release"
debug = true
# Size-optimized crates: determined via `cargo bloat --release --crates -p next-napi-bindings`.
# Wasmer/cranelift crates are used by the plugin feature (wasm builds).
# Wasmtime/cranelift crates are used by the plugin feature (wasm builds).
[profile.release.package.browserslist-rs]
opt-level = "s"
......@@ -142,16 +142,16 @@ opt-level = "s"
[profile.release.package.swc_ecma_transforms_proposal]
opt-level = "s"
[profile.release.package.swc_plugin_backend_wasmer]
[profile.release.package.swc_plugin_backend_wasmtime]
opt-level = "s"
[profile.release.package.swc_plugin_runner]
opt-level = "s"
[profile.release.package.wasmer]
[profile.release.package.wasmtime]
opt-level = "s"
[profile.release.package.wasmer-compiler]
[profile.release.package.wasmtime-internal-cranelift]
opt-level = "s"
[workspace.dependencies]
......@@ -215,7 +215,8 @@ swc_core = { version = "63.1.0", default-features = false, features = [
"ecma_loader_parking_lot",
"parallel_rayon",
] }
swc_plugin_backend_wasmer = { version = "10.0.0" }
swc_plugin_backend_wasmtime = "9.0.0"
testing = "22.0.0"
# Keep consistent with preset_env_base through swc_core
......
......@@ -93,7 +93,8 @@ process_pool = ["turbopack-node/process_pool"]
worker_pool = ["turbopack-node/worker_pool"]
next-font-local = []
plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/__plugin_transform_host",
"swc_core/__plugin_transform_host_schema_v1",
"turbopack-ecmascript-plugins/swc_ecma_transform_plugin",
]
image-webp = ["turbopack-image/webp"]
......
......@@ -6,7 +6,8 @@ publish = false
[features]
plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/__plugin_transform_host",
"swc_core/__plugin_transform_host_schema_v1",
"turbopack-ecmascript-plugins/swc_ecma_transform_plugin",
]
......@@ -74,6 +75,7 @@ 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", "__plugin_transform_host_schema_v1"] }
swc_plugin_backend_wasmtime = { workspace = true }
turbopack-ecmascript-plugins = { workspace = true, features = ["swc_ecma_transform_plugin"] }
use std::path::{Path, PathBuf};
use std::{
path::{Path, PathBuf},
sync::Arc,
};
use next_custom_transforms::chain_transforms::{TransformOptions, custom_before_pass};
use serde::de::DeserializeOwned;
......@@ -87,6 +90,8 @@ fn test(input: &Path, minify: bool) {
let unresolved_mark = Mark::new();
let mut options = options.patch(&fm);
options.swc.unresolved_mark = Some(unresolved_mark);
options.swc.runtime_options = swc_core::base::config::RuntimeOptions::default()
.plugin_runtime(Arc::new(swc_plugin_backend_wasmtime::WasmtimeRuntime));
let comments = SingleThreadedComments::default();
match c.process_js_with_custom_pass(
......
......@@ -17,7 +17,7 @@ static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {
});
#[test]
fn should_collect_estimated_third_part_packages() {
fn should_collect_estimated_third_party_packages() {
let eliminated_packages: Rc<RefCell<FxHashSet<Atom>>> = Default::default();
let fm = COMPILER.cm.new_source_file(
FileName::Real("fixture.js".into()).into(),
......@@ -37,23 +37,29 @@ export function getServerSideProps() {
"#
.to_owned(),
);
assert!(
try_with_handler(COMPILER.cm.clone(), Default::default(), |handler| {
GLOBALS.set(&Default::default(), || {
let comments = SingleThreadedComments::default();
COMPILER.process_js_with_custom_pass(
fm,
None,
handler,
&Default::default(),
comments,
|_| next_ssg(eliminated_packages.clone()),
|_| noop_pass(),
)
})
let swc_options = swc_core::base::config::Options {
runtime_options: swc_core::base::config::RuntimeOptions::default()
.plugin_runtime(Arc::new(swc_plugin_backend_wasmtime::WasmtimeRuntime)),
..Default::default()
};
try_with_handler(COMPILER.cm.clone(), Default::default(), |handler| {
GLOBALS.set(&Default::default(), || {
let comments = SingleThreadedComments::default();
COMPILER.process_js_with_custom_pass(
fm,
None,
handler,
&swc_options,
comments,
|_| next_ssg(eliminated_packages.clone()),
|_| noop_pass(),
)
})
.is_ok()
);
})
.unwrap();
let mut eliminated_packages_vec = Rc::into_inner(eliminated_packages)
.expect("we should have the only remaining reference to `eliminated_packages`")
.into_inner()
......
......@@ -14,10 +14,11 @@ crate-type = ["cdylib"]
# time (i.e wasmer/default vs wasmer/js-default) while cargo merges all the
# features at once.
plugin = [
"swc_core/plugin_transform_host_native",
# Use granular plugin features to avoid pulling in wasmer via __plugin_transform_env_native.
"swc_core/__plugin_transform_host",
"swc_core/__plugin_transform_host_schema_v1",
"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 +53,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 +110,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();
......
......@@ -14,9 +14,13 @@ default = ["swc_ecma_transform_plugin"]
transform_emotion = []
# [NOTE]: Be careful to explicitly enable this only for the supported platform / targets.
swc_ecma_transform_plugin = [
"swc_core/plugin_transform_host_native",
"swc_core/plugin_transform_host_native_shared_runtime",
"swc_plugin_backend_wasmer"
# 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_schema_v1 provides the schema version support.
# We provide WasmtimeRuntime explicitly, so we don't need swc's env-native backend wiring.
"swc_core/__plugin_transform_host",
"swc_core/__plugin_transform_host_schema_v1",
"swc_plugin_backend_wasmtime"
]
[lints]
......@@ -42,7 +46,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