Skip to content

Commit

Permalink
Use a parcel_filesystem crate (#9682)
Browse files Browse the repository at this point in the history
* Use a parcel_filesystem crate

* Move the `parcel_resolver::fs` module into its own crate
* Move our in-memory and JSDelegate implementations into that crate
* Move the napi helper functions from node-bindings into another crate
* Change all usages to use these functions

* Fix windows dev-dependencies
  • Loading branch information
yamadapc committed May 1, 2024
1 parent f5f0bb4 commit 3227fc4
Show file tree
Hide file tree
Showing 20 changed files with 392 additions and 227 deletions.
62 changes: 42 additions & 20 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/macros/Cargo.toml
Expand Up @@ -19,5 +19,5 @@ swc_core = { version = "0.89.6", features = [
] }
serde = "1.0.123"
napi-derive = { version = "2.12.5", optional = true }
napi = { version = "2.12.6", features = ["serde-json", "napi4", "napi5"], optional = true }
napi = { version = "2.16.4", features = ["serde-json", "napi4", "napi5"], optional = true }
crossbeam-channel = { version = "0.5.6", optional = true }
9 changes: 5 additions & 4 deletions crates/node-bindings/Cargo.toml
Expand Up @@ -14,9 +14,10 @@ rustls = ["sentry/rustls"]
openssl = ["sentry/native-tls"]

[dependencies]
napi-derive = "2.12.5"
napi-derive = "2.16.3"
parcel-js-swc-core = { path = "../../packages/transformers/js/core" }
parcel-resolver = { path = "../../packages/utils/node-resolver-rs" }
parcel_filesystem = { path = "../parcel_filesystem" }
dashmap = "5.4.0"
xxhash-rust = { version = "0.8.2", features = ["xxh3"] }
log = "0.4.21"
Expand All @@ -32,7 +33,7 @@ anyhow = "1.0.82"
mockall = "0.12.1"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
napi = { version = "2.12.6", features = ["serde-json", "napi4", "napi5"] }
napi = { version = "2.16.4", features = ["serde-json", "napi4", "napi5"] }
parcel-dev-dep-resolver = { path = "../../packages/utils/dev-dep-resolver" }
parcel-macros = { path = "../macros", features = ["napi"] }
oxipng = "8.0.0"
Expand All @@ -43,7 +44,7 @@ crossbeam-channel = "0.5.6"
indexmap = "1.9.2"

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

[target.'cfg(target_os = "macos")'.dependencies]
Expand All @@ -55,4 +56,4 @@ mimalloc = { version = "0.1.25", default-features = false }
[dev-dependencies]

[build-dependencies]
napi-build = "2"
napi-build = "2.1.3"
6 changes: 0 additions & 6 deletions crates/node-bindings/src/core/filesystem/mod.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/node-bindings/src/core/mod.rs
@@ -1,5 +1,4 @@
//! Core re-implementation in Rust

mod filesystem;
/// Request types and run functions
mod requests;
4 changes: 2 additions & 2 deletions crates/node-bindings/src/core/requests/config_request/mod.rs
Expand Up @@ -155,10 +155,10 @@ struct RequestOptions {}

#[cfg(test)]
mod test {
use parcel_resolver::OsFileSystem;
use parcel_filesystem::in_memory_file_system::InMemoryFileSystem;
use parcel_filesystem::os_file_system::OsFileSystem;

use super::*;
use crate::core::filesystem::in_memory_file_system::InMemoryFileSystem;
use crate::core::requests::config_request::run_config_request;
use crate::core::requests::request_api::MockRequestApi;

Expand Down
2 changes: 1 addition & 1 deletion crates/node-bindings/src/core/requests/mod.rs
Expand Up @@ -9,9 +9,9 @@ use napi::JsUnknown;
use napi::NapiRaw;
use napi_derive::napi;

use crate::core::filesystem::js_delegate_file_system::JSDelegateFileSystem;
use crate::core::requests::config_request::ConfigRequest;
use crate::core::requests::request_api::js_request_api::JSRequestApi;
use parcel_filesystem::js_delegate_file_system::JSDelegateFileSystem;

mod config_request;
mod request_api;
Expand Down
7 changes: 6 additions & 1 deletion crates/node-bindings/src/image.rs
Expand Up @@ -32,7 +32,12 @@ pub fn optimize_image(kind: String, buf: Buffer, env: Env) -> Result<JsBuffer> {
match optimize_jpeg(slice) {
Ok(res) => Ok(
env
.create_buffer_with_borrowed_data(res.as_ptr(), res.len(), res.as_mut_ptr(), finalize)?
.create_buffer_with_borrowed_data(
res.as_mut_ptr(),
res.len(),
res.as_mut_ptr(),
finalize,
)?
.into_raw(),
),
Err(err) => {
Expand Down
17 changes: 17 additions & 0 deletions crates/parcel_filesystem/Cargo.toml
@@ -0,0 +1,17 @@
[package]
name = "parcel_filesystem"
version = "0.1.0"
edition = "2021"
description = "FileSystem wrapper trait for use in Parcel codebase."

[dependencies]
parcel_napi_helpers = { path = "../parcel_napi_helpers" }
napi = "2.16.4"
dashmap = "5.5.3"
anyhow = "1.0.82"

[dev-dependencies]
assert_fs = "1.1.1"

[target.'cfg(windows)'.dev-dependencies]
is_elevated = "0.1.2"
Expand Up @@ -3,7 +3,7 @@ use std::path::Component;
use std::path::Path;
use std::path::PathBuf;

use parcel_resolver::FileSystem;
use crate::FileSystem;

/// In memory implementation of a file-system entry
enum InMemoryFileSystemEntry {
Expand Down Expand Up @@ -49,11 +49,11 @@ impl Default for InMemoryFileSystem {
}

impl FileSystem for InMemoryFileSystem {
fn canonicalize<P: AsRef<Path>>(
&self,
path: P,
_cache: &dashmap::DashMap<PathBuf, Option<PathBuf>>,
) -> std::io::Result<PathBuf> {
fn cwd(&self) -> std::io::Result<PathBuf> {
Ok(self.current_working_directory.clone())
}

fn canonicalize_base<P: AsRef<Path>>(&self, path: P) -> std::io::Result<PathBuf> {
let path = path.as_ref();

let mut result = if path.is_absolute() {
Expand Down
Expand Up @@ -2,13 +2,13 @@ use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;

use dashmap::DashMap;
use napi::bindgen_prelude::FromNapiValue;
use napi::Env;
use napi::JsObject;
use parcel_resolver::FileSystem;
use napi::JsUnknown;
use parcel_napi_helpers::call_method;

use crate::core::requests::call_method;
use crate::FileSystem;

/// An implementation of `FileSystem` that delegates calls to a `JsObject`.
///
Expand All @@ -25,6 +25,12 @@ impl JSDelegateFileSystem {
}
}

fn path_from_js(result: JsUnknown) -> napi::Result<PathBuf> {
let result_string = result.coerce_to_string()?;
let result_string = result_string.into_utf8()?.as_str()?.to_string();
Ok(PathBuf::from(result_string))
}

// Convert arbitrary errors to io errors. This is wrong; the `FileSystem` trait should use
// `anyhow::Result`
fn run_with_errors<T>(block: impl FnOnce() -> anyhow::Result<T>) -> Result<T, std::io::Error> {
Expand All @@ -33,11 +39,14 @@ fn run_with_errors<T>(block: impl FnOnce() -> anyhow::Result<T>) -> Result<T, st
}

impl FileSystem for JSDelegateFileSystem {
fn canonicalize<P: AsRef<Path>>(
&self,
path: P,
_cache: &DashMap<PathBuf, Option<PathBuf>>,
) -> std::io::Result<PathBuf> {
fn cwd(&self) -> std::io::Result<PathBuf> {
run_with_errors(|| {
let result = call_method(&self.env, &self.js_delegate, "cwd", &[])?;
Ok(path_from_js(result)?)
})
}

fn canonicalize_base<P: AsRef<Path>>(&self, path: P) -> std::io::Result<PathBuf> {
run_with_errors(|| {
let path = path.as_ref().to_str().unwrap();
let js_path = self.env.create_string(path)?;
Expand All @@ -47,9 +56,7 @@ impl FileSystem for JSDelegateFileSystem {
"canonicalize",
&[&js_path.into_unknown()],
)?;
let result_string = result.coerce_to_string()?;
let result_string = result_string.into_utf8()?.as_str()?.to_string();
Ok(PathBuf::from(result_string))
Ok(path_from_js(result)?)
})
}

Expand Down

0 comments on commit 3227fc4

Please sign in to comment.