Skip to content

Commit 7f6e0a1

Browse files
committed
refactor: Introduce CollectModuleGraphEffectsArtifact for improved async module handling
- Removed the DependenciesDiagnosticsArtifact type and replaced it with CollectModuleGraphEffectsArtifact, which now encapsulates dependencies diagnostics and async module information. - Updated ModuleGraph methods to utilize the new artifact for checking async modules instead of relying on the previous async_modules_artifact. - Refactored the collect_build_module_graph_effects function to leverage the new artifact, ensuring better organization of module graph effects during compilation. - Adjusted various plugins and compilation hooks to accommodate the new artifact structure, enhancing modularity and clarity in async module management.
1 parent 2eacc2b commit 7f6e0a1

File tree

26 files changed

+381
-265
lines changed

26 files changed

+381
-265
lines changed

crates/rspack_binding_api/src/module_graph.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@ impl JsModuleGraph {
257257
#[napi(ts_args_type = "module: Module")]
258258
pub fn is_async(&self, module: ModuleObjectRef) -> napi::Result<bool> {
259259
let (compilation, _) = self.as_ref()?;
260-
Ok(ModuleGraph::is_async(compilation, &module.identifier))
260+
Ok(ModuleGraph::is_async(
261+
&compilation.collect_build_module_graph_effects_artifact,
262+
&module.identifier,
263+
))
261264
}
262265
}

crates/rspack_binding_api/src/plugins/interceptor.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ use rspack_core::{
4141
NormalModuleFactoryFactorizeHook, NormalModuleFactoryResolve,
4242
NormalModuleFactoryResolveForScheme, NormalModuleFactoryResolveForSchemeHook,
4343
NormalModuleFactoryResolveHook, NormalModuleFactoryResolveResult, ResourceData, RuntimeGlobals,
44-
Scheme, parse_resource, rspack_sources::RawStringSource,
44+
Scheme, collect_module_graph_effects::artifact::CollectModuleGraphEffectsArtifact,
45+
incremental::Mutations, parse_resource, rspack_sources::RawStringSource,
4546
};
4647
use rspack_hash::RspackHash;
4748
use rspack_hook::{Hook, Interceptor};
@@ -1211,7 +1212,11 @@ impl CompilationExecuteModule for CompilationExecuteModuleTap {
12111212

12121213
#[async_trait]
12131214
impl CompilationFinishModules for CompilationFinishModulesTap {
1214-
async fn run(&self, compilation: &mut Compilation) -> rspack_error::Result<()> {
1215+
async fn run(
1216+
&self,
1217+
compilation: &mut Compilation,
1218+
_artifacts: &mut CollectModuleGraphEffectsArtifact,
1219+
) -> rspack_error::Result<()> {
12151220
let compilation = JsCompilationWrapper::new(compilation);
12161221
self.function.call_with_promise(compilation).await
12171222
}

crates/rspack_core/src/artifacts/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rspack_collections::{IdentifierMap, IdentifierSet, UkeyMap};
2-
use rspack_error::Diagnostic;
32

43
use crate::{ChunkRenderResult, ChunkUkey, ModuleId, RuntimeGlobals, chunk_graph_chunk::ChunkId};
54

@@ -23,7 +22,7 @@ pub use side_effects_do_optimize_artifact::*;
2322

2423
pub type AsyncModulesArtifact = IdentifierSet;
2524
pub type ImportedByDeferModulesArtifact = IdentifierSet;
26-
pub type DependenciesDiagnosticsArtifact = IdentifierMap<Vec<Diagnostic>>;
25+
2726
pub type ModuleIdsArtifact = IdentifierMap<ModuleId>;
2827
pub type ChunkIdsArtifact = UkeyMap<ChunkUkey, ChunkId>;
2928
pub type CgcRuntimeRequirementsArtifact = UkeyMap<ChunkUkey, RuntimeGlobals>;

crates/rspack_core/src/chunk_graph/chunk_graph_module.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,11 @@ impl ChunkGraph {
383383
.hash(&mut hasher);
384384
module.source_types(&mg).dyn_hash(&mut hasher);
385385

386-
ModuleGraph::is_async(compilation, &module_identifier).dyn_hash(&mut hasher);
386+
ModuleGraph::is_async(
387+
&compilation.collect_build_module_graph_effects_artifact,
388+
&module_identifier,
389+
)
390+
.dyn_hash(&mut hasher);
387391
let exports_info =
388392
mg.get_prefetched_exports_info(&module_identifier, PrefetchExportsInfoMode::Full);
389393
let (entry, exports) = exports_info.meta();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use rspack_collections::{IdentifierMap, IdentifierSet};
2+
use rspack_error::Diagnostic;
3+
4+
use crate::incremental::Mutations;
5+
6+
#[derive(Debug, Default)]
7+
pub struct CollectModuleGraphEffectsArtifact {
8+
pub dependencies_diagnostics: DependenciesDiagnostics,
9+
pub async_module_info: IdentifierSet,
10+
// incremental mutations
11+
pub mutations: Mutations,
12+
}
13+
14+
pub type DependenciesDiagnostics = IdentifierMap<Vec<Diagnostic>>;
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
pub mod artifact;
2+
use std::mem;
3+
4+
use rayon::iter::{IntoParallelRefIterator as _, ParallelIterator};
5+
use rspack_error::Result;
6+
7+
use crate::{
8+
Compilation, Logger as _,
9+
collect_module_graph_effects::artifact::{
10+
CollectModuleGraphEffectsArtifact, DependenciesDiagnostics,
11+
},
12+
incremental::{self, IncrementalPasses, Mutation},
13+
};
14+
pub async fn collect_build_module_graph_effects(compilation: &mut Compilation) -> Result<()> {
15+
let mut artifact = mem::take(&mut compilation.collect_build_module_graph_effects_artifact);
16+
collect_build_module_graph_effects_inner(compilation, &mut artifact).await?;
17+
compilation.diagnostics.extend(
18+
artifact
19+
.dependencies_diagnostics
20+
.clone()
21+
.into_values()
22+
.flatten(),
23+
);
24+
let mutations = mem::take(&mut artifact.mutations);
25+
for mutation in mutations {
26+
if let Some(mutations) = compilation.incremental.mutations_write() {
27+
mutations.add(mutation);
28+
}
29+
}
30+
compilation.collect_build_module_graph_effects_artifact = artifact;
31+
Ok(())
32+
}
33+
// collect build module graph effects for incremental compilation
34+
#[tracing::instrument("Compilation:collect_build_module_graph_effects", skip_all)]
35+
pub async fn collect_build_module_graph_effects_inner(
36+
ctx: &mut Compilation,
37+
artifact: &mut CollectModuleGraphEffectsArtifact,
38+
) -> Result<()> {
39+
let logger = ctx.get_logger("rspack.Compilation");
40+
41+
artifact.mutations.extend(
42+
ctx
43+
.build_module_graph_artifact
44+
.affected_dependencies
45+
.updated()
46+
.iter()
47+
.map(|&dependency| Mutation::DependencyUpdate { dependency }),
48+
);
49+
50+
artifact.mutations.extend(
51+
ctx
52+
.build_module_graph_artifact
53+
.affected_modules
54+
.removed()
55+
.iter()
56+
.map(|&module| Mutation::ModuleRemove { module }),
57+
);
58+
artifact.mutations.extend(
59+
ctx
60+
.build_module_graph_artifact
61+
.affected_modules
62+
.updated()
63+
.iter()
64+
.map(|&module| Mutation::ModuleUpdate { module }),
65+
);
66+
artifact.mutations.extend(
67+
ctx
68+
.build_module_graph_artifact
69+
.affected_modules
70+
.added()
71+
.iter()
72+
.map(|&module| Mutation::ModuleAdd { module }),
73+
);
74+
tracing::debug!(target: incremental::TRACING_TARGET, passes = %IncrementalPasses::MAKE, %artifact.mutations);
75+
76+
let start = logger.time("finish modules");
77+
// finish_modules means the module graph (modules, connections, dependencies) are
78+
// frozen and start to optimize (provided exports, infer async, etc.) based on the
79+
// module graph, so any kind of change that affect these should be done before the
80+
// finish_modules
81+
82+
ctx
83+
.plugin_driver
84+
.clone()
85+
.compilation_hooks
86+
.finish_modules
87+
.call(ctx, artifact)
88+
.await?;
89+
90+
logger.time_end(start);
91+
92+
// https://github.com/webpack/webpack/blob/19ca74127f7668aaf60d59f4af8fcaee7924541a/lib/Compilation.js#L2988
93+
ctx.module_graph_cache_artifact.freeze();
94+
// Collect dependencies diagnostics at here to make sure:
95+
// 1. after finish_modules: has provide exports info
96+
// 2. before optimize dependencies: side effects free module hasn't been skipped
97+
collect_dependencies_diagnostics(ctx, artifact);
98+
ctx.module_graph_cache_artifact.unfreeze();
99+
Ok(())
100+
}
101+
#[tracing::instrument("Compilation:collect_dependencies_diagnostics", skip_all)]
102+
fn collect_dependencies_diagnostics(
103+
ctx: &Compilation,
104+
artifact: &mut CollectModuleGraphEffectsArtifact,
105+
) {
106+
let mutations = ctx
107+
.incremental
108+
.mutations_read(IncrementalPasses::DEPENDENCIES_DIAGNOSTICS);
109+
// TODO move diagnostic collect to make
110+
let modules = if let Some(mutations) = mutations
111+
&& !artifact.dependencies_diagnostics.is_empty()
112+
{
113+
let revoked_modules = mutations.iter().filter_map(|mutation| match mutation {
114+
Mutation::ModuleRemove { module } => Some(*module),
115+
_ => None,
116+
});
117+
for revoked_module in revoked_modules {
118+
artifact.dependencies_diagnostics.remove(&revoked_module);
119+
}
120+
let modules = mutations.get_affected_modules_with_module_graph(&ctx.get_module_graph());
121+
let logger = ctx.get_logger("rspack.incremental.dependenciesDiagnostics");
122+
logger.log(format!(
123+
"{} modules are affected, {} in total",
124+
modules.len(),
125+
ctx.get_module_graph().modules().len()
126+
));
127+
modules
128+
} else {
129+
ctx.get_module_graph().modules().keys().copied().collect()
130+
};
131+
let module_graph = ctx.get_module_graph();
132+
let module_graph_cache = &ctx.module_graph_cache_artifact;
133+
let dependencies_diagnostics: DependenciesDiagnostics = modules
134+
.par_iter()
135+
.map(|module_identifier| {
136+
let mgm = module_graph
137+
.module_graph_module_by_identifier(module_identifier)
138+
.expect("should have mgm");
139+
let diagnostics = mgm
140+
.all_dependencies
141+
.iter()
142+
.filter_map(|dependency_id| module_graph.dependency_by_id(dependency_id))
143+
.filter_map(|dependency| {
144+
dependency
145+
.get_diagnostics(&module_graph, module_graph_cache)
146+
.map(|diagnostics| {
147+
diagnostics.into_iter().map(|mut diagnostic| {
148+
diagnostic.module_identifier = Some(*module_identifier);
149+
diagnostic.loc = dependency.loc();
150+
diagnostic
151+
})
152+
})
153+
})
154+
.flatten()
155+
.collect::<Vec<_>>();
156+
(*module_identifier, diagnostics)
157+
})
158+
.collect();
159+
160+
artifact
161+
.dependencies_diagnostics
162+
.extend(dependencies_diagnostics);
163+
}

0 commit comments

Comments
 (0)