Skip to content

Commit

Permalink
feat: add chunk matcher for mf
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Feb 20, 2025
1 parent 666455c commit fd67d72
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions crates/mako/src/generate/chunk_pot/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ pub(crate) fn runtime_code(context: &Arc<Context>) -> Result<String> {
let chunk_graph = context.chunk_graph.read().unwrap();
let has_dynamic_chunks = chunk_graph.get_all_chunks().len() > 1;
let has_hmr = context.args.watch;
let chunk_matcher = context.config.module_federation.as_ref().and_then(|mf| {
mf.remotes.as_ref().and_then(|remotes| {
if remotes.is_empty() {
None
} else {
Some(r#"/^mako\/container\/remote\//"#.to_string())
}
})
});
let app_runtime = AppRuntimeTemplate {
has_dynamic_chunks,
has_hmr,
Expand All @@ -127,6 +136,7 @@ pub(crate) fn runtime_code(context: &Arc<Context>) -> Result<String> {
.as_ref()
.map_or(false, |o| o.concatenate_modules.unwrap_or(false)),
global_module_registry: context.config.output.global_module_registry,
chunk_matcher,
};
let app_runtime = app_runtime.render_once()?;
let app_runtime = app_runtime.replace(
Expand Down
1 change: 1 addition & 0 deletions crates/mako/src/generate/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ pub struct AppRuntimeTemplate {
pub concatenate_enabled: bool,
pub cross_origin_loading: Option<String>,
pub global_module_registry: bool,
pub chunk_matcher: Option<String>,
}
7 changes: 5 additions & 2 deletions crates/mako/templates/app_runtime.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function createRuntime(makoModules, entryModuleId, global) {
// required modules
requireModule.m = makoModules;

// required modules
// modules registry
requireModule.c = modulesRegistry;

<% if concatenate_enabled { %>
Expand Down Expand Up @@ -136,7 +136,10 @@ function createRuntime(makoModules, entryModuleId, global) {
if (data === 0) return;

// skip federation remote chunk
if (chunkId.startsWith("mako/container/remote/")) return;

<% if chunk_matcher.is_some() { %>
if (<%= chunk_matcher.unwrap() %>.test(chunkId)) return
<% } %>

if (data) {
// 0 1 2
Expand Down

0 comments on commit fd67d72

Please sign in to comment.