From dc1146b511f85de269ab93bb9b800d51b52d6f1d 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 | 2 ++ .../rspack_binding_values/src/chunk_group.rs | 35 ++++++++++++++----- 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 | 8 +++++ 7 files changed, 75 insertions(+), 9 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 1cbf6ef44e62..88a6c6c5c1f9 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -87,6 +87,8 @@ export declare class JsChunkGroup { getParents(): JsChunkGroup[] getRuntimeChunk(): JsChunk getFiles(): Array + getModulePreOrderIndex(module: JsModule): number | null + getModulePostOrderIndex(module: JsModule): number | null } export declare class JsCompilation { diff --git a/crates/rspack_binding_values/src/chunk_group.rs b/crates/rspack_binding_values/src/chunk_group.rs index e8e63c1d0acd..85bc33e909b5 100644 --- a/crates/rspack_binding_values/src/chunk_group.rs +++ b/crates/rspack_binding_values/src/chunk_group.rs @@ -6,7 +6,7 @@ use rspack_core::{ChunkGroup, ChunkGroupUkey, Compilation, CompilationId}; use rspack_napi::OneShotRef; use rustc_hash::FxHashMap as HashMap; -use crate::{JsChunkWrapper, JsModuleWrapper}; +use crate::{JsChunkWrapper, JsModule, JsModuleWrapper}; #[napi] pub struct JsChunkGroup { @@ -89,15 +89,15 @@ impl JsChunkGroup { impl JsChunkGroup { #[napi] pub fn is_initial(&self) -> napi::Result { - let (_, chunk_graph) = self.as_ref()?; - Ok(chunk_graph.is_initial()) + let (_, chunk_group) = self.as_ref()?; + Ok(chunk_group.is_initial()) } #[napi(ts_return_type = "JsChunkGroup[]")] pub fn get_parents(&self) -> napi::Result> { - let (compilation, chunk_graph) = self.as_ref()?; + let (compilation, chunk_group) = self.as_ref()?; Ok( - chunk_graph + chunk_group .parents .iter() .map(|ukey| JsChunkGroupWrapper::new(*ukey, compilation)) @@ -107,16 +107,16 @@ impl JsChunkGroup { #[napi(ts_return_type = "JsChunk")] pub fn get_runtime_chunk(&self) -> napi::Result { - let (compilation, chunk_graph) = self.as_ref()?; - let chunk_ukey = chunk_graph.get_runtime_chunk(&compilation.chunk_group_by_ukey); + let (compilation, chunk_group) = self.as_ref()?; + let chunk_ukey = chunk_group.get_runtime_chunk(&compilation.chunk_group_by_ukey); Ok(JsChunkWrapper::new(chunk_ukey, compilation)) } #[napi] pub fn get_files(&self) -> napi::Result> { - let (compilation, chunk_graph) = self.as_ref()?; + let (compilation, chunk_group) = self.as_ref()?; Ok( - chunk_graph + chunk_group .chunks .iter() .filter_map(|chunk_ukey| { @@ -129,6 +129,23 @@ impl JsChunkGroup { .collect::>(), ) } + + #[napi] + pub fn get_module_pre_order_index(&self, module: &JsModule) -> Option { + let (_, chunk_group) = self.as_ref().expect("should have chunk group"); + chunk_group + .module_pre_order_index(&module.identifier) + .map(|v| v as u32) + } + + #[napi] + pub fn get_module_post_order_index(&self, module: &JsModule) -> Option { + let (_, chunk_group) = self.as_ref().expect("should have chunk group"); + + chunk_group + .module_post_order_index(&module.identifier) + .map(|v| v as u32) + } } thread_local! { diff --git a/crates/rspack_core/src/chunk_group.rs b/crates/rspack_core/src/chunk_group.rs index 8adb7077195e..39cf5f2bfde0 100644 --- a/crates/rspack_core/src/chunk_group.rs +++ b/crates/rspack_core/src/chunk_group.rs @@ -76,6 +76,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 000000000000..4fdf28c01e40 --- /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 000000000000..94363ba578cd --- /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.chunks[0]).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 1b554fb26aec..36fba5383e9e 100644 --- a/packages/rspack/etc/core.api.md +++ b/packages/rspack/etc/core.api.md @@ -511,6 +511,10 @@ export class ChunkGroup { // (undocumented) getFiles(): ReadonlyArray; // (undocumented) + getModulePostOrderIndex(module: Module): number | null; + // (undocumented) + getModulePreOrderIndex(module: Module): number | null; + // (undocumented) getParents(): ReadonlyArray; // (undocumented) readonly index?: number; diff --git a/packages/rspack/src/ChunkGroup.ts b/packages/rspack/src/ChunkGroup.ts index 59614ff67811..e730c6797225 100644 --- a/packages/rspack/src/ChunkGroup.ts +++ b/packages/rspack/src/ChunkGroup.ts @@ -74,6 +74,14 @@ export class ChunkGroup { isInitial(): boolean { return this.#inner.isInitial(); } + + getModulePreOrderIndex(module: Module) { + return this.#inner.getModulePreOrderIndex(Module.__to_binding(module)); + } + + getModulePostOrderIndex(module: Module) { + return this.#inner.getModulePostOrderIndex(Module.__to_binding(module)); + } } interface ChunkGroupOrigin {