Skip to content

Commit

Permalink
Add core rust types (#9714)
Browse files Browse the repository at this point in the history
* Add rust types

* Remove bitflags

* Use ahash for better platform support

* Reexport core types
  • Loading branch information
MonicaOlejniczak committed May 14, 2024
1 parent ef63bff commit a4a4033
Show file tree
Hide file tree
Showing 20 changed files with 1,479 additions and 7 deletions.
105 changes: 102 additions & 3 deletions Cargo.lock

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

11 changes: 8 additions & 3 deletions crates/parcel_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ parcel_filesystem = { path = "../parcel_filesystem" }
parcel_napi_helpers = { path = "../parcel_napi_helpers" }
parcel-resolver = { path = "../../packages/utils/node-resolver-rs" }

ahash = "0.8.11"
anyhow = "1.0.82"
browserslist-rs = "0.15.0"
glob = "0.3.1"
mockall = "0.12.1"
napi = "2.16.4"
napi-derive = { version = "2.16.3" }
serde = "1.0.200"
serde_json = "1.0.116"
nodejs-semver = "4.0.0"
serde = { version = "1.0.200", features = ["derive"] }
serde_json = { version = "1.0.116", features = ["preserve_order"] }
serde_repr = "0.1.19"
serde-value = "0.7.0"
toml = "0.8.12"
xxhash-rust = { version = "0.8.2", features = ["xxh3"] }
xxhash-rust = { version = "0.8.2", features = ["xxh3"] }
3 changes: 3 additions & 0 deletions crates/parcel_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Core re-implementation in Rust

pub mod hash;
pub mod types;

/// New-type for paths relative to a project-root
pub mod project_path;

/// Request types and run functions
pub mod requests;
29 changes: 29 additions & 0 deletions crates/parcel_core/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
mod asset;
pub use self::asset::*;

mod bundle;
pub use self::bundle::*;

mod dependency;
pub use self::dependency::*;

mod environment;
pub use self::environment::*;

mod file_type;
pub use self::file_type::*;

mod json;
pub use self::json::*;

mod parcel_options;
pub use self::parcel_options::*;

mod source;
pub use self::source::*;

mod symbol;
pub use self::symbol::*;

mod target;
pub use self::target::*;
102 changes: 102 additions & 0 deletions crates/parcel_core/src/types/asset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use std::hash::Hash;
use std::hash::Hasher;
use std::num::NonZeroU32;
use std::path::PathBuf;

use ahash::AHasher;
use serde::Deserialize;
use serde::Serialize;

use super::bundle::BundleBehavior;
use super::environment::Environment;
use super::file_type::FileType;
use super::json::JSONObject;
use super::symbol::Symbol;

#[derive(PartialEq, Hash, Clone, Copy, Debug)]
pub struct AssetId(pub NonZeroU32);

/// An asset is a file or part of a file that may represent any data type including source code, binary data, etc.
///
/// Note that assets may exist in the file system or virtually.
///
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Asset {
/// The file type of the asset, which may change during transformation
#[serde(rename = "type")]
pub asset_type: FileType,

/// Controls which bundle the asset is placed into
pub bundle_behavior: BundleBehavior,

/// The environment of the asset
pub env: Environment,

/// The file path to the asset
pub file_path: PathBuf,

/// Indicates if the asset is used as a bundle entry
///
/// This controls whether a bundle can be split into multiple, or whether all of the
/// dependencies must be placed in a single bundle.
///
pub is_bundle_splittable: bool,

/// Whether this asset is part of the project, and not an external dependency
///
/// This indicates that transformation using the project configuration should be applied.
///
pub is_source: bool,

/// Plugin specific metadata for the asset
pub meta: JSONObject,

/// The pipeline defined in .parcelrc that the asset should be processed with
pub pipeline: Option<String>,

/// The transformer options for the asset from the dependency query string
pub query: Option<String>,

/// Whether this asset can be omitted if none of its exports are being used
///
/// This is initially set by the resolver, but can be overridden by transformers.
///
pub side_effects: bool,

/// Statistics about the asset
pub stats: AssetStats,

/// The symbols that the asset exports
pub symbols: Vec<Symbol>,

/// A unique key that identifies an asset
///
/// When a transformer returns multiple assets, it can give them unique keys to identify them.
/// This can be used to find assets during packaging, or to create dependencies between multiple
/// assets returned by a transformer by using the unique key as the dependency specifier.
///
pub unique_key: Option<String>,
}

impl Asset {
pub fn id(&self) -> u64 {
let mut hasher = AHasher::default();

self.asset_type.hash(&mut hasher);
self.env.hash(&mut hasher);
self.file_path.hash(&mut hasher);
self.pipeline.hash(&mut hasher);
self.query.hash(&mut hasher);
self.unique_key.hash(&mut hasher);

hasher.finish()
}
}

/// Statistics that pertain to an asset
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct AssetStats {
pub size: u32,
pub time: u32,
}

0 comments on commit a4a4033

Please sign in to comment.