Commit fb4332ff authored by Luke Sandberg's avatar Luke Sandberg Committed by Luke Sandberg

snappy wtf

parent 4abd123c
...@@ -49,12 +49,12 @@ jobs: ...@@ -49,12 +49,12 @@ jobs:
- name: Build benchmarks for tests - name: Build benchmarks for tests
timeout-minutes: 120 timeout-minutes: 120
run: | run: |
cargo test --benches --workspace --release --no-fail-fast --exclude turbopack-bench --exclude next-napi-bindings --no-run cargo test --benches --workspace --release --no-fail-fast --exclude turbopack-bench --exclude next-napi-bindings --exclude turbopack-ecmascript-plugins --exclude next-core --no-run
- name: Run cargo test on benchmarks - name: Run cargo test on benchmarks
timeout-minutes: 120 timeout-minutes: 120
run: | run: |
cargo test --benches --workspace --release --no-fail-fast --exclude turbopack-bench --exclude next-napi-bindings cargo test --benches --workspace --release --no-fail-fast --exclude turbopack-bench --exclude next-napi-bindings --exclude turbopack-ecmascript-plugins --exclude next-core
- name: Build benchmarks for tests for other bundlers - name: Build benchmarks for tests for other bundlers
if: inputs.all if: inputs.all
......
...@@ -188,6 +188,7 @@ impl AppProject { ...@@ -188,6 +188,7 @@ impl AppProject {
self.project().next_mode(), self.project().next_mode(),
self.project().next_config(), self.project().next_config(),
self.project().encryption_key(), self.project().encryption_key(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -220,6 +221,7 @@ impl AppProject { ...@@ -220,6 +221,7 @@ impl AppProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().server_compile_time_info().environment(), self.project().server_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -235,6 +237,7 @@ impl AppProject { ...@@ -235,6 +237,7 @@ impl AppProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().edge_compile_time_info().environment(), self.project().edge_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -250,6 +253,7 @@ impl AppProject { ...@@ -250,6 +253,7 @@ impl AppProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().server_compile_time_info().environment(), self.project().server_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -265,6 +269,7 @@ impl AppProject { ...@@ -265,6 +269,7 @@ impl AppProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().edge_compile_time_info().environment(), self.project().edge_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -592,6 +597,7 @@ impl AppProject { ...@@ -592,6 +597,7 @@ impl AppProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().server_compile_time_info().environment(), self.project().server_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -607,6 +613,7 @@ impl AppProject { ...@@ -607,6 +613,7 @@ impl AppProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().edge_compile_time_info().environment(), self.project().edge_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
......
...@@ -357,6 +357,7 @@ impl PagesProject { ...@@ -357,6 +357,7 @@ impl PagesProject {
self.project().next_mode(), self.project().next_mode(),
self.project().next_config(), self.project().next_config(),
self.project().encryption_key(), self.project().encryption_key(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -444,6 +445,7 @@ impl PagesProject { ...@@ -444,6 +445,7 @@ impl PagesProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().server_compile_time_info().environment(), self.project().server_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -461,6 +463,7 @@ impl PagesProject { ...@@ -461,6 +463,7 @@ impl PagesProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().edge_compile_time_info().environment(), self.project().edge_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -478,6 +481,7 @@ impl PagesProject { ...@@ -478,6 +481,7 @@ impl PagesProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().server_compile_time_info().environment(), self.project().server_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
...@@ -495,6 +499,7 @@ impl PagesProject { ...@@ -495,6 +499,7 @@ impl PagesProject {
self.project().encryption_key(), self.project().encryption_key(),
self.project().edge_compile_time_info().environment(), self.project().edge_compile_time_info().environment(),
self.project().client_compile_time_info().environment(), self.project().client_compile_time_info().environment(),
*self.project().wasm_plugin_runtime_id().await?,
)) ))
} }
......
...@@ -375,6 +375,9 @@ pub struct ProjectOptions { ...@@ -375,6 +375,9 @@ pub struct ProjectOptions {
/// force new filenames without changing file content. Empty string means /// force new filenames without changing file content. Empty string means
/// no salt. /// no salt.
pub hash_salt: RcStr, pub hash_salt: RcStr,
/// The runtime_id from register_wasm_plugin_runtime, used to look up the
/// NAPI-based WASM plugin runtime. 0 means no runtime registered.
pub wasm_plugin_runtime_id: u64,
} }
#[derive(Default)] #[derive(Default)]
...@@ -824,6 +827,7 @@ impl ProjectContainer { ...@@ -824,6 +827,7 @@ impl ProjectContainer {
let is_persistent_caching_enabled; let is_persistent_caching_enabled;
let server_hmr; let server_hmr;
let hash_salt; let hash_salt;
let wasm_plugin_runtime_id;
{ {
let options = self.options_state.get(); let options = self.options_state.get();
let options = options let options = options
...@@ -853,6 +857,7 @@ impl ProjectContainer { ...@@ -853,6 +857,7 @@ impl ProjectContainer {
is_persistent_caching_enabled = options.is_persistent_caching_enabled; is_persistent_caching_enabled = options.is_persistent_caching_enabled;
server_hmr = options.server_hmr; server_hmr = options.server_hmr;
hash_salt = options.hash_salt.clone(); hash_salt = options.hash_salt.clone();
wasm_plugin_runtime_id = options.wasm_plugin_runtime_id;
} }
let dist_dir = next_config.dist_dir().owned().await?; let dist_dir = next_config.dist_dir().owned().await?;
...@@ -884,6 +889,7 @@ impl ProjectContainer { ...@@ -884,6 +889,7 @@ impl ProjectContainer {
is_persistent_caching_enabled, is_persistent_caching_enabled,
server_hmr, server_hmr,
hash_salt, hash_salt,
wasm_plugin_runtime_id,
} }
.cell()) .cell())
} }
...@@ -991,6 +997,9 @@ pub struct Project { ...@@ -991,6 +997,9 @@ pub struct Project {
/// A salt to mix into chunk and asset content hashes. Empty string means /// A salt to mix into chunk and asset content hashes. Empty string means
/// no salt. /// no salt.
hash_salt: RcStr, hash_salt: RcStr,
/// The runtime_id from register_wasm_plugin_runtime, used to look up the
/// NAPI-based WASM plugin runtime. 0 means no runtime registered.
wasm_plugin_runtime_id: u64,
} }
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -1245,6 +1254,11 @@ impl Project { ...@@ -1245,6 +1254,11 @@ impl Project {
Vc::cell(self.encryption_key.clone()) Vc::cell(self.encryption_key.clone())
} }
#[turbo_tasks::function]
pub(super) fn wasm_plugin_runtime_id(&self) -> Vc<u64> {
Vc::cell(self.wasm_plugin_runtime_id)
}
#[turbo_tasks::function] #[turbo_tasks::function]
pub(super) fn no_mangling(&self) -> Vc<bool> { pub(super) fn no_mangling(&self) -> Vc<bool> {
Vc::cell(self.no_mangling) Vc::cell(self.no_mangling)
...@@ -1942,6 +1956,7 @@ impl Project { ...@@ -1942,6 +1956,7 @@ impl Project {
self.encryption_key(), self.encryption_key(),
self.edge_compile_time_info().environment(), self.edge_compile_time_info().environment(),
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
*self.wasm_plugin_runtime_id().await?,
), ),
get_edge_resolve_options_context( get_edge_resolve_options_context(
self.project_path().owned().await?, self.project_path().owned().await?,
...@@ -2005,6 +2020,7 @@ impl Project { ...@@ -2005,6 +2020,7 @@ impl Project {
self.encryption_key(), self.encryption_key(),
self.server_compile_time_info().environment(), self.server_compile_time_info().environment(),
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
*self.wasm_plugin_runtime_id().await?,
), ),
get_server_resolve_options_context( get_server_resolve_options_context(
self.project_path().owned().await?, self.project_path().owned().await?,
...@@ -2120,6 +2136,7 @@ impl Project { ...@@ -2120,6 +2136,7 @@ impl Project {
self.encryption_key(), self.encryption_key(),
self.server_compile_time_info().environment(), self.server_compile_time_info().environment(),
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
*self.wasm_plugin_runtime_id().await?,
), ),
get_server_resolve_options_context( get_server_resolve_options_context(
self.project_path().owned().await?, self.project_path().owned().await?,
...@@ -2183,6 +2200,7 @@ impl Project { ...@@ -2183,6 +2200,7 @@ impl Project {
self.encryption_key(), self.encryption_key(),
self.edge_compile_time_info().environment(), self.edge_compile_time_info().environment(),
self.client_compile_time_info().environment(), self.client_compile_time_info().environment(),
*self.wasm_plugin_runtime_id().await?,
), ),
get_edge_resolve_options_context( get_edge_resolve_options_context(
self.project_path().owned().await?, self.project_path().owned().await?,
......
...@@ -233,6 +233,7 @@ pub async fn get_client_module_options_context( ...@@ -233,6 +233,7 @@ pub async fn get_client_module_options_context(
mode: Vc<NextMode>, mode: Vc<NextMode>,
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
encryption_key: ResolvedVc<RcStr>, encryption_key: ResolvedVc<RcStr>,
wasm_plugin_runtime_id: u64,
) -> Result<Vc<ModuleOptionsContext>> { ) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?; let next_mode = mode.await?;
let resolve_options_context = get_client_resolve_options_context( let resolve_options_context = get_client_resolve_options_context(
...@@ -298,7 +299,12 @@ pub async fn get_client_module_options_context( ...@@ -298,7 +299,12 @@ pub async fn get_client_module_options_context(
get_next_client_transforms_rules(next_config, ty.clone(), mode, true, encryption_key) get_next_client_transforms_rules(next_config, ty.clone(), mode, true, encryption_key)
.await?; .await?;
let additional_rules: Vec<ModuleRule> = vec![ let additional_rules: Vec<ModuleRule> = vec![
get_swc_ecma_transform_plugin_rule(next_config, project_path.clone()).await?, get_swc_ecma_transform_plugin_rule(
next_config,
project_path.clone(),
wasm_plugin_runtime_id,
)
.await?,
get_relay_transform_rule(next_config, project_path.clone()).await?, get_relay_transform_rule(next_config, project_path.clone()).await?,
get_emotion_transform_rule(next_config).await?, get_emotion_transform_rule(next_config).await?,
get_styled_components_transform_rule(next_config).await?, get_styled_components_transform_rule(next_config).await?,
......
...@@ -418,6 +418,7 @@ pub async fn get_server_module_options_context( ...@@ -418,6 +418,7 @@ pub async fn get_server_module_options_context(
encryption_key: ResolvedVc<RcStr>, encryption_key: ResolvedVc<RcStr>,
environment: ResolvedVc<Environment>, environment: ResolvedVc<Environment>,
client_environment: ResolvedVc<Environment>, client_environment: ResolvedVc<Environment>,
wasm_plugin_runtime_id: u64,
) -> Result<Vc<ModuleOptionsContext>> { ) -> Result<Vc<ModuleOptionsContext>> {
let next_mode = mode.await?; let next_mode = mode.await?;
let mut next_server_rules = get_next_server_transforms_rules( let mut next_server_rules = get_next_server_transforms_rules(
...@@ -537,7 +538,12 @@ pub async fn get_server_module_options_context( ...@@ -537,7 +538,12 @@ pub async fn get_server_module_options_context(
get_react_remove_properties_transform_rule(next_config).await?, get_react_remove_properties_transform_rule(next_config).await?,
get_emotion_transform_rule(next_config).await?, get_emotion_transform_rule(next_config).await?,
get_relay_transform_rule(next_config, project_path.clone()).await?, get_relay_transform_rule(next_config, project_path.clone()).await?,
get_swc_ecma_transform_plugin_rule(next_config, project_path.clone()).await?, get_swc_ecma_transform_plugin_rule(
next_config,
project_path.clone(),
wasm_plugin_runtime_id,
)
.await?,
] ]
.into_iter() .into_iter()
.flatten() .flatten()
......
...@@ -12,18 +12,25 @@ use crate::next_config::NextConfig; ...@@ -12,18 +12,25 @@ use crate::next_config::NextConfig;
pub async fn get_swc_ecma_transform_plugin_rule( pub async fn get_swc_ecma_transform_plugin_rule(
next_config: Vc<NextConfig>, next_config: Vc<NextConfig>,
project_path: FileSystemPath, project_path: FileSystemPath,
wasm_plugin_runtime_id: u64,
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
let plugin_configs = next_config.experimental_swc_plugins().await?; let plugin_configs = next_config.experimental_swc_plugins().await?;
if !plugin_configs.is_empty() { if !plugin_configs.is_empty() {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
{ {
let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let enable_mdx_rs = next_config.mdx_rs().await?.is_some();
get_swc_ecma_transform_rule_impl(project_path, &plugin_configs, enable_mdx_rs).await get_swc_ecma_transform_rule_impl(
project_path,
&plugin_configs,
enable_mdx_rs,
wasm_plugin_runtime_id,
)
.await
} }
#[cfg(not(feature = "plugin"))] #[cfg(not(feature = "plugin"))]
{ {
let _ = project_path; // To satisfy lint let _ = (project_path, wasm_plugin_runtime_id);
Ok(None) Ok(None)
} }
} else { } else {
...@@ -66,6 +73,7 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -66,6 +73,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
project_path: FileSystemPath, project_path: FileSystemPath,
plugin_configs: &[(RcStr, serde_json::Value)], plugin_configs: &[(RcStr, serde_json::Value)],
enable_mdx_rs: bool, enable_mdx_rs: bool,
wasm_plugin_runtime_id: u64,
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
use anyhow::bail; use anyhow::bail;
use turbo_tasks::TryFlatJoinIterExt; use turbo_tasks::TryFlatJoinIterExt;
...@@ -147,7 +155,11 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -147,7 +155,11 @@ pub async fn get_swc_ecma_transform_rule_impl(
}; };
Ok(Some(( Ok(Some((
SwcPluginModule::new(name.clone(), file.content().to_bytes().to_vec()) SwcPluginModule::new(
name.clone(),
file.content().to_bytes().to_vec(),
wasm_plugin_runtime_id,
)
.resolved_cell(), .resolved_cell(),
config.clone(), config.clone(),
))) )))
...@@ -157,7 +169,10 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -157,7 +169,10 @@ pub async fn get_swc_ecma_transform_rule_impl(
.await?; .await?;
Ok(Some(get_ecma_transform_rule( Ok(Some(get_ecma_transform_rule(
Box::new(SwcEcmaTransformPluginsTransformer::new(plugins)), Box::new(SwcEcmaTransformPluginsTransformer::new(
plugins,
wasm_plugin_runtime_id,
)),
enable_mdx_rs, enable_mdx_rs,
EcmascriptTransformStage::Main, EcmascriptTransformStage::Main,
))) )))
......
...@@ -221,6 +221,9 @@ pub struct NapiProjectOptions { ...@@ -221,6 +221,9 @@ pub struct NapiProjectOptions {
/// force new filenames without changing file content. Empty string means /// force new filenames without changing file content. Empty string means
/// no salt. /// no salt.
pub hash_salt: RcStr, pub hash_salt: RcStr,
/// The runtime_id from registerWasmPluginRuntime, used to look up the
/// NAPI-based WASM plugin runtime. Omit or pass 0 if no plugins are used.
pub wasm_plugin_runtime_id: Option<f64>,
} }
/// [NapiProjectOptions] with all fields optional. /// [NapiProjectOptions] with all fields optional.
...@@ -335,6 +338,7 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -335,6 +338,7 @@ impl From<NapiProjectOptions> for ProjectOptions {
next_version, next_version,
server_hmr, server_hmr,
hash_salt, hash_salt,
wasm_plugin_runtime_id: _wasm_plugin_runtime_id,
} = val; } = val;
ProjectOptions { ProjectOptions {
root_path, root_path,
...@@ -360,6 +364,7 @@ impl From<NapiProjectOptions> for ProjectOptions { ...@@ -360,6 +364,7 @@ impl From<NapiProjectOptions> for ProjectOptions {
next_version, next_version,
server_hmr: server_hmr.unwrap_or(false), server_hmr: server_hmr.unwrap_or(false),
hash_salt, hash_salt,
wasm_plugin_runtime_id: _wasm_plugin_runtime_id.map(|v| v as u64).unwrap_or(0),
} }
} }
} }
......
...@@ -50,6 +50,11 @@ use swc_core::{ ...@@ -50,6 +50,11 @@ use swc_core::{
use crate::{complete_output, get_compiler, util::MapErr}; use crate::{complete_output, get_compiler, util::MapErr};
/// The runtime_id from the most recent register_wasm_plugin_runtime call.
/// Used by the webpack transform path which doesn't go through Project.
#[cfg(feature = "plugin")]
static CURRENT_WASM_RUNTIME_ID: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
/// Input to transform /// Input to transform
#[derive(Debug)] #[derive(Debug)]
pub enum Input { pub enum Input {
...@@ -63,6 +68,8 @@ pub struct TransformTask { ...@@ -63,6 +68,8 @@ pub struct TransformTask {
pub c: Compiler, pub c: Compiler,
pub input: Input, pub input: Input,
pub options: Buffer, pub options: Buffer,
#[cfg(feature = "plugin")]
pub wasm_plugin_runtime_id: u64,
} }
fn skip_filename() -> bool { fn skip_filename() -> bool {
...@@ -133,11 +140,16 @@ impl Task for TransformTask { ...@@ -133,11 +140,16 @@ impl Task for TransformTask {
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
{ {
if self.wasm_plugin_runtime_id != 0 {
let runtime =
swc_plugin_backend_napi::NapiRuntime::from_runtime_id(
self.wasm_plugin_runtime_id,
)
.map_err(|e| anyhow!(e))?;
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(runtime));
swc_plugin_backend_napi::NapiRuntime, }
));
} }
let cm = self.c.cm.clone(); let cm = self.c.cm.clone();
...@@ -227,7 +239,13 @@ pub fn transform( ...@@ -227,7 +239,13 @@ pub fn transform(
Either3::C(_) => Input::FromFilename, Either3::C(_) => Input::FromFilename,
}; };
let task = TransformTask { c, input, options }; let task = TransformTask {
c,
input,
options,
#[cfg(feature = "plugin")]
wasm_plugin_runtime_id: CURRENT_WASM_RUNTIME_ID.load(std::sync::atomic::Ordering::Relaxed),
};
Ok(AsyncTask::with_optional_signal(task, signal)) Ok(AsyncTask::with_optional_signal(task, signal))
} }
...@@ -248,7 +266,13 @@ pub fn transform_sync( ...@@ -248,7 +266,13 @@ pub fn transform_sync(
Either3::C(_) => Input::FromFilename, Either3::C(_) => Input::FromFilename,
}; };
let mut task = TransformTask { c, input, options }; let mut task = TransformTask {
c,
input,
options,
#[cfg(feature = "plugin")]
wasm_plugin_runtime_id: CURRENT_WASM_RUNTIME_ID.load(std::sync::atomic::Ordering::Relaxed),
};
let output = task.compute()?; let output = task.compute()?;
task.resolve(env, output) task.resolve(env, output)
} }
...@@ -263,10 +287,13 @@ fn test_deser() { ...@@ -263,10 +287,13 @@ fn test_deser() {
/// Register the NAPI-based WASM plugin runtime. /// Register the NAPI-based WASM plugin runtime.
/// Must be called from JS before any SWC transforms that use plugins. /// Must be called from JS before any SWC transforms that use plugins.
/// Returns the runtime_id that identifies this registration.
#[cfg(feature = "plugin")] #[cfg(feature = "plugin")]
#[napi] #[napi]
pub fn register_wasm_plugin_runtime(env: Env, js_manager: JsObject) -> napi::Result<()> { pub fn register_wasm_plugin_runtime(env: Env, js_manager: JsObject) -> napi::Result<f64> {
swc_plugin_backend_napi::register_wasm_runtime(&env, &js_manager) let runtime_id = swc_plugin_backend_napi::register_wasm_runtime(&env, &js_manager)?;
CURRENT_WASM_RUNTIME_ID.store(runtime_id, std::sync::atomic::Ordering::Relaxed);
Ok(runtime_id as f64)
} }
#[test] #[test]
......
This diff is collapsed.
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
"rust-check-doc": "RUSTDOCFLAGS='-Zunstable-options --output-format=json' cargo doc --no-deps --workspace", "rust-check-doc": "RUSTDOCFLAGS='-Zunstable-options --output-format=json' cargo doc --no-deps --workspace",
"rust-check-fmt": "cd ../..; cargo fmt -- --check", "rust-check-fmt": "cd ../..; cargo fmt -- --check",
"rust-check-napi": "cargo check -p next-napi-bindings", "rust-check-napi": "cargo check -p next-napi-bindings",
"test-cargo-unit": "cargo nextest run --workspace --exclude next-napi-bindings --exclude turbo-tasks-macros --cargo-profile release-with-assertions --no-fail-fast" "test-cargo-unit": "cargo nextest run --workspace --exclude next-napi-bindings --exclude turbopack-ecmascript-plugins --exclude next-core --exclude turbo-tasks-macros --cargo-profile release-with-assertions --no-fail-fast"
}, },
"napi": { "napi": {
"name": "next-swc", "name": "next-swc",
......
...@@ -21,7 +21,8 @@ export function lightningCssTransformStyleAttribute( ...@@ -21,7 +21,8 @@ export function lightningCssTransformStyleAttribute(
export function lightningcssFeatureNamesToMaskNapi(names: Array<string>): number export function lightningcssFeatureNamesToMaskNapi(names: Array<string>): number
// Optional: only available when built with the "plugin" feature. // Optional: only available when built with the "plugin" feature.
export function registerWasmPluginRuntime(jsManager: object): void // Returns the runtime_id that identifies this registration.
export function registerWasmPluginRuntime(jsManager: object): number
// GENERATED-TYPES-BELOW // GENERATED-TYPES-BELOW
// DO NOT MANUALLY EDIT THESE TYPES // DO NOT MANUALLY EDIT THESE TYPES
...@@ -38,30 +39,6 @@ export declare class ExternalObject<T> { ...@@ -38,30 +39,6 @@ export declare class ExternalObject<T> {
[K: symbol]: T [K: symbol]: T
} }
} }
export declare function registerWorkerScheduler(
creator: (arg: NapiWorkerCreation) => any,
terminator: (arg: NapiWorkerTermination) => any
): void
export declare function workerCreated(workerId: number): void
export interface NapiWorkerCreation {
options: NapiWorkerOptions
}
export interface NapiWorkerOptions {
filename: RcStr
cwd: RcStr
}
export interface NapiWorkerTermination {
options: NapiWorkerOptions
workerId: number
}
export interface NapiTaskMessage {
taskId: number
data: Buffer
}
export declare function recvTaskMessageInWorker(
workerId: number
): Promise<NapiTaskMessage>
export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
/** /**
* Called by worker after WASM instantiation to register a dispatch callback. * Called by worker after WASM instantiation to register a dispatch callback.
* *
...@@ -78,6 +55,7 @@ export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void> ...@@ -78,6 +55,7 @@ export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
* method on `ops` and sends the result back via a sync channel. * method on `ops` and sends the result back via a sync channel.
*/ */
export declare function wasmWorkerRegisterCallback( export declare function wasmWorkerRegisterCallback(
runtimeId: number,
instanceId: number, instanceId: number,
ops: object ops: object
): void ): void
...@@ -92,11 +70,36 @@ export declare function wasmWorkerRegisterCallback( ...@@ -92,11 +70,36 @@ export declare function wasmWorkerRegisterCallback(
* - free(ptr, size) → number * - free(ptr, size) → number
*/ */
export declare function wasmWorkerDispatchHostFn( export declare function wasmWorkerDispatchHostFn(
runtimeId: number,
instanceId: number, instanceId: number,
fnIndex: number, fnIndex: number,
args: Array<number>, args: Array<number>,
memoryAccessor: object memoryAccessor: object
): unknown ): unknown
export declare function registerWorkerScheduler(
creator: (arg: NapiWorkerCreation) => any,
terminator: (arg: NapiWorkerTermination) => any
): void
export declare function workerCreated(workerId: number): void
export interface NapiWorkerCreation {
options: NapiWorkerOptions
}
export interface NapiWorkerOptions {
filename: RcStr
cwd: RcStr
}
export interface NapiWorkerTermination {
options: NapiWorkerOptions
workerId: number
}
export interface NapiTaskMessage {
taskId: number
data: Buffer
}
export declare function recvTaskMessageInWorker(
workerId: number
): Promise<NapiTaskMessage>
export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
export interface NapiLocation { export interface NapiLocation {
line: number line: number
column?: number column?: number
...@@ -302,6 +305,10 @@ export interface NapiProjectOptions { ...@@ -302,6 +305,10 @@ export interface NapiProjectOptions {
* no salt. * no salt.
*/ */
hashSalt: RcStr hashSalt: RcStr
* The runtime_id from registerWasmPluginRuntime, used to look up the
* NAPI-based WASM plugin runtime. Omit or pass 0 if no plugins are used.
*/
wasmPluginRuntimeId?: number
} }
/** [NapiProjectOptions] with all fields optional. */ /** [NapiProjectOptions] with all fields optional. */
export interface NapiPartialProjectOptions { export interface NapiPartialProjectOptions {
...@@ -656,8 +663,9 @@ export declare function transformSync( ...@@ -656,8 +663,9 @@ export declare function transformSync(
/** /**
* Register the NAPI-based WASM plugin runtime. * Register the NAPI-based WASM plugin runtime.
* Must be called from JS before any SWC transforms that use plugins. * Must be called from JS before any SWC transforms that use plugins.
* Returns the runtime_id that identifies this registration.
*/ */
export declare function registerWasmPluginRuntime(jsManager: object): void export declare function registerWasmPluginRuntime(jsManager: object): number
export declare function startTurbopackTraceServer( export declare function startTurbopackTraceServer(
path: string, path: string,
port?: number | undefined | null port?: number | undefined | null
......
...@@ -172,6 +172,9 @@ let loadedBindings: Binding | undefined = undefined ...@@ -172,6 +172,9 @@ let loadedBindings: Binding | undefined = undefined
let downloadWasmPromise: any let downloadWasmPromise: any
let swcTraceFlushGuard: any let swcTraceFlushGuard: any
let downloadNativeBindingsPromise: Promise<void> | undefined = undefined let downloadNativeBindingsPromise: Promise<void> | undefined = undefined
// The runtime_id returned by registerWasmPluginRuntime, passed to turbopack
// project creation so it can look up the correct WASM plugin runtime.
let wasmPluginRuntimeId: number = 0
export const lockfilePatchPromise: { cur?: Promise<void> } = {} export const lockfilePatchPromise: { cur?: Promise<void> } = {}
...@@ -662,6 +665,7 @@ function bindingToApi( ...@@ -662,6 +665,7 @@ function bindingToApi(
path.join(options.rootPath, options.projectPath) path.join(options.rootPath, options.projectPath)
), ),
env: rustifyEnv(options.env), env: rustifyEnv(options.env),
wasmPluginRuntimeId: wasmPluginRuntimeId || undefined,
} }
} }
...@@ -1743,7 +1747,8 @@ function loadNative(importPath?: string): Binding { ...@@ -1743,7 +1747,8 @@ function loadNative(importPath?: string): Binding {
const { wasmManager } = const { wasmManager } =
require('./wasm-manager') as typeof import('./wasm-manager') require('./wasm-manager') as typeof import('./wasm-manager')
wasmManager.setBindingsPath(bindingsPath) wasmManager.setBindingsPath(bindingsPath)
bindings.registerWasmPluginRuntime(wasmManager) wasmPluginRuntimeId = bindings.registerWasmPluginRuntime(wasmManager)
wasmManager.setRuntimeId(wasmPluginRuntimeId)
} }
return loadedBindings! return loadedBindings!
......
...@@ -39,6 +39,9 @@ const instanceWorkerMap = new Map<number, number>() ...@@ -39,6 +39,9 @@ const instanceWorkerMap = new Map<number, number>()
// Path to the native .node binding, set during init // Path to the native .node binding, set during init
let nativeBindingsPath: string | null = null let nativeBindingsPath: string | null = null
// The runtime_id from registerWasmPluginRuntime, passed to workers
let runtimeId: number = 0
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Module cache: compile once, clone to workers for free // Module cache: compile once, clone to workers for free
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
...@@ -57,7 +60,7 @@ function createWorker(): WorkerEntry { ...@@ -57,7 +60,7 @@ function createWorker(): WorkerEntry {
const workerPath = path.join(__dirname, 'wasm-worker.js') const workerPath = path.join(__dirname, 'wasm-worker.js')
const worker = new Worker(workerPath, { const worker = new Worker(workerPath, {
workerData: { nativeBindingsPath }, workerData: { nativeBindingsPath, runtimeId },
// Suppress the WASI warnings, this isn't a great solution // Suppress the WASI warnings, this isn't a great solution
execArgv: ['--no-warnings'], execArgv: ['--no-warnings'],
}) })
...@@ -145,6 +148,14 @@ export const wasmManager = { ...@@ -145,6 +148,14 @@ export const wasmManager = {
nativeBindingsPath = bindingsPath nativeBindingsPath = bindingsPath
}, },
/**
* Set the runtime_id returned by registerWasmPluginRuntime.
* Workers use this to look up the correct runtime state in NAPI calls.
*/
setRuntimeId(id: number): void {
runtimeId = id
},
/** /**
* Compile a WASM module and cache it. Returns a module ID. * Compile a WASM module and cache it. Returns a module ID.
*/ */
......
...@@ -23,6 +23,7 @@ if (!parentPort) { ...@@ -23,6 +23,7 @@ if (!parentPort) {
// Load native addon directly in the worker thread. // Load native addon directly in the worker thread.
// This gives us direct NAPI access for host function callbacks. // This gives us direct NAPI access for host function callbacks.
const bindings = require(workerData.nativeBindingsPath) const bindings = require(workerData.nativeBindingsPath)
const runtimeId: number = workerData.runtimeId
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Instance storage — the ops object closes over the WASM instance/memory, // Instance storage — the ops object closes over the WASM instance/memory,
...@@ -105,6 +106,7 @@ parentPort.on('message', (req: any) => { ...@@ -105,6 +106,7 @@ parentPort.on('message', (req: any) => {
throw new Error(`Instance ${instanceId} not found for host fn`) throw new Error(`Instance ${instanceId} not found for host fn`)
} }
return bindings.wasmWorkerDispatchHostFn( return bindings.wasmWorkerDispatchHostFn(
runtimeId,
instanceId, instanceId,
index, index,
args.slice(0, paramCount), args.slice(0, paramCount),
...@@ -128,7 +130,7 @@ parentPort.on('message', (req: any) => { ...@@ -128,7 +130,7 @@ parentPort.on('message', (req: any) => {
// the instance handle and the ops interface for Rust. // the instance handle and the ops interface for Rust.
const ops = createInstanceOps(instance, memory) const ops = createInstanceOps(instance, memory)
instances.set(instanceId, ops) instances.set(instanceId, ops)
bindings.wasmWorkerRegisterCallback(instanceId, ops) bindings.wasmWorkerRegisterCallback(runtimeId, instanceId, ops)
response = { result: instanceId } response = { result: instanceId }
break break
......
...@@ -22,7 +22,7 @@ pub struct SwcPluginModule { ...@@ -22,7 +22,7 @@ pub struct SwcPluginModule {
} }
impl SwcPluginModule { impl SwcPluginModule {
pub fn new(plugin_name: RcStr, plugin_bytes: Vec<u8>) -> Self { pub fn new(plugin_name: RcStr, plugin_bytes: Vec<u8>, wasm_plugin_runtime_id: u64) -> Self {
#[cfg(feature = "swc_ecma_transform_plugin")] #[cfg(feature = "swc_ecma_transform_plugin")]
{ {
use swc_core::plugin_runner::plugin_module_bytes::{ use swc_core::plugin_runner::plugin_module_bytes::{
...@@ -30,9 +30,12 @@ impl SwcPluginModule { ...@@ -30,9 +30,12 @@ impl SwcPluginModule {
}; };
use swc_plugin_backend_napi::NapiRuntime; use swc_plugin_backend_napi::NapiRuntime;
let runtime = NapiRuntime::from_runtime_id(wasm_plugin_runtime_id)
.expect("WASM plugin runtime not registered");
Self { Self {
plugin: CompiledPluginModuleBytes::from_raw_module( plugin: CompiledPluginModuleBytes::from_raw_module(
&NapiRuntime, &runtime,
RawPluginModuleBytes::new(plugin_name.to_string(), plugin_bytes), RawPluginModuleBytes::new(plugin_name.to_string(), plugin_bytes),
), ),
name: plugin_name, name: plugin_name,
...@@ -41,7 +44,7 @@ impl SwcPluginModule { ...@@ -41,7 +44,7 @@ impl SwcPluginModule {
#[cfg(not(feature = "swc_ecma_transform_plugin"))] #[cfg(not(feature = "swc_ecma_transform_plugin"))]
{ {
let _ = plugin_bytes; let _ = (plugin_bytes, wasm_plugin_runtime_id);
Self { name: plugin_name } Self { name: plugin_name }
} }
} }
...@@ -140,14 +143,20 @@ impl Issue for SwcEcmaTransformFailureIssue { ...@@ -140,14 +143,20 @@ impl Issue for SwcEcmaTransformFailureIssue {
pub struct SwcEcmaTransformPluginsTransformer { pub struct SwcEcmaTransformPluginsTransformer {
#[cfg(feature = "swc_ecma_transform_plugin")] #[cfg(feature = "swc_ecma_transform_plugin")]
plugins: Vec<(turbo_tasks::ResolvedVc<SwcPluginModule>, serde_json::Value)>, plugins: Vec<(turbo_tasks::ResolvedVc<SwcPluginModule>, serde_json::Value)>,
#[cfg(feature = "swc_ecma_transform_plugin")]
wasm_plugin_runtime_id: u64,
} }
impl SwcEcmaTransformPluginsTransformer { impl SwcEcmaTransformPluginsTransformer {
#[cfg(feature = "swc_ecma_transform_plugin")] #[cfg(feature = "swc_ecma_transform_plugin")]
pub fn new( pub fn new(
plugins: Vec<(turbo_tasks::ResolvedVc<SwcPluginModule>, serde_json::Value)>, plugins: Vec<(turbo_tasks::ResolvedVc<SwcPluginModule>, serde_json::Value)>,
wasm_plugin_runtime_id: u64,
) -> Self { ) -> Self {
Self { plugins } Self {
plugins,
wasm_plugin_runtime_id,
}
} }
// [TODO] Due to WEB-1102 putting this module itself behind compile time feature // [TODO] Due to WEB-1102 putting this module itself behind compile time feature
...@@ -184,6 +193,9 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -184,6 +193,9 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
use swc_plugin_backend_napi::NapiRuntime; use swc_plugin_backend_napi::NapiRuntime;
use turbo_tasks::TryJoinIterExt; use turbo_tasks::TryJoinIterExt;
let runtime = NapiRuntime::from_runtime_id(self.wasm_plugin_runtime_id)
.expect("WASM plugin runtime not registered");
let plugins = self let plugins = self
.plugins .plugins
.iter() .iter()
...@@ -192,7 +204,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -192,7 +204,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(&NapiRuntime)), Box::new(plugin_module.plugin.clone_module(&runtime)),
)) ))
}) })
.try_join() .try_join()
...@@ -232,8 +244,10 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -232,8 +244,10 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
ctx: &TransformContext<'_>, ctx: &TransformContext<'_>,
plugins: Vec<(RcStr, serde_json::Value, Box<CompiledPluginModuleBytes>)>, plugins: Vec<(RcStr, serde_json::Value, Box<CompiledPluginModuleBytes>)>,
should_enable_comments_proxy: bool, should_enable_comments_proxy: bool,
runtime_id: u64,
) -> Result<Program> { ) -> Result<Program> {
use either::Either; use either::Either;
use swc_plugin_backend_napi::NapiRuntime;
let transform_metadata_context = Arc::new(TransformPluginMetadataContext::new( let transform_metadata_context = Arc::new(TransformPluginMetadataContext::new(
Some(ctx.file_path_str.to_string()), Some(ctx.file_path_str.to_string()),
...@@ -251,6 +265,8 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -251,6 +265,8 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
// still have to construct from raw bytes internally to perform actual // still have to construct from raw bytes internally to perform actual
// transform. // transform.
for (plugin_name, plugin_config, plugin_module) in plugins { for (plugin_name, plugin_config, plugin_module) in plugins {
let runtime = NapiRuntime::from_runtime_id(runtime_id)
.expect("WASM plugin runtime not registered");
let mut transform_plugin_executor = let mut transform_plugin_executor =
swc_core::plugin_runner::create_plugin_transform_executor( swc_core::plugin_runner::create_plugin_transform_executor(
ctx.source_map, ctx.source_map,
...@@ -259,7 +275,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -259,7 +275,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
None, None,
plugin_module, plugin_module,
Some(plugin_config), Some(plugin_config),
Arc::new(NapiRuntime), Arc::new(runtime),
); );
serialized_program = Either::Right( serialized_program = Either::Right(
...@@ -294,6 +310,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer { ...@@ -294,6 +310,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
ctx, ctx,
plugins, plugins,
should_enable_comments_proxy, should_enable_comments_proxy,
self.wasm_plugin_runtime_id,
) { ) {
Ok(program) => anyhow::Ok(program), Ok(program) => anyhow::Ok(program),
Err(e) => { Err(e) => {
......
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