-
-
Notifications
You must be signed in to change notification settings - Fork 776
Expand file tree
/
Copy pathembed_federation_runtime_module.rs
More file actions
144 lines (130 loc) · 4.73 KB
/
embed_federation_runtime_module.rs
File metadata and controls
144 lines (130 loc) · 4.73 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
//! # EmbedFederationRuntimeModule
//!
//! Runtime module that wraps the startup function to ensure federation runtime dependencies
//! execute before other modules. Generates a "prevStartup wrapper" pattern with defensive
//! checks that intercepts and modifies the startup execution order.
use rspack_cacheable::cacheable;
use rspack_core::{
DependencyId, RuntimeModule, RuntimeModuleGenerateContext, RuntimeModuleStage, RuntimeTemplate,
impl_runtime_module,
};
use rspack_error::Result;
use super::module_federation_runtime_plugin::ModuleFederationRuntimeExperimentsOptions;
#[cacheable]
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
pub struct EmbedFederationRuntimeModuleOptions {
pub collected_dependency_ids: Vec<DependencyId>,
pub experiments: ModuleFederationRuntimeExperimentsOptions,
}
#[impl_runtime_module]
#[derive(Debug)]
pub struct EmbedFederationRuntimeModule {
options: EmbedFederationRuntimeModuleOptions,
}
impl EmbedFederationRuntimeModule {
pub fn new(
runtime_template: &RuntimeTemplate,
options: EmbedFederationRuntimeModuleOptions,
) -> Self {
Self::with_name(runtime_template, "embed_federation_runtime", options)
}
}
enum TemplateId {
Async,
Sync,
}
impl EmbedFederationRuntimeModule {
fn template_id(&self, template_id: TemplateId) -> String {
match template_id {
TemplateId::Async => format!("{}_async", self.id),
TemplateId::Sync => format!("{}_sync", self.id),
}
}
}
#[async_trait::async_trait]
impl RuntimeModule for EmbedFederationRuntimeModule {
fn template(&self) -> Vec<(String, String)> {
vec![
(
self.template_id(TemplateId::Async),
include_str!("./embed_federation_runtime_async.ejs").to_string(),
),
(
self.template_id(TemplateId::Sync),
include_str!("./embed_federation_runtime_sync.ejs").to_string(),
),
]
}
async fn generate(&self, context: &RuntimeModuleGenerateContext<'_>) -> Result<String> {
let compilation = context.compilation;
let chunk_ukey = self
.chunk
.expect("Chunk should be attached to RuntimeModule");
let collected_deps = &self.options.collected_dependency_ids;
let module_graph = compilation.get_module_graph();
let mut federation_runtime_modules = Vec::new();
// Find federation runtime dependencies in this chunk
if !collected_deps.is_empty() {
for dep_id in collected_deps.iter() {
if let Some(module_dyn) = module_graph.get_module_by_dependency_id(dep_id) {
let is_in_chunk = compilation
.build_chunk_graph_artifact
.chunk_graph
.is_module_in_chunk(&module_dyn.identifier(), chunk_ukey);
if is_in_chunk {
federation_runtime_modules.push(*dep_id);
}
}
}
}
// Generate module execution code for each federation runtime dependency
let mut module_executions = String::with_capacity(federation_runtime_modules.len() * 64);
let mut runtime_template = compilation.runtime_template.create_module_code_template();
for dep_id in federation_runtime_modules {
let module_str = runtime_template.module_raw(compilation, &dep_id, "", false);
module_executions.push_str("\t\t");
module_executions.push_str(&module_str);
module_executions.push('\n');
}
if self.options.experiments.async_startup {
let entry_chunk_ids = compilation
.build_chunk_graph_artifact
.chunk_by_ukey
.expect_get(&chunk_ukey)
.get_all_initial_chunks(&compilation.build_chunk_graph_artifact.chunk_group_by_ukey)
.into_iter()
.map(|chunk_ukey| {
compilation
.build_chunk_graph_artifact
.chunk_by_ukey
.expect_get(&chunk_ukey)
.expect_id()
.to_string()
})
.collect::<Vec<_>>();
let entry_chunk_ids_literal =
serde_json::to_string(&entry_chunk_ids).expect("Invalid json to string");
Ok(context.runtime_template.render(
&self.template_id(TemplateId::Async),
Some(serde_json::json!({
"_module_executions": module_executions,
"_entry_chunk_ids": entry_chunk_ids_literal,
})),
)?)
} else {
if module_executions.is_empty() {
return Ok("// Federation runtime entry modules not found in this chunk.".into());
}
// Sync startup: keep the legacy prevStartup wrapper for minimal surface area.
Ok(context.runtime_template.render(
&self.template_id(TemplateId::Sync),
Some(serde_json::json!({
"_module_executions": module_executions,
})),
)?)
}
}
fn stage(&self) -> RuntimeModuleStage {
RuntimeModuleStage::Trigger // Run after RemoteRuntimeModule and StartupChunkDependenciesRuntimeModule
}
}