-
-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathmod.rs
162 lines (142 loc) · 6.07 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
mod cutout;
pub mod repair;
use std::{hash::BuildHasherDefault, path::PathBuf};
use indexmap::IndexSet;
use rayon::prelude::*;
use rspack_error::{Diagnostic, Result};
use rspack_identifier::{IdentifierMap, IdentifierSet};
use rustc_hash::{FxHashSet as HashSet, FxHasher};
use self::{cutout::Cutout, repair::repair};
use crate::{
tree_shaking::{visitor::OptimizeAnalyzeResult, BailoutFlag},
BuildDependency, Compilation, DependencyId, DependencyType, ModuleGraph, ModuleGraphPartial,
ModuleIdentifier,
};
#[derive(Debug, Default)]
pub struct MakeArtifact {
module_graph_partial: ModuleGraphPartial,
pub make_failed_dependencies: HashSet<BuildDependency>,
pub make_failed_module: HashSet<ModuleIdentifier>,
pub diagnostics: Vec<Diagnostic>,
entry_module_identifiers: IdentifierSet,
optimize_analyze_result_map: IdentifierMap<OptimizeAnalyzeResult>,
pub file_dependencies: IndexSet<PathBuf, BuildHasherDefault<FxHasher>>,
pub context_dependencies: IndexSet<PathBuf, BuildHasherDefault<FxHasher>>,
pub missing_dependencies: IndexSet<PathBuf, BuildHasherDefault<FxHasher>>,
pub build_dependencies: IndexSet<PathBuf, BuildHasherDefault<FxHasher>>,
has_module_graph_change: bool,
}
impl MakeArtifact {
fn get_module_graph(&self) -> ModuleGraph {
ModuleGraph::new(vec![&self.module_graph_partial], None)
}
fn get_module_graph_mut(&mut self) -> ModuleGraph {
ModuleGraph::new(vec![], Some(&mut self.module_graph_partial))
}
// TODO remove it
pub fn get_module_graph_partial(&self) -> &ModuleGraphPartial {
&self.module_graph_partial
}
// TODO remove it
pub fn get_module_graph_partial_mut(&mut self) -> &mut ModuleGraphPartial {
&mut self.module_graph_partial
}
// TODO remove it
fn move_data_from_compilation(&mut self, compilation: &mut Compilation) {
self.entry_module_identifiers = std::mem::take(&mut compilation.entry_module_identifiers);
self.optimize_analyze_result_map = std::mem::take(&mut compilation.optimize_analyze_result_map);
self.file_dependencies = std::mem::take(&mut compilation.file_dependencies);
self.context_dependencies = std::mem::take(&mut compilation.context_dependencies);
self.missing_dependencies = std::mem::take(&mut compilation.missing_dependencies);
self.build_dependencies = std::mem::take(&mut compilation.build_dependencies);
self.has_module_graph_change = compilation.has_module_import_export_change;
}
// TODO remove it
fn move_data_to_compilation(&mut self, compilation: &mut Compilation) {
compilation.entry_module_identifiers = std::mem::take(&mut self.entry_module_identifiers);
compilation.optimize_analyze_result_map = std::mem::take(&mut self.optimize_analyze_result_map);
compilation.file_dependencies = std::mem::take(&mut self.file_dependencies);
compilation.context_dependencies = std::mem::take(&mut self.context_dependencies);
compilation.missing_dependencies = std::mem::take(&mut self.missing_dependencies);
compilation.build_dependencies = std::mem::take(&mut self.build_dependencies);
compilation.has_module_import_export_change = self.has_module_graph_change;
compilation.push_batch_diagnostic(std::mem::take(&mut self.diagnostics));
compilation.make_failed_module = std::mem::take(&mut self.make_failed_module);
compilation.make_failed_dependencies = std::mem::take(&mut self.make_failed_dependencies);
}
}
#[derive(Debug, Clone)]
pub enum MakeParam {
ModifiedFiles(HashSet<PathBuf>),
DeletedFiles(HashSet<PathBuf>),
ForceBuildDeps(HashSet<BuildDependency>),
ForceBuildModules(HashSet<ModuleIdentifier>),
}
impl MakeParam {
pub fn new_force_build_dep_param(dep: DependencyId, module: Option<ModuleIdentifier>) -> Self {
let mut data = HashSet::default();
data.insert((dep, module));
Self::ForceBuildDeps(data)
}
}
pub async fn update_module_graph(
compilation: &mut Compilation,
params: Vec<MakeParam>,
) -> Result<()> {
let mut artifact = MakeArtifact::default();
compilation.swap_make_artifact(&mut artifact);
artifact.move_data_from_compilation(compilation);
artifact = update_module_graph_with_artifact(compilation, artifact, params).await?;
// Avoid to introduce too much overhead,
// until we find a better way to align with webpack hmr behavior
// add context module and context element module to bailout_module_identifiers
if compilation.options.builtins.tree_shaking.enable() {
let module_graph = artifact.get_module_graph();
compilation.bailout_module_identifiers = module_graph
.dependencies()
.values()
.par_bridge()
.filter_map(|dep| {
if dep.as_context_dependency().is_some()
&& let Some(module) = module_graph.get_module_by_dependency_id(dep.id())
{
let mut values = vec![(module.identifier(), BailoutFlag::CONTEXT_MODULE)];
if let Some(dependencies) = module_graph.get_module_all_dependencies(&module.identifier())
{
for dependency in dependencies {
if let Some(dependency_module) =
module_graph.module_identifier_by_dependency_id(dependency)
{
values.push((*dependency_module, BailoutFlag::CONTEXT_MODULE));
}
}
}
Some(values)
} else if matches!(
dep.dependency_type(),
DependencyType::ContainerExposed | DependencyType::ProvideModuleForShared
) && let Some(module) = module_graph.get_module_by_dependency_id(dep.id())
{
Some(vec![(module.identifier(), BailoutFlag::CONTAINER_EXPOSED)])
} else {
None
}
})
.flatten()
.collect();
}
artifact.move_data_to_compilation(compilation);
compilation.swap_make_artifact(&mut artifact);
Ok(())
}
pub async fn update_module_graph_with_artifact(
compilation: &Compilation,
mut artifact: MakeArtifact,
params: Vec<MakeParam>,
) -> Result<MakeArtifact> {
let mut cutout = Cutout::default();
let build_dependencies = cutout.cutout_artifact(&mut artifact, params);
artifact = repair(compilation, artifact, build_dependencies)?;
cutout.fix_artifact(&mut artifact);
Ok(artifact)
}