Skip to content

Commit

Permalink
Merge all native Rust modules into one package (#9146)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Sep 11, 2023
1 parent c98a4a0 commit 2792324
Show file tree
Hide file tree
Showing 87 changed files with 285 additions and 711 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ linker = "aarch64-linux-gnu-gcc"

[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"

[target.wasm32-unknown-unknown]
rustflags = ["-C", "link-arg=--export-table"]
6 changes: 1 addition & 5 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ dist
/test/dist
/test/input
*.min.js
packages/optimizers/image/native.js
packages/transformers/js/native.js
packages/utils/fs-search/index.js
packages/utils/hash/index.js
packages/utils/node-resolver-core/index.js
packages/core/rust/index.js

coverage
node_modules
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/nightly-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
name: bindings-${{ matrix.os }}
path: packages/*/*/*.node
- name: Smoke test
run: node -e "require('@parcel/fs-search')"
run: node -e "require('@parcel/rust')"

build-linux-gnu-x64:
name: linux-gnu-x64
Expand Down Expand Up @@ -63,7 +63,7 @@ jobs:
- name: debug
run: ls -l packages/*/*/*.node
- name: Smoke test
run: node -e 'require("@parcel/fs-search")'
run: node -e 'require("@parcel/rust")'

build-linux-gnu-arm:
strategy:
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
with:
image: ghcr.io/devongovett/multiarch-node:node14-${{ matrix.arch }}-focal
options: -v ${{github.workspace}}:/work
run: cd /work && node -e "require('@parcel/fs-search')"
run: cd /work && node -e "require('@parcel/rust')"

build-linux-musl:
strategy:
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
run: ls -l packages/*/*/*.node
- name: Smoke test
if: ${{ matrix.target == 'x86_64-unknown-linux-musl' }}
run: node -e 'require("@parcel/fs-search")'
run: node -e 'require("@parcel/rust")'

build-apple-silicon:
name: aarch64-apple-darwin
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/tag-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
name: bindings-${{ matrix.os }}
path: packages/*/*/*.node
- name: Smoke test
run: node -e "require('@parcel/fs-search')"
run: node -e "require('@parcel/rust')"

build-linux-gnu-x64:
name: linux-gnu-x64
Expand Down Expand Up @@ -63,7 +63,7 @@ jobs:
- name: debug
run: ls -l packages/*/*/*.node
- name: Smoke test
run: node -e 'require("@parcel/fs-search")'
run: node -e 'require("@parcel/rust")'

build-linux-gnu-arm:
strategy:
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
with:
image: ghcr.io/devongovett/multiarch-node:node14-${{ matrix.arch }}-focal
options: -v ${{github.workspace}}:/work
run: cd /work && node -e "require('@parcel/fs-search')"
run: cd /work && node -e "require('@parcel/rust')"

build-linux-musl:
strategy:
Expand Down Expand Up @@ -163,7 +163,7 @@ jobs:
run: ls -l packages/*/*/*.node
- name: Smoke test
if: ${{ matrix.target == 'x86_64-unknown-linux-musl' }}
run: node -e 'require("@parcel/fs-search")'
run: node -e 'require("@parcel/rust")'

build-apple-silicon:
name: aarch64-apple-darwin
Expand Down
85 changes: 11 additions & 74 deletions Cargo.lock

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

9 changes: 3 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ resolver = "2"
members = [
"crates/*",
"packages/transformers/js/core",
"packages/transformers/js/napi",
"packages/transformers/js/wasm",
"packages/utils/fs-search",
"packages/utils/hash",
"packages/optimizers/image",
"packages/utils/node-resolver-rs",
"packages/utils/node-resolver-core",
"packages/utils/dev-dep-resolver"
]

[patch.crates-io]
browserslist-rs = { git = "https://github.com/devongovett/browserslist-rs.git", branch = "wasm" }
2 changes: 2 additions & 0 deletions crates/node-bindings/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.wasm32-unknown-unknown]
rustflags = ["-C", "link-arg=--export-table"]
34 changes: 34 additions & 0 deletions crates/node-bindings/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
authors = ["Devon Govett <[email protected]>"]
name = "parcel-node-bindings"
version = "0.1.0"
edition = "2021"

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

[dependencies]
napi = {version = "2.12.6", features = ["serde-json"]}
napi-derive = "2.12.5"
parcel-js-swc-core = { path = "../../packages/transformers/js/core" }
parcel-resolver = { path = "../../packages/utils/node-resolver-rs" }
dashmap = "5.4.0"
xxhash-rust = { version = "0.8.2", features = ["xxh3"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
parcel-dev-dep-resolver = { path = "../../packages/utils/dev-dep-resolver" }
oxipng = "8.0.0"
mozjpeg-sys = "1.0.0"
libc = "0.2"

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", features = ["custom"], default-features = false }

[target.'cfg(target_os = "macos")'.dependencies]
jemallocator = { version = "0.3.2", features = ["disable_initial_exec_tls"] }

[target.'cfg(windows)'.dependencies]
mimalloc = { version = "0.1.25", default-features = false }

[build-dependencies]
napi-build = "2"
7 changes: 7 additions & 0 deletions crates/node-bindings/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#[cfg(not(target_arch = "wasm32"))]
extern crate napi_build;

fn main() {
#[cfg(not(target_arch = "wasm32"))]
napi_build::setup();
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ptr;
use std::slice;

#[napi]
pub fn optimize(kind: String, buf: Buffer, env: Env) -> Result<JsBuffer> {
pub fn optimize_image(kind: String, buf: Buffer, env: Env) -> Result<JsBuffer> {
let slice = buf.as_ref();

match kind.as_ref() {
Expand Down
53 changes: 53 additions & 0 deletions crates/node-bindings/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#[cfg(target_arch = "wasm32")]
use std::alloc::{alloc, Layout};

#[cfg(target_os = "macos")]
#[global_allocator]
static GLOBAL: jemallocator::Jemalloc = jemallocator::Jemalloc;

#[cfg(windows)]
#[global_allocator]
static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

#[cfg(not(target_arch = "wasm32"))]
mod fs_search;
mod hash;
#[cfg(not(target_arch = "wasm32"))]
mod image;
mod resolver;
mod transformer;

#[cfg(target_arch = "wasm32")]
#[no_mangle]
pub extern "C" fn napi_wasm_malloc(size: usize) -> *mut u8 {
let align = std::mem::align_of::<usize>();
if let Ok(layout) = Layout::from_size_align(size, align) {
unsafe {
if layout.size() > 0 {
let ptr = alloc(layout);
if !ptr.is_null() {
return ptr;
}
} else {
return align as *mut u8;
}
}
}

std::process::abort();
}

#[cfg(target_arch = "wasm32")]
mod wasm {
use core::num::NonZeroU32;
use getrandom::register_custom_getrandom;
use getrandom::Error;

// TODO
pub fn always_fail(buf: &mut [u8]) -> Result<(), Error> {
let code = NonZeroU32::new(Error::CUSTOM_START + 42).unwrap();
Err(Error::from(code))
}

register_custom_getrandom!(always_fail);
}
Loading

0 comments on commit 2792324

Please sign in to comment.