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: do not split root module for async chunks #929

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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: 19 additions & 4 deletions crates/mako/src/optimize_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,32 @@ impl Compiler {
) {
let chunk_graph = self.context.chunk_graph.read().unwrap();
let chunks = chunk_graph.get_all_chunks();
let async_chunk_root_modules = chunks
.iter()
.filter_map(|chunk| match chunk.chunk_type {
ChunkType::Async => chunk.modules.last(),
_ => None,
})
.collect::<Vec<_>>();
let modules_in_chunk = match modules_in_chunk {
Some(modules_in_chunk) => modules_in_chunk,
None => chunks.iter().fold(vec![], |mut acc, chunk| {
acc.append(
&mut chunk
.modules
.iter()
.filter_map(|m| match &chunk.chunk_type {
// entry module of entry chunk should not be optimized
ChunkType::Entry(entry_id, _, false) if m.id == entry_id.id => None,
_ => Some((m, &chunk.id, &chunk.chunk_type)),
.filter_map(|m| {
match (&chunk.chunk_type, async_chunk_root_modules.contains(&m)) {
// async chunk root module should not be optimized
(_, true) => None,
// entry module of entry chunk should not be optimized
(ChunkType::Entry(entry_id, _, false), _)
if m.id == entry_id.id =>
{
None
}
_ => Some((m, &chunk.id, &chunk.chunk_type)),
}
})
.collect::<Vec<_>>(),
);
Expand Down
16 changes: 3 additions & 13 deletions e2e/fixtures/code-splitting.complex/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,10 @@ assert.match(
"should ensure splitting dependent chunks on demand (not-full)"
);

assert.match(
assert.doesNotMatch(
files["common-async.js"],
moduleReg("src/should-be-common.ts", "tooLongText"),
"should merge common async chunk to common chunk"
);

assert.match(
files["index.js"].replace(/\s/g, ""),
new RegExp(`Promise.all\\(\\[${
[
"common"
].map((f) => `__mako_require__.ensure\\("${f}"\\)`).join(",")
}\\]\\).then\\(__mako_require__.bind\\(__mako_require__,"src\\/should-be-common.ts"\\)\\)`),
"should load merged common async chunk with common chunk and without self"
moduleReg("src/should-not-be-common.ts", "tooLongText"),
"should not merge common async chunk to common chunk"
);

assert(
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures/code-splitting.complex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import Button from 'antd/es/button';
console.log(React, Button);

import('./should-be-split').then((m) => console.log(m));
import('./should-be-common').then((m) => console.log(m));
import('./should-not-be-common').then((m) => console.log(m));
import('./should-be-merged').then((m) => console.log(m));
import('./other-dynamic').then((m) => console.log(m));
2 changes: 1 addition & 1 deletion e2e/fixtures/code-splitting.complex/src/should-be-split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import * as icons from '@ant-design/icons';
import context from './context';
import common from './common';
// make sure `should-be-split-self` has 2+ parents
import other from './should-be-common';
import other from './should-not-be-common';

console.log(React, ReactDOM, less, overlay, refresh, antd, icons, context, common, other);

Expand Down
Loading