Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e9b18a

Browse files
committed
feat(rust): support advanced_chunks option
1 parent 723cf99 commit 3e9b18a

16 files changed

Lines changed: 277 additions & 18 deletions

File tree

crates/rolldown/src/chunk_graph.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use arcstr::ArcStr;
21
use oxc::index::{index_vec, IndexVec};
32
use rolldown_common::{Chunk, ChunkIdx, ChunkTable, ModuleIdx, ModuleTable};
43
use rustc_hash::FxHashMap;
@@ -10,7 +9,6 @@ pub struct ChunkGraph {
109
/// Module to chunk that contains the module
1110
pub module_to_chunk: IndexVec<ModuleIdx, Option<ChunkIdx>>,
1211
pub entry_module_to_entry_chunk: FxHashMap<ModuleIdx, ChunkIdx>,
13-
chunk_idx_by_name: FxHashMap<ArcStr, ChunkIdx>,
1412
}
1513

1614
impl ChunkGraph {
@@ -20,7 +18,6 @@ impl ChunkGraph {
2018
module_to_chunk: index_vec![None; module_table.modules.len()],
2119
sorted_chunk_idx_vec: Vec::new(),
2220
entry_module_to_entry_chunk: FxHashMap::default(),
23-
chunk_idx_by_name: FxHashMap::default(),
2421
}
2522
}
2623

@@ -30,16 +27,7 @@ impl ChunkGraph {
3027
}
3128

3229
pub fn add_chunk(&mut self, chunk: Chunk) -> ChunkIdx {
33-
let idx = self.chunk_table.push(chunk);
34-
let chunk = &self.chunk_table.chunks[idx];
35-
if let Some(name) = &chunk.name {
36-
debug_assert!(
37-
!self.chunk_idx_by_name.contains_key(name),
38-
"Should not have duplicate chunk name"
39-
);
40-
self.chunk_idx_by_name.insert(name.clone(), idx);
41-
}
42-
idx
30+
self.chunk_table.push(chunk)
4331
}
4432

4533
pub fn add_module_to_chunk(&mut self, module_idx: ModuleIdx, chunk_idx: ChunkIdx) {

crates/rolldown/src/stages/generate_stage/code_splitting.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::cmp::Ordering;
22

33
use crate::chunk_graph::ChunkGraph;
4+
use arcstr::ArcStr;
45
use itertools::Itertools;
56
use oxc::index::IndexVec;
67
use rolldown_common::{Chunk, ChunkIdx, ChunkKind, Module, ModuleIdx, OutputFormat};
@@ -12,7 +13,7 @@ use super::GenerateStage;
1213

1314
impl<'a> GenerateStage<'a> {
1415
#[tracing::instrument(level = "debug", skip_all)]
15-
pub fn generate_chunks(&mut self) -> ChunkGraph {
16+
pub async fn generate_chunks(&mut self) -> anyhow::Result<ChunkGraph> {
1617
if matches!(self.options.format, OutputFormat::Iife) {
1718
let user_defined_entry_count =
1819
self.link_output.entries.iter().filter(|entry| entry.kind.is_user_defined()).count();
@@ -63,18 +64,47 @@ impl<'a> GenerateStage<'a> {
6364
);
6465
});
6566

67+
let mut module_to_assigned: IndexVec<ModuleIdx, bool> =
68+
oxc::index::index_vec![false; self.link_output.module_table.modules.len()];
69+
6670
// 1. Assign modules to corresponding chunks
6771
// 2. Create shared chunks to store modules that belong to multiple chunks.
6872
for normal_module in self.link_output.module_table.modules.iter().filter_map(Module::as_ecma) {
6973
if !normal_module.is_included {
7074
continue;
7175
}
7276

77+
if module_to_assigned[normal_module.idx] {
78+
continue;
79+
}
80+
module_to_assigned[normal_module.idx] = true;
81+
7382
let bits = &module_to_bits[normal_module.idx];
7483
debug_assert!(
7584
!bits.is_empty(),
7685
"Empty bits means the module is not reachable, so it should bail out with `is_included: false` {:?}", normal_module.stable_id
7786
);
87+
88+
let mut manual_chunk_by_name = FxHashMap::default();
89+
90+
if let Some(manual_chunks) = &self.options.advanced_chunks {
91+
if let Some(matched) = (manual_chunks)(&normal_module.id).await? {
92+
let chunk_name = ArcStr::from(matched.name);
93+
if let Some(chunk_idx) = manual_chunk_by_name.get(&chunk_name).copied() {
94+
let chunk: &mut Chunk = &mut chunk_graph.chunk_table[chunk_idx];
95+
chunk.bits.union(bits);
96+
chunk_graph.add_module_to_chunk(normal_module.idx, chunk_idx);
97+
} else {
98+
let chunk =
99+
Chunk::new(Some(chunk_name.clone()), bits.clone(), vec![], ChunkKind::Common);
100+
let chunk_id = chunk_graph.add_chunk(chunk);
101+
chunk_graph.add_module_to_chunk(normal_module.idx, chunk_id);
102+
manual_chunk_by_name.insert(chunk_name, chunk_id);
103+
}
104+
continue;
105+
}
106+
}
107+
78108
if let Some(chunk_id) = bits_to_chunk.get(bits).copied() {
79109
chunk_graph.add_module_to_chunk(normal_module.idx, chunk_id);
80110
} else {
@@ -196,7 +226,7 @@ impl<'a> GenerateStage<'a> {
196226
chunk_graph.sorted_chunk_idx_vec = sorted_chunk_idx_vec;
197227
chunk_graph.entry_module_to_entry_chunk = entry_module_to_entry_chunk;
198228

199-
chunk_graph
229+
Ok(chunk_graph)
200230
}
201231

202232
fn determine_reachable_modules_for_entry(

crates/rolldown/src/stages/generate_stage/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl<'a> GenerateStage<'a> {
5555

5656
#[tracing::instrument(level = "debug", skip_all)]
5757
pub async fn generate(&mut self) -> Result<BundleOutput> {
58-
let mut chunk_graph = self.generate_chunks();
58+
let mut chunk_graph = self.generate_chunks().await?;
5959

6060
self.generate_chunk_name_and_preliminary_filenames(&mut chunk_graph).await?;
6161

crates/rolldown/src/utils/normalize_options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ pub fn normalize_options(mut raw_options: crate::BundlerOptions) -> NormalizeOpt
120120
extend: raw_options.extend.unwrap_or(false),
121121
external_live_bindings: raw_options.external_live_bindings.unwrap_or(true),
122122
inline_dynamic_imports: raw_options.inline_dynamic_imports.unwrap_or(false),
123+
advanced_chunks: raw_options.advanced_chunks,
123124
};
124125

125126
NormalizeOptionsReturn { options: normalized, resolve_options: raw_resolve }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"config": {
3+
"input": [
4+
{
5+
"name": "a",
6+
"import": "./a.js"
7+
},
8+
{
9+
"name": "b",
10+
"import": "./b.js"
11+
}
12+
],
13+
"advancedChunks": [
14+
{
15+
"filter": ".js",
16+
"name": "common"
17+
}
18+
]
19+
}
20+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const a = 'a'
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
source: crates/rolldown_testing/src/integration_test.rs
3+
---
4+
# Assets
5+
6+
## a.mjs
7+
8+
```js
9+
import { a } from "./a~1.mjs";
10+
11+
export { a };
12+
```
13+
## a~1.mjs
14+
15+
```js
16+
17+
//#region a.js
18+
const a = "a";
19+
20+
//#endregion
21+
export { a };
22+
```
23+
## b.mjs
24+
25+
```js
26+
import { b } from "./b~1.mjs";
27+
28+
export { b };
29+
```
30+
## b~1.mjs
31+
32+
```js
33+
34+
//#region b.js
35+
const b = "a";
36+
37+
//#endregion
38+
export { b };
39+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const b = 'a'

crates/rolldown/tests/snapshots/integration_rolldown__filename_with_hash.snap

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,6 +1454,13 @@ expression: "snapshot_outputs.join(\"\\n\")"
14541454
# tests/rolldown/errors/unresolved_entry
14551455

14561456

1457+
# tests/rolldown/function/advanced_chunks/basic
1458+
1459+
- a-!~{000}~.mjs => a-SUCnoi0A.mjs
1460+
- b-!~{001}~.mjs => b-SonL6YaA.mjs
1461+
- a~1-!~{002}~.mjs => a~1-tSH6rdht.mjs
1462+
- b~1-!~{004}~.mjs => b~1-xejoaD21.mjs
1463+
14571464
# tests/rolldown/function/define/node_env
14581465

14591466
- main-!~{000}~.mjs => main-zmFhpGWu.mjs

crates/rolldown_binding/src/utils/normalize_binding_options.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ pub fn normalize_binding_options(
181181
.map(|inner| inner.into_iter().map(normalize_binding_inject_import).collect()),
182182
external_live_bindings: output_options.external_live_bindings,
183183
inline_dynamic_imports: output_options.inline_dynamic_imports,
184+
advanced_chunks: None,
184185
};
185186

186187
#[cfg(not(target_family = "wasm"))]

0 commit comments

Comments
 (0)