From 0726ce597a2e158ebf944e07fe2342ca200ad93a Mon Sep 17 00:00:00 2001 From: jserfeng <1114550440@qq.com> Date: Mon, 2 Dec 2024 15:13:13 +0800 Subject: [PATCH] feat: support chunkGroup.getModulePreOrderIndex --- crates/node_binding/binding.d.ts | 4 +++ .../rspack_binding_values/src/chunk_group.rs | 28 ++++++++++++++++++- crates/rspack_core/src/chunk_group.rs | 8 ++++++ .../chunks/module-order-index/index.js | 3 ++ .../module-order-index/rspack.config.js | 24 ++++++++++++++++ packages/rspack/etc/core.api.md | 4 +++ packages/rspack/src/ChunkGroup.ts | 21 +++++++++++++- 7 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 packages/rspack-test-tools/tests/configCases/chunks/module-order-index/index.js create mode 100644 packages/rspack-test-tools/tests/configCases/chunks/module-order-index/rspack.config.js diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 14f8c3e395e..21e4474995a 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -224,6 +224,10 @@ export declare function __chunk_graph_inner_get_chunk_modules_iterable_by_source export declare function __chunk_group_inner_get_chunk_group(ukey: number, jsCompilation: JsCompilation): JsChunkGroup +export declare function __chunk_group_inner_get_module_post_order_index(ukey: number, jsCompilation: JsCompilation, moduleIdentifier: string): number | null + +export declare function __chunk_group_inner_get_module_pre_order_index(ukey: number, jsCompilation: JsCompilation, moduleIdentifier: string): number | null + export declare function __chunk_inner_can_be_initial(jsChunkUkey: number, jsCompilation: JsCompilation): boolean export declare function __chunk_inner_get_all_async_chunks(jsChunkUkey: number, jsCompilation: JsCompilation): Array diff --git a/crates/rspack_binding_values/src/chunk_group.rs b/crates/rspack_binding_values/src/chunk_group.rs index 2fa10af8663..5ff03420528 100644 --- a/crates/rspack_binding_values/src/chunk_group.rs +++ b/crates/rspack_binding_values/src/chunk_group.rs @@ -1,5 +1,5 @@ use napi_derive::napi; -use rspack_core::{ChunkGroup, ChunkGroupUkey, Compilation}; +use rspack_core::{ChunkGroup, ChunkGroupUkey, Compilation, ModuleIdentifier}; use crate::{JsChunk, JsCompilation, JsModuleWrapper}; @@ -69,6 +69,32 @@ pub fn get_chunk_group(ukey: u32, js_compilation: &JsCompilation) -> JsChunkGrou JsChunkGroup::from_chunk_group(cg, compilation) } +#[napi(js_name = "__chunk_group_inner_get_module_pre_order_index")] +pub fn get_module_pre_order_index( + ukey: u32, + js_compilation: &JsCompilation, + module_identifier: String, +) -> Option { + let compilation = unsafe { js_compilation.inner.as_ref() }; + + let cg = chunk_group(ukey, compilation); + cg.module_pre_order_index(&ModuleIdentifier::from(module_identifier)) + .map(|v| v as u32) +} + +#[napi(js_name = "__chunk_group_inner_get_module_post_order_index")] +pub fn get_module_post_order_index( + ukey: u32, + js_compilation: &JsCompilation, + module_identifier: String, +) -> Option { + let compilation = unsafe { js_compilation.inner.as_ref() }; + + let cg = chunk_group(ukey, compilation); + cg.module_post_order_index(&ModuleIdentifier::from(module_identifier)) + .map(|v| v as u32) +} + #[napi(js_name = "__entrypoint_inner_get_runtime_chunk")] pub fn get_runtime_chunk(ukey: u32, js_compilation: &JsCompilation) -> JsChunk { let compilation = unsafe { js_compilation.inner.as_ref() }; diff --git a/crates/rspack_core/src/chunk_group.rs b/crates/rspack_core/src/chunk_group.rs index 65a2406f75b..b6f5979a0c5 100644 --- a/crates/rspack_core/src/chunk_group.rs +++ b/crates/rspack_core/src/chunk_group.rs @@ -75,6 +75,14 @@ impl ChunkGroup { self.parents.iter() } + pub fn module_pre_order_index(&self, module_identifier: &ModuleIdentifier) -> Option { + // A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module + self + .module_pre_order_indices + .get(module_identifier) + .copied() + } + pub fn module_post_order_index(&self, module_identifier: &ModuleIdentifier) -> Option { // A module could split into another ChunkGroup, which doesn't have the module_post_order_indices of the module self diff --git a/packages/rspack-test-tools/tests/configCases/chunks/module-order-index/index.js b/packages/rspack-test-tools/tests/configCases/chunks/module-order-index/index.js new file mode 100644 index 00000000000..4fdf28c01e4 --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/chunks/module-order-index/index.js @@ -0,0 +1,3 @@ +it("should compile", function (done) { + done() +}); diff --git a/packages/rspack-test-tools/tests/configCases/chunks/module-order-index/rspack.config.js b/packages/rspack-test-tools/tests/configCases/chunks/module-order-index/rspack.config.js new file mode 100644 index 00000000000..cd51fcd92aa --- /dev/null +++ b/packages/rspack-test-tools/tests/configCases/chunks/module-order-index/rspack.config.js @@ -0,0 +1,24 @@ +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + target: "node", + entry: { + 'main': './index.js' + }, + plugins: [ + { + /**@param {import("@rspack/core").Compiler} compiler */ + apply(compiler) { + compiler.hooks.thisCompilation.tap('test', (compilation) => { + compilation.hooks.afterSeal.tap('test', () => { + let entrypoint = compilation.entrypoints.get('main') + + compilation.chunkGraph.getChunkModules(entrypoint).forEach((m) => { + expect(entrypoint.getModulePreOrderIndex(m)).toBeDefined(); + expect(entrypoint.getModulePostOrderIndex(m)).toBeDefined(); + }) + }) + }) + } + } + ] +}; diff --git a/packages/rspack/etc/core.api.md b/packages/rspack/etc/core.api.md index bd280bbce8c..8cd1c5e66cc 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -504,6 +504,10 @@ export class ChunkGroup { // (undocumented) getFiles(): ReadonlyArray; // (undocumented) + getModulePostOrderIndex(module: Module): number | null; + // (undocumented) + getModulePreOrderIndex(module: Module): number | null; + // (undocumented) getParents(): ReadonlyArray; // (undocumented) get index(): Readonly; diff --git a/packages/rspack/src/ChunkGroup.ts b/packages/rspack/src/ChunkGroup.ts index 76c7d410a67..fd5673caae2 100644 --- a/packages/rspack/src/ChunkGroup.ts +++ b/packages/rspack/src/ChunkGroup.ts @@ -2,10 +2,13 @@ import { type JsChunkGroup, type JsChunkGroupOrigin, type JsCompilation, - __chunk_group_inner_get_chunk_group + __chunk_group_inner_get_chunk_group, + __chunk_group_inner_get_module_post_order_index, + __chunk_group_inner_get_module_pre_order_index } from "@rspack/binding"; import { Chunk } from "./Chunk"; +import type { Module } from "./Module"; export class ChunkGroup { #inner: JsChunkGroup; @@ -46,6 +49,22 @@ export class ChunkGroup { return this.#inner.isInitial; } + getModulePreOrderIndex(module: Module) { + return __chunk_group_inner_get_module_pre_order_index( + this.#inner.__inner_ukey, + this.#innerCompilation, + module.identifier() + ); + } + + getModulePostOrderIndex(module: Module) { + return __chunk_group_inner_get_module_post_order_index( + this.#inner.__inner_ukey, + this.#innerCompilation, + module.identifier() + ); + } + get chunks(): ReadonlyArray { return this.#inner.chunks.map(c => Chunk.__from_binding(c, this.#innerCompilation)