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 Dec 4, 2024
1 parent 5503834 commit 0726ce5
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JsChunk>
Expand Down
28 changes: 27 additions & 1 deletion crates/rspack_binding_values/src/chunk_group.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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<u32> {
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<u32> {
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() };
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 @@ -75,6 +75,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();
})
})
})
}
}
]
};
4 changes: 4 additions & 0 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,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)
get index(): Readonly<number | undefined>;
Expand Down
21 changes: 20 additions & 1 deletion packages/rspack/src/ChunkGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Chunk> {
return this.#inner.chunks.map(c =>
Chunk.__from_binding(c, this.#innerCompilation)
Expand Down

0 comments on commit 0726ce5

Please sign in to comment.