Skip to content

Commit

Permalink
feat: support compilation.builtModules (#7984)
Browse files Browse the repository at this point in the history
* feat: support compilation.builtModules

* feat: support compilation.builtModules
  • Loading branch information
LingyuCoder authored Sep 26, 2024
1 parent cfd514a commit 139fc1b
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export class JsCompilation {
getAsset(name: string): JsAsset | null
getAssetSource(name: string): JsCompatSource | null
get modules(): Array<ModuleDTO>
get builtModules(): Array<ModuleDTO>
getOptimizationBailout(): Array<JsStatsOptimizationBailout>
getChunks(): Array<JsChunk>
getNamedChunkKeys(): Array<string>
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions crates/rspack_binding_values/src/compilation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ impl JsCompilation {
.collect::<Vec<_>>()
}

#[napi(getter, ts_return_type = "Array<ModuleDTO>")]
pub fn built_modules(&'static self) -> Vec<ModuleDTOWrapper> {
self
.0
.built_modules
.iter()
.map(|module_id| ModuleDTOWrapper::new(*module_id, self.0))
.collect::<Vec<_>>()
}

#[napi]
pub fn get_optimization_bailout(&self) -> Vec<JsStatsOptimizationBailout> {
self
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 'foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { v } from "./foo";

v;

it("should run", function () { });
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const v = 'fooo'
Original file line number Diff line number Diff line change
@@ -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"]);
}
});
});
}
}]
};
2 changes: 2 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,8 @@ export class Compilation {
addAll: (deps: Iterable<string>) => void;
};
// (undocumented)
get builtModules(): ReadonlySet<Module>;
// (undocumented)
children: Compilation[];
// (undocumented)
childrenCounters: Record<string, number>;
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/Compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
);
}

get builtModules(): ReadonlySet<Module> {
return new Set(
this.#inner.builtModules.map(module =>
Module.__from_binding(module, this)
)
);
}

get chunks(): ReadonlySet<Chunk> {
return memoizeValue(() => new Set(this.__internal__getChunks()));
}
Expand Down
16 changes: 16 additions & 0 deletions website/docs/en/api/javascript-api/compilation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,22 @@ List of all modules, with the structure as follows:
</CollapsePanel>
</Collapse>
### builtModules
**Type:** `ReadonlySet<Module>`
List of built modules that were not be cached, with the structure as follows:
<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="Module.ts"
key="Module"
>
<ModuleType />
</CollapsePanel>
</Collapse>
### chunks
**Type:** `ReadonlySet<Chunk>`
Expand Down
16 changes: 16 additions & 0 deletions website/docs/zh/api/javascript-api/compilation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,22 @@ getCache(name: string): CacheFacade;
</CollapsePanel>
</Collapse>
### builtModules
**类型:** `ReadonlySet<Module>`
获取未缓存被构建的模块,其结构如下:
<Collapse>
<CollapsePanel
className="collapse-code-panel"
header="Module.ts"
key="Module"
>
<ModuleType />
</CollapsePanel>
</Collapse>
### chunks
**类型:** `ReadonlySet<Chunk>`
Expand Down

2 comments on commit 139fc1b

@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-09-26 cfd514a) Current Change
10000_development-mode + exec 2.24 s ± 15 ms 2.27 s ± 41 ms +1.02 %
10000_development-mode_hmr + exec 706 ms ± 9.9 ms 712 ms ± 17 ms +0.73 %
10000_production-mode + exec 2.9 s ± 29 ms 2.9 s ± 36 ms -0.01 %
arco-pro_development-mode + exec 1.87 s ± 77 ms 1.87 s ± 96 ms -0.24 %
arco-pro_development-mode_hmr + exec 438 ms ± 3.2 ms 437 ms ± 4.4 ms -0.14 %
arco-pro_production-mode + exec 3.34 s ± 97 ms 3.33 s ± 95 ms -0.50 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.37 s ± 84 ms 3.38 s ± 83 ms +0.44 %
threejs_development-mode_10x + exec 1.71 s ± 25 ms 1.72 s ± 17 ms +0.30 %
threejs_development-mode_10x_hmr + exec 802 ms ± 9.4 ms 803 ms ± 9.1 ms +0.09 %
threejs_production-mode_10x + exec 5.27 s ± 38 ms 5.27 s ± 39 ms +0.07 %

@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
nx ❌ failure
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ✅ success

Please sign in to comment.