Skip to content

Commit

Permalink
fix: not deasync small async chunk (#1435)
Browse files Browse the repository at this point in the history
  • Loading branch information
xusd320 authored Jul 22, 2024
1 parent 3f20b78 commit e8a1748
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 116 deletions.
46 changes: 1 addition & 45 deletions crates/mako/src/generate/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
use std::hash::Hasher;

use petgraph::stable_graph::{DefaultIx, NodeIndex, StableDiGraph};
use petgraph::visit::{Dfs, EdgeRef};
use petgraph::visit::Dfs;
use petgraph::Direction;
use twox_hash::XxHash64;

Expand Down Expand Up @@ -192,50 +192,6 @@ impl ChunkGraph {
let idx = self.id_index_map.remove(chunk_id).unwrap();
self.graph.remove_node(idx);
}

pub fn merge_to_chunk(&mut self, chunk_id: &ChunkId, from: &ChunkId) {
let idx = self.id_index_map.remove(chunk_id);
let from_idx = self.id_index_map.get(from);
if let (Some(idx), Some(from_idx)) = (idx, from_idx) {
let outgoing_nodes = self
.graph
.neighbors_directed(idx, Direction::Outgoing)
.collect::<Vec<NodeIndex<DefaultIx>>>();
self.graph.remove_node(idx);
for node in outgoing_nodes {
if !self
.graph
.edges_directed(*from_idx, Direction::Outgoing)
.any(|e| e.target() == node)
{
self.graph.add_edge(*from_idx, node, ());
}
}
}
}

pub fn connect_isolated_nodes_to_chunk(&mut self, chunk_id: &ChunkId) {
let from = self.id_index_map.get(chunk_id).unwrap();
let isolated_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 isolated_nodes {
self.graph.add_edge(*from, node, ());
}
}
}

impl Default for ChunkGraph {
Expand Down
55 changes: 0 additions & 55 deletions crates/mako/src/generate/optimize_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ impl Compiler {

optimize_chunks_infos.sort_by_key(|o| -o.group_options.priority);

// stage: deasync
self.merge_minimal_async_chunks(&optimize_options);

// stage: modules
self.module_to_optimize_infos(&mut optimize_chunks_infos, None);

Expand Down Expand Up @@ -117,58 +114,6 @@ impl Compiler {
}
}

fn merge_minimal_async_chunks(&self, options: &CodeSplittingAdvancedOptions) {
let mut async_to_entry = vec![];
let chunk_graph = self.context.chunk_graph.read().unwrap();
let chunks = chunk_graph.get_all_chunks();

// find minimal async chunks to merge to entry chunk
// TODO: continue to merge deep-level async chunk
for chunk in chunks {
if chunk.chunk_type == ChunkType::Async && self.get_chunk_size(chunk) < options.min_size
{
let entry_ids = chunk_graph.entry_dependents_chunk(&chunk.id);

// merge if there is only one entry chunk
// TODO: don't merge if entry chunk size is greater than max_size
if entry_ids.len() == 1 {
async_to_entry.push((
chunk.id.clone(),
entry_ids[0].clone(),
chunk.modules.iter().cloned().collect::<Vec<_>>(),
));
}
}
}
drop(chunk_graph);

// update chunk_graph
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.iter() {
let entry_chunk: &mut Chunk = chunk_graph.mut_chunk(entry_chunk_id).unwrap();

// merge modules to entry chunk
for m in chunk_modules {
entry_chunk.add_module(m.clone());
merged_modules.push(m);
}

// remove original async chunks
chunk_graph.merge_to_chunk(chunk_id, 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));
}
}
}

fn module_to_optimize_infos<'a>(
&'a self,
optimize_chunks_infos: &'a mut Vec<OptimizeChunksInfo>,
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures.umi/config.babel-plugin-import/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

const content = files["umi.js"];
const content = files["pages_index_tsx-async.js"];

assert.doesNotMatch(
content,
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures.umi/config.define/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

let content = files["umi.js"];
let content = files["pages_index_tsx-async.js"];
content = content.replace(/\s/g, "");

assert(content.includes("\"production\""), "support process.env.NODE_ENV");
Expand Down
2 changes: 1 addition & 1 deletion e2e/fixtures.umi/config.flex-bugs.default.true/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ const assert = require("assert");
const { parseBuildResult, trim } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

let content = files["umi.css"];
let content = files["pages_index_tsx-async.css"];

assert(content.includes(`flex: 1 1;`), "should enable flexBugs by default");
2 changes: 1 addition & 1 deletion e2e/fixtures.umi/config.less.math/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

let content = files["umi.css"];
let content = files["pages_index_tsx-async.css"];
content = content.replace(/\s/g, "");

assert(content.includes(`left:24px`), "should support math");
2 changes: 1 addition & 1 deletion e2e/fixtures.umi/config.less.modifyVars/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

let content = files["umi.css"];
let content = files["pages_index_tsx-async.css"];
content = content.replace(/\s/g, "");

assert(content.includes(`color:blue;`), "should prefer less.modifyVars than config.theme");
2 changes: 1 addition & 1 deletion e2e/fixtures.umi/config.less.plugins/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const assert = require("assert");
const { parseBuildResult, moduleReg } = require("../../../scripts/test-utils");
const { files } = parseBuildResult(__dirname);

let content = files["umi.css"];
let content = files["pages_index_tsx-async.css"];
content = content.replace(/\s/g, "");

assert(content.includes(`height:1.1px;`), "less-plugin-clean-css should work");
6 changes: 0 additions & 6 deletions e2e/fixtures/code-splitting.complex/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ assert(
"big vendors should be split again"
);

assert(
files["index.js"].includes("\"src/context.ts\":")
&& !files["src_should-be-split_ts-async.js"].includes("\"src/context.ts\":"),
"async chunk should reuse modules that already merged into entry with another minimal async chunk"
);

assert.match(
files["index.js"].replace(/\s/g, ""),
new RegExp(`Promise.all\\(\\[${
Expand Down
4 changes: 0 additions & 4 deletions packages/mako/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@

/* auto-generated by NAPI-RS */

export interface TransformOutput {
code: string;
map?: string;
}
export interface JsHooks {
name?: string;
load?: (
Expand Down

0 comments on commit e8a1748

Please sign in to comment.