Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ rustflags = [
"-C", "link-args=/DEFAULTLIB:ucrt.lib"
]


[target.wasm32-wasip1-threads]
rustflags = ["--cfg", "tokio_unstable"]

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]
[target.i686-pc-windows-msvc]
Expand Down
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ smol_str = { version = "0.3.0" }
stacker = { version = "0.1.17" }
sugar_path = { version = "1.2.0", features = ["cached_current_dir"] }
syn = { version = "2.0.95" }
tokio = { version = "1.42.0", features = ["rt-multi-thread", "rt"] }
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread"] }
# don't include debug & trace in release binary to save release binary size
tracing = { version = "0.1.41", features = ["max_level_trace", "release_max_level_info"] }
tracing-subscriber = { version = "0.3.19" }
Expand Down Expand Up @@ -387,6 +387,14 @@ lto = "thin"
split-debuginfo = "off"
strip = false

[profile.release-wasi]
codegen-units = 16
debug = 'full'
inherits = "release"
lto = "thin"
opt-level = "s"
strip = "none"


# the following lints rules are from https://github.com/biomejs/biome/blob/4bd3d6f09642952ee14445ed56af81a73796cea1/Cargo.toml#L7C1-L75C1
[workspace.lints.rust]
Expand Down
4 changes: 2 additions & 2 deletions crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ rustc-hash = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
swc_core = { workspace = true, default-features = false, features = ["ecma_transforms_react"] }
tokio = { workspace = true, features = ["rt", "macros", "test-util", "parking_lot", "tracing"] }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "tracing"] }

rayon = { workspace = true }
rspack_loader_lightningcss = { workspace = true }
Expand Down Expand Up @@ -103,7 +103,7 @@ rspack_plugin_web_worker_template = { workspace = true }
rspack_plugin_worker = { workspace = true }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
tokio = { workspace = true, features = ["rt-multi-thread"] }
tokio = { workspace = true, features = ["parking_lot"] }

rspack_tracing = { workspace = true, features = ["otel"] }

Expand Down
16 changes: 16 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2510,6 +2510,22 @@ export interface RegisterJsTaps {
registerRsdoctorPluginAssetsTaps: (stages: Array<number>) => Array<{ function: ((arg: JsRsdoctorAssetPatch) => Promise<boolean | undefined>); stage: number; }>
}

/**
* Shutdown the tokio runtime manually.
*
* This is required for the wasm target with `tokio_unstable` cfg.
* In the wasm runtime, the `park` threads will hang there until the tokio::Runtime is shutdown.
*/
export declare function shutdownAsyncRuntime(): void

/**
* Start the async runtime manually.
*
* This is required when the async runtime is shutdown manually.
* Usually it's used in test.
*/
export declare function startAsyncRuntime(): void

export interface ThreadsafeNodeFS {
writeFile: (name: string, content: Buffer) => Promise<void>
removeFile: (name: string) => Promise<void>
Expand Down
1 change: 1 addition & 0 deletions crates/node_binding/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fn main() {
println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");
napi_build::setup();
}
3 changes: 2 additions & 1 deletion crates/node_binding/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"build:ci": "node scripts/build.js --profile ci",
"build:profiling": "node scripts/build.js --profile profiling",
"build:release": "node scripts/build.js --profile release",
"build:wasm": "DISABLE_PLUGIN=1 RUST_TARGET=wasm32-wasip1-threads node scripts/build.js",
"build:dev:wasm": "DISABLE_PLUGIN=1 RUST_TARGET=wasm32-wasip1-threads node scripts/build.js",
"build:release:wasm": "DISABLE_PLUGIN=1 RUST_TARGET=wasm32-wasip1-threads node scripts/build.js --profile release-wasi",
"move-binding": "node scripts/move-binding",
"test": "tsc -p tsconfig.type-test.json"
},
Expand Down
2 changes: 2 additions & 0 deletions crates/node_binding/rspack.wasi-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,5 @@ export const JsRspackSeverity = __napiModule.exports.JsRspackSeverity
export const RawRuleSetConditionType = __napiModule.exports.RawRuleSetConditionType
export const registerGlobalTrace = __napiModule.exports.registerGlobalTrace
export const RegisterJsTapKind = __napiModule.exports.RegisterJsTapKind
export const shutdownAsyncRuntime = __napiModule.exports.shutdownAsyncRuntime
export const startAsyncRuntime = __napiModule.exports.startAsyncRuntime
2 changes: 2 additions & 0 deletions crates/node_binding/rspack.wasi.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ module.exports.JsRspackSeverity = __napiModule.exports.JsRspackSeverity
module.exports.RawRuleSetConditionType = __napiModule.exports.RawRuleSetConditionType
module.exports.registerGlobalTrace = __napiModule.exports.registerGlobalTrace
module.exports.RegisterJsTapKind = __napiModule.exports.RegisterJsTapKind
module.exports.shutdownAsyncRuntime = __napiModule.exports.shutdownAsyncRuntime
module.exports.startAsyncRuntime = __napiModule.exports.startAsyncRuntime
20 changes: 20 additions & 0 deletions crates/node_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,23 @@ pub fn cleanup_global_trace() {
*state = TraceState::Off;
});
}

#[napi]
/// Shutdown the tokio runtime manually.
///
/// This is required for the wasm target with `tokio_unstable` cfg.
/// In the wasm runtime, the `park` threads will hang there until the tokio::Runtime is shutdown.
pub fn shutdown_async_runtime() {
#[cfg(all(target_family = "wasm", tokio_unstable))]
napi::bindgen_prelude::shutdown_async_runtime();
}

#[napi]
/// Start the async runtime manually.
///
/// This is required when the async runtime is shutdown manually.
/// Usually it's used in test.
pub fn start_async_runtime() {
#[cfg(all(target_family = "wasm", tokio_unstable))]
napi::bindgen_prelude::start_async_runtime();
}
27 changes: 25 additions & 2 deletions crates/rspack_fs/src/native_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,31 @@ impl ReadableFileSystem for NativeFileSystem {
}

fn canonicalize(&self, path: &Utf8Path) -> Result<Utf8PathBuf> {
let path = dunce::canonicalize(path)?;
Ok(path.assert_utf8())
// Comes from rspack_resolver
use std::path::Component;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this wasi only and should be put into cfg_if!?

Copy link
Contributor Author

@CPunisher CPunisher Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let mut path_buf = path.to_path_buf();
loop {
let link = fs::read_link(&path_buf)?;
path_buf.pop();
for component in link.components() {
match component {
Component::ParentDir => {
path_buf.pop();
}
Component::Normal(seg) => {
path_buf.push(seg.to_string_lossy().trim_end_matches('\0'));
}
Component::RootDir => {
path_buf = Utf8PathBuf::from("/");
}
Component::CurDir | Component::Prefix(_) => {}
}
}
if !fs::symlink_metadata(&path_buf)?.is_symlink() {
break;
}
}
Ok(path_buf)
}

async fn async_read(&self, file: &Utf8Path) -> Result<Vec<u8>> {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_regex/src/napi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl FromNapiValue for RspackRegex {
let global = env.get_global()?;
let object_prototype_to_string = global
.get_named_property_unchecked::<JsObject>("Object")?
.get_named_property::<JsObject>("prototype")?
.get_named_property::<Function>("toString")?;
.get_named_property_unchecked::<JsObject>("prototype")?
.get_named_property_unchecked::<Function>("toString")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this changed?

Copy link
Contributor Author

@CPunisher CPunisher Apr 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but from my experiments:

  1. .get_named_property_unchecked::<JsObject>("prototype") returns an External in wasm rather than JsObject.
  2. But _unchecked in .get_named_property_unchecked::<Function>("toString")? is actually needless.


let js_string = object_prototype_to_string
.apply(&js_object, env.get_undefined()?.into_unknown())?
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
"check-dependency-version": "pnpx check-dependency-version-consistency@5 . --ignore-dep chalk --ignore-package webpack-test --ignore-package webpack-examples",
"build:js": "pnpm --filter \"@rspack/core\" build:force && pnpm --parallel --filter \"@rspack/*\" --filter \"create-rspack\" --filter \"!@rspack/core\" build",
"build:cli:dev": "npm run build:binding:dev && npm run build:js",
"build:cli:dev:wasm": "pnpm --filter @rspack/binding build:dev:wasm && npm run build:js",
"build:cli:release": "npm run build:binding:release && npm run build:js",
"build:cli:release:all": "pnpm --filter @rspack/binding build:release:all && npm run build:js",
"build:cli:release:arm64": "pnpm --filter @rspack/binding build:release:arm64 && npm run build:js",
"build:cli:release:x64": "pnpm --filter @rspack/binding build:release:x64 && npm run build:js",
"build:cli:release:linux": "pnpm --filter @rspack/binding build:release:linux && npm run build:js",
"build:cli:release:win": "pnpm --filter @rspack/binding build:release:win && npm run build:js",
"build:cli:release:wasm": "pnpm --filter @rspack/binding build:release:wasm && npm run build:js",
"test:js": "pnpm -r run test",
"format:rs": "cargo fmt --all",
"format:js": "pnpm run format-ci:js --write",
Expand Down
Loading