Skip to content

Commit

Permalink
fix: not generate chunk for mf remote module
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 committed Jan 24, 2025
1 parent 2ac48bc commit 1b8b9d9
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/mako/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::ast::file::{Content, File, JsContent};
use crate::ast::utils::get_module_system;
use crate::compiler::{Compiler, Context};
use crate::generate::chunk_pot::util::hash_hashmap;
use crate::module::{FedereationModuleType, Module, ModuleAst, ModuleId, ModuleInfo};
use crate::module::{FedereationModuleType, Module, ModuleAst, ModuleId, ModuleInfo, ModuleSystem};
use crate::plugin::NextBuildParam;
use crate::resolve::{ConsumeSharedInfo, RemoteInfo, ResolverResource};
use crate::utils::thread_pool;
Expand Down Expand Up @@ -376,7 +376,7 @@ __mako_require__.loadScript('{}', (e) => e.type === 'load' ? resolve() : reject(
deps: consume_share_info.deps.clone(),
resolved_resource: Some(ResolverResource::Shared(consume_share_info.clone())),
federation: Some(FedereationModuleType::ConsumeShare),
module_system: crate::module::ModuleSystem::Custom,
module_system: ModuleSystem::Custom,
..Default::default()
}),
side_effects: true,
Expand Down
4 changes: 4 additions & 0 deletions crates/mako/src/generate/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ impl Chunk {
self.modules.contains(module_id)
}

pub fn root_module(&self) -> Option<&ModuleId> {
self.modules.iter().last()
}

pub fn hash(&self, mg: &ModuleGraph) -> u64 {
let mut sorted_module_ids = self.modules.iter().cloned().collect::<Vec<ModuleId>>();
sorted_module_ids.sort_by_key(|m| m.id.clone());
Expand Down
23 changes: 21 additions & 2 deletions crates/mako/src/generate/generate_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,19 @@ type ChunksHashReplacer = HashMap<String, String>;

impl Compiler {
pub fn generate_chunk_files(&self, hmr_hash: u64) -> Result<Vec<ChunkFile>> {
crate::mako_profile_function!();
let module_graph = self.context.module_graph.read().unwrap();
let chunk_graph = self.context.chunk_graph.read().unwrap();
let chunks = chunk_graph.get_chunks();

let chunks: Vec<&Chunk> = chunk_graph
.get_chunks()
.into_iter()
.filter(|c| {
!module_graph
.get_module(c.root_module().unwrap())
.unwrap()
.is_remote()
})
.collect();

let (entry_chunks, normal_chunks): (Vec<&Chunk>, Vec<&Chunk>) = chunks
.into_iter()
Expand Down Expand Up @@ -173,6 +183,15 @@ impl Compiler {
let descendant_chunk = chunk_graph.chunk(descendant_chunk_id).unwrap();
// TODO: maybe we can split chunks to chunk pots before generate, because normal chunks will be
// split here and fn generate_normal_chunk_files twice
//
if module_graph
.get_module(descendant_chunk.root_module().unwrap())
.unwrap()
.is_remote()
{
return (acc_js, acc_css);
}

let chunk_pot =
ChunkPot::from(descendant_chunk, &module_graph, &context);

Expand Down
2 changes: 1 addition & 1 deletion crates/mako/src/generate/optimize_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl Compiler {
let async_chunk_root_modules = chunks
.iter()
.filter_map(|chunk| match chunk.chunk_type {
ChunkType::Async => chunk.modules.iter().last(),
ChunkType::Async => chunk.root_module(),
_ => None,
})
.collect::<Vec<_>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use super::{
ModuleFederationPlugin, FEDERATION_REMOTE_MODULE_PREFIX, FEDERATION_REMOTE_REFERENCE_PREFIX,
};
use crate::compiler::Context;
use crate::module::FedereationModuleType;
use crate::resolve::{RemoteInfo, ResolverResource};

impl ModuleFederationPlugin {
Expand All @@ -23,9 +22,7 @@ impl ModuleFederationPlugin {
all_chunks.iter().for_each(|c| {
c.modules.iter().for_each(|m| {
if let Some(m) = module_graph.get_module(m) {
if let Some(info) = m.info.as_ref()
&& let Some(FedereationModuleType::Remote) = info.federation.as_ref()
{
if m.is_remote() {
{
chunk_mapping
.entry(c.id.id.as_str())
Expand Down
6 changes: 2 additions & 4 deletions crates/mako/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,9 @@ impl Compiler {
ChunkType::Sync => chunk_graph
.dependents_chunk(&chunk.id)
.iter()
.filter_map(|chunk_id| {
chunk_graph.chunk(chunk_id).unwrap().modules.iter().last()
})
.filter_map(|chunk_id| chunk_graph.chunk(chunk_id).unwrap().root_module())
.collect::<Vec<_>>(),
_ => vec![chunk.modules.iter().last().unwrap()],
_ => vec![chunk.root_module().unwrap()],
};
let mut origins_set = IndexMap::new();
for origin_chunk_module in origin_chunk_modules {
Expand Down
3 changes: 3 additions & 0 deletions crates/mako/templates/app_runtime.stpl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ function createRuntime(makoModules, entryModuleId, global) {
var data = installedChunks[chunkId];
if (data === 0) return;

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

if (data) {
// 0 1 2
// [resolve, reject, promise]
Expand Down
3 changes: 2 additions & 1 deletion examples/module-federation/host/mako.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
"entry": {
"app1": "./src/index.ts"
},
"minify": false,
"moduleFederation": {
"name": "mfHost",
"remotes": {
"widget": "mfWidget@http://localhost:3000/mfWidget.js"
},
"shared": { "react": {}, "react-dom": {} },
"shared": { "react": { "eager": true }, "react-dom": { "eager": true } },
"implementation": "../../../../../packages/mako/node_modules/@module-federation/webpack-bundler-runtime"
},
"experimental": {
Expand Down
4 changes: 3 additions & 1 deletion examples/module-federation/host/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.0.1",
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
"react-dom": "18.2.0",
"@types/react": "18.2.0",
"@types/react-dom": "18.2.0"
}
}
15 changes: 11 additions & 4 deletions examples/module-federation/host/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react';
import Widget1 from 'widget/App1';
import Widget2 from 'widget/App2';
// import Widget1 from 'widget/App1';
// import Widget2 from 'widget/App2';

const Widget1 = React.lazy(() => import('widget/App1'));
const Widget2 = React.lazy(() => import('widget/App2'));

const App = () => {
return (
Expand All @@ -15,8 +18,12 @@ const App = () => {
>
<h1>Host App</h1>
</div>
<Widget1 />
<Widget2 />
<React.Suspense>
<Widget1 />
</React.Suspense>
<React.Suspense>
<Widget2 />
</React.Suspense>
</div>
);
};
Expand Down
2 changes: 1 addition & 1 deletion examples/module-federation/host/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import('./bootstrap');
import './bootstrap';
4 changes: 2 additions & 2 deletions examples/module-federation/widget/mako.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"entry": {
"app2": "./src/index.ts"
},
"minify": false,
"publicPath": "auto",
"moduleFederation": {
"name": "mfWidget",
Expand All @@ -25,8 +26,7 @@
"groups": [
{
"name": "vendor",
"allowChunks": "all",
"minSize": 1,
"allowChunks": "async",
"test": "node_modules"
}
]
Expand Down
4 changes: 3 additions & 1 deletion examples/module-federation/widget/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "0.0.1",
"dependencies": {
"react": "18.2.0",
"react-dom": "18.2.0"
"react-dom": "18.2.0",
"@types/react": "18.2.0",
"@types/react-dom": "18.2.0"
}
}

0 comments on commit 1b8b9d9

Please sign in to comment.