rspack_core/
module_factory.rs1use std::{fmt::Debug, sync::Arc};
2
3use rspack_error::{Diagnostic, Result};
4use rspack_paths::{ArcPath, ArcPathSet};
5
6use crate::{
7 BoxDependency, BoxModule, CompilationId, CompilerId, CompilerOptions, Context, ModuleIdentifier,
8 ModuleLayer, Resolve, ResolverFactory,
9};
10
11#[derive(Debug, Clone)]
12pub struct ModuleFactoryCreateData {
13 pub compiler_id: CompilerId,
14 pub compilation_id: CompilationId,
15 pub resolve_options: Option<Arc<Resolve>>,
16 pub options: Arc<CompilerOptions>,
17 pub request: String,
18 pub context: Context,
19 pub dependencies: Vec<BoxDependency>,
20 pub issuer: Option<Box<str>>,
21 pub issuer_identifier: Option<ModuleIdentifier>,
22 pub issuer_layer: Option<ModuleLayer>,
23 pub resolver_factory: Arc<ResolverFactory>,
24
25 pub file_dependencies: ArcPathSet,
26 pub context_dependencies: ArcPathSet,
27 pub missing_dependencies: ArcPathSet,
28 pub diagnostics: Vec<Diagnostic>,
29}
30
31impl ModuleFactoryCreateData {
32 pub fn add_file_dependency<F: Into<ArcPath>>(&mut self, file: F) {
33 self.file_dependencies.insert(file.into());
34 }
35
36 pub fn add_file_dependencies<F: Into<ArcPath>>(&mut self, files: impl IntoIterator<Item = F>) {
37 self
38 .file_dependencies
39 .extend(files.into_iter().map(Into::into));
40 }
41
42 pub fn add_context_dependency<F: Into<ArcPath>>(&mut self, context: F) {
43 self.context_dependencies.insert(context.into());
44 }
45
46 pub fn add_context_dependencies<F: Into<ArcPath>>(
47 &mut self,
48 contexts: impl IntoIterator<Item = F>,
49 ) {
50 self
51 .context_dependencies
52 .extend(contexts.into_iter().map(Into::into));
53 }
54
55 pub fn add_missing_dependency<F: Into<ArcPath>>(&mut self, missing: F) {
56 self.missing_dependencies.insert(missing.into());
57 }
58
59 pub fn add_missing_dependencies<F: Into<ArcPath>>(
60 &mut self,
61 missing: impl IntoIterator<Item = F>,
62 ) {
63 self
64 .missing_dependencies
65 .extend(missing.into_iter().map(Into::into));
66 }
67}
68
69#[derive(Debug, Default)]
70pub struct ModuleFactoryResult {
71 pub module: Option<BoxModule>,
72}
73
74impl ModuleFactoryResult {
75 pub fn new_with_module(module: BoxModule) -> Self {
76 Self {
77 module: Some(module),
78 }
79 }
80
81 pub fn module(mut self, module: Option<BoxModule>) -> Self {
82 self.module = module;
83 self
84 }
85}
86
87#[async_trait::async_trait]
88pub trait ModuleFactory: Debug + Sync + Send {
89 async fn create(&self, data: &mut ModuleFactoryCreateData) -> Result<ModuleFactoryResult>;
90}