Skip to content

Commit

Permalink
feat: support chunkGroup.getModulePreOrderIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Jan 2, 2025
1 parent 46e8278 commit f211d45
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 27 deletions.
2 changes: 2 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ export declare class JsChunkGroup {
getParents(): JsChunkGroup[]
getRuntimeChunk(): JsChunk
getFiles(): Array<string>
getModulePreOrderIndex(moduleIdentifier: string): number | null
getModulePostOrderIndex(moduleIdentifier: string): number | null
}

export declare class JsCompilation {
Expand Down
35 changes: 26 additions & 9 deletions crates/rspack_binding_values/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{cell::RefCell, ptr::NonNull};

use napi::{bindgen_prelude::ToNapiValue, Either, Env, JsString};
use napi_derive::napi;
use rspack_core::{ChunkGroup, ChunkGroupUkey, Compilation, CompilationId};
use rspack_core::{ChunkGroup, ChunkGroupUkey, Compilation, CompilationId, ModuleIdentifier};
use rspack_napi::OneShotRef;
use rustc_hash::FxHashMap as HashMap;

Expand Down Expand Up @@ -89,15 +89,15 @@ impl JsChunkGroup {
impl JsChunkGroup {
#[napi]
pub fn is_initial(&self) -> napi::Result<bool> {
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<Vec<JsChunkGroupWrapper>> {
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))
Expand All @@ -107,16 +107,16 @@ impl JsChunkGroup {

#[napi(ts_return_type = "JsChunk")]
pub fn get_runtime_chunk(&self) -> napi::Result<JsChunkWrapper> {
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<Vec<&String>> {
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| {
Expand All @@ -129,6 +129,23 @@ impl JsChunkGroup {
.collect::<Vec<_>>(),
)
}

#[napi]
pub fn get_module_pre_order_index(&self, module_identifier: String) -> Option<u32> {
let (_, chunk_group) = self.as_ref().expect("should have chunk group");
chunk_group
.module_pre_order_index(&ModuleIdentifier::from(module_identifier))
.map(|v| v as u32)
}

#[napi]
pub fn get_module_post_order_index(&self, module_identifier: String) -> Option<u32> {
let (_, chunk_group) = self.as_ref().expect("should have chunk group");

chunk_group
.module_post_order_index(&ModuleIdentifier::from(module_identifier))
.map(|v| v as u32)
}
}

thread_local! {
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ impl ChunkGroup {
self.parents.iter()
}

pub fn module_pre_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
.module_pre_order_indices
.get(module_identifier)
.copied()
}

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
it("should compile", function (done) {
done()
});
Original file line number Diff line number Diff line change
@@ -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();
})
})
})
}
}
]
};
40 changes: 22 additions & 18 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ export class ChunkGroup {
// (undocumented)
getFiles(): ReadonlyArray<string>;
// (undocumented)
getModulePostOrderIndex(module: Module): number | null;
// (undocumented)
getModulePreOrderIndex(module: Module): number | null;
// (undocumented)
getParents(): ReadonlyArray<ChunkGroup>;
// (undocumented)
readonly index?: number;
Expand Down Expand Up @@ -7816,7 +7820,7 @@ export const rspackOptions: z.ZodObject<{
}, {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
}>, z.ZodFunction<z.ZodTuple<[z.ZodType<Buffer<ArrayBuffer>, z.ZodTypeDef, Buffer<ArrayBuffer>>, z.ZodObject<{
}>, z.ZodFunction<z.ZodTuple<[z.ZodType<Buffer, z.ZodTypeDef, Buffer>, z.ZodObject<{
filename: z.ZodString;
module: z.ZodType<Module, z.ZodTypeDef, Module>;
}, "strict", z.ZodTypeAny, {
Expand All @@ -7836,7 +7840,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -7847,7 +7851,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -7863,7 +7867,7 @@ export const rspackOptions: z.ZodObject<{
}, {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
}>, z.ZodFunction<z.ZodTuple<[z.ZodType<Buffer<ArrayBuffer>, z.ZodTypeDef, Buffer<ArrayBuffer>>, z.ZodObject<{
}>, z.ZodFunction<z.ZodTuple<[z.ZodType<Buffer, z.ZodTypeDef, Buffer>, z.ZodObject<{
filename: z.ZodString;
module: z.ZodType<Module, z.ZodTypeDef, Module>;
}, "strict", z.ZodTypeAny, {
Expand All @@ -7877,15 +7881,15 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
}, {
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand Down Expand Up @@ -7956,7 +7960,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -7978,7 +7982,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -7999,7 +8003,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -8021,7 +8025,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand Down Expand Up @@ -8147,7 +8151,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -8169,7 +8173,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand Down Expand Up @@ -8295,7 +8299,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -8317,7 +8321,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand Down Expand Up @@ -8467,7 +8471,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -8489,7 +8493,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand Down Expand Up @@ -9071,7 +9075,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand All @@ -9093,7 +9097,7 @@ export const rspackOptions: z.ZodObject<{
dataUrl?: {
encoding?: false | "base64" | undefined;
mimetype?: string | undefined;
} | ((args_0: Buffer<ArrayBuffer>, args_1: {
} | ((args_0: Buffer, args_1: {
module: Module;
filename: string;
}, ...args: unknown[]) => string) | undefined;
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/ChunkGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ export class ChunkGroup {
isInitial(): boolean {
return this.#inner.isInitial();
}

getModulePreOrderIndex(module: Module) {
return this.#inner.getModulePreOrderIndex(module.identifier());
}

getModulePostOrderIndex(module: Module) {
return this.#inner.getModulePostOrderIndex(module.identifier());
}
}

interface ChunkGroupOrigin {
Expand Down

0 comments on commit f211d45

Please sign in to comment.