Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: isolated chunk miss connection to chunksIdToUrlMap #988

Merged
merged 7 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions crates/mako/src/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,29 @@ impl ChunkGraph {
let idx = self.id_index_map.remove(chunk_id).unwrap();
self.graph.remove_node(idx);
}

pub fn connect_vacant_nodes_to_entry_chunk(&mut self, entry_chunk_id: &ChunkId) {
Jinbao1001 marked this conversation as resolved.
Show resolved Hide resolved
let vacant_nodes = self
.graph
.node_indices()
.filter(|node| {
self.graph
.edges_directed(*node, Direction::Outgoing)
.count()
== 0
&& self
.graph
.edges_directed(*node, Direction::Incoming)
.count()
== 0
})
.collect::<Vec<_>>();

for node in vacant_nodes {
let to = self.id_index_map.get(entry_chunk_id).unwrap();
self.graph.add_edge(*to, node, ());
Jinbao1001 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

impl Default for ChunkGraph {
Expand Down
14 changes: 10 additions & 4 deletions crates/mako/src/optimize_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ impl Compiler {
let mut chunk_graph = self.context.chunk_graph.write().unwrap();
let mut merged_modules = vec![];

for (chunk_id, entry_chunk_id, chunk_modules) in async_to_entry.clone() {
let entry_chunk = chunk_graph.mut_chunk(&entry_chunk_id).unwrap();
for (index, (chunk_id, entry_chunk_id, chunk_modules)) in async_to_entry.iter().enumerate()
{
let entry_chunk: &mut Chunk = chunk_graph.mut_chunk(entry_chunk_id).unwrap();

// merge modules to entry chunk
for m in chunk_modules {
Expand All @@ -165,15 +166,20 @@ impl Compiler {
}

// remove original async chunks
chunk_graph.remove_chunk(&chunk_id);
chunk_graph.remove_chunk(chunk_id);

// connect that has be optimized chunk dependents to the entry chunk
if index == async_to_entry.len() - 1 {
chunk_graph.connect_vacant_nodes_to_entry_chunk(entry_chunk_id);
}
}

// remove merged modules from other async chunks
let mut chunks = chunk_graph.mut_chunks();

for chunk in chunks.iter_mut() {
if chunk.chunk_type == ChunkType::Async {
chunk.modules.retain(|m| !merged_modules.contains(m));
chunk.modules.retain(|m| !merged_modules.contains(&m));
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions crates/node/src/js_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ impl Plugin for JsPlugin {
let (tx, rx) = mpsc::channel::<napi::Result<Option<LoadResult>>>();
hook.call(
ReadMessage {
// TODO: 感觉这里应该发 param.file.path
message: param.file.pathname.to_string_lossy().to_string(),
message: param.file.path.to_string_lossy().to_string(),
tx,
},
threadsafe_function::ThreadsafeFunctionCallMode::Blocking,
Expand Down
4 changes: 4 additions & 0 deletions e2e/fixtures/code-splitting.complex/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ assert(
"minimal async chunk should be merged"
);

assert(
files["index.js"].includes("src_vacant_ts-async.js"),
"vacant dependences should connect to chunksIdToUrlMap"
)
assert(
Object.keys(files).every((f) => !f.includes("_isNumeric_js")),
"empty chunk should be removed"
Expand Down
3 changes: 2 additions & 1 deletion e2e/fixtures/code-splitting.complex/src/should-be-merged.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import context from './context';

console.log(React, context);
const vacant = React.lazy(() => import('./vacant'));
console.log(React, context, vacant);

export default 1;
2 changes: 2 additions & 0 deletions e2e/fixtures/code-splitting.complex/src/vacant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import React from 'react';
export default React.createElement("button", null, "inline")
Loading