Skip to main content

rspack_core/
lib.rs

1use std::{fmt, sync::Arc};
2mod artifacts;
3mod binding;
4mod compilation;
5mod transient_cache;
6
7mod exports;
8mod value_cache_versions;
9pub use artifacts::*;
10pub use binding::*;
11pub use compilation::{
12  build_module_graph::{ExecuteModuleId, ForwardId, LazyUntil},
13  *,
14};
15pub use exports::*;
16pub use transient_cache::*;
17pub use value_cache_versions::ValueCacheVersions;
18mod dependencies_block;
19pub mod diagnostics;
20pub mod incremental;
21pub use dependencies_block::{
22  AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, AsyncDependenciesBlockIdentifierMap,
23  AsyncDependenciesBlockIdentifierSet, DependenciesBlock,
24};
25mod fake_namespace_object;
26pub use fake_namespace_object::*;
27mod runtime_template;
28pub use runtime_template::*;
29use rustc_hash::FxHashMap;
30pub mod external_module;
31pub use external_module::*;
32mod logger;
33pub use logger::*;
34pub mod cache;
35mod normal_module;
36mod raw_module;
37pub use raw_module::*;
38pub mod module;
39pub mod parser_and_generator;
40pub use concatenated_module::*;
41pub use module::*;
42pub use parser_and_generator::*;
43mod runtime_globals;
44pub use normal_module::*;
45pub use runtime_globals::{MODULE_GLOBALS, REQUIRE_SCOPE_GLOBALS, RuntimeGlobals, RuntimeVariable};
46mod plugin;
47pub use plugin::*;
48mod context_module;
49pub use context_module::*;
50mod context_module_factory;
51pub use context_module_factory::*;
52mod init_fragment;
53pub use init_fragment::*;
54mod module_factory;
55pub use module_factory::*;
56mod normal_module_factory;
57pub use normal_module_factory::*;
58mod ignore_error_module_factory;
59pub use ignore_error_module_factory::*;
60mod self_module_factory;
61pub use self_module_factory::*;
62mod self_module;
63pub use self_module::*;
64mod compiler;
65pub use compiler::*;
66mod options;
67pub use options::*;
68mod module_graph;
69pub use module_graph::*;
70mod chunk;
71pub use chunk::*;
72mod dependency;
73pub use dependency::*;
74mod utils;
75use ustr::Ustr;
76pub use utils::*;
77mod chunk_graph;
78pub use chunk_graph::*;
79mod stats;
80pub use stats::*;
81mod runtime;
82mod runtime_module;
83pub use runtime::*;
84pub use runtime_module::*;
85mod entrypoint;
86pub use entrypoint::*;
87mod loader;
88pub use loader::*;
89// mod external;
90// pub use external::*;
91mod chunk_group;
92pub use chunk_group::*;
93mod ukey;
94pub use ukey::*;
95pub mod resolver;
96pub use resolver::*;
97pub use rspack_location::{
98  DependencyLocation, RealDependencyLocation, SourcePosition, SyntheticDependencyLocation,
99};
100pub mod concatenated_module;
101pub mod reserved_names;
102use rspack_cacheable::{cacheable, with::AsPreset};
103pub use rspack_loader_runner::{
104  AdditionalData, BUILTIN_LOADER_PREFIX, ParseMeta, ResourceData, ResourceParsedData, Scheme,
105  get_scheme, parse_resource,
106};
107pub use rspack_macros::{impl_runtime_module, impl_source_map_config};
108pub use rspack_sources;
109
110#[cfg(debug_assertions)]
111pub mod debug_info;
112
113#[cacheable]
114#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
115pub enum SourceType {
116  JavaScript,
117  Css,
118  CssUrl,
119  Wasm,
120  Asset,
121  Expose,
122  Remote,
123  ShareInit,
124  ConsumeShared,
125  ShareContainerShared,
126  Custom(#[cacheable(with=AsPreset)] Ustr),
127  #[default]
128  Unknown,
129  CssImport,
130  Runtime,
131}
132
133impl std::fmt::Display for SourceType {
134  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135    match self {
136      SourceType::JavaScript => write!(f, "javascript"),
137      SourceType::Css => write!(f, "css"),
138      SourceType::CssUrl => write!(f, "css-url"),
139      SourceType::Wasm => write!(f, "wasm"),
140      SourceType::Asset => write!(f, "asset"),
141      SourceType::Expose => write!(f, "expose"),
142      SourceType::Remote => write!(f, "remote"),
143      SourceType::ShareInit => write!(f, "share-init"),
144      SourceType::ConsumeShared => write!(f, "consume-shared"),
145      SourceType::ShareContainerShared => write!(f, "share-container-shared"),
146      SourceType::Unknown => write!(f, "unknown"),
147      SourceType::CssImport => write!(f, "css-import"),
148      SourceType::Custom(source_type) => f.write_str(source_type),
149      SourceType::Runtime => write!(f, "runtime"),
150    }
151  }
152}
153
154impl From<&str> for SourceType {
155  fn from(value: &str) -> Self {
156    match value {
157      "javascript" => Self::JavaScript,
158      "css" => Self::Css,
159      "wasm" => Self::Wasm,
160      "asset" => Self::Asset,
161      "expose" => Self::Expose,
162      "remote" => Self::Remote,
163      "share-init" => Self::ShareInit,
164      "consume-shared" => Self::ConsumeShared,
165      "share-container-shared" => Self::ShareContainerShared,
166      "unknown" => Self::Unknown,
167      "css-import" => Self::CssImport,
168      other => SourceType::Custom(other.into()),
169    }
170  }
171}
172
173impl From<&ModuleType> for SourceType {
174  fn from(value: &ModuleType) -> Self {
175    match value {
176      ModuleType::JsAuto | ModuleType::JsEsm | ModuleType::JsDynamic => Self::JavaScript,
177      ModuleType::Css | ModuleType::CssModule | ModuleType::CssAuto => Self::Css,
178      ModuleType::WasmSync | ModuleType::WasmAsync => Self::Wasm,
179      ModuleType::Asset | ModuleType::AssetInline | ModuleType::AssetResource => Self::Asset,
180      ModuleType::ConsumeShared => Self::ConsumeShared,
181      ModuleType::ShareContainerShared => Self::ShareContainerShared,
182      _ => Self::Unknown,
183    }
184  }
185}
186
187#[cacheable]
188#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
189pub enum ModuleType {
190  Json,
191  Css,
192  CssModule,
193  CssAuto,
194  JsAuto,
195  JsDynamic,
196  JsEsm,
197  WasmSync,
198  WasmAsync,
199  AssetInline,
200  AssetResource,
201  AssetSource,
202  AssetBytes,
203  Asset,
204  Runtime,
205  Remote,
206  Fallback,
207  ProvideShared,
208  ConsumeShared,
209  ShareContainerShared,
210  SelfReference,
211  Custom(#[cacheable(with=AsPreset)] Ustr),
212}
213
214impl ModuleType {
215  pub fn is_js_like(&self) -> bool {
216    matches!(
217      self,
218      ModuleType::JsAuto | ModuleType::JsEsm | ModuleType::JsDynamic
219    )
220  }
221
222  pub fn is_asset_like(&self) -> bool {
223    matches!(
224      self,
225      ModuleType::Asset | ModuleType::AssetInline | ModuleType::AssetResource
226    )
227  }
228
229  pub fn is_wasm_like(&self) -> bool {
230    matches!(self, ModuleType::WasmSync | ModuleType::WasmAsync)
231  }
232
233  pub fn is_js_auto(&self) -> bool {
234    matches!(self, ModuleType::JsAuto)
235  }
236
237  pub fn is_js_esm(&self) -> bool {
238    matches!(self, ModuleType::JsEsm)
239  }
240
241  pub fn is_js_dynamic(&self) -> bool {
242    matches!(self, ModuleType::JsDynamic)
243  }
244
245  /// Webpack arbitrary determines the binary type from [NormalModule.binary](https://github.com/webpack/webpack/blob/1f99ad6367f2b8a6ef17cce0e058f7a67fb7db18/lib/NormalModule.js#L302)
246  pub fn is_binary(&self) -> bool {
247    self.is_asset_like() || self.is_wasm_like()
248  }
249
250  pub fn as_str(&self) -> &'static str {
251    match self {
252      ModuleType::JsAuto => "javascript/auto",
253      ModuleType::JsEsm => "javascript/esm",
254      ModuleType::JsDynamic => "javascript/dynamic",
255
256      ModuleType::Css => "css",
257      ModuleType::CssModule => "css/module",
258      ModuleType::CssAuto => "css/auto",
259
260      ModuleType::Json => "json",
261
262      ModuleType::WasmSync => "webassembly/sync",
263      ModuleType::WasmAsync => "webassembly/async",
264
265      ModuleType::Asset => "asset",
266      ModuleType::AssetSource => "asset/source",
267      ModuleType::AssetResource => "asset/resource",
268      ModuleType::AssetInline => "asset/inline",
269      ModuleType::AssetBytes => "asset/bytes",
270      ModuleType::Runtime => "runtime",
271      ModuleType::Remote => "remote-module",
272      ModuleType::Fallback => "fallback-module",
273      ModuleType::ProvideShared => "provide-module",
274      ModuleType::ConsumeShared => "consume-shared-module",
275      ModuleType::ShareContainerShared => "share-container-shared-module",
276      ModuleType::SelfReference => "self-reference-module",
277
278      ModuleType::Custom(custom) => custom.as_str(),
279    }
280  }
281}
282
283impl fmt::Display for ModuleType {
284  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
285    write!(f, "{}", self.as_str(),)
286  }
287}
288
289impl From<&str> for ModuleType {
290  fn from(value: &str) -> Self {
291    match value {
292      "mjs" => Self::JsEsm,
293      "cjs" => Self::JsDynamic,
294      "javascript/auto" => Self::JsAuto,
295      "javascript/dynamic" => Self::JsDynamic,
296      "javascript/esm" => Self::JsEsm,
297
298      "css" => Self::Css,
299      "css/module" => Self::CssModule,
300      "css/auto" => Self::CssAuto,
301
302      "json" => Self::Json,
303
304      "webassembly/sync" => Self::WasmSync,
305      "webassembly/async" => Self::WasmAsync,
306
307      "asset" => Self::Asset,
308      "asset/resource" => Self::AssetResource,
309      "asset/source" => Self::AssetSource,
310      "asset/inline" => Self::AssetInline,
311      "asset/bytes" => Self::AssetBytes,
312
313      custom => Self::Custom(custom.into()),
314    }
315  }
316}
317
318pub type ModuleLayer = String;
319
320pub(crate) type SharedPluginDriver = Arc<PluginDriver>;
321
322#[derive(Debug, Default, Clone)]
323pub struct ChunkByUkey {
324  inner: FxHashMap<ChunkUkey, Chunk>,
325}
326
327impl ChunkByUkey {
328  pub fn get(&self, ukey: &ChunkUkey) -> Option<&Chunk> {
329    self.inner.get(ukey)
330  }
331
332  pub fn get_mut(&mut self, ukey: &ChunkUkey) -> Option<&mut Chunk> {
333    self.inner.get_mut(ukey)
334  }
335
336  pub fn get_many_mut<const N: usize>(
337    &mut self,
338    ukeys: [&ChunkUkey; N],
339  ) -> [Option<&mut Chunk>; N] {
340    self.inner.get_disjoint_mut(ukeys)
341  }
342
343  pub fn expect_get(&self, ukey: &ChunkUkey) -> &Chunk {
344    self
345      .get(ukey)
346      .unwrap_or_else(|| panic!("Chunk({ukey:?}) not found in ChunkByUkey"))
347  }
348
349  pub fn expect_get_mut(&mut self, ukey: &ChunkUkey) -> &mut Chunk {
350    self
351      .get_mut(ukey)
352      .unwrap_or_else(|| panic!("Chunk({ukey:?}) not found in ChunkByUkey"))
353  }
354
355  pub fn add(&mut self, chunk: Chunk) -> &mut Chunk {
356    let ukey = chunk.ukey();
357    debug_assert!(!self.inner.contains_key(&ukey));
358    self.inner.entry(ukey).or_insert(chunk)
359  }
360
361  pub fn remove(&mut self, ukey: &ChunkUkey) -> Option<Chunk> {
362    self.inner.remove(ukey)
363  }
364
365  pub fn entry(
366    &mut self,
367    ukey: ChunkUkey,
368  ) -> std::collections::hash_map::Entry<'_, ChunkUkey, Chunk> {
369    self.inner.entry(ukey)
370  }
371
372  pub fn contains(&self, ukey: &ChunkUkey) -> bool {
373    self.inner.contains_key(ukey)
374  }
375
376  pub fn keys(&self) -> impl Iterator<Item = &ChunkUkey> {
377    self.inner.keys()
378  }
379
380  pub fn values(&self) -> impl Iterator<Item = &Chunk> {
381    self.inner.values()
382  }
383
384  pub fn values_mut(&mut self) -> impl Iterator<Item = &mut Chunk> {
385    self.inner.values_mut()
386  }
387
388  pub fn iter(&self) -> impl Iterator<Item = (&ChunkUkey, &Chunk)> {
389    self.inner.iter()
390  }
391
392  pub fn iter_mut(&mut self) -> impl Iterator<Item = (&ChunkUkey, &mut Chunk)> {
393    self.inner.iter_mut()
394  }
395
396  pub fn len(&self) -> usize {
397    self.inner.len()
398  }
399
400  pub fn is_empty(&self) -> bool {
401    self.inner.is_empty()
402  }
403}
404
405#[derive(Debug, Default, Clone)]
406pub struct ChunkGroupByUkey {
407  inner: FxHashMap<ChunkGroupUkey, ChunkGroup>,
408}
409
410impl ChunkGroupByUkey {
411  pub fn get(&self, ukey: &ChunkGroupUkey) -> Option<&ChunkGroup> {
412    self.inner.get(ukey)
413  }
414
415  pub fn get_mut(&mut self, ukey: &ChunkGroupUkey) -> Option<&mut ChunkGroup> {
416    self.inner.get_mut(ukey)
417  }
418
419  pub fn get_many_mut<const N: usize>(
420    &mut self,
421    ukeys: [&ChunkGroupUkey; N],
422  ) -> [Option<&mut ChunkGroup>; N] {
423    self.inner.get_disjoint_mut(ukeys)
424  }
425
426  pub fn expect_get(&self, ukey: &ChunkGroupUkey) -> &ChunkGroup {
427    self
428      .get(ukey)
429      .unwrap_or_else(|| panic!("ChunkGroup({ukey:?}) not found in ChunkGroupByUkey"))
430  }
431
432  pub fn expect_get_mut(&mut self, ukey: &ChunkGroupUkey) -> &mut ChunkGroup {
433    self
434      .get_mut(ukey)
435      .unwrap_or_else(|| panic!("ChunkGroup({ukey:?}) not found in ChunkGroupByUkey"))
436  }
437
438  pub fn add(&mut self, chunk: ChunkGroup) -> &mut ChunkGroup {
439    let ukey = chunk.ukey();
440    debug_assert!(!self.inner.contains_key(&ukey));
441    self.inner.entry(ukey).or_insert(chunk)
442  }
443
444  pub fn remove(&mut self, ukey: &ChunkGroupUkey) -> Option<ChunkGroup> {
445    self.inner.remove(ukey)
446  }
447
448  pub fn entry(
449    &mut self,
450    ukey: ChunkGroupUkey,
451  ) -> std::collections::hash_map::Entry<'_, ChunkGroupUkey, ChunkGroup> {
452    self.inner.entry(ukey)
453  }
454
455  pub fn contains(&self, ukey: &ChunkGroupUkey) -> bool {
456    self.inner.contains_key(ukey)
457  }
458
459  pub fn keys(&self) -> impl Iterator<Item = &ChunkGroupUkey> {
460    self.inner.keys()
461  }
462
463  pub fn values(&self) -> impl Iterator<Item = &ChunkGroup> {
464    self.inner.values()
465  }
466
467  pub fn values_mut(&mut self) -> impl Iterator<Item = &mut ChunkGroup> {
468    self.inner.values_mut()
469  }
470
471  pub fn iter(&self) -> impl Iterator<Item = (&ChunkGroupUkey, &ChunkGroup)> {
472    self.inner.iter()
473  }
474
475  pub fn iter_mut(&mut self) -> impl Iterator<Item = (&ChunkGroupUkey, &mut ChunkGroup)> {
476    self.inner.iter_mut()
477  }
478}