mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
## 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 -->
71 lines
2.2 KiB
Rust
71 lines
2.2 KiB
Rust
use std::{cell::RefCell, rc::Rc, sync::Arc};
|
|
|
|
use next_custom_transforms::transforms::next_ssg::next_ssg;
|
|
use once_cell::sync::Lazy;
|
|
use rustc_hash::FxHashSet;
|
|
use swc_core::{
|
|
atoms::Atom,
|
|
base::{Compiler, try_with_handler},
|
|
common::{FileName, FilePathMapping, GLOBALS, SourceMap, comments::SingleThreadedComments},
|
|
ecma::ast::noop_pass,
|
|
};
|
|
|
|
static COMPILER: Lazy<Arc<Compiler>> = Lazy::new(|| {
|
|
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
Arc::new(Compiler::new(cm))
|
|
});
|
|
|
|
#[test]
|
|
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(),
|
|
r#"import http from 'http'
|
|
import { hash } from '@napi-rs/bcrypt'
|
|
|
|
import { omit } from '~/utils/omit'
|
|
import config from './data.json'
|
|
|
|
export default () => 'Hello World'
|
|
|
|
export function getServerSideProps() {
|
|
console.log(http)
|
|
console.log(config)
|
|
return { props: { digest: hash('hello') } }
|
|
}
|
|
"#
|
|
.to_owned(),
|
|
);
|
|
|
|
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(),
|
|
)
|
|
})
|
|
})
|
|
.unwrap();
|
|
|
|
let mut eliminated_packages_vec = Rc::into_inner(eliminated_packages)
|
|
.expect("we should have the only remaining reference to `eliminated_packages`")
|
|
.into_inner()
|
|
.into_iter()
|
|
.collect::<Vec<Atom>>();
|
|
eliminated_packages_vec.sort_unstable(); // HashSet order is random/arbitrary
|
|
assert_eq!(eliminated_packages_vec, vec!["@napi-rs/bcrypt", "http"]);
|
|
}
|