-
-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathcontainer_plugin.rs
More file actions
181 lines (167 loc) · 5.02 KB
/
container_plugin.rs
File metadata and controls
181 lines (167 loc) · 5.02 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
use std::sync::Arc;
use rspack_core::{
ChunkUkey, Compilation, CompilationAdditionalTreeRuntimeRequirements, CompilationParams,
CompilationRuntimeRequirementInTree, CompilerCompilation, CompilerMake, DependencyType,
EntryOptions, EntryRuntime, Filename, LibraryOptions, ModuleLayer, Plugin, RuntimeGlobals,
RuntimeModule, SourceType,
};
use rspack_error::Result;
use rspack_hook::{plugin, plugin_hook};
use serde::Serialize;
use super::{
container_entry_dependency::ContainerEntryDependency,
container_entry_module_factory::ContainerEntryModuleFactory,
expose_runtime_module::ExposeRuntimeModule, federation_modules_plugin::FederationModulesPlugin,
};
use crate::ShareScope;
#[derive(Debug)]
pub struct ContainerPluginOptions {
pub name: String,
pub share_scope: ShareScope,
pub library: LibraryOptions,
pub runtime: Option<EntryRuntime>,
pub filename: Option<Filename>,
pub exposes: Vec<(String, ExposeOptions)>,
pub enhanced: bool,
}
#[rspack_cacheable::cacheable]
#[derive(Debug, Clone, Serialize)]
pub struct ExposeOptions {
pub name: Option<String>,
pub layer: Option<ModuleLayer>,
pub import: Vec<String>,
}
#[plugin]
#[derive(Debug)]
pub struct ContainerPlugin {
options: ContainerPluginOptions,
}
impl ContainerPlugin {
pub fn new(options: ContainerPluginOptions) -> Self {
Self::new_inner(options)
}
}
#[plugin_hook(CompilerCompilation for ContainerPlugin)]
async fn compilation(
&self,
compilation: &mut Compilation,
params: &mut CompilationParams,
) -> Result<()> {
compilation.set_dependency_factory(
DependencyType::ContainerEntry,
Arc::new(ContainerEntryModuleFactory),
);
compilation.set_dependency_factory(
DependencyType::ContainerExposed,
params.normal_module_factory.clone(),
);
Ok(())
}
#[plugin_hook(CompilerMake for ContainerPlugin)]
async fn make(&self, compilation: &mut Compilation) -> Result<()> {
let dep = ContainerEntryDependency::new(
self.options.name.clone(),
self.options.exposes.clone(),
self.options.share_scope.clone(),
self.options.enhanced,
);
// Call federation hook for dependency tracking
let hooks = FederationModulesPlugin::get_compilation_hooks(compilation);
hooks
.add_container_entry_dependency
.lock()
.await
.call(&dep)
.await?;
compilation
.add_entry(
Box::new(dep),
EntryOptions {
name: Some(self.options.name.clone()),
runtime: self.options.runtime.clone(),
filename: self.options.filename.clone(),
library: Some(self.options.library.clone()),
..Default::default()
},
)
.await?;
Ok(())
}
#[plugin_hook(CompilationAdditionalTreeRuntimeRequirements for ContainerPlugin)]
async fn additional_tree_runtime_requirements(
&self,
compilation: &Compilation,
chunk_ukey: &ChunkUkey,
runtime_requirements: &mut RuntimeGlobals,
_runtime_modules: &mut Vec<Box<dyn RuntimeModule>>,
) -> Result<()> {
let Some(entry_options) = compilation
.build_chunk_graph_artifact
.chunk_by_ukey
.get(chunk_ukey)
.and_then(|chunk| {
chunk.get_entry_options(&compilation.build_chunk_graph_artifact.chunk_group_by_ukey)
})
else {
return Ok(());
};
if matches!(&entry_options.name, Some(name) if name == &self.options.name)
&& compilation
.build_chunk_graph_artifact
.chunk_graph
.has_chunk_module_by_source_type(
chunk_ukey,
SourceType::Expose,
compilation.get_module_graph(),
)
&& compilation
.build_chunk_graph_artifact
.chunk_graph
.has_chunk_entry_dependent_chunks(
chunk_ukey,
&compilation.build_chunk_graph_artifact.chunk_group_by_ukey,
)
{
runtime_requirements.insert(RuntimeGlobals::STARTUP_CHUNK_DEPENDENCIES);
}
Ok(())
}
#[plugin_hook(CompilationRuntimeRequirementInTree for ContainerPlugin)]
async fn runtime_requirements_in_tree(
&self,
compilation: &Compilation,
chunk_ukey: &ChunkUkey,
_all_runtime_requirements: &RuntimeGlobals,
runtime_requirements: &RuntimeGlobals,
runtime_requirements_mut: &mut RuntimeGlobals,
runtime_modules_to_add: &mut Vec<(ChunkUkey, Box<dyn RuntimeModule>)>,
) -> Result<Option<()>> {
if runtime_requirements.contains(RuntimeGlobals::CURRENT_REMOTE_GET_SCOPE) {
runtime_requirements_mut.insert(RuntimeGlobals::HAS_OWN_PROPERTY);
if self.options.enhanced {
runtime_modules_to_add.push((
*chunk_ukey,
Box::new(ExposeRuntimeModule::new(&compilation.runtime_template)),
));
}
}
Ok(None)
}
impl Plugin for ContainerPlugin {
fn name(&self) -> &'static str {
"rspack.ContainerPlugin"
}
fn apply(&self, ctx: &mut rspack_core::ApplyContext<'_>) -> Result<()> {
ctx.compiler_hooks.compilation.tap(compilation::new(self));
ctx.compiler_hooks.make.tap(make::new(self));
ctx
.compilation_hooks
.additional_tree_runtime_requirements
.tap(additional_tree_runtime_requirements::new(self));
ctx
.compilation_hooks
.runtime_requirement_in_tree
.tap(runtime_requirements_in_tree::new(self));
Ok(())
}
}