Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
fb4332ff
Commit
fb4332ff
authored
Apr 02, 2026
by
Luke Sandberg
Committed by
Luke Sandberg
Apr 09, 2026
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
snappy wtf
parent
4abd123c
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
346 additions
and
126 deletions
+346
-126
test-turbopack-rust-bench-test.yml
.github/workflows/test-turbopack-rust-bench-test.yml
+2
-2
app.rs
crates/next-api/src/app.rs
+7
-0
pages.rs
crates/next-api/src/pages.rs
+5
-0
project.rs
crates/next-api/src/project.rs
+18
-0
context.rs
crates/next-core/src/next_client/context.rs
+7
-1
context.rs
crates/next-core/src/next_server/context.rs
+7
-1
swc_ecma_transform_plugins.rs
.../src/next_shared/transforms/swc_ecma_transform_plugins.rs
+20
-5
project.rs
crates/next-napi-bindings/src/next_api/project.rs
+5
-0
transform.rs
crates/next-napi-bindings/src/transform.rs
+36
-9
lib.rs
crates/swc-plugin-backend-napi/src/lib.rs
+160
-72
package.json
packages/next-swc/package.json
+1
-1
generated-native.d.ts
packages/next/src/build/swc/generated-native.d.ts
+34
-26
index.ts
packages/next/src/build/swc/index.ts
+6
-1
wasm-manager.ts
packages/next/src/build/swc/wasm-manager.ts
+12
-1
wasm-worker.ts
packages/next/src/build/swc/wasm-worker.ts
+3
-1
swc_ecma_transform_plugins.rs
...cript-plugins/src/transform/swc_ecma_transform_plugins.rs
+23
-6
No files found.
.github/workflows/test-turbopack-rust-bench-test.yml
View file @
fb4332ff
...
...
@@ -49,12 +49,12 @@ jobs:
-
name
:
Build benchmarks for tests
timeout-minutes
:
120
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
timeout-minutes
:
120
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
if
:
inputs.all
...
...
crates/next-api/src/app.rs
View file @
fb4332ff
...
...
@@ -188,6 +188,7 @@ impl AppProject {
self
.project
()
.next_mode
(),
self
.project
()
.next_config
(),
self
.project
()
.encryption_key
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -220,6 +221,7 @@ impl AppProject {
self
.project
()
.encryption_key
(),
self
.project
()
.server_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -235,6 +237,7 @@ impl AppProject {
self
.project
()
.encryption_key
(),
self
.project
()
.edge_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -250,6 +253,7 @@ impl AppProject {
self
.project
()
.encryption_key
(),
self
.project
()
.server_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -265,6 +269,7 @@ impl AppProject {
self
.project
()
.encryption_key
(),
self
.project
()
.edge_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -592,6 +597,7 @@ impl AppProject {
self
.project
()
.encryption_key
(),
self
.project
()
.server_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -607,6 +613,7 @@ impl AppProject {
self
.project
()
.encryption_key
(),
self
.project
()
.edge_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
crates/next-api/src/pages.rs
View file @
fb4332ff
...
...
@@ -357,6 +357,7 @@ impl PagesProject {
self
.project
()
.next_mode
(),
self
.project
()
.next_config
(),
self
.project
()
.encryption_key
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -444,6 +445,7 @@ impl PagesProject {
self
.project
()
.encryption_key
(),
self
.project
()
.server_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -461,6 +463,7 @@ impl PagesProject {
self
.project
()
.encryption_key
(),
self
.project
()
.edge_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -478,6 +481,7 @@ impl PagesProject {
self
.project
()
.encryption_key
(),
self
.project
()
.server_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
@@ -495,6 +499,7 @@ impl PagesProject {
self
.project
()
.encryption_key
(),
self
.project
()
.edge_compile_time_info
()
.environment
(),
self
.project
()
.client_compile_time_info
()
.environment
(),
*
self
.project
()
.wasm_plugin_runtime_id
()
.await
?
,
))
}
...
...
crates/next-api/src/project.rs
View file @
fb4332ff
...
...
@@ -375,6 +375,9 @@ pub struct ProjectOptions {
/// force new filenames without changing file content. Empty string means
/// no salt.
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)]
...
...
@@ -824,6 +827,7 @@ impl ProjectContainer {
let
is_persistent_caching_enabled
;
let
server_hmr
;
let
hash_salt
;
let
wasm_plugin_runtime_id
;
{
let
options
=
self
.options_state
.get
();
let
options
=
options
...
...
@@ -853,6 +857,7 @@ impl ProjectContainer {
is_persistent_caching_enabled
=
options
.is_persistent_caching_enabled
;
server_hmr
=
options
.server_hmr
;
hash_salt
=
options
.hash_salt
.clone
();
wasm_plugin_runtime_id
=
options
.wasm_plugin_runtime_id
;
}
let
dist_dir
=
next_config
.dist_dir
()
.owned
()
.await
?
;
...
...
@@ -884,6 +889,7 @@ impl ProjectContainer {
is_persistent_caching_enabled
,
server_hmr
,
hash_salt
,
wasm_plugin_runtime_id
,
}
.cell
())
}
...
...
@@ -991,6 +997,9 @@ pub struct Project {
/// A salt to mix into chunk and asset content hashes. Empty string means
/// no salt.
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]
...
...
@@ -1245,6 +1254,11 @@ impl Project {
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]
pub
(
super
)
fn
no_mangling
(
&
self
)
->
Vc
<
bool
>
{
Vc
::
cell
(
self
.no_mangling
)
...
...
@@ -1942,6 +1956,7 @@ impl Project {
self
.encryption_key
(),
self
.edge_compile_time_info
()
.environment
(),
self
.client_compile_time_info
()
.environment
(),
*
self
.wasm_plugin_runtime_id
()
.await
?
,
),
get_edge_resolve_options_context
(
self
.project_path
()
.owned
()
.await
?
,
...
...
@@ -2005,6 +2020,7 @@ impl Project {
self
.encryption_key
(),
self
.server_compile_time_info
()
.environment
(),
self
.client_compile_time_info
()
.environment
(),
*
self
.wasm_plugin_runtime_id
()
.await
?
,
),
get_server_resolve_options_context
(
self
.project_path
()
.owned
()
.await
?
,
...
...
@@ -2120,6 +2136,7 @@ impl Project {
self
.encryption_key
(),
self
.server_compile_time_info
()
.environment
(),
self
.client_compile_time_info
()
.environment
(),
*
self
.wasm_plugin_runtime_id
()
.await
?
,
),
get_server_resolve_options_context
(
self
.project_path
()
.owned
()
.await
?
,
...
...
@@ -2183,6 +2200,7 @@ impl Project {
self
.encryption_key
(),
self
.edge_compile_time_info
()
.environment
(),
self
.client_compile_time_info
()
.environment
(),
*
self
.wasm_plugin_runtime_id
()
.await
?
,
),
get_edge_resolve_options_context
(
self
.project_path
()
.owned
()
.await
?
,
...
...
crates/next-core/src/next_client/context.rs
View file @
fb4332ff
...
...
@@ -233,6 +233,7 @@ pub async fn get_client_module_options_context(
mode
:
Vc
<
NextMode
>
,
next_config
:
Vc
<
NextConfig
>
,
encryption_key
:
ResolvedVc
<
RcStr
>
,
wasm_plugin_runtime_id
:
u64
,
)
->
Result
<
Vc
<
ModuleOptionsContext
>>
{
let
next_mode
=
mode
.await
?
;
let
resolve_options_context
=
get_client_resolve_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
)
.await
?
;
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_emotion_transform_rule
(
next_config
)
.await
?
,
get_styled_components_transform_rule
(
next_config
)
.await
?
,
...
...
crates/next-core/src/next_server/context.rs
View file @
fb4332ff
...
...
@@ -418,6 +418,7 @@ pub async fn get_server_module_options_context(
encryption_key
:
ResolvedVc
<
RcStr
>
,
environment
:
ResolvedVc
<
Environment
>
,
client_environment
:
ResolvedVc
<
Environment
>
,
wasm_plugin_runtime_id
:
u64
,
)
->
Result
<
Vc
<
ModuleOptionsContext
>>
{
let
next_mode
=
mode
.await
?
;
let
mut
next_server_rules
=
get_next_server_transforms_rules
(
...
...
@@ -537,7 +538,12 @@ pub async fn get_server_module_options_context(
get_react_remove_properties_transform_rule
(
next_config
)
.await
?
,
get_emotion_transform_rule
(
next_config
)
.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
()
.flatten
()
...
...
crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs
View file @
fb4332ff
...
...
@@ -12,18 +12,25 @@ use crate::next_config::NextConfig;
pub
async
fn
get_swc_ecma_transform_plugin_rule
(
next_config
:
Vc
<
NextConfig
>
,
project_path
:
FileSystemPath
,
wasm_plugin_runtime_id
:
u64
,
)
->
Result
<
Option
<
ModuleRule
>>
{
let
plugin_configs
=
next_config
.experimental_swc_plugins
()
.await
?
;
if
!
plugin_configs
.is_empty
()
{
#[cfg(feature
=
"plugin"
)]
{
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"
))]
{
let
_
=
project_path
;
// To satisfy lint
let
_
=
(
project_path
,
wasm_plugin_runtime_id
);
Ok
(
None
)
}
}
else
{
...
...
@@ -66,6 +73,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
project_path
:
FileSystemPath
,
plugin_configs
:
&
[(
RcStr
,
serde_json
::
Value
)],
enable_mdx_rs
:
bool
,
wasm_plugin_runtime_id
:
u64
,
)
->
Result
<
Option
<
ModuleRule
>>
{
use
anyhow
::
bail
;
use
turbo_tasks
::
TryFlatJoinIterExt
;
...
...
@@ -147,8 +155,12 @@ pub async fn get_swc_ecma_transform_rule_impl(
};
Ok
(
Some
((
SwcPluginModule
::
new
(
name
.clone
(),
file
.content
()
.to_bytes
()
.to_vec
())
.resolved_cell
(),
SwcPluginModule
::
new
(
name
.clone
(),
file
.content
()
.to_bytes
()
.to_vec
(),
wasm_plugin_runtime_id
,
)
.resolved_cell
(),
config
.clone
(),
)))
}
...
...
@@ -157,7 +169,10 @@ pub async fn get_swc_ecma_transform_rule_impl(
.await
?
;
Ok
(
Some
(
get_ecma_transform_rule
(
Box
::
new
(
SwcEcmaTransformPluginsTransformer
::
new
(
plugins
)),
Box
::
new
(
SwcEcmaTransformPluginsTransformer
::
new
(
plugins
,
wasm_plugin_runtime_id
,
)),
enable_mdx_rs
,
EcmascriptTransformStage
::
Main
,
)))
...
...
crates/next-napi-bindings/src/next_api/project.rs
View file @
fb4332ff
...
...
@@ -221,6 +221,9 @@ pub struct NapiProjectOptions {
/// force new filenames without changing file content. Empty string means
/// no salt.
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.
...
...
@@ -335,6 +338,7 @@ impl From<NapiProjectOptions> for ProjectOptions {
next_version
,
server_hmr
,
hash_salt
,
wasm_plugin_runtime_id
:
_wasm_plugin_runtime_id
,
}
=
val
;
ProjectOptions
{
root_path
,
...
...
@@ -360,6 +364,7 @@ impl From<NapiProjectOptions> for ProjectOptions {
next_version
,
server_hmr
:
server_hmr
.unwrap_or
(
false
),
hash_salt
,
wasm_plugin_runtime_id
:
_wasm_plugin_runtime_id
.map
(|
v
|
v
as
u64
)
.unwrap_or
(
0
),
}
}
}
...
...
crates/next-napi-bindings/src/transform.rs
View file @
fb4332ff
...
...
@@ -50,6 +50,11 @@ use swc_core::{
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
#[derive(Debug)]
pub
enum
Input
{
...
...
@@ -63,6 +68,8 @@ pub struct TransformTask {
pub
c
:
Compiler
,
pub
input
:
Input
,
pub
options
:
Buffer
,
#[cfg(feature
=
"plugin"
)]
pub
wasm_plugin_runtime_id
:
u64
,
}
fn
skip_filename
()
->
bool
{
...
...
@@ -133,11 +140,16 @@ impl Task for TransformTask {
#[cfg(feature
=
"plugin"
)]
{
options
.swc.runtime_options
=
swc_core
::
base
::
config
::
RuntimeOptions
::
default
()
.plugin_runtime
(
std
::
sync
::
Arc
::
new
(
swc_plugin_backend_napi
::
NapiRuntime
,
));
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
=
swc_core
::
base
::
config
::
RuntimeOptions
::
default
()
.plugin_runtime
(
std
::
sync
::
Arc
::
new
(
runtime
));
}
}
let
cm
=
self
.c.cm
.clone
();
...
...
@@ -227,7 +239,13 @@ pub fn transform(
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
))
}
...
...
@@ -248,7 +266,13 @@ pub fn transform_sync(
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
()
?
;
task
.resolve
(
env
,
output
)
}
...
...
@@ -263,10 +287,13 @@ fn test_deser() {
/// Register the NAPI-based WASM plugin runtime.
/// Must be called from JS before any SWC transforms that use plugins.
/// Returns the runtime_id that identifies this registration.
#[cfg(feature
=
"plugin"
)]
#[napi]
pub
fn
register_wasm_plugin_runtime
(
env
:
Env
,
js_manager
:
JsObject
)
->
napi
::
Result
<
()
>
{
swc_plugin_backend_napi
::
register_wasm_runtime
(
&
env
,
&
js_manager
)
pub
fn
register_wasm_plugin_runtime
(
env
:
Env
,
js_manager
:
JsObject
)
->
napi
::
Result
<
f64
>
{
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]
...
...
crates/swc-plugin-backend-napi/src/lib.rs
View file @
fb4332ff
This diff is collapsed.
Click to expand it.
packages/next-swc/package.json
View file @
fb4332ff
...
...
@@ -21,7 +21,7 @@
"rust-check-doc"
:
"RUSTDOCFLAGS='-Zunstable-options --output-format=json' cargo doc --no-deps --workspace"
,
"rust-check-fmt"
:
"cd ../..; cargo fmt -- --check"
,
"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 turbo
pack-ecmascript-plugins --exclude next-core --exclude turbo
-tasks-macros --cargo-profile release-with-assertions --no-fail-fast"
},
"napi"
:
{
"name"
:
"next-swc"
,
...
...
packages/next/src/build/swc/generated-native.d.ts
View file @
fb4332ff
...
...
@@ -21,7 +21,8 @@ export function lightningCssTransformStyleAttribute(
export
function
lightningcssFeatureNamesToMaskNapi
(
names
:
Array
<
string
>
):
number
// 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
// DO NOT MANUALLY EDIT THESE TYPES
...
...
@@ -38,30 +39,6 @@ export declare class ExternalObject<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.
*
...
...
@@ -78,6 +55,7 @@ export declare function sendTaskMessage(message: NapiTaskMessage): Promise<void>
* method on `ops` and sends the result back via a sync channel.
*/
export
declare
function
wasmWorkerRegisterCallback
(
runtimeId
:
number
,
instanceId
:
number
,
ops
:
object
):
void
...
...
@@ -92,11 +70,36 @@ export declare function wasmWorkerRegisterCallback(
* - free(ptr, size) → number
*/
export
declare
function
wasmWorkerDispatchHostFn
(
runtimeId
:
number
,
instanceId
:
number
,
fnIndex
:
number
,
args
:
Array
<
number
>
,
memoryAccessor
:
object
):
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
{
line
:
number
column
?:
number
...
...
@@ -302,6 +305,10 @@ export interface NapiProjectOptions {
* no salt.
*/
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. */
export
interface
NapiPartialProjectOptions
{
...
...
@@ -656,8 +663,9 @@ export declare function transformSync(
/**
* Register the NAPI-based WASM plugin runtime.
* 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
(
path
:
string
,
port
?:
number
|
undefined
|
null
...
...
packages/next/src/build/swc/index.ts
View file @
fb4332ff
...
...
@@ -172,6 +172,9 @@ let loadedBindings: Binding | undefined = undefined
let
downloadWasmPromise
:
any
let
swcTraceFlushGuard
:
any
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
>
}
=
{}
...
...
@@ -662,6 +665,7 @@ function bindingToApi(
path.join(options.rootPath, options.projectPath)
),
env: rustifyEnv(options.env),
wasmPluginRuntimeId: wasmPluginRuntimeId || undefined,
}
}
...
...
@@ -1743,7 +1747,8 @@ function loadNative(importPath?: string): Binding {
const { wasmManager } =
require('./wasm-manager') as typeof import('./wasm-manager')
wasmManager.setBindingsPath(bindingsPath)
bindings.registerWasmPluginRuntime(wasmManager)
wasmPluginRuntimeId = bindings.registerWasmPluginRuntime(wasmManager)
wasmManager.setRuntimeId(wasmPluginRuntimeId)
}
return loadedBindings!
...
...
packages/next/src/build/swc/wasm-manager.ts
View file @
fb4332ff
...
...
@@ -39,6 +39,9 @@ const instanceWorkerMap = new Map<number, number>()
// Path to the native .node binding, set during init
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
// ---------------------------------------------------------------------------
...
...
@@ -57,7 +60,7 @@ function createWorker(): WorkerEntry {
const
workerPath
=
path
.
join
(
__dirname
,
'wasm-worker.js'
)
const
worker
=
new
Worker
(
workerPath
,
{
workerData
:
{
nativeBindingsPath
},
workerData
:
{
nativeBindingsPath
,
runtimeId
},
// Suppress the WASI warnings, this isn't a great solution
execArgv
:
[
'--no-warnings'
],
})
...
...
@@ -145,6 +148,14 @@ export const wasmManager = {
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.
*/
...
...
packages/next/src/build/swc/wasm-worker.ts
View file @
fb4332ff
...
...
@@ -23,6 +23,7 @@ if (!parentPort) {
// Load native addon directly in the worker thread.
// This gives us direct NAPI access for host function callbacks.
const
bindings
=
require
(
workerData
.
nativeBindingsPath
)
const
runtimeId
:
number
=
workerData
.
runtimeId
// ---------------------------------------------------------------------------
// Instance storage — the ops object closes over the WASM instance/memory,
...
...
@@ -105,6 +106,7 @@ parentPort.on('message', (req: any) => {
throw
new
Error
(
`Instance
${
instanceId
}
not found for host fn`
)
}
return
bindings
.
wasmWorkerDispatchHostFn
(
runtimeId
,
instanceId
,
index
,
args
.
slice
(
0
,
paramCount
),
...
...
@@ -128,7 +130,7 @@ parentPort.on('message', (req: any) => {
// the instance handle and the ops interface for Rust.
const
ops
=
createInstanceOps
(
instance
,
memory
)
instances
.
set
(
instanceId
,
ops
)
bindings
.
wasmWorkerRegisterCallback
(
instanceId
,
ops
)
bindings
.
wasmWorkerRegisterCallback
(
runtimeId
,
instanceId
,
ops
)
response
=
{
result
:
instanceId
}
break
...
...
turbopack/crates/turbopack-ecmascript-plugins/src/transform/swc_ecma_transform_plugins.rs
View file @
fb4332ff
...
...
@@ -22,7 +22,7 @@ pub struct 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"
)]
{
use
swc_core
::
plugin_runner
::
plugin_module_bytes
::{
...
...
@@ -30,9 +30,12 @@ impl SwcPluginModule {
};
use
swc_plugin_backend_napi
::
NapiRuntime
;
let
runtime
=
NapiRuntime
::
from_runtime_id
(
wasm_plugin_runtime_id
)
.expect
(
"WASM plugin runtime not registered"
);
Self
{
plugin
:
CompiledPluginModuleBytes
::
from_raw_module
(
&
NapiR
untime
,
&
r
untime
,
RawPluginModuleBytes
::
new
(
plugin_name
.to_string
(),
plugin_bytes
),
),
name
:
plugin_name
,
...
...
@@ -41,7 +44,7 @@ impl SwcPluginModule {
#[cfg(not(feature
=
"swc_ecma_transform_plugin"
))]
{
let
_
=
plugin_bytes
;
let
_
=
(
plugin_bytes
,
wasm_plugin_runtime_id
)
;
Self
{
name
:
plugin_name
}
}
}
...
...
@@ -140,14 +143,20 @@ impl Issue for SwcEcmaTransformFailureIssue {
pub
struct
SwcEcmaTransformPluginsTransformer
{
#[cfg(feature
=
"swc_ecma_transform_plugin"
)]
plugins
:
Vec
<
(
turbo_tasks
::
ResolvedVc
<
SwcPluginModule
>
,
serde_json
::
Value
)
>
,
#[cfg(feature
=
"swc_ecma_transform_plugin"
)]
wasm_plugin_runtime_id
:
u64
,
}
impl
SwcEcmaTransformPluginsTransformer
{
#[cfg(feature
=
"swc_ecma_transform_plugin"
)]
pub
fn
new
(
plugins
:
Vec
<
(
turbo_tasks
::
ResolvedVc
<
SwcPluginModule
>
,
serde_json
::
Value
)
>
,
wasm_plugin_runtime_id
:
u64
,
)
->
Self
{
Self
{
plugins
}
Self
{
plugins
,
wasm_plugin_runtime_id
,
}
}
// [TODO] Due to WEB-1102 putting this module itself behind compile time feature
...
...
@@ -184,6 +193,9 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
use
swc_plugin_backend_napi
::
NapiRuntime
;
use
turbo_tasks
::
TryJoinIterExt
;
let
runtime
=
NapiRuntime
::
from_runtime_id
(
self
.wasm_plugin_runtime_id
)
.expect
(
"WASM plugin runtime not registered"
);
let
plugins
=
self
.plugins
.iter
()
...
...
@@ -192,7 +204,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
Ok
((
plugin_module
.name
.clone
(),
config
.clone
(),
Box
::
new
(
plugin_module
.plugin
.clone_module
(
&
NapiR
untime
)),
Box
::
new
(
plugin_module
.plugin
.clone_module
(
&
r
untime
)),
))
})
.try_join
()
...
...
@@ -232,8 +244,10 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
ctx
:
&
TransformContext
<
'_
>
,
plugins
:
Vec
<
(
RcStr
,
serde_json
::
Value
,
Box
<
CompiledPluginModuleBytes
>
)
>
,
should_enable_comments_proxy
:
bool
,
runtime_id
:
u64
,
)
->
Result
<
Program
>
{
use
either
::
Either
;
use
swc_plugin_backend_napi
::
NapiRuntime
;
let
transform_metadata_context
=
Arc
::
new
(
TransformPluginMetadataContext
::
new
(
Some
(
ctx
.file_path_str
.to_string
()),
...
...
@@ -251,6 +265,8 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
// still have to construct from raw bytes internally to perform actual
// transform.
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
=
swc_core
::
plugin_runner
::
create_plugin_transform_executor
(
ctx
.source_map
,
...
...
@@ -259,7 +275,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
None
,
plugin_module
,
Some
(
plugin_config
),
Arc
::
new
(
NapiR
untime
),
Arc
::
new
(
r
untime
),
);
serialized_program
=
Either
::
Right
(
...
...
@@ -294,6 +310,7 @@ impl CustomTransformer for SwcEcmaTransformPluginsTransformer {
ctx
,
plugins
,
should_enable_comments_proxy
,
self
.wasm_plugin_runtime_id
,
)
{
Ok
(
program
)
=>
anyhow
::
Ok
(
program
),
Err
(
e
)
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment