Skip to content

Commit ea46e7b

Browse files
committed
chore: add effects artifact
1 parent 711cb55 commit ea46e7b

File tree

14 files changed

+258
-193
lines changed

14 files changed

+258
-193
lines changed

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

0 commit comments

Comments
 (0)