Skip to content

Commit b241619

Browse files
feat(mf): add layers support for module federation
Made-with: Cursor
1 parent 61bd287 commit b241619

File tree

11 files changed

+282
-9
lines changed

11 files changed

+282
-9
lines changed

crates/rspack_binding_api/src/raw_options/raw_builtins/raw_mf.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rspack_plugin_mf::{
99
ModuleFederationRuntimeExperimentsOptions, ModuleFederationRuntimePluginOptions,
1010
OptimizeSharedConfig, ProvideOptions, ProvideVersion, RemoteAliasTarget, RemoteOptions,
1111
ShareScope, SharedContainerPluginOptions, SharedUsedExportsOptimizerPluginOptions,
12-
StatsBuildInfo,
12+
StatsBuildInfo, debug_log,
1313
};
1414
use rspack_util::fx_hash::FxHashMap as HashMap;
1515

@@ -136,6 +136,35 @@ pub struct RawProvideOptions {
136136

137137
impl From<RawProvideOptions> for (String, ProvideOptions) {
138138
fn from(value: RawProvideOptions) -> Self {
139+
// #region agent log
140+
if value.share_key == "react" || value.layer.is_some() || value.request.is_some() {
141+
debug_log(
142+
"H1",
143+
"raw_mf.rs:137",
144+
"raw provide options converted",
145+
serde_json::json!({
146+
"key": value.key,
147+
"request": value.request,
148+
"layer": value.layer,
149+
"shareKey": value.share_key,
150+
"shareScope": match &value.share_scope {
151+
Either::A(scope) => serde_json::json!(scope),
152+
Either::B(scopes) => serde_json::json!(scopes),
153+
},
154+
"version": value.version.as_ref().map(|v| match v {
155+
Either::A(version) => version.clone(),
156+
Either::B(flag) => flag.to_string(),
157+
}),
158+
"requiredVersion": value.required_version.as_ref().map(|v| match v {
159+
Either::A(version) => version.clone(),
160+
Either::B(flag) => flag.to_string(),
161+
}),
162+
"singleton": value.singleton,
163+
"strictVersion": value.strict_version,
164+
}),
165+
);
166+
}
167+
// #endregion
139168
(
140169
value.key,
141170
ProvideOptions {
@@ -288,6 +317,35 @@ pub struct RawConsumeOptions {
288317

289318
impl From<RawConsumeOptions> for (String, ConsumeOptions) {
290319
fn from(value: RawConsumeOptions) -> Self {
320+
// #region agent log
321+
if value.share_key == "react" || value.layer.is_some() || value.issuer_layer.is_some() {
322+
debug_log(
323+
"H1",
324+
"raw_mf.rs:289",
325+
"raw consume options converted",
326+
serde_json::json!({
327+
"key": value.key,
328+
"request": value.request,
329+
"issuerLayer": value.issuer_layer,
330+
"layer": value.layer,
331+
"import": value.import,
332+
"importResolved": value.import_resolved,
333+
"shareKey": value.share_key,
334+
"shareScope": match &value.share_scope {
335+
Either::A(scope) => serde_json::json!(scope),
336+
Either::B(scopes) => serde_json::json!(scopes),
337+
},
338+
"requiredVersion": value.required_version.as_ref().map(|v| match v {
339+
Either::A(version) => version.clone(),
340+
Either::B(flag) => flag.to_string(),
341+
}),
342+
"strictVersion": value.strict_version,
343+
"singleton": value.singleton,
344+
"eager": value.eager,
345+
}),
346+
);
347+
}
348+
// #endregion
291349
(
292350
value.key,
293351
ConsumeOptions {

crates/rspack_plugin_mf/src/container/container_entry_module.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_hash::FxHashSet;
2121
use super::{
2222
container_exposed_dependency::ContainerExposedDependency, container_plugin::ExposeOptions,
2323
};
24-
use crate::{ShareScope, utils::json_stringify};
24+
use crate::{ShareScope, debug_log, utils::json_stringify};
2525

2626
#[impl_source_map_config]
2727
#[cacheable]
@@ -344,6 +344,18 @@ impl Module for ContainerEntryModule {
344344
let module_map = ExposeModuleMap::new(compilation, self, runtime_template);
345345
let module_map_str = module_map.render(runtime_template);
346346
let source = if self.enhanced {
347+
// #region agent log
348+
debug_log(
349+
"S1",
350+
"container_entry_module.rs:346",
351+
"rspack enhanced container entry codegen",
352+
serde_json::json!({
353+
"name": self.name,
354+
"shareScope": self.share_scope,
355+
"moduleMapKeys": self.exposes.iter().map(|(name, _)| name.clone()).collect::<Vec<_>>(),
356+
}),
357+
);
358+
// #endregion
347359
let define_property_getters =
348360
runtime_template.render_runtime_globals(&RuntimeGlobals::DEFINE_PROPERTY_GETTERS);
349361
let get_container = format!(

crates/rspack_plugin_mf/src/container/embed_federation_runtime_module.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rspack_core::{
1212
use rspack_error::Result;
1313

1414
use super::module_federation_runtime_plugin::ModuleFederationRuntimeExperimentsOptions;
15+
use crate::debug_log;
1516

1617
#[cacheable]
1718
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
@@ -92,6 +93,7 @@ impl RuntimeModule for EmbedFederationRuntimeModule {
9293

9394
// Generate module execution code for each federation runtime dependency
9495
let mut module_executions = String::with_capacity(federation_runtime_modules.len() * 64);
96+
let federation_runtime_modules_count = federation_runtime_modules.len();
9597
let mut runtime_template = compilation.runtime_template.create_module_code_template();
9698

9799
for dep_id in federation_runtime_modules {
@@ -100,6 +102,23 @@ impl RuntimeModule for EmbedFederationRuntimeModule {
100102
module_executions.push_str(&module_str);
101103
module_executions.push('\n');
102104
}
105+
// #region agent log
106+
debug_log(
107+
"S3",
108+
"embed_federation_runtime_module.rs:103",
109+
"rspack embed federation runtime generate",
110+
serde_json::json!({
111+
"asyncStartup": self.options.experiments.async_startup,
112+
"federationRuntimeModulesCount": federation_runtime_modules_count,
113+
"entryChunk": compilation
114+
.build_chunk_graph_artifact
115+
.chunk_by_ukey
116+
.expect_get(&chunk_ukey)
117+
.id()
118+
.map(ToString::to_string),
119+
}),
120+
);
121+
// #endregion
103122

104123
if self.options.experiments.async_startup {
105124
let entry_chunk_ids = compilation

crates/rspack_plugin_mf/src/container/expose_runtime_module.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rspack_core::{
44
};
55

66
use super::container_entry_module::CodeGenerationDataExpose;
7-
use crate::utils::json_stringify;
7+
use crate::{debug_log, utils::json_stringify};
88

99
#[impl_runtime_module]
1010
#[derive(Debug)]
@@ -69,6 +69,16 @@ impl RuntimeModule for ExposeRuntimeModule {
6969
let Some(data) = self.find_expose_data(&chunk_ukey, compilation) else {
7070
return Ok(String::new());
7171
};
72+
// #region agent log
73+
debug_log(
74+
"S2",
75+
"expose_runtime_module.rs:69",
76+
"rspack expose runtime data",
77+
serde_json::json!({
78+
"shareScope": data.share_scope,
79+
}),
80+
);
81+
// #endregion
7282
let mut runtime_template = compilation.runtime_template.create_module_code_template();
7383
let module_map = data.module_map.render(&mut runtime_template);
7484
let require_name = runtime_template.render_runtime_globals(&RuntimeGlobals::REQUIRE);

crates/rspack_plugin_mf/src/container/remote_runtime_module.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hash::FxHashMap;
1111
use serde::Serialize;
1212

1313
use super::remote_module::RemoteModule;
14-
use crate::{ShareScope, utils::json_stringify};
14+
use crate::{ShareScope, debug_log, utils::json_stringify};
1515

1616
static REMOTES_LOADING_TEMPLATE: &str = include_str!("./remotesLoading.ejs");
1717
static REMOTES_LOADING_RUNTIME_REQUIREMENTS: LazyLock<RuntimeGlobals> =
@@ -100,6 +100,29 @@ impl RuntimeModule for RemoteRuntimeModule {
100100
external_module.identifier(),
101101
)
102102
.expect("should have module_id at <RemoteRuntimeModule as RuntimeModule>::generate");
103+
// #region agent log
104+
if m.remote_key == "containerA" || matches!(share_scope, ShareScopeField::Multiple(_)) {
105+
debug_log(
106+
"H7",
107+
"remote_runtime_module.rs:98",
108+
"remote runtime mapping emitted",
109+
serde_json::json!({
110+
"moduleId": id.to_string(),
111+
"remoteName": m.remote_key,
112+
"request": name,
113+
"shareScope": match &m.share_scope {
114+
ShareScope::Single(scope) => serde_json::json!(scope),
115+
ShareScope::Multiple(scopes) => serde_json::json!(scopes),
116+
},
117+
"externalModuleId": external_module_id,
118+
"remoteInfo": remote_info.as_ref().map(|info| serde_json::json!({
119+
"externalType": info.external_type,
120+
"name": info.name,
121+
})),
122+
}),
123+
);
124+
}
125+
// #endregion
103126
remotes.push(id.to_string());
104127
id_to_remote_data_mapping.insert(
105128
id,

crates/rspack_plugin_mf/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,27 @@ mod utils {
7575
serde_json::to_string(v).unwrap_or_else(|e| panic!("{e}: {v:?} should able to json stringify"))
7676
}
7777
}
78+
79+
pub fn debug_log(hypothesis_id: &str, location: &str, message: &str, data: serde_json::Value) {
80+
let timestamp = std::time::SystemTime::now()
81+
.duration_since(std::time::UNIX_EPOCH)
82+
.map(|duration| duration.as_millis())
83+
.unwrap_or_default();
84+
let payload = serde_json::json!({
85+
"sessionId": "c5c28d",
86+
"runId": "rust-stack-1",
87+
"hypothesisId": hypothesis_id,
88+
"location": location,
89+
"message": message,
90+
"data": data,
91+
"timestamp": timestamp,
92+
});
93+
if let Ok(mut file) = std::fs::OpenOptions::new()
94+
.create(true)
95+
.append(true)
96+
.open("/Users/zackjackson/rspack/.cursor/debug-c5c28d.log")
97+
{
98+
let _ = std::io::Write::write_all(&mut file, payload.to_string().as_bytes());
99+
let _ = std::io::Write::write_all(&mut file, b"\n");
100+
}
101+
}

crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use super::{
2424
consume_shared_runtime_module::ConsumeSharedRuntimeModule, create_lookup_key_for_sharing,
2525
strip_lookup_layer_prefix,
2626
};
27-
use crate::ShareScope;
27+
use crate::{ShareScope, debug_log};
2828

2929
#[cacheable]
3030
#[derive(Debug, Clone, Hash)]
@@ -359,6 +359,29 @@ impl ConsumeSharedPlugin {
359359
let required_version = self
360360
.get_required_version(context, request, config.clone(), add_diagnostic)
361361
.await;
362+
// #region agent log
363+
if config.share_key == "react" || config.layer.is_some() || config.issuer_layer.is_some() {
364+
debug_log(
365+
"H2",
366+
"consume_shared_plugin.rs:359",
367+
"create consume shared module",
368+
serde_json::json!({
369+
"request": request,
370+
"context": context.as_str(),
371+
"configRequest": config.request,
372+
"issuerLayer": config.issuer_layer,
373+
"layer": config.layer,
374+
"import": config.import,
375+
"importResolved": import_resolved.clone(),
376+
"shareKey": config.share_key,
377+
"shareScope": config.share_scope,
378+
"requiredVersion": required_version.as_ref().map(ToString::to_string),
379+
"singleton": config.singleton,
380+
"eager": config.eager,
381+
}),
382+
);
383+
}
384+
// #endregion
362385
ConsumeSharedModule::new(
363386
if direct_fallback {
364387
self.get_context()

crates/rspack_plugin_mf/src/sharing/consume_shared_runtime_module.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rspack_util::json_stringify_str;
1010
use rustc_hash::FxHashMap;
1111

1212
use super::consume_shared_plugin::ConsumeVersion;
13-
use crate::{ShareScope, utils::json_stringify};
13+
use crate::{ShareScope, debug_log, utils::json_stringify};
1414

1515
static CONSUMES_COMMON_TEMPLATE: &str = include_str!("./consumesCommon.ejs");
1616
static CONSUMES_INITIAL_TEMPLATE: &str = include_str!("./consumesInitial.ejs");
@@ -105,6 +105,28 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
105105
let fallback_source = code_gen
106106
.get(&SourceType::ConsumeShared)
107107
.map(|source| source.source().into_string_lossy().into_owned());
108+
// #region agent log
109+
if data.share_key == "react" || data.layer.is_some() {
110+
debug_log(
111+
"H6",
112+
"consume_shared_runtime_module.rs:104",
113+
"consume runtime mapping source",
114+
serde_json::json!({
115+
"moduleId": id,
116+
"shareKey": data.share_key,
117+
"shareScope": data.share_scope,
118+
"requiredVersion": data.required_version.as_ref().map(ToString::to_string),
119+
"strictVersion": data.strict_version,
120+
"singleton": data.singleton,
121+
"eager": data.eager,
122+
"layer": data.layer,
123+
"import": data.import,
124+
"hasFallbackFactory": data.fallback.is_some(),
125+
"hasFallbackSource": fallback_source.is_some(),
126+
}),
127+
);
128+
}
129+
// #endregion
108130
let share_scope_json = if enhanced {
109131
json_stringify(&data.share_scope)
110132
} else {

crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::{
2121
CodeGenerationDataShareInit, DataInitInfo, ProvideSharedInfo, ShareInitData,
2222
},
2323
};
24-
use crate::{ConsumeVersion, ShareScope};
24+
use crate::{ConsumeVersion, ShareScope, debug_log};
2525

2626
#[impl_source_map_config]
2727
#[cacheable]
@@ -216,6 +216,26 @@ impl Module for ProvideSharedModule {
216216
} else {
217217
runtime_template.async_module_factory(&self.get_blocks()[0], &self.request, compilation)
218218
};
219+
// #region agent log
220+
if self.name == "react" || self.layer.is_some() {
221+
debug_log(
222+
"H4",
223+
"provide_shared_module.rs:214",
224+
"provide shared module codegen",
225+
serde_json::json!({
226+
"request": self.request,
227+
"shareKey": self.name,
228+
"shareScope": self.share_scope,
229+
"version": self.version.to_string(),
230+
"eager": self.eager,
231+
"singleton": self.singleton,
232+
"requiredVersion": self.required_version.as_ref().map(ToString::to_string),
233+
"strictVersion": self.strict_version,
234+
"layer": self.layer,
235+
}),
236+
);
237+
}
238+
// #endregion
219239
code_generation_result
220240
.data
221241
.insert(CodeGenerationDataShareInit {

0 commit comments

Comments
 (0)