From 75f20f78d19cd0ddcbb143c55fb0703aa430a973 Mon Sep 17 00:00:00 2001 From: xusd320 Date: Mon, 22 Jul 2024 18:59:52 +0800 Subject: [PATCH] fix: not deasync small async chunk --- crates/mako/src/generate/chunk_graph.rs | 46 +--------------- crates/mako/src/generate/optimize_chunk.rs | 55 ------------------- .../config.babel-plugin-import/expect.js | 2 +- e2e/fixtures.umi/config.define/expect.js | 2 +- .../config.flex-bugs.default.true/expect.js | 2 +- e2e/fixtures.umi/config.less.math/expect.js | 2 +- .../config.less.modifyVars/expect.js | 2 +- .../config.less.plugins/expect.js | 2 +- e2e/fixtures/code-splitting.complex/expect.js | 6 -- packages/mako/binding.d.ts | 4 -- 10 files changed, 7 insertions(+), 116 deletions(-) diff --git a/crates/mako/src/generate/chunk_graph.rs b/crates/mako/src/generate/chunk_graph.rs index 4a2b61889..9193287fa 100644 --- a/crates/mako/src/generate/chunk_graph.rs +++ b/crates/mako/src/generate/chunk_graph.rs @@ -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; @@ -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::>>(); - 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::>(); - - for node in isolated_nodes { - self.graph.add_edge(*from, node, ()); - } - } } impl Default for ChunkGraph { diff --git a/crates/mako/src/generate/optimize_chunk.rs b/crates/mako/src/generate/optimize_chunk.rs index 1d4b7b0af..5dbc1b9c7 100644 --- a/crates/mako/src/generate/optimize_chunk.rs +++ b/crates/mako/src/generate/optimize_chunk.rs @@ -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); @@ -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::>(), - )); - } - } - } - 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, diff --git a/e2e/fixtures.umi/config.babel-plugin-import/expect.js b/e2e/fixtures.umi/config.babel-plugin-import/expect.js index 01da2f212..07de51954 100644 --- a/e2e/fixtures.umi/config.babel-plugin-import/expect.js +++ b/e2e/fixtures.umi/config.babel-plugin-import/expect.js @@ -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, diff --git a/e2e/fixtures.umi/config.define/expect.js b/e2e/fixtures.umi/config.define/expect.js index 4d6b44df0..f2a598c99 100644 --- a/e2e/fixtures.umi/config.define/expect.js +++ b/e2e/fixtures.umi/config.define/expect.js @@ -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"); diff --git a/e2e/fixtures.umi/config.flex-bugs.default.true/expect.js b/e2e/fixtures.umi/config.flex-bugs.default.true/expect.js index 79e5ccb6a..8db6eff6c 100644 --- a/e2e/fixtures.umi/config.flex-bugs.default.true/expect.js +++ b/e2e/fixtures.umi/config.flex-bugs.default.true/expect.js @@ -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"); diff --git a/e2e/fixtures.umi/config.less.math/expect.js b/e2e/fixtures.umi/config.less.math/expect.js index 34431f959..13c30e063 100644 --- a/e2e/fixtures.umi/config.less.math/expect.js +++ b/e2e/fixtures.umi/config.less.math/expect.js @@ -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"); diff --git a/e2e/fixtures.umi/config.less.modifyVars/expect.js b/e2e/fixtures.umi/config.less.modifyVars/expect.js index ca4f2f9ab..4274c75d2 100644 --- a/e2e/fixtures.umi/config.less.modifyVars/expect.js +++ b/e2e/fixtures.umi/config.less.modifyVars/expect.js @@ -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"); diff --git a/e2e/fixtures.umi/config.less.plugins/expect.js b/e2e/fixtures.umi/config.less.plugins/expect.js index 38873a491..c3d25c294 100644 --- a/e2e/fixtures.umi/config.less.plugins/expect.js +++ b/e2e/fixtures.umi/config.less.plugins/expect.js @@ -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"); diff --git a/e2e/fixtures/code-splitting.complex/expect.js b/e2e/fixtures/code-splitting.complex/expect.js index 2789a68fc..250f30bce 100644 --- a/e2e/fixtures/code-splitting.complex/expect.js +++ b/e2e/fixtures/code-splitting.complex/expect.js @@ -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\\(\\[${ diff --git a/packages/mako/binding.d.ts b/packages/mako/binding.d.ts index 969219c72..2f8916ca1 100644 --- a/packages/mako/binding.d.ts +++ b/packages/mako/binding.d.ts @@ -3,10 +3,6 @@ /* auto-generated by NAPI-RS */ -export interface TransformOutput { - code: string; - map?: string; -} export interface JsHooks { name?: string; load?: (