Skip to content

Commit a3f673a

Browse files
cursoragenthardfist
andcommitted
feat: Add getChunkGroupBlocks to ChunkGraph API
This commit adds a new method to the ChunkGraph API that allows retrieving all async dependency blocks associated with a given chunk group. This enhances the ability to inspect and manipulate the chunk graph. Co-authored-by: yangjianzju <yangjianzju@gmail.com>
1 parent a9e97ca commit a3f673a

File tree

7 files changed

+83
-3
lines changed

7 files changed

+83
-3
lines changed

crates/node_binding/napi-binding.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ export declare class ChunkGraph {
150150
getModuleId(module: Module): string | number | null
151151
_getModuleHash(module: Module, runtime: string | string[] | undefined): string | null
152152
getBlockChunkGroup(jsBlock: AsyncDependenciesBlock): ChunkGroup | null
153+
getChunkGroupBlocks(chunkGroup: ChunkGroup): AsyncDependenciesBlock[]
153154
}
154155

155156
export declare class ChunkGroup {

crates/rspack_binding_api/src/chunk_graph.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use napi_derive::napi;
55
use rspack_core::{Compilation, ModuleId, SourceType};
66

77
use crate::{
8-
async_dependency_block::AsyncDependenciesBlock,
8+
async_dependency_block::{AsyncDependenciesBlock, AsyncDependenciesBlockWrapper},
99
chunk::{Chunk, ChunkWrapper},
10-
chunk_group::ChunkGroupWrapper,
10+
chunk_group::{ChunkGroup, ChunkGroupWrapper},
1111
module::{ModuleObject, ModuleObjectRef},
1212
runtime::JsRuntimeSpec,
1313
};
@@ -210,4 +210,29 @@ impl ChunkGraph {
210210
.map(|chunk_group| ChunkGroupWrapper::new(chunk_group.ukey, compilation)),
211211
)
212212
}
213+
214+
#[napi(
215+
ts_args_type = "chunkGroup: ChunkGroup",
216+
ts_return_type = "AsyncDependenciesBlock[]"
217+
)]
218+
pub fn get_chunk_group_blocks(
219+
&self,
220+
chunk_group: &ChunkGroup,
221+
) -> napi::Result<Vec<AsyncDependenciesBlockWrapper>> {
222+
let compilation = self.as_ref()?;
223+
let module_graph = compilation.get_module_graph();
224+
225+
Ok(
226+
compilation
227+
.chunk_graph
228+
.get_chunk_group_blocks(chunk_group.chunk_group_ukey)
229+
.into_iter()
230+
.filter_map(|block_id| {
231+
module_graph
232+
.block_by_id(&block_id)
233+
.map(|block| AsyncDependenciesBlockWrapper::new(block, compilation))
234+
})
235+
.collect(),
236+
)
237+
}
213238
}

crates/rspack_binding_api/src/chunk_group.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414

1515
#[napi]
1616
pub struct ChunkGroup {
17-
chunk_group_ukey: rspack_core::ChunkGroupUkey,
17+
pub(crate) chunk_group_ukey: rspack_core::ChunkGroupUkey,
1818
compilation: NonNull<Compilation>,
1919
}
2020

crates/rspack_core/src/chunk_graph/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ impl ChunkGraph {
3131
let cgm = self.expect_chunk_graph_module(*module_id);
3232
!cgm.entry_in_chunks.is_empty()
3333
}
34+
35+
pub fn get_chunk_group_blocks(
36+
&self,
37+
chunk_group_ukey: ChunkGroupUkey,
38+
) -> Vec<AsyncDependenciesBlockIdentifier> {
39+
self
40+
.block_to_chunk_group_ukey
41+
.iter()
42+
.filter_map(|(block_id, group_ukey)| {
43+
if *group_ukey == chunk_group_ukey {
44+
Some(*block_id)
45+
} else {
46+
None
47+
}
48+
})
49+
.collect()
50+
}
3451
}
3552
static INDENT: &str = " ";
3653

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "foo";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import(/* webpackChunkName: "foo-blocks" */ "./foo");
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Plugin {
2+
apply(compiler) {
3+
compiler.hooks.compilation.tap("Test", compilation => {
4+
compilation.hooks.processAssets.tap("Test", () => {
5+
const entry = compilation.entries.get("main");
6+
const entryDependency = entry.dependencies[0];
7+
const entryModule = compilation.moduleGraph.getModule(entryDependency);
8+
const block = entryModule.blocks[0];
9+
const chunkGroup = compilation.chunkGraph.getBlockChunkGroup(block);
10+
11+
expect(chunkGroup.name).toBe("foo-blocks");
12+
13+
const blocks = compilation.chunkGraph.getChunkGroupBlocks(chunkGroup);
14+
expect(blocks.length).toBe(1);
15+
expect(blocks[0]).toBe(block);
16+
});
17+
});
18+
}
19+
}
20+
21+
/** @type {import("@rspack/core").Configuration} */
22+
module.exports = {
23+
target: "web",
24+
node: false,
25+
entry: {
26+
main: "./index.js"
27+
},
28+
output: {
29+
filename: "[name].js"
30+
},
31+
optimization: {
32+
sideEffects: false
33+
},
34+
plugins: [new Plugin()]
35+
};

0 commit comments

Comments
 (0)