|
| 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