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