Skip to content

Commit 1d3fe07

Browse files
committed
refactor(loader): dispatch parallel loaders from Rust
1 parent 1e06873 commit 1d3fe07

76 files changed

Lines changed: 2780 additions & 1656 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 48 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ aho-corasick = { version = "1.1.4", default-features = false }
2121
anyhow = { version = "1.0.102", default-features = false, features = ["backtrace", "std"] }
2222
anymap = { package = "anymap3", version = "1.1.0", default-features = false, features = ["std"] }
2323
arc-swap = { version = "1.9.1", default-features = false }
24+
async-channel = { version = "2.5.0", default-features = false, features = ["std"] }
2425
async-recursion = { version = "1.1.1", default-features = false }
2526
async-trait = { version = "0.1.89", default-features = false }
2627
atomic_refcell = { version = "0.1.14", default-features = false }

crates/node_binding/napi-binding.d.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,17 @@ export declare class VirtualFileStore {
513513
}
514514
export type JsVirtualFileStore = VirtualFileStore
515515

516+
/** Owns one Rust loader context received from the process-wide native MPMC queue. */
517+
export declare class WorkerTask {
518+
/**
519+
* Materializes the ordinary JsLoaderContext DTO in the worker isolate. Until this boundary the
520+
* queue contains only the canonical Rust LoaderContext.
521+
*/
522+
takeContext(): JsLoaderContext
523+
complete(context: JsLoaderContext): void
524+
fail(error: string): void
525+
}
526+
516527
export interface AssetInfoRelated {
517528
sourceMap?: string | null
518529
}
@@ -967,9 +978,14 @@ export interface JsLoaderContext {
967978
resource: string
968979
_module: Module
969980
hot: Readonly<boolean>
981+
rootContext: string
982+
mode: string
983+
loaderContextExtensions: string
984+
__internal__runHooksOnly: boolean
985+
__internal__hookExtensions?: string
970986
/** Content maybe empty in pitching stage */
971987
content: null | Buffer
972-
additionalData?: any
988+
additionalData?: string
973989
__internal__parseMeta: Record<string, string>
974990
sourceMap?: Buffer
975991
cacheable: boolean
@@ -997,6 +1013,9 @@ export interface JsLoaderItem {
9971013
loader: string
9981014
type: string
9991015
cache: boolean
1016+
parallel: boolean
1017+
optionsHandle?: number
1018+
ident?: string
10001019
data: any
10011020
normalExecuted: boolean
10021021
pitchExecuted: boolean
@@ -2772,8 +2791,11 @@ export interface RawModuleRule {
27722791
export interface RawModuleRuleUse {
27732792
loader: string
27742793
options?: string
2794+
/** Handle for ordinary JavaScript loader options kept in the main JS isolate. */
2795+
jsOptionsHandle?: number
27752796
cache: boolean
27762797
optionsCacheKey: string
2798+
parallel: boolean
27772799
}
27782800

27792801
export interface RawNewCache {
@@ -3226,6 +3248,12 @@ export interface RealDependencyLocation {
32263248
end?: SourcePosition
32273249
}
32283250

3251+
/**
3252+
* Every JavaScript worker loops around this receive operation, so all workers compete for the
3253+
* same unbounded MPMC receiver without registration or per-pool state.
3254+
*/
3255+
export declare function recvWorkerTask(): Promise<WorkerTask>
3256+
32293257
/** * this is a process level tracing, which means it would be shared by all compilers in the same process
32303258
* only the first call would take effect, the following calls would be ignored
32313259
* Some code is modified based on

crates/rspack/tests/loader.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ async fn lightningcss() {
2828
options: Some(json!({
2929
"include": 1 // lower nesting syntax
3030
}).to_string()),
31+
js_options_handle: None,
3132
cache: false,
32-
options_cache_key: String::new(),
33+
options_cache_key: Default::default(),
34+
parallel: false,
3335
}]),
3436
..Default::default()
3537
},
@@ -81,8 +83,10 @@ async fn swc() {
8183
}
8284
}
8385
}).to_string()),
86+
js_options_handle: None,
8487
cache: false,
85-
options_cache_key: String::new(),
88+
options_cache_key: Default::default(),
89+
parallel: false,
8690
}]),
8791
..Default::default()
8892
},
@@ -119,8 +123,10 @@ async fn react_refresh() {
119123
ModuleRuleUseLoader {
120124
loader: "builtin:react-refresh-loader".to_string(),
121125
options: None,
126+
js_options_handle: None,
122127
cache: false,
123-
options_cache_key: String::new(),
128+
options_cache_key: Default::default(),
129+
parallel: false,
124130
},
125131
ModuleRuleUseLoader {
126132
loader: "builtin:swc-loader".to_string(),
@@ -141,8 +147,10 @@ async fn react_refresh() {
141147
}
142148
}
143149
}).to_string()),
150+
js_options_handle: None,
144151
cache: false,
145-
options_cache_key: String::new(),
152+
options_cache_key: Default::default(),
153+
parallel: false,
146154
}]),
147155
..Default::default()
148156
},
@@ -180,8 +188,10 @@ async fn preact_refresh() {
180188
ModuleRuleUseLoader {
181189
loader: "builtin:preact-refresh-loader".to_string(),
182190
options: None,
191+
js_options_handle: None,
183192
cache: false,
184-
options_cache_key: String::new(),
193+
options_cache_key: Default::default(),
194+
parallel: false,
185195
},
186196
ModuleRuleUseLoader {
187197
loader: "builtin:swc-loader".to_string(),
@@ -202,8 +212,10 @@ async fn preact_refresh() {
202212
}
203213
}
204214
}).to_string()),
215+
js_options_handle: None,
205216
cache: false,
206-
options_cache_key: String::new(),
217+
options_cache_key: Default::default(),
218+
parallel: false,
207219
}]),
208220
..Default::default()
209221
},

crates/rspack_binding_api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ mod module;
7979
mod module_graph;
8080
mod module_graph_connection;
8181
mod modules;
82+
mod native_reference;
8283
mod native_watcher;
8384
mod normal_module_factory;
8485
mod options;
@@ -101,6 +102,7 @@ mod swc;
101102
mod trace_event;
102103
mod utils;
103104
mod virtual_modules;
105+
mod worker;
104106

105107
use std::{
106108
cell::RefCell,

0 commit comments

Comments
 (0)