From 9a1332f5b75dbbc0f94bf167bf72548b0be918ca Mon Sep 17 00:00:00 2001 From: LingyuCoder Date: Thu, 1 Aug 2024 17:08:53 +0800 Subject: [PATCH] feat: add chunkGroup.isInitial() --- crates/node_binding/binding.d.ts | 1 + crates/rspack_binding_values/src/chunk_group.rs | 2 ++ .../compilation-chunk-groups/rspack.config.js | 1 + packages/rspack/etc/api.md | 2 ++ packages/rspack/src/ChunkGroup.ts | 4 ++++ website/docs/en/types/chunk-group.mdx | 11 ++++++----- website/docs/en/types/entrypoint.mdx | 13 +++++++------ website/docs/zh/types/chunk-group.mdx | 1 + website/docs/zh/types/entrypoint.mdx | 3 ++- 9 files changed, 26 insertions(+), 12 deletions(-) diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 19d7c78656cf..53fe3dd9bbf8 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -390,6 +390,7 @@ export interface JsChunkGroup { chunks: Array index?: number name?: string + isInitial: boolean } export interface JsChunkPathData { diff --git a/crates/rspack_binding_values/src/chunk_group.rs b/crates/rspack_binding_values/src/chunk_group.rs index a91f9169f795..7f306c285325 100644 --- a/crates/rspack_binding_values/src/chunk_group.rs +++ b/crates/rspack_binding_values/src/chunk_group.rs @@ -12,6 +12,7 @@ pub struct JsChunkGroup { pub chunks: Vec, pub index: Option, pub name: Option, + pub is_initial: bool, } impl JsChunkGroup { @@ -29,6 +30,7 @@ impl JsChunkGroup { 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(), } } } diff --git a/packages/rspack-test-tools/tests/configCases/plugins/compilation-chunk-groups/rspack.config.js b/packages/rspack-test-tools/tests/configCases/plugins/compilation-chunk-groups/rspack.config.js index c06bb9019221..2cf4dbf218b9 100644 --- a/packages/rspack-test-tools/tests/configCases/plugins/compilation-chunk-groups/rspack.config.js +++ b/packages/rspack-test-tools/tests/configCases/plugins/compilation-chunk-groups/rspack.config.js @@ -5,6 +5,7 @@ function plugin(compiler) { expect(chunkGroups.length).toBe(6); expect(chunkGroups.find(i => i.name === 'a').getFiles()).toEqual(['a.js']); expect(chunkGroups.find(i => i.name === 'b').getFiles()).toEqual(['b.js']); + expect(chunkGroups.filter(i => i.isInitial()).length).toEqual(2); const namedChunkGroups = compilation.namedChunkGroups; expect(Array.from(namedChunkGroups.keys()).length).toBe(2); diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index 8858c483609a..5193297ff393 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -929,6 +929,8 @@ export class ChunkGroup { // (undocumented) get index(): Readonly; // (undocumented) + isInitial(): boolean; + // (undocumented) get name(): Readonly; } diff --git a/packages/rspack/src/ChunkGroup.ts b/packages/rspack/src/ChunkGroup.ts index 8b0feb3f26da..fcc68d306879 100644 --- a/packages/rspack/src/ChunkGroup.ts +++ b/packages/rspack/src/ChunkGroup.ts @@ -41,6 +41,10 @@ export class ChunkGroup { }); } + isInitial(): boolean { + return this.#inner.isInitial; + } + get chunks(): ReadonlyArray { return this.#inner.chunks.map(c => Chunk.__from_binding(c, this.#innerCompilation) diff --git a/website/docs/en/types/chunk-group.mdx b/website/docs/en/types/chunk-group.mdx index 69c913a3b3af..d23b1be9b5a1 100644 --- a/website/docs/en/types/chunk-group.mdx +++ b/website/docs/en/types/chunk-group.mdx @@ -2,11 +2,12 @@ ```ts type ChunkGroup = { - name?: Readonly; - index?: Readonly; - chunks: ReadonlyArray; - getFiles(): ReadonlyArray; - getParents(): ReadonlyArray; + name?: Readonly; // name of this chunk group + index?: Readonly; // creating index of chunk group + chunks: ReadonlyArray; // chunks in this chunk group + isInitial(): boolean; // whether this chunk group will be loaded on initial page load + getFiles(): ReadonlyArray; // the files contained this chunk group + getParents(): ReadonlyArray; // returns the parent chunk groups }; ``` diff --git a/website/docs/en/types/entrypoint.mdx b/website/docs/en/types/entrypoint.mdx index b32e17fb7143..71adeec713ba 100644 --- a/website/docs/en/types/entrypoint.mdx +++ b/website/docs/en/types/entrypoint.mdx @@ -2,12 +2,13 @@ ```ts type Entrypoint = { - name?: Readonly; - index?: Readonly; - chunks: ReadonlyArray; - getFiles(): ReadonlyArray; - getParents(): ReadonlyArray; - getRuntimeChunk(): Readonly; + name?: Readonly; // name of this chunk group + index?: Readonly; // creating index of chunk group + chunks: ReadonlyArray; // chunks in this chunk group + isInitial(): boolean; // whether this chunk group will be loaded on initial page load + getFiles(): ReadonlyArray; // the files contained this chunk group + getParents(): ReadonlyArray; // returns the parent chunk groups + getRuntimeChunk(): Readonly; // the chunk containing the webpack bootstrap code }; ``` diff --git a/website/docs/zh/types/chunk-group.mdx b/website/docs/zh/types/chunk-group.mdx index a2a84f0224e7..f3e6e0cec10f 100644 --- a/website/docs/zh/types/chunk-group.mdx +++ b/website/docs/zh/types/chunk-group.mdx @@ -5,6 +5,7 @@ type ChunkGroup = { name?: Readonly; // 名称 index?: Readonly; // 索引序 chunks: ReadonlyArray; // 包含的 chunk 列表 + isInitial(): boolean; // 是否在页面初始加载 getFiles(): ReadonlyArray; // 关联的文件列表 getParents(): ReadonlyArray; // 父 chunk group 列表 }; diff --git a/website/docs/zh/types/entrypoint.mdx b/website/docs/zh/types/entrypoint.mdx index cc958fdc200e..e8410ea2c074 100644 --- a/website/docs/zh/types/entrypoint.mdx +++ b/website/docs/zh/types/entrypoint.mdx @@ -5,9 +5,10 @@ type Entrypoint = { name?: Readonly; // 名称 index?: Readonly; // 索引序 chunks: ReadonlyArray; // 包含的 chunk 列表 + isInitial(): boolean; // 是否在页面初始加载 getFiles(): ReadonlyArray; // 关联的文件列表 getParents(): ReadonlyArray; // 父 chunk group 列表 - getRuntimeChunk(): Readonly; + getRuntimeChunk(): Readonly; // 包含 webpack 启动代码的 chunk }; ```