Skip to content

Commit cd3c104

Browse files
committed
refactor(turbopack-node): support execution by napi and worker_threads
1 parent 614b361 commit cd3c104

File tree

16 files changed

+620
-171
lines changed

16 files changed

+620
-171
lines changed

Cargo.lock

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ napi = { version = "2", default-features = false, features = [
423423
"napi5",
424424
"compat-mode",
425425
] }
426+
napi-derive = "2"
427+
napi-build = "2"
426428
notify = "8.1.0"
427429
once_cell = "1.17.1"
428430
owo-colors = "4.2.2"

crates/napi/Cargo.toml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ workspace = true
4646

4747
[package.metadata.cargo-shear]
4848
ignored = [
49-
# we need to set features on these packages when building for WASM, but we don't directly use them
50-
"getrandom",
51-
"iana-time-zone",
52-
# the plugins feature needs to set a feature on this transitively depended-on package, we never
53-
# directly import it
54-
"turbopack-ecmascript-plugins",
49+
# we need to set features on these packages when building for WASM, but we don't directly use them
50+
"getrandom",
51+
"iana-time-zone",
52+
# the plugins feature needs to set a feature on this transitively depended-on package, we never
53+
# directly import it
54+
"turbopack-ecmascript-plugins",
5555
]
5656

5757
[dependencies]
@@ -63,7 +63,7 @@ flate2 = { workspace = true }
6363
futures-util = { workspace = true }
6464
owo-colors = { workspace = true }
6565
napi = { workspace = true }
66-
napi-derive = "2"
66+
napi-derive = { workspace = true }
6767
next-custom-transforms = { workspace = true }
6868
next-taskless = { workspace = true }
6969
rand = { workspace = true }
@@ -115,7 +115,7 @@ next-core = { workspace = true }
115115
mdxjs = { workspace = true, features = ["serializable"] }
116116

117117
turbo-tasks-malloc = { workspace = true, default-features = false, features = [
118-
"custom_allocator"
118+
"custom_allocator",
119119
] }
120120

121121
turbopack-core = { workspace = true }
@@ -143,8 +143,7 @@ tokio = { workspace = true, features = ["full"] }
143143

144144
[build-dependencies]
145145
anyhow = { workspace = true }
146-
napi-build = "2"
146+
napi-build = { workspace = true }
147147
serde = { workspace = true }
148148
serde_json = { workspace = true }
149149
vergen-gitcl = { workspace = true }
150-

crates/next-api/src/project.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,11 +1057,12 @@ impl Project {
10571057

10581058
// At this point all modules have been computed and we can get rid of the node.js
10591059
// process pools
1060-
if *self.is_watch_enabled().await? {
1061-
turbopack_node::evaluate::scale_down();
1062-
} else {
1063-
turbopack_node::evaluate::scale_zero();
1064-
}
1060+
// TODO:
1061+
// if *self.is_watch_enabled().await? {
1062+
// turbopack_node::evaluate::scale_down();
1063+
// } else {
1064+
// turbopack_node::evaluate::scale_zero();
1065+
// }
10651066

10661067
Ok(module_graphs_vc)
10671068
}

packages/next/src/build/swc/generated-native.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ export declare class ExternalObject<T> {
3434
[K: symbol]: T
3535
}
3636
}
37+
export declare function recvPoolRequest(): Promise<string>
38+
export declare function notifyPoolCreated(filename: string): Promise<void>
39+
export declare function recvWorkerRequest(poolId: string): Promise<void>
40+
export declare function notifyWorkerAck(poolId: string): Promise<void>
41+
export declare function recvEvaluation(poolId: string): Promise<Array<number>>
42+
export declare function recvMessageInWorker(
43+
workerId: number
44+
): Promise<Array<number>>
45+
export declare function sendTaskResponse(
46+
taskId: string,
47+
data: Array<number>
48+
): Promise<void>
3749
export declare function lockfileTryAcquireSync(
3850
path: string
3951
): { __napiType: 'Lockfile' } | null

turbopack/crates/turbopack-node/Cargo.toml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ autobenches = false
1010
bench = false
1111

1212
[features]
13+
default = ["worker_thread"]
1314
# enable "HMR" for embedded assets
1415
dynamic_embed_contents = ["turbo-tasks-fs/dynamic_embed_contents"]
16+
child_process = ["tokio"]
17+
worker_thread = ["napi", "napi-derive"]
1518

1619
[lints]
1720
workspace = true
@@ -20,6 +23,8 @@ workspace = true
2023
anyhow = { workspace = true }
2124
async-stream = "0.3.4"
2225
async-trait = { workspace = true }
26+
async-channel = "2.5.0"
27+
dashmap = { workspace = true }
2328
base64 = "0.21.0"
2429
const_format = { workspace = true }
2530
either = { workspace = true, features = ["serde"] }
@@ -34,7 +39,10 @@ rustc-hash = { workspace = true }
3439
serde = { workspace = true }
3540
serde_json = { workspace = true }
3641
serde_with = { workspace = true, features = ["base64"] }
37-
tokio = { workspace = true, features = ["full"] }
42+
tokio = { workspace = true, optional = true, features = ["full"] }
43+
uuid = { workspace = true, features = ["v4"] }
44+
napi = { workspace = true, optional = true, features = ["anyhow"] }
45+
napi-derive = { workspace = true, optional = true }
3846
tracing = { workspace = true }
3947
turbo-rcstr = { workspace = true }
4048
turbo-tasks = { workspace = true }
@@ -46,3 +54,5 @@ turbopack-core = { workspace = true }
4654
turbopack-ecmascript = { workspace = true }
4755
turbopack-resolve = { workspace = true }
4856

57+
[build-dependencies]
58+
napi-build = { workspace = true }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
#[cfg(not(all(target_os = "macos", target_arch = "aarch64")))]
3+
if std::env::var("CARGO_FEATURE_WORKER_THREAD").is_ok() {
4+
napi_build::setup();
5+
}
6+
}

0 commit comments

Comments
 (0)