Skip to content

Commit

Permalink
add chunkgroup childrenIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Aug 6, 2024
1 parent 7beabab commit 09a0550
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
5 changes: 4 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ export function __chunk_graph_inner_get_chunk_modules_iterable_by_source_type(js

export function __chunk_graph_inner_get_module_id(module: string, compilation: JsCompilation): string | null

export function __chunk_group_inner_children_iterable(ukey: number, compilation: JsCompilation): Array<JsChunkGroup>

export function __chunk_group_inner_get_chunk_group(ukey: number, compilation: JsCompilation): JsChunkGroup

export function __chunk_group_inner_parents_iterable(ukey: number, compilation: JsCompilation): Array<JsChunkGroup>

export function __chunk_inner_can_be_initial(jsChunkUkey: number, compilation: JsCompilation): boolean

export function __chunk_inner_get_all_async_chunks(jsChunkUkey: number, compilation: JsCompilation): Array<JsChunk>
Expand Down Expand Up @@ -360,7 +364,6 @@ export interface JsChunkAssetArgs {
}

export interface JsChunkGroup {
__inner_parents: Array<number>
__inner_ukey: number
chunks: Array<JsChunk>
index?: number
Expand Down
25 changes: 22 additions & 3 deletions crates/rspack_binding_values/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::{JsChunk, JsCompilation};

#[napi(object)]
pub struct JsChunkGroup {
#[napi(js_name = "__inner_parents")]
pub inner_parents: Vec<u32>,
#[napi(js_name = "__inner_ukey")]
pub inner_ukey: u32,
pub chunks: Vec<JsChunk>,
Expand All @@ -27,7 +25,6 @@ impl JsChunkGroup {
.map(|k| JsChunk::from(compilation.chunk_by_ukey.expect_get(k)))
.collect(),
index: cg.index,
inner_parents: cg.parents.iter().map(|ukey| ukey.as_u32()).collect(),
inner_ukey: cg.ukey.as_u32(),
name: cg.name().map(|name| name.to_string()),
is_initial: cg.is_initial(),
Expand All @@ -47,6 +44,28 @@ pub fn get_chunk_group(ukey: u32, compilation: &JsCompilation) -> JsChunkGroup {
JsChunkGroup::from_chunk_group(cg, compilation)
}

#[napi(js_name = "__chunk_group_inner_parents_iterable")]
pub fn parents_iterable(ukey: u32, compilation: &JsCompilation) -> Vec<JsChunkGroup> {
let compilation = &compilation.0;
let cg = chunk_group(ukey, compilation);
cg.parents_iterable()
.map(|k| {
JsChunkGroup::from_chunk_group(compilation.chunk_group_by_ukey.expect_get(&k), compilation)
})
.collect()
}

#[napi(js_name = "__chunk_group_inner_children_iterable")]
pub fn children_iterable(ukey: u32, compilation: &JsCompilation) -> Vec<JsChunkGroup> {
let compilation = &compilation.0;
let cg = chunk_group(ukey, compilation);
cg.children_iterable()
.map(|k| {
JsChunkGroup::from_chunk_group(compilation.chunk_group_by_ukey.expect_get(&k), compilation)
})
.collect()
}

#[napi(js_name = "__entrypoint_inner_get_runtime_chunk")]
pub fn get_runtime_chunk(ukey: u32, compilation: &JsCompilation) -> JsChunk {
let compilation = &compilation.0;
Expand Down
6 changes: 5 additions & 1 deletion crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct ChunkGroup {
pub parents: UkeySet<ChunkGroupUkey>,
pub(crate) module_pre_order_indices: IdentifierMap<usize>,
pub(crate) module_post_order_indices: IdentifierMap<usize>,
pub(crate) children: UkeySet<ChunkGroupUkey>,
pub children: UkeySet<ChunkGroupUkey>,
async_entrypoints: UkeySet<ChunkGroupUkey>,
// ChunkGroupInfo
pub(crate) next_pre_order_index: usize,
Expand Down Expand Up @@ -85,6 +85,10 @@ impl ChunkGroup {
self.parents.iter()
}

pub fn children_iterable(&self) -> impl Iterator<Item = &ChunkGroupUkey> {
self.children.iter()
}

pub fn module_post_order_index(&self, module_identifier: &ModuleIdentifier) -> Option<usize> {
// A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module
self
Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -930,8 +930,12 @@ export class ChunkGroup {
// @internal
__internal__innerUkey(): number;
// (undocumented)
get childrenIterable(): Iterable<ChunkGroup>;
// (undocumented)
get chunks(): ReadonlyArray<Chunk>;
// (undocumented)
getChildren(): ReadonlyArray<ChunkGroup>;
// (undocumented)
getFiles(): ReadonlyArray<string>;
// (undocumented)
getParents(): ReadonlyArray<ChunkGroup>;
Expand Down
26 changes: 18 additions & 8 deletions packages/rspack/src/ChunkGroup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {
type JsChunkGroup,
type JsCompilation,
__chunk_group_inner_get_chunk_group
__chunk_group_inner_children_iterable,
__chunk_group_inner_get_chunk_group,
__chunk_group_inner_parents_iterable
} from "@rspack/binding";

import { Chunk } from "./Chunk";
Expand Down Expand Up @@ -32,13 +34,17 @@ export class ChunkGroup {
}

getParents(): ReadonlyArray<ChunkGroup> {
return this.#inner.__inner_parents.map(parent => {
const cg = __chunk_group_inner_get_chunk_group(
parent,
this.#innerCompilation
);
return ChunkGroup.__from_binding(cg, this.#innerCompilation);
});
return __chunk_group_inner_parents_iterable(
this.#inner.__inner_ukey,
this.#innerCompilation
).map(cg => ChunkGroup.__from_binding(cg, this.#innerCompilation));
}

getChildren(): ReadonlyArray<ChunkGroup> {
return __chunk_group_inner_children_iterable(
this.#inner.__inner_ukey,
this.#innerCompilation
).map(cg => ChunkGroup.__from_binding(cg, this.#innerCompilation));
}

isInitial(): boolean {
Expand All @@ -51,6 +57,10 @@ export class ChunkGroup {
);
}

get childrenIterable(): Iterable<ChunkGroup> {
return this.getChildren();
}

get index(): Readonly<number | undefined> {
return this.#inner.index;
}
Expand Down

0 comments on commit 09a0550

Please sign in to comment.