Skip to content

Commit 58a6af6

Browse files
authored
refactor: code splitter (#8823)
* refactor: code splitting * chore: update
1 parent 82b09c3 commit 58a6af6

File tree

35 files changed

+3372
-41
lines changed

35 files changed

+3372
-41
lines changed

crates/node_binding/binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,7 @@ export interface RawExperiments {
13931393
layers: boolean
13941394
topLevelAwait: boolean
13951395
incremental?: false | { [key: string]: boolean }
1396+
parallelCodeSplitting: boolean
13961397
rspackFuture?: RawRspackFuture
13971398
cache: boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }
13981399
}

crates/rspack/src/builder/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2686,6 +2686,7 @@ pub struct ExperimentsBuilder {
26862686
output_module: Option<bool>,
26872687
future_defaults: Option<bool>,
26882688
css: Option<bool>,
2689+
parallel_code_splitting: Option<bool>,
26892690
async_web_assembly: Option<bool>,
26902691
// TODO: lazy compilation
26912692
}
@@ -2701,6 +2702,7 @@ impl From<&mut ExperimentsBuilder> for ExperimentsBuilder {
27012702
output_module: value.output_module.take(),
27022703
future_defaults: value.future_defaults.take(),
27032704
css: value.css.take(),
2705+
parallel_code_splitting: value.parallel_code_splitting.take(),
27042706
async_web_assembly: value.async_web_assembly.take(),
27052707
}
27062708
}
@@ -2742,6 +2744,11 @@ impl ExperimentsBuilder {
27422744
self
27432745
}
27442746

2747+
pub fn parallel_code_splitting(&mut self, parallel_code_splitting: bool) -> &mut Self {
2748+
self.parallel_code_splitting = Some(parallel_code_splitting);
2749+
self
2750+
}
2751+
27452752
pub fn build(
27462753
&mut self,
27472754
_builder_context: &mut BuilderContext,
@@ -2772,11 +2779,14 @@ impl ExperimentsBuilder {
27722779
w!(self.async_web_assembly, *future_defaults);
27732780
w!(self.output_module, false);
27742781

2782+
let parallel_code_splitting = d!(self.parallel_code_splitting, false);
2783+
27752784
Experiments {
27762785
layers,
27772786
incremental,
27782787
top_level_await,
27792788
rspack_future,
2789+
parallel_code_splitting,
27802790
cache,
27812791
}
27822792
}

crates/rspack_binding_values/src/raw_options/raw_experiments/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub struct RawExperiments {
1717
pub top_level_await: bool,
1818
#[napi(ts_type = "false | { [key: string]: boolean }")]
1919
pub incremental: Option<WithFalse<RawIncremental>>,
20+
pub parallel_code_splitting: bool,
2021
pub rspack_future: Option<RawRspackFuture>,
2122
#[napi(
2223
ts_type = r#"boolean | { type: "persistent" } & RawExperimentCacheOptionsPersistent | { type: "memory" }"#
@@ -34,6 +35,7 @@ impl From<RawExperiments> for Experiments {
3435
},
3536
None => IncrementalPasses::empty(),
3637
},
38+
parallel_code_splitting: value.parallel_code_splitting,
3739
layers: value.layers,
3840
top_level_await: value.top_level_await,
3941
rspack_future: value.rspack_future.unwrap_or_default().into(),

crates/rspack_core/src/build_chunk_graph/code_splitter.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
659659
assign_depths(
660660
&mut assign_depths_map,
661661
&compilation.get_module_graph(),
662-
modules.iter().collect(),
662+
modules.iter(),
663663
);
664664
input_entrypoints_and_modules.insert(entry_point, modules);
665665
}
@@ -1559,7 +1559,11 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on
15591559
return modules.clone();
15601560
}
15611561

1562-
self.extract_block_modules(module.get_root_block(compilation), runtime, compilation);
1562+
self.extract_block_modules(
1563+
module.get_root_block(&compilation.get_module_graph()),
1564+
runtime,
1565+
compilation,
1566+
);
15631567
self
15641568
.block_modules_runtime_map
15651569
.get::<OptionalRuntimeSpec>(&runtime.cloned().into())
@@ -1952,11 +1956,10 @@ pub(crate) enum DependenciesBlockIdentifier {
19521956
}
19531957

19541958
impl DependenciesBlockIdentifier {
1955-
pub fn get_root_block<'a>(&'a self, compilation: &'a Compilation) -> ModuleIdentifier {
1959+
pub fn get_root_block<'a>(&'a self, module_graph: &'a ModuleGraph) -> ModuleIdentifier {
19561960
match self {
19571961
DependenciesBlockIdentifier::Module(m) => *m,
1958-
DependenciesBlockIdentifier::AsyncDependenciesBlock(id) => *compilation
1959-
.get_module_graph()
1962+
DependenciesBlockIdentifier::AsyncDependenciesBlock(id) => *module_graph
19601963
.block_by_id(id)
19611964
.expect("should have block")
19621965
.parent(),

crates/rspack_core/src/build_chunk_graph/incremental.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl CodeSplitter {
129129
continue;
130130
};
131131

132-
parent_cg.children.remove(&chunk_group_ukey);
132+
parent_cg.children.swap_remove_full(&chunk_group_ukey);
133133

134134
if let Some(parent_cgi) = self.chunk_group_info_map.get(parent) {
135135
if let Some(parent_cgi) = self.chunk_group_infos.get_mut(parent_cgi) {

crates/rspack_core/src/build_chunk_graph/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{incremental::IncrementalPasses, Compilation};
77

88
pub(crate) mod code_splitter;
99
pub(crate) mod incremental;
10+
pub(crate) mod new_code_splitter;
1011

1112
#[instrument("Compilation:build_chunk_graph", skip_all)]
1213
pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::Result<()> {
@@ -46,3 +47,9 @@ pub(crate) fn build_chunk_graph(compilation: &mut Compilation) -> rspack_error::
4647

4748
Ok(())
4849
}
50+
51+
#[instrument(skip_all)]
52+
pub(crate) fn build_chunk_graph_new(compilation: &mut Compilation) -> rspack_error::Result<()> {
53+
new_code_splitter::code_split(compilation)?;
54+
Ok(())
55+
}

0 commit comments

Comments
 (0)