mirror of
https://github.com/vercel/next.js.git
synced 2026-09-20 02:25:18 +08:00
a24462d9e5
## 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 -->
137 lines
4.5 KiB
Rust
137 lines
4.5 KiB
Rust
use std::{
|
|
path::{Path, PathBuf},
|
|
sync::Arc,
|
|
};
|
|
|
|
use next_custom_transforms::chain_transforms::{TransformOptions, custom_before_pass};
|
|
use serde::de::DeserializeOwned;
|
|
use swc_core::{
|
|
base::Compiler,
|
|
common::{Mark, comments::SingleThreadedComments},
|
|
ecma::{
|
|
ast::noop_pass,
|
|
parser::{Syntax, TsSyntax},
|
|
},
|
|
};
|
|
use testing::{NormalizedOutput, Tester};
|
|
|
|
#[testing::fixture("tests/full/**/input.js")]
|
|
fn full(input: PathBuf) {
|
|
test(&input, true);
|
|
}
|
|
|
|
#[testing::fixture("tests/loader/**/input.js")]
|
|
fn loader(input: PathBuf) {
|
|
test(&input, false);
|
|
}
|
|
|
|
fn test(input: &Path, minify: bool) {
|
|
let output = input.parent().unwrap().join("output.js");
|
|
|
|
Tester::new()
|
|
.print_errors(|cm, handler| {
|
|
let c = Compiler::new(cm.clone());
|
|
|
|
let fm = cm.load_file(input).expect("failed to load file");
|
|
|
|
let options = TransformOptions {
|
|
lint_codemod_comments: true,
|
|
swc: swc_core::base::config::Options {
|
|
swcrc: true,
|
|
output_path: Some(output.clone()),
|
|
|
|
config: swc_core::base::config::Config {
|
|
is_module: Some(swc_core::base::config::IsModule::Unknown),
|
|
|
|
jsc: swc_core::base::config::JscConfig {
|
|
minify: if minify {
|
|
Some(assert_json("{ \"compress\": true, \"mangle\": true }"))
|
|
} else {
|
|
None
|
|
},
|
|
syntax: Some(Syntax::Typescript(TsSyntax {
|
|
tsx: true,
|
|
..Default::default()
|
|
})),
|
|
external_helpers: true.into(),
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
},
|
|
..Default::default()
|
|
},
|
|
disable_next_ssg: false,
|
|
pages_dir: None,
|
|
is_page_file: false,
|
|
is_development: true,
|
|
is_server_compiler: false,
|
|
server_components: None,
|
|
styled_components: Some(assert_json("{}")),
|
|
styled_jsx: assert_json("{}"),
|
|
remove_console: None,
|
|
react_remove_properties: None,
|
|
relay: None,
|
|
shake_exports: None,
|
|
emotion: Some(assert_json("{}")),
|
|
modularize_imports: None,
|
|
font_loaders: None,
|
|
app_dir: None,
|
|
server_actions: None,
|
|
cjs_require_optimizer: None,
|
|
auto_modularize_imports: None,
|
|
optimize_barrel_exports: None,
|
|
optimize_server_react: None,
|
|
prefer_esm: false,
|
|
debug_function_name: false,
|
|
css_env: None,
|
|
track_dynamic_imports: false,
|
|
};
|
|
|
|
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(
|
|
fm.clone(),
|
|
None,
|
|
&handler,
|
|
&options.swc,
|
|
comments.clone(),
|
|
|_| {
|
|
custom_before_pass(
|
|
cm.clone(),
|
|
fm.clone(),
|
|
&options,
|
|
comments.clone(),
|
|
Default::default(),
|
|
unresolved_mark,
|
|
Default::default(),
|
|
)
|
|
},
|
|
|_| noop_pass(),
|
|
) {
|
|
Ok(v) => {
|
|
NormalizedOutput::from(v.code)
|
|
.compare_to_file(output)
|
|
.unwrap();
|
|
}
|
|
Err(err) => panic!("Error: {:?}", err),
|
|
};
|
|
|
|
Ok(())
|
|
})
|
|
.map(|_| ())
|
|
.expect("failed");
|
|
}
|
|
|
|
/// Using this, we don't have to break code by adding field.s
|
|
fn assert_json<T>(json_str: &str) -> T
|
|
where
|
|
T: DeserializeOwned,
|
|
{
|
|
serde_json::from_str(json_str).expect("failed to deserialize")
|
|
}
|