Skip to content

Commit

Permalink
Cache module info getters before output generation (#5438)
Browse files Browse the repository at this point in the history
* Cache module info getters before output generation

* Update ExternalModule.ts

* Update Module.ts

* Update src/utils/getter.ts

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>

* Remove safeguards as we are converting a hand-picked list

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
3 people committed Mar 28, 2024
1 parent fffaede commit 41ba566
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/ExternalModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ExternalVariable from './ast/variables/ExternalVariable';
import type { CustomPluginOptions, ModuleInfo, NormalizedInputOptions } from './rollup/types';
import { EMPTY_ARRAY } from './utils/blank';
import { cacheObjectGetters } from './utils/getter';
import { makeLegal } from './utils/identifierHelpers';
import { LOGLEVEL_WARN } from './utils/logging';
import { logUnusedExternalImports } from './utils/logs';
Expand Down Expand Up @@ -59,6 +60,10 @@ export default class ExternalModule {
};
}

cacheInfoGetters(): void {
cacheObjectGetters(this.info, ['dynamicImporters', 'importers']);
}

getVariableForExportName(name: string): [variable: ExternalVariable] {
const declaration = this.declarations.get(name);
if (declaration) return [declaration];
Expand Down
1 change: 1 addition & 0 deletions src/Graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export default class Graph {
throw new Error('You must supply options.input to rollup');
}
for (const module of this.modulesById.values()) {
module.cacheInfoGetters();
if (module instanceof Module) {
this.modules.push(module);
} else {
Expand Down
17 changes: 17 additions & 0 deletions src/Module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { decodedSourcemap, resetSourcemapCache } from './utils/decodedSourcemap'
import { getId } from './utils/getId';
import { getNewSet, getOrCreate } from './utils/getOrCreate';
import { getOriginalLocation } from './utils/getOriginalLocation';
import { cacheObjectGetters } from './utils/getter';
import { makeLegal } from './utils/identifierHelpers';
import { LOGLEVEL_WARN } from './utils/logging';
import {
Expand Down Expand Up @@ -390,6 +391,22 @@ export default class Module {
this.ast!.bind();
}

cacheInfoGetters(): void {
cacheObjectGetters(this.info, [
'dynamicallyImportedIdResolutions',
'dynamicallyImportedIds',
'dynamicImporters',
'exportedBindings',
'exports',
'hasDefaultExport',
'implicitlyLoadedAfterOneOf',
'implicitlyLoadedBefore',
'importedIdResolutions',
'importedIds',
'importers'
]);
}

error(properties: RollupError, pos: number | undefined): never {
pos !== undefined && this.addLocationToLogProps(properties, pos);
return error(properties);
Expand Down
16 changes: 16 additions & 0 deletions src/utils/getter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export function cacheObjectGetters<T, K extends PropertyKey = keyof T>(
object: T,
getterProperties: K[]
) {
for (const property of getterProperties) {
const propertyGetter = Object.getOwnPropertyDescriptor(object, property)!.get!;
Object.defineProperty(object, property, {
get() {
const value = propertyGetter.call(object);
// This replaces the getter with a fixed value for subsequent calls
Object.defineProperty(object, property, { value });
return value;
}
});
}
}

0 comments on commit 41ba566

Please sign in to comment.