Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache module info getters before output generation #5438

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 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 @@ -30,6 +31,7 @@ export default class ExternalModule {
this.suggestedVariableName = makeLegal(id.split(/[/\\]/).pop()!);

const { importers, dynamicImporters } = this;
// NOTE: any getter props should also be defined in cacheInfoGetters
this.info = {
ast: null,
attributes,
Expand Down Expand Up @@ -59,6 +61,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
19 changes: 19 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 @@ -293,6 +294,7 @@ export default class Module {
sourcesWithAttributes
} = this;

// NOTE: any getter props should also be defined in cacheInfoGetters
this.info = {
ast: null,
attributes,
Expand Down Expand Up @@ -390,6 +392,23 @@ export default class Module {
this.ast!.bind();
}

cacheInfoGetters(): void {
cacheObjectGetters(this.info, [
'dynamicallyImportedIdResolutions',
'dynamicallyImportedIds',
'dynamicImporters',
'exportedBindings',
'exports',
'hasDefaultExport',
'implicitlyLoadedAfterOneOf',
'implicitlyLoadedBefore',
'importedIdResolutions',
'importedIds',
'importers',
'isIncluded'
bluwy marked this conversation as resolved.
Show resolved Hide resolved
]);
}

error(properties: RollupError, pos: number | undefined): never {
pos !== undefined && this.addLocationToLogProps(properties, pos);
return error(properties);
Expand Down
19 changes: 19 additions & 0 deletions src/utils/getter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export function cacheObjectGetters<T, K extends PropertyKey = keyof T>(
bluwy marked this conversation as resolved.
Show resolved Hide resolved
object: T,
getterProperties: K[]
) {
for (const property of getterProperties) {
const propertyGetter = Object.getOwnPropertyDescriptor(object, property)?.get;
if (propertyGetter) {
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved
let cached: unknown;
Object.defineProperty(object, property, {
get: () => {
if (cached === undefined) {
cached = propertyGetter.call(object);
}
return cached;
}
});
bluwy marked this conversation as resolved.
Show resolved Hide resolved
}
}
}