diff --git a/crates/node_binding/binding.d.ts b/crates/node_binding/binding.d.ts index 74a5881aa4a..d5d6d7cc54b 100644 --- a/crates/node_binding/binding.d.ts +++ b/crates/node_binding/binding.d.ts @@ -81,6 +81,7 @@ export class JsCompilation { getAsset(name: string): JsAsset | null getAssetSource(name: string): JsCompatSource | null get modules(): Array + get builtModules(): Array getOptimizationBailout(): Array getChunks(): Array getNamedChunkKeys(): Array @@ -1880,7 +1881,7 @@ export interface RawTrustedTypes { * Author Donny/강동윤 * Copyright (c) */ -export function registerGlobalTrace(filter: string, layer: "chrome" | "logger" | "console", output: string): void +export function registerGlobalTrace(filter: string, layer: "chrome" | "logger"| "console", output: string): void export enum RegisterJsTapKind { CompilerThisCompilation = 0, diff --git a/crates/rspack_binding_values/src/compilation/mod.rs b/crates/rspack_binding_values/src/compilation/mod.rs index 37a36ea7d13..58b999d2063 100644 --- a/crates/rspack_binding_values/src/compilation/mod.rs +++ b/crates/rspack_binding_values/src/compilation/mod.rs @@ -148,6 +148,16 @@ impl JsCompilation { .collect::>() } + #[napi(getter, ts_return_type = "Array")] + pub fn built_modules(&'static self) -> Vec { + self + .0 + .built_modules + .iter() + .map(|module_id| ModuleDTOWrapper::new(*module_id, self.0)) + .collect::>() + } + #[napi] pub fn get_optimization_bailout(&self) -> Vec { self diff --git a/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/0/foo.js b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/0/foo.js new file mode 100644 index 00000000000..221452b02f2 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/0/foo.js @@ -0,0 +1 @@ +export const v = 'foo' diff --git a/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/0/index.js b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/0/index.js new file mode 100644 index 00000000000..d316c62b00a --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/0/index.js @@ -0,0 +1,5 @@ +import { v } from "./foo"; + +v; + +it("should run", function () { }); diff --git a/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/1/foo.js b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/1/foo.js new file mode 100644 index 00000000000..6756fa14942 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/1/foo.js @@ -0,0 +1 @@ +export const v = 'fooo' diff --git a/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/rspack.config.js b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/rspack.config.js new file mode 100644 index 00000000000..b7b059f03a5 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/compilation/built-modules/rspack.config.js @@ -0,0 +1,21 @@ +let firstRun = true; + +/** @type {import("@rspack/core").Configuration} */ +module.exports = { + plugins: [{ + apply(compiler) { + compiler.hooks.compilation.tap("test", (compilation) => { + compilation.hooks.seal.tap("test", () => { + const builtModules = Array.from(compilation.builtModules).map(m => m.rawRequest); + builtModules.sort(); + if (firstRun) { + expect(builtModules).toEqual(["./foo", "./index.js"]); + firstRun = false; + } else { + expect(builtModules).toEqual(["./foo"]); + } + }); + }); + } + }] +}; diff --git a/packages/rspack/etc/api.md b/packages/rspack/etc/api.md index f595de39522..203cfc8534d 100644 --- a/packages/rspack/etc/api.md +++ b/packages/rspack/etc/api.md @@ -1061,6 +1061,8 @@ export class Compilation { addAll: (deps: Iterable) => void; }; // (undocumented) + get builtModules(): ReadonlySet; + // (undocumented) children: Compilation[]; // (undocumented) childrenCounters: Record; diff --git a/packages/rspack/src/Compilation.ts b/packages/rspack/src/Compilation.ts index 11ce94a4ac3..fee170e0507 100644 --- a/packages/rspack/src/Compilation.ts +++ b/packages/rspack/src/Compilation.ts @@ -431,6 +431,14 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si ); } + get builtModules(): ReadonlySet { + return new Set( + this.#inner.builtModules.map(module => + Module.__from_binding(module, this) + ) + ); + } + get chunks(): ReadonlySet { return memoizeValue(() => new Set(this.__internal__getChunks())); } diff --git a/website/docs/en/api/javascript-api/compilation.mdx b/website/docs/en/api/javascript-api/compilation.mdx index f837d3db905..a667b081b33 100644 --- a/website/docs/en/api/javascript-api/compilation.mdx +++ b/website/docs/en/api/javascript-api/compilation.mdx @@ -669,6 +669,22 @@ List of all modules, with the structure as follows: +### builtModules + +**Type:** `ReadonlySet` + +List of built modules that were not be cached, with the structure as follows: + + + + + + + ### chunks **Type:** `ReadonlySet` diff --git a/website/docs/zh/api/javascript-api/compilation.mdx b/website/docs/zh/api/javascript-api/compilation.mdx index 683b86409c3..8bfbb9c59fd 100644 --- a/website/docs/zh/api/javascript-api/compilation.mdx +++ b/website/docs/zh/api/javascript-api/compilation.mdx @@ -666,6 +666,22 @@ getCache(name: string): CacheFacade; +### builtModules + +**类型:** `ReadonlySet` + +获取未缓存被构建的模块,其结构如下: + + + + + + + ### chunks **类型:** `ReadonlySet`