Skip to content
Open
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
58 changes: 58 additions & 0 deletions .changeset/compress-native-bindings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
"@swc/core": major
"@swc/core-darwin-arm64": major
"@swc/core-darwin-x64": major
"@swc/core-linux-arm-gnueabihf": major
"@swc/core-linux-arm64-gnu": major
"@swc/core-linux-arm64-musl": major
"@swc/core-linux-ppc64-gnu": major
"@swc/core-linux-s390x-gnu": major
"@swc/core-linux-x64-gnu": major
"@swc/core-linux-x64-musl": major
"@swc/core-win32-arm64-msvc": major
"@swc/core-win32-ia32-msvc": major
"@swc/core-win32-x64-msvc": major
"@swc/html": major
"@swc/html-darwin-arm64": major
"@swc/html-darwin-x64": major
"@swc/html-linux-arm-gnueabihf": major
"@swc/html-linux-arm64-gnu": major
"@swc/html-linux-arm64-musl": major
"@swc/html-linux-ppc64-gnu": major
"@swc/html-linux-s390x-gnu": major
"@swc/html-linux-x64-gnu": major
"@swc/html-linux-x64-musl": major
"@swc/html-win32-arm64-msvc": major
"@swc/html-win32-ia32-msvc": major
"@swc/html-win32-x64-msvc": major
"@swc/minifier": major
"@swc/minifier-darwin-arm64": major
"@swc/minifier-darwin-x64": major
"@swc/minifier-linux-arm-gnueabihf": major
"@swc/minifier-linux-arm64-gnu": major
"@swc/minifier-linux-arm64-musl": major
"@swc/minifier-linux-ppc64-gnu": major
"@swc/minifier-linux-s390x-gnu": major
"@swc/minifier-linux-x64-gnu": major
"@swc/minifier-linux-x64-musl": major
"@swc/minifier-win32-arm64-msvc": major
"@swc/minifier-win32-ia32-msvc": major
"@swc/minifier-win32-x64-msvc": major
"@swc/react-compiler": major
"@swc/react-compiler-darwin-arm64": major
"@swc/react-compiler-darwin-x64": major
"@swc/react-compiler-linux-arm-gnueabihf": major
"@swc/react-compiler-linux-arm64-gnu": major
"@swc/react-compiler-linux-arm64-musl": major
"@swc/react-compiler-linux-ppc64-gnu": major
"@swc/react-compiler-linux-s390x-gnu": major
"@swc/react-compiler-linux-x64-gnu": major
"@swc/react-compiler-linux-x64-musl": major
"@swc/react-compiler-win32-arm64-msvc": major
"@swc/react-compiler-win32-ia32-msvc": major
"@swc/react-compiler-win32-x64-msvc": major
---

Compress native `.node` packages into a small Rust loader with a zstd-packed addon payload. The first load verifies the payload SHA-512, materializes the raw addon using OS transparent compression when available, and falls back to a per-user cache otherwise, reducing native package download and install size.

This also raises the Node.js engine requirement for all native SWC npm packages to Node 20 or newer.
4 changes: 2 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ jobs:
- uses: ./.github/actions/setup-node
id: setup-node
with:
node-version: 18
node-version: 20
install-dependencies: "false"

- name: Set platform name
Expand Down Expand Up @@ -465,7 +465,7 @@ jobs:
- uses: ./.github/actions/setup-node
id: setup-node
with:
node-version: 18
node-version: 20
install-dependencies: "false"

- name: Prepare
Expand Down
103 changes: 98 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"xtask",
"bindings/*",
"crates/*",
"tools/swc-compressed-binding-tool",
"tools/generate-code",
"tools/swc-releaser",
]
Expand Down Expand Up @@ -48,6 +49,7 @@ copyless = "0.1.5"
crc = "2.1.0"
criterion = "0.5.1"
dashmap = "6.1.0"
decmpfs = { version = "0.1.0", default-features = false }
dialoguer = "0.10.2"
difference = "2"
dragonbox_ecma = "0.1.0"
Expand All @@ -67,13 +69,15 @@ jsonc-parser = "0.26.2"
kstring = "2.0.0"
lazy_static = "1.4.0"
lexical = "6.1.0"
libloading = "0.8.1"
lightningcss = "1.0.0-alpha.68"
lru = "0.16.1"
memchr = "2.6.1"
miette = "7.6.0"
napi = { version = "3", default-features = false }
napi-build = "2"
napi-derive = { version = "3", default-features = false }
napi-sys = "3"
new_debug_unreachable = "1.0.6"
nom = "7.1.3"
ntest = "0.7.2"
Expand Down Expand Up @@ -138,6 +142,7 @@ wasm-bindgen-futures = "0.4.50"
wasmer = { version = "6.1.0-rc.3", default-features = false }
wasmer-wasix = { version = "0.601.0-rc.3", default-features = false }
wasmtime = { version = "38", default-features = false }
zstd = "0.13.3"


cbor4ii = "1.2"
Expand Down
24 changes: 24 additions & 0 deletions bindings/binding_compressed_node_loader/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[package]
authors = ["강동윤 <kdy1997.dev@gmail.com>"]
build = "build.rs"
edition = "2021"
exclude = ["artifacts.json", "index.node"]
license = "Apache-2.0"
name = "binding_compressed_node_loader"
publish = false
version = "0.1.0"

[lib]
bench = false
crate-type = ["cdylib"]

[build-dependencies]
napi-build = { workspace = true }

[dependencies]
decmpfs = { workspace = true, features = ["addon"] }
hex = { workspace = true }
libloading = { workspace = true }
napi-sys = { workspace = true, features = ["napi9"] }
sha2 = { workspace = true }
zstd = { workspace = true }
53 changes: 53 additions & 0 deletions bindings/binding_compressed_node_loader/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::{
env, fs, io,
path::{Path, PathBuf},
};

fn main() {
napi_build::setup();

println!("cargo:rerun-if-env-changed=SWC_COMPRESSED_BINDING_PAYLOAD");

let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR must be set"));
let generated = out_dir.join("pressed_data.rs");

match env::var_os("SWC_COMPRESSED_BINDING_PAYLOAD") {
Some(payload) => {
let payload = PathBuf::from(payload);
println!("cargo:rerun-if-changed={}", payload.display());
let data_path = out_dir.join("pressed-data.bin");
copy_payload(&payload, &data_path).unwrap_or_else(|err| {
panic!(
"failed to copy compressed binding payload from {}: {err}",
payload.display()
)
});
fs::write(
generated,
r#"
#[used]
#[cfg_attr(target_os = "macos", link_section = "__SMOL,__PRESSED_DATA")]
#[cfg_attr(all(unix, not(target_os = "macos")), link_section = ".PRESSED_DATA")]
#[cfg_attr(windows, link_section = ".PRESSED")]
pub static PRESSED_DATA: [u8; include_bytes!(concat!(env!("OUT_DIR"), "/pressed-data.bin")).len()] =
*include_bytes!(concat!(env!("OUT_DIR"), "/pressed-data.bin"));
"#,
)
.expect("failed to write generated pressed data module");
}
None => {
fs::write(
generated,
r#"
#[used]
pub static PRESSED_DATA: [u8; 0] = [];
"#,
)
.expect("failed to write empty pressed data module");
}
}
}

fn copy_payload(from: &Path, to: &Path) -> io::Result<()> {
fs::copy(from, to).map(|_| ())
}
Loading
Loading