diff --git a/packages/rspack/src/Dependency.ts b/packages/rspack/src/Dependency.ts index 0e86b221d08..6bfd3f1a7c1 100644 --- a/packages/rspack/src/Dependency.ts +++ b/packages/rspack/src/Dependency.ts @@ -26,20 +26,27 @@ export const bindingDependencyFactory = { }; export class Dependency { + #type: string | undefined; + #category: string | undefined; + get type(): string { - const binding = bindingDependencyFactory.getBinding(this); - if (binding) { - return binding.type; + if (this.#type === undefined) { + const binding = bindingDependencyFactory.getBinding(this); + if (binding) { + this.#type = binding.type; + } } - return "unknown"; + return this.#type || "unknown"; } get category(): string { - const binding = bindingDependencyFactory.getBinding(this); - if (binding) { - return binding.category; + if (this.#category === undefined) { + const binding = bindingDependencyFactory.getBinding(this); + if (binding) { + this.#category = binding.category; + } } - return "unknown"; + return this.#category || "unknown"; } get request(): string | undefined { diff --git a/packages/rspack/src/Module.ts b/packages/rspack/src/Module.ts index 5c80563658b..7a36e624873 100644 --- a/packages/rspack/src/Module.ts +++ b/packages/rspack/src/Module.ts @@ -202,6 +202,7 @@ const BUILD_META_MAPPINGS = new Map>(); export class Module { #inner: JsModule; + #identifier: string | undefined; declare readonly context?: string; declare readonly resource?: string; @@ -372,7 +373,10 @@ export class Module { } identifier(): string { - return this.#inner.moduleIdentifier; + if (this.#identifier === undefined) { + this.#identifier = this.#inner.moduleIdentifier; + } + return this.#identifier; } nameForCondition(): string | null { diff --git a/packages/rspack/src/ModuleGraphConnection.ts b/packages/rspack/src/ModuleGraphConnection.ts index abda403c6f5..1e6b18d8986 100644 --- a/packages/rspack/src/ModuleGraphConnection.ts +++ b/packages/rspack/src/ModuleGraphConnection.ts @@ -12,6 +12,8 @@ export class ModuleGraphConnection { declare readonly dependency: Dependency; #inner: JsModuleGraphConnection; + #dependency: Dependency | undefined; + #resolvedModule: Module | undefined | null; static __from_binding(binding: JsModuleGraphConnection) { let connection = MODULE_GRAPH_CONNECTION_MAPPINGS.get(binding); @@ -39,19 +41,27 @@ export class ModuleGraphConnection { }, dependency: { enumerable: true, - get(): Dependency { - return bindingDependencyFactory.create( + get: (): Dependency => { + if (this.#dependency !== undefined) { + return this.#dependency; + } + this.#dependency = bindingDependencyFactory.create( Dependency, binding.dependency ); + return this.#dependency; } }, resolvedModule: { enumerable: true, - get(): Module | null { - return binding.resolvedModule + get: (): Module | null => { + if (this.#resolvedModule !== undefined) { + return this.#resolvedModule; + } + this.#resolvedModule = binding.resolvedModule ? Module.__from_binding(binding.resolvedModule) : null; + return this.#resolvedModule; } }, originModule: {