Skip to content

Commit

Permalink
feat: chunkGraph.getModuleId binding API (#8680)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind authored Dec 12, 2024
1 parent e21a703 commit d1079dc
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 46 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export declare class JsChunkGraph {
getChunkEntryDependentChunksIterable(chunk: JsChunk): JsChunk[]
getChunkModulesIterableBySourceType(chunk: JsChunk, sourceType: string): JsModule[]
getModuleChunks(module: JsModule): JsChunk[]
getModuleId(jsModule: JsModule): string | null
}

export declare class JsChunkGroup {
Expand Down
11 changes: 10 additions & 1 deletion crates/rspack_binding_values/src/chunk_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ptr::NonNull;

use napi::Result;
use napi_derive::napi;
use rspack_core::{Compilation, SourceType};
use rspack_core::{ChunkGraph, Compilation, SourceType};

use crate::{JsChunk, JsChunkWrapper, JsModule, JsModuleWrapper};

Expand Down Expand Up @@ -118,4 +118,13 @@ impl JsChunkGraph {
.collect(),
)
}

#[napi]
pub fn get_module_id(&self, js_module: &JsModule) -> napi::Result<Option<&str>> {
let compilation = self.as_ref()?;
Ok(
ChunkGraph::get_module_id(&compilation.module_ids, js_module.identifier)
.map(|module_id| module_id.as_str()),
)
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Plugin {
apply(compiler) {
compiler.hooks.compilation.tap("Test", compilation => {
compilation.hooks.processAssets.tap("Test", () => {
const module = Array.from(compilation.modules)[0];
const moduleId = compilation.chunkGraph.getModuleId(module);
expect(moduleId).toBeTruthy();
});
});
}
}

/** @type {import("@rspack/core").Configuration} */
module.exports = {
target: 'web',
node: false,
entry: {
main: "./index.js"
},
output: {
filename: "[name].js"
},
optimization: {
sideEffects: false,
},
plugins: [
new Plugin()
]
};
92 changes: 47 additions & 45 deletions packages/rspack/etc/core.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,27 @@ interface BaseModuleConfig {

// @public (undocumented)
interface BaseResolveRequest {
// (undocumented)
// (undocumented)
__innerRequest?: string;
// (undocumented)
// (undocumented)
__innerRequest_relativePath?: string;
// (undocumented)
// (undocumented)
__innerRequest_request?: string;
// (undocumented)
// (undocumented)
context?: object;
// (undocumented)
// (undocumented)
descriptionFileData?: JsonObject;
// (undocumented)
// (undocumented)
descriptionFilePath?: string;
// (undocumented)
// (undocumented)
descriptionFileRoot?: string;
// (undocumented)
// (undocumented)
fullySpecified?: boolean;
// (undocumented)
// (undocumented)
ignoreSymlinks?: boolean;
// (undocumented)
// (undocumented)
path: string | false;
// (undocumented)
// (undocumented)
relativePath?: string;
}

Expand Down Expand Up @@ -492,6 +492,8 @@ class ChunkGraph {
getModuleChunks(module: Module): Chunk[];
// (undocumented)
getModuleChunksIterable(module: Module): Iterable<Chunk>;
// (undocumented)
getModuleId(module: Module): string | null;
}

// @public (undocumented)
Expand Down Expand Up @@ -2196,11 +2198,11 @@ type GroupOptions = {

// @public (undocumented)
class Hash {
constructor();
constructor();

digest(encoding?: string): string | Buffer;
digest(encoding?: string): string | Buffer;

update(data: string | Buffer, inputEncoding?: string): Hash;
update(data: string | Buffer, inputEncoding?: string): Hash;
}

// @public (undocumented)
Expand Down Expand Up @@ -2693,14 +2695,14 @@ type JsonArray = JsonValue_2[];

// @public (undocumented)
type JsonObject = { [index: string]: JsonValue } & {
[index: string]:
| undefined
| null
| string
| number
| boolean
| JsonObject
| JsonValue[];
[index: string]:
| undefined
| null
| string
| number
| boolean
| JsonObject
| JsonValue[];
};

// @public (undocumented)
Expand Down Expand Up @@ -4360,19 +4362,19 @@ interface ParseContext {

// @public (undocumented)
interface ParsedIdentifier {
// (undocumented)
// (undocumented)
directory: boolean;
// (undocumented)
// (undocumented)
file: boolean;
// (undocumented)
// (undocumented)
fragment: string;
// (undocumented)
// (undocumented)
internal: boolean;
// (undocumented)
// (undocumented)
module: boolean;
// (undocumented)
// (undocumented)
query: string;
// (undocumented)
// (undocumented)
request: string;
}

Expand Down Expand Up @@ -4642,13 +4644,13 @@ type RawCreateParams = {

// @public (undocumented)
type RawSourceMap = {
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
version: number;
sources: string[];
names: string[];
sourceRoot?: string;
sourcesContent?: string[];
mappings: string;
file: string;
};

// @public (undocumented)
Expand Down Expand Up @@ -9886,25 +9888,25 @@ export type SnapshotOptions = {};

// @public (undocumented)
abstract class Source {
// (undocumented)
// (undocumented)
buffer(): Buffer;

// (undocumented)
// (undocumented)
map(options?: MapOptions): RawSourceMap | null;

// (undocumented)
// (undocumented)
size(): number;

// (undocumented)
// (undocumented)
source(): string | Buffer;

// (undocumented)
// (undocumented)
sourceAndMap(options?: MapOptions): {
source: string | Buffer;
map: Object;
};
source: string | Buffer;
map: Object;
};

// (undocumented)
// (undocumented)
updateHash(hash: Hash): void;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/rspack/src/ChunkGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ export class ChunkGraph {
.getModuleChunks(Module.__to_binding(module))
.map(binding => Chunk.__from_binding(binding));
}

getModuleId(module: Module): string | null {
return this.#inner.getModuleId(Module.__to_binding(module));
}
}

2 comments on commit d1079dc

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rsdoctor ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success
nuxt ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-12-12 bd04c60) Current Change
10000_big_production-mode_disable-minimize + exec 37.6 s ± 508 ms 37.5 s ± 709 ms -0.30 %
10000_development-mode + exec 1.81 s ± 43 ms 1.79 s ± 23 ms -0.67 %
10000_development-mode_hmr + exec 664 ms ± 20 ms 636 ms ± 7.4 ms -4.17 %
10000_production-mode + exec 2.36 s ± 57 ms 2.33 s ± 31 ms -1.38 %
arco-pro_development-mode + exec 1.78 s ± 81 ms 1.74 s ± 63 ms -2.04 %
arco-pro_development-mode_hmr + exec 426 ms ± 9.5 ms 379 ms ± 1.5 ms -11.11 %
arco-pro_production-mode + exec 3.17 s ± 81 ms 3.09 s ± 59 ms -2.47 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.16 s ± 62 ms 3.17 s ± 62 ms +0.25 %
arco-pro_production-mode_traverse-chunk-modules + exec 3.17 s ± 74 ms 3.1 s ± 74 ms -2.47 %
threejs_development-mode_10x + exec 1.62 s ± 15 ms 1.61 s ± 18 ms -0.49 %
threejs_development-mode_10x_hmr + exec 807 ms ± 23 ms 784 ms ± 15 ms -2.89 %
threejs_production-mode_10x + exec 5.17 s ± 112 ms 4.87 s ± 28 ms -5.79 %
10000_big_production-mode_disable-minimize + rss memory 9583 MiB ± 89.8 MiB 9815 MiB ± 108 MiB +2.42 %
10000_development-mode + rss memory 794 MiB ± 14.3 MiB 827 MiB ± 43.6 MiB +4.09 %
10000_development-mode_hmr + rss memory 1821 MiB ± 230 MiB 1882 MiB ± 235 MiB +3.32 %
10000_production-mode + rss memory 690 MiB ± 41 MiB 684 MiB ± 30.1 MiB -0.88 %
arco-pro_development-mode + rss memory 704 MiB ± 24.9 MiB 712 MiB ± 54.6 MiB +1.10 %
arco-pro_development-mode_hmr + rss memory 916 MiB ± 59.6 MiB 818 MiB ± 90.7 MiB -10.67 %
arco-pro_production-mode + rss memory 806 MiB ± 38.3 MiB 800 MiB ± 34.8 MiB -0.80 %
arco-pro_production-mode_generate-package-json-webpack-plugin + rss memory 808 MiB ± 46 MiB 816 MiB ± 26.7 MiB +0.93 %
arco-pro_production-mode_traverse-chunk-modules + rss memory 801 MiB ± 43.9 MiB 833 MiB ± 52.5 MiB +3.91 %
threejs_development-mode_10x + rss memory 769 MiB ± 37.9 MiB 782 MiB ± 47.5 MiB +1.78 %
threejs_development-mode_10x_hmr + rss memory 1548 MiB ± 311 MiB 1694 MiB ± 124 MiB +9.47 %
threejs_production-mode_10x + rss memory 1095 MiB ± 61.1 MiB 1135 MiB ± 55.3 MiB +3.72 %

Please sign in to comment.