Skip to content

Commit a4a4033

Browse files
Add core rust types (#9714)
* Add rust types * Remove bitflags * Use ahash for better platform support * Reexport core types
1 parent ef63bff commit a4a4033

20 files changed

+1479
-7
lines changed

Cargo.lock

+102-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/parcel_core/Cargo.toml

+8-3
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ parcel_filesystem = { path = "../parcel_filesystem" }
1313
parcel_napi_helpers = { path = "../parcel_napi_helpers" }
1414
parcel-resolver = { path = "../../packages/utils/node-resolver-rs" }
1515

16+
ahash = "0.8.11"
1617
anyhow = "1.0.82"
18+
browserslist-rs = "0.15.0"
1719
glob = "0.3.1"
1820
mockall = "0.12.1"
1921
napi = "2.16.4"
2022
napi-derive = { version = "2.16.3" }
21-
serde = "1.0.200"
22-
serde_json = "1.0.116"
23+
nodejs-semver = "4.0.0"
24+
serde = { version = "1.0.200", features = ["derive"] }
25+
serde_json = { version = "1.0.116", features = ["preserve_order"] }
26+
serde_repr = "0.1.19"
27+
serde-value = "0.7.0"
2328
toml = "0.8.12"
24-
xxhash-rust = { version = "0.8.2", features = ["xxh3"] }
29+
xxhash-rust = { version = "0.8.2", features = ["xxh3"] }

crates/parcel_core/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
//! Core re-implementation in Rust
22
33
pub mod hash;
4+
pub mod types;
5+
46
/// New-type for paths relative to a project-root
57
pub mod project_path;
8+
69
/// Request types and run functions
710
pub mod requests;

crates/parcel_core/src/types.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
mod asset;
2+
pub use self::asset::*;
3+
4+
mod bundle;
5+
pub use self::bundle::*;
6+
7+
mod dependency;
8+
pub use self::dependency::*;
9+
10+
mod environment;
11+
pub use self::environment::*;
12+
13+
mod file_type;
14+
pub use self::file_type::*;
15+
16+
mod json;
17+
pub use self::json::*;
18+
19+
mod parcel_options;
20+
pub use self::parcel_options::*;
21+
22+
mod source;
23+
pub use self::source::*;
24+
25+
mod symbol;
26+
pub use self::symbol::*;
27+
28+
mod target;
29+
pub use self::target::*;

crates/parcel_core/src/types/asset.rs

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use std::hash::Hash;
2+
use std::hash::Hasher;
3+
use std::num::NonZeroU32;
4+
use std::path::PathBuf;
5+
6+
use ahash::AHasher;
7+
use serde::Deserialize;
8+
use serde::Serialize;
9+
10+
use super::bundle::BundleBehavior;
11+
use super::environment::Environment;
12+
use super::file_type::FileType;
13+
use super::json::JSONObject;
14+
use super::symbol::Symbol;
15+
16+
#[derive(PartialEq, Hash, Clone, Copy, Debug)]
17+
pub struct AssetId(pub NonZeroU32);
18+
19+
/// An asset is a file or part of a file that may represent any data type including source code, binary data, etc.
20+
///
21+
/// Note that assets may exist in the file system or virtually.
22+
///
23+
#[derive(Clone, Debug, Deserialize, Serialize)]
24+
#[serde(rename_all = "camelCase")]
25+
pub struct Asset {
26+
/// The file type of the asset, which may change during transformation
27+
#[serde(rename = "type")]
28+
pub asset_type: FileType,
29+
30+
/// Controls which bundle the asset is placed into
31+
pub bundle_behavior: BundleBehavior,
32+
33+
/// The environment of the asset
34+
pub env: Environment,
35+
36+
/// The file path to the asset
37+
pub file_path: PathBuf,
38+
39+
/// Indicates if the asset is used as a bundle entry
40+
///
41+
/// This controls whether a bundle can be split into multiple, or whether all of the
42+
/// dependencies must be placed in a single bundle.
43+
///
44+
pub is_bundle_splittable: bool,
45+
46+
/// Whether this asset is part of the project, and not an external dependency
47+
///
48+
/// This indicates that transformation using the project configuration should be applied.
49+
///
50+
pub is_source: bool,
51+
52+
/// Plugin specific metadata for the asset
53+
pub meta: JSONObject,
54+
55+
/// The pipeline defined in .parcelrc that the asset should be processed with
56+
pub pipeline: Option<String>,
57+
58+
/// The transformer options for the asset from the dependency query string
59+
pub query: Option<String>,
60+
61+
/// Whether this asset can be omitted if none of its exports are being used
62+
///
63+
/// This is initially set by the resolver, but can be overridden by transformers.
64+
///
65+
pub side_effects: bool,
66+
67+
/// Statistics about the asset
68+
pub stats: AssetStats,
69+
70+
/// The symbols that the asset exports
71+
pub symbols: Vec<Symbol>,
72+
73+
/// A unique key that identifies an asset
74+
///
75+
/// When a transformer returns multiple assets, it can give them unique keys to identify them.
76+
/// This can be used to find assets during packaging, or to create dependencies between multiple
77+
/// assets returned by a transformer by using the unique key as the dependency specifier.
78+
///
79+
pub unique_key: Option<String>,
80+
}
81+
82+
impl Asset {
83+
pub fn id(&self) -> u64 {
84+
let mut hasher = AHasher::default();
85+
86+
self.asset_type.hash(&mut hasher);
87+
self.env.hash(&mut hasher);
88+
self.file_path.hash(&mut hasher);
89+
self.pipeline.hash(&mut hasher);
90+
self.query.hash(&mut hasher);
91+
self.unique_key.hash(&mut hasher);
92+
93+
hasher.finish()
94+
}
95+
}
96+
97+
/// Statistics that pertain to an asset
98+
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
99+
pub struct AssetStats {
100+
pub size: u32,
101+
pub time: u32,
102+
}

0 commit comments

Comments
 (0)