Skip to content

Commit 7537b7b

Browse files
authored
Merge branch 'main' into perf-js-loader-runner
2 parents e0c593b + 792ad27 commit 7537b7b

File tree

156 files changed

+2797
-2528
lines changed

Some content is hidden

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

156 files changed

+2797
-2528
lines changed

.github/actions/cache/restore/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ runs:
3232
- name: Restore cache to local
3333
id: local-cache
3434
if: ${{ runner.environment == 'self-hosted' }}
35-
uses: lynx-infra/cache/restore@558d7c999f9f97ac02ed7e711503bb81d82ff8ee # main
35+
uses: lynx-infra/cache/restore@5c6160a6a4c7fca80a2f3057bb9dfc9513fcb732 # main
3636
with:
3737
key: ${{ inputs.key }}
3838
path: ${{ inputs.path }}

.github/actions/cache/save/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ runs:
2121
path: ${{ inputs.path }}
2222
- name: Save cache to local
2323
if: ${{ runner.environment == 'self-hosted' }}
24-
uses: lynx-infra/cache/save@558d7c999f9f97ac02ed7e711503bb81d82ff8ee # main
24+
uses: lynx-infra/cache/save@5c6160a6a4c7fca80a2f3057bb9dfc9513fcb732 # main
2525
with:
2626
key: ${{ inputs.key }}
2727
path: ${{ inputs.path }}

.github/workflows/ci-rust.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
key: check
8484

8585
- name: Install cargo-deny
86-
uses: taiki-e/install-action@52039f232923165906a3c824622a332e84ca4560 # v2
86+
uses: taiki-e/install-action@84c20235bedc3797c7e1ddf685c574a4a6c666da # v2
8787
with:
8888
8989
- name: Check licenses

.github/workflows/reusable-build-bench.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
key: build-bench-${{ inputs.target }}
4343

4444
- name: Install cargo-codspeed binary
45-
uses: taiki-e/install-action@52039f232923165906a3c824622a332e84ca4560 # v2
45+
uses: taiki-e/install-action@84c20235bedc3797c7e1ddf685c574a4a6c666da # v2
4646
with:
4747
4848

Cargo.lock

Lines changed: 16 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
@@ -178,6 +178,7 @@ rspack_plugin_real_content_hash = { version = "0.2.0", path = "crates/rsp
178178
rspack_plugin_remove_duplicate_modules = { version = "0.2.0", path = "crates/rspack_plugin_remove_duplicate_modules" }
179179
rspack_plugin_remove_empty_chunks = { version = "0.2.0", path = "crates/rspack_plugin_remove_empty_chunks" }
180180
rspack_plugin_rsdoctor = { version = "0.2.0", path = "crates/rspack_plugin_rsdoctor" }
181+
rspack_plugin_rstest = { version = "0.2.0", path = "crates/rspack_plugin_rstest" }
181182
rspack_plugin_runtime = { version = "0.2.0", path = "crates/rspack_plugin_runtime" }
182183
rspack_plugin_runtime_chunk = { version = "0.2.0", path = "crates/rspack_plugin_runtime_chunk" }
183184
rspack_plugin_schemes = { version = "0.2.0", path = "crates/rspack_plugin_schemes" }

crates/node_binding/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ rspack_paths = { workspace = true }
2828
rspack_plugin_html = { workspace = true }
2929
rspack_plugin_javascript = { workspace = true }
3030
rspack_plugin_rsdoctor = { workspace = true }
31+
rspack_plugin_rstest = { workspace = true }
3132
rspack_plugin_runtime = { workspace = true }
3233
rspack_util = { workspace = true }
3334

@@ -105,6 +106,7 @@ serde = { workspace = true }
105106
serde_json = { workspace = true }
106107
swc_core = { workspace = true, default-features = false, features = ["ecma_transforms_react"] }
107108
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "tracing"] }
109+
ustr = { workspace = true }
108110

109111
[target.'cfg(not(target_family = "wasm"))'.dependencies]
110112
tokio = { workspace = true, features = ["parking_lot"] }

crates/node_binding/binding.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export declare class JsCompilation {
274274
* Using async and mutable reference to `Compilation` at the same time would likely to cause data races.
275275
*/
276276
rebuildModule(moduleIdentifiers: Array<string>, f: any): void
277-
importModule(request: string, layer: string | undefined | null, publicPath: JsFilename | undefined | null, baseUri: string | undefined | null, originalModule: string | undefined | null, originalModuleContext: string | undefined | null, callback: any): void
277+
importModule(request: string, layer: string | undefined | null, publicPath: JsFilename | undefined | null, baseUri: string | undefined | null, originalModule: string, originalModuleContext: string | undefined | null, callback: any): void
278278
get entries(): JsEntries
279279
addRuntimeModule(chunk: JsChunk, runtimeModule: JsAddingRuntimeModule): void
280280
get moduleGraph(): JsModuleGraph
@@ -493,6 +493,7 @@ export declare enum BuiltinPluginName {
493493
CssExtractRspackPlugin = 'CssExtractRspackPlugin',
494494
SubresourceIntegrityPlugin = 'SubresourceIntegrityPlugin',
495495
RsdoctorPlugin = 'RsdoctorPlugin',
496+
RstestPlugin = 'RstestPlugin',
496497
CircularDependencyRspackPlugin = 'CircularDependencyRspackPlugin',
497498
JsLoaderRspackPlugin = 'JsLoaderRspackPlugin',
498499
LazyCompilationPlugin = 'LazyCompilationPlugin',
@@ -2431,6 +2432,10 @@ export interface RawRspackFuture {
24312432

24322433
}
24332434

2435+
export interface RawRstestPluginOptions {
2436+
injectModulePathName: boolean
2437+
}
2438+
24342439
export interface RawRuleSetCondition {
24352440
type: RawRuleSetConditionType
24362441
string?: string

crates/node_binding/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rspack/binding",
3-
"version": "1.3.11",
3+
"version": "1.3.12",
44
"license": "MIT",
55
"description": "Node binding for rspack",
66
"main": "binding.js",
@@ -47,8 +47,7 @@
4747
"aarch64-unknown-linux-gnu",
4848
"aarch64-apple-darwin",
4949
"aarch64-unknown-linux-musl",
50-
"aarch64-pc-windows-msvc",
51-
"wasm32-wasip1-threads"
50+
"aarch64-pc-windows-msvc"
5251
],
5352
"wasm": {
5453
"initialMemory": 16384,
@@ -64,4 +63,4 @@
6463
"@rspack/binding-wasm32-wasi": "workspace:*",
6564
"@rspack/binding-win32-x64-msvc": "workspace:*"
6665
}
67-
}
66+
}

crates/node_binding/rspack.wasi.cjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,29 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
6565
worker.onmessage = ({ data }) => {
6666
__wasmCreateOnMessageForFsProxy(__nodeFs)(data)
6767
}
68+
69+
// The main thread of Node.js waits for all the active handles before exiting.
70+
// But Rust threads are never waited without `thread::join`.
71+
// So here we hack the code of Node.js to prevent the workers from being referenced (active).
72+
// According to https://github.com/nodejs/node/blob/19e0d472728c79d418b74bddff588bea70a403d0/lib/internal/worker.js#L415,
73+
// a worker is consist of two handles: kPublicPort and kHandle.
74+
{
75+
const kPublicPort = Object.getOwnPropertySymbols(worker).find(s =>
76+
s.toString().includes("kPublicPort")
77+
);
78+
if (kPublicPort) {
79+
worker[kPublicPort].ref = () => {};
80+
}
81+
82+
const kHandle = Object.getOwnPropertySymbols(worker).find(s =>
83+
s.toString().includes("kHandle")
84+
);
85+
if (kPublicPort) {
86+
worker[kHandle].ref = () => {};
87+
}
88+
89+
worker.unref();
90+
}
6891
return worker
6992
},
7093
overwriteImports(importObject) {

crates/node_binding/src/chunk_graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ptr::NonNull, sync::Arc};
1+
use std::ptr::NonNull;
22

33
use napi::{Either, Result};
44
use napi_derive::napi;
@@ -173,8 +173,8 @@ impl JsChunkGraph {
173173
) -> napi::Result<Option<&str>> {
174174
let compilation = self.as_ref()?;
175175
let Some(runtime) = js_runtime.map(|js_runtime| match js_runtime {
176-
Either::A(str) => std::iter::once(str).map(Arc::from).collect(),
177-
Either::B(vec) => vec.into_iter().map(Arc::from).collect(),
176+
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
177+
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
178178
}) else {
179179
return Ok(None);
180180
};

crates/node_binding/src/compilation/code_generation_results.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::sync::Arc;
2-
31
use napi::Either;
42
use rspack_core::{Reflector, WeakBindingCell};
53
use rustc_hash::FxHashMap;
@@ -112,8 +110,8 @@ impl CodeGenerationResults {
112110
pub fn get(&self, module: ModuleObjectRef, runtime: JsRuntimeSpec) -> napi::Result<Reflector> {
113111
self.with_ref(|code_generation_results| {
114112
let rt: Option<rspack_core::RuntimeSpec> = runtime.map(|val| match val {
115-
Either::A(str) => std::iter::once(str).map(Arc::from).collect(),
116-
Either::B(vec) => vec.into_iter().map(Arc::from).collect(),
113+
Either::A(str) => std::iter::once(str).map(Into::into).collect(),
114+
Either::B(vec) => vec.into_iter().map(Into::into).collect(),
117115
});
118116

119117
let code_generation_result = code_generation_results.get(&module.identifier, rt.as_ref());

crates/node_binding/src/compilation/mod.rs

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ use rustc_hash::FxHashMap;
2424

2525
use super::PathWithInfo;
2626
use crate::{
27-
entry::JsEntryOptions, utils::callbackify, AssetInfo, EntryDependency, JsAddingRuntimeModule,
28-
JsAsset, JsChunk, JsChunkGraph, JsChunkGroupWrapper, JsChunkWrapper, JsCompatSource, JsFilename,
29-
JsModuleGraph, JsPathData, JsRspackDiagnostic, JsRspackError, JsStats,
30-
JsStatsOptimizationBailout, ModuleObject, RspackResultToNapiResultExt, ToJsCompatSource,
27+
entry::JsEntryOptions, utils::callbackify, AssetInfo, EntryDependency, ErrorCode,
28+
JsAddingRuntimeModule, JsAsset, JsChunk, JsChunkGraph, JsChunkGroupWrapper, JsChunkWrapper,
29+
JsCompatSource, JsFilename, JsModuleGraph, JsPathData, JsRspackDiagnostic, JsRspackError,
30+
JsStats, JsStatsOptimizationBailout, ModuleObject, RspackResultToNapiResultExt, ToJsCompatSource,
3131
COMPILER_REFERENCES,
3232
};
3333

@@ -569,8 +569,10 @@ impl JsCompilation {
569569
reference: Reference<JsCompilation>,
570570
module_identifiers: Vec<String>,
571571
f: Function<'static>,
572-
) -> Result<()> {
573-
let compilation = self.as_mut()?;
572+
) -> Result<(), ErrorCode> {
573+
let compilation = self
574+
.as_mut()
575+
.map_err(|err| napi::Error::new(err.status.into(), err.reason.clone()))?;
574576

575577
callbackify(
576578
f,
@@ -605,11 +607,13 @@ impl JsCompilation {
605607
layer: Option<String>,
606608
public_path: Option<JsFilename>,
607609
base_uri: Option<String>,
608-
original_module: Option<String>,
610+
original_module: String,
609611
original_module_context: Option<String>,
610612
callback: Function<'static>,
611-
) -> Result<()> {
612-
let compilation = self.as_ref()?;
613+
) -> Result<(), ErrorCode> {
614+
let compilation = self
615+
.as_ref()
616+
.map_err(|err| napi::Error::new(err.status.into(), err.reason.clone()))?;
613617

614618
callbackify(
615619
callback,
@@ -625,7 +629,7 @@ impl JsCompilation {
625629
public_path.map(|p| p.into()),
626630
base_uri,
627631
original_module_context.map(rspack_core::Context::from),
628-
original_module.map(ModuleIdentifier::from),
632+
ModuleIdentifier::from(original_module),
629633
)
630634
.await;
631635

@@ -705,19 +709,23 @@ impl JsCompilation {
705709
reference: Reference<JsCompilation>,
706710
js_args: Vec<(String, &mut EntryDependency, Option<JsEntryOptions>)>,
707711
f: Function<'static>,
708-
) -> napi::Result<()> {
709-
let compilation = self.as_mut()?;
712+
) -> napi::Result<(), ErrorCode> {
713+
let compilation = self
714+
.as_mut()
715+
.map_err(|err| napi::Error::new(err.status.into(), err.reason.clone()))?;
710716

711717
let Some(mut compiler_reference) = COMPILER_REFERENCES.with(|ref_cell| {
712718
let references = ref_cell.borrow_mut();
713719
references.get(&compilation.compiler_id()).cloned()
714720
}) else {
715-
return Err(napi::Error::from_reason(
721+
return Err(napi::Error::new(
722+
napi::Status::GenericFailure.into(),
716723
"Unable to addEntry now. The Compiler has been garbage collected by JavaScript.",
717724
));
718725
};
719726
let Some(js_compiler) = compiler_reference.get_mut() else {
720-
return Err(napi::Error::from_reason(
727+
return Err(napi::Error::new(
728+
napi::Status::GenericFailure.into(),
721729
"Unable to addEntry now. The Compiler has been garbage collected by JavaScript.",
722730
));
723731
};
@@ -752,7 +760,8 @@ impl JsCompilation {
752760
};
753761
Ok((dependency, options))
754762
})
755-
.collect::<napi::Result<Vec<(BoxDependency, EntryOptions)>>>()?;
763+
.collect::<napi::Result<Vec<(BoxDependency, EntryOptions)>>>()
764+
.map_err(|err| napi::Error::new(err.status.into(), err.reason.clone()))?;
756765

757766
callbackify(
758767
f,
@@ -802,19 +811,23 @@ impl JsCompilation {
802811
reference: Reference<JsCompilation>,
803812
js_args: Vec<(String, &mut EntryDependency, Option<JsEntryOptions>)>,
804813
f: Function<'static>,
805-
) -> napi::Result<()> {
806-
let compilation = self.as_mut()?;
814+
) -> napi::Result<(), ErrorCode> {
815+
let compilation = self
816+
.as_mut()
817+
.map_err(|err| napi::Error::new(err.status.into(), err.reason.clone()))?;
807818

808819
let Some(mut compiler_reference) = COMPILER_REFERENCES.with(|ref_cell| {
809820
let references = ref_cell.borrow_mut();
810821
references.get(&compilation.compiler_id()).cloned()
811822
}) else {
812-
return Err(napi::Error::from_reason(
823+
return Err(napi::Error::new(
824+
napi::Status::GenericFailure.into(),
813825
"Unable to addInclude now. The Compiler has been garbage collected by JavaScript.",
814826
));
815827
};
816828
let Some(js_compiler) = compiler_reference.get_mut() else {
817-
return Err(napi::Error::from_reason(
829+
return Err(napi::Error::new(
830+
napi::Status::GenericFailure.into(),
818831
"Unable to addInclude now. The Compiler has been garbage collected by JavaScript.",
819832
));
820833
};
@@ -849,7 +862,8 @@ impl JsCompilation {
849862
};
850863
Ok((dependency, options))
851864
})
852-
.collect::<napi::Result<Vec<(BoxDependency, EntryOptions)>>>()?;
865+
.collect::<napi::Result<Vec<(BoxDependency, EntryOptions)>>>()
866+
.map_err(|err| napi::Error::new(err.status.into(), err.reason.clone()))?;
853867

854868
callbackify(
855869
f,

crates/node_binding/src/error.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
use napi_derive::napi;
22
use rspack_error::{miette, Diagnostic, Result, RspackSeverity};
33

4+
pub enum ErrorCode {
5+
Napi(napi::Status),
6+
Custom(String),
7+
}
8+
9+
impl From<napi::Status> for ErrorCode {
10+
fn from(value: napi::Status) -> Self {
11+
Self::Napi(value)
12+
}
13+
}
14+
15+
impl AsRef<str> for ErrorCode {
16+
fn as_ref(&self) -> &str {
17+
match self {
18+
ErrorCode::Napi(status) => status.as_ref(),
19+
ErrorCode::Custom(code) => code.as_str(),
20+
}
21+
}
22+
}
23+
424
#[napi(object)]
525
pub struct JsRspackDiagnostic {
626
pub severity: JsRspackSeverity,
@@ -83,9 +103,9 @@ impl JsRspackError {
83103
}
84104
}
85105

86-
pub trait RspackResultToNapiResultExt<T, E: ToString> {
87-
fn to_napi_result(self) -> napi::Result<T>;
88-
fn to_napi_result_with_message(self, f: impl FnOnce(E) -> String) -> napi::Result<T>;
106+
pub trait RspackResultToNapiResultExt<T, E: ToString, S: AsRef<str> = napi::Status> {
107+
fn to_napi_result(self) -> napi::Result<T, S>;
108+
fn to_napi_result_with_message(self, f: impl FnOnce(E) -> String) -> napi::Result<T, S>;
89109
}
90110

91111
impl<T, E: ToString> RspackResultToNapiResultExt<T, E> for Result<T, E> {
@@ -96,3 +116,12 @@ impl<T, E: ToString> RspackResultToNapiResultExt<T, E> for Result<T, E> {
96116
self.map_err(|e| napi::Error::from_reason(f(e)))
97117
}
98118
}
119+
120+
impl<T, E: ToString> RspackResultToNapiResultExt<T, E, ErrorCode> for Result<T, E> {
121+
fn to_napi_result(self) -> napi::Result<T, ErrorCode> {
122+
self.map_err(|e| napi::Error::new(napi::Status::GenericFailure.into(), e.to_string()))
123+
}
124+
fn to_napi_result_with_message(self, f: impl FnOnce(E) -> String) -> napi::Result<T, ErrorCode> {
125+
self.map_err(|e| napi::Error::new(napi::Status::GenericFailure.into(), f(e)))
126+
}
127+
}

0 commit comments

Comments
 (0)