Skip to content

Commit

Permalink
refactor: convert exportsByName object to map (#4362)
Browse files Browse the repository at this point in the history
* refactor: convert exportsByName object to map

* refactor: use nullish assigment, nullish coalescing

* Make Addons properties non-optional

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 Jan 25, 2022
1 parent 9eeb6a0 commit f30e6f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 29 deletions.
30 changes: 12 additions & 18 deletions src/Chunk.ts
Expand Up @@ -143,7 +143,7 @@ export default class Chunk {
private dynamicName: string | null = null;
private readonly exportNamesByVariable = new Map<Variable, string[]>();
private readonly exports = new Set<Variable>();
private readonly exportsByName: Record<string, Variable> = Object.create(null);
private readonly exportsByName = new Map<string, Variable>();
private fileName: string | null = null;
private implicitEntryModules: Module[] = [];
private readonly implicitlyLoadedBefore = new Set<Chunk>();
Expand Down Expand Up @@ -295,7 +295,7 @@ export default class Chunk {
for (const [variable, exportNames] of exportNamesByVariable) {
this.exportNamesByVariable.set(variable, [...exportNames]);
for (const exportName of exportNames) {
this.exportsByName[exportName] = variable;
this.exportsByName.set(exportName, variable);
}
remainingExports.delete(variable);
}
Expand Down Expand Up @@ -503,15 +503,11 @@ export default class Chunk {
}

getChunkName(): string {
return (
this.name || (this.name = this.outputOptions.sanitizeFileName(this.getFallbackChunkName()))
);
return (this.name ??= this.outputOptions.sanitizeFileName(this.getFallbackChunkName()));
}

getExportNames(): string[] {
return (
this.sortedExportNames || (this.sortedExportNames = Object.keys(this.exportsByName).sort())
);
return (this.sortedExportNames ??= Array.from(this.exportsByName.keys()).sort());
}

getRenderedHash(): string {
Expand All @@ -533,7 +529,7 @@ export default class Chunk {
hash.update(
this.getExportNames()
.map(exportName => {
const variable = this.exportsByName[exportName];
const variable = this.exportsByName.get(exportName)!;
return `${relativeId((variable.module as Module).id).replace(/\\/g, '/')}:${
variable.name
}:${exportName}`;
Expand Down Expand Up @@ -741,13 +737,13 @@ export default class Chunk {
hasExports,
id: this.id,
indent: this.indentString,
intro: addons.intro!,
intro: addons.intro,
isEntryFacade:
this.outputOptions.preserveModules ||
(this.facadeModule !== null && this.facadeModule.info.isEntry),
isModuleFacade: this.facadeModule !== null,
namedExportsMode: this.exportMode !== 'default',
outro: addons.outro!,
outro: addons.outro,
snippets,
usesTopLevelAwait,
warn: this.inputOptions.onwarn
Expand Down Expand Up @@ -868,14 +864,12 @@ export default class Chunk {
existingNames: Record<string, unknown>
): string {
const hash = createHash();
hash.update(
[addons.intro, addons.outro, addons.banner, addons.footer].map(addon => addon || '').join(':')
);
hash.update([addons.intro, addons.outro, addons.banner, addons.footer].join(':'));
hash.update(options.format);
const dependenciesForHashing = new Set<Chunk | ExternalModule>([this]);
for (const current of dependenciesForHashing) {
if (current instanceof ExternalModule) {
hash.update(':' + current.renderPath);
hash.update(`:${current.renderPath}`);
} else {
hash.update(current.getRenderedHash());
hash.update(current.generateId(addons, options, existingNames, false));
Expand Down Expand Up @@ -1013,7 +1007,7 @@ export default class Chunk {
for (const exportName of this.getExportNames()) {
if (exportName[0] === '*') continue;

const variable = this.exportsByName[exportName];
const variable = this.exportsByName.get(exportName)!;
if (!(variable instanceof SyntheticNamedExportVariable)) {
const module = variable.module;
if (module && this.chunkByModule.get(module as Module) !== this) continue;
Expand Down Expand Up @@ -1171,7 +1165,7 @@ export default class Chunk {
dependency = this.modulesById.get(id) as ExternalModule;
imported = exportName = '*';
} else {
const variable = this.exportsByName[exportName];
const variable = this.exportsByName.get(exportName)!;
if (variable instanceof SyntheticNamedExportVariable) continue;
const module = variable.module!;
if (module instanceof Module) {
Expand Down Expand Up @@ -1287,7 +1281,7 @@ export default class Chunk {
}: NormalizedOutputOptions) {
const syntheticExports = new Set<SyntheticNamedExportVariable>();
for (const exportName of this.getExportNames()) {
const exportVariable = this.exportsByName[exportName];
const exportVariable = this.exportsByName.get(exportName)!;
if (
format !== 'es' &&
format !== 'system' &&
Expand Down
8 changes: 4 additions & 4 deletions src/utils/addons.ts
Expand Up @@ -3,10 +3,10 @@ import { PluginDriver } from './PluginDriver';
import { error } from './error';

export interface Addons {
banner?: string;
footer?: string;
intro?: string;
outro?: string;
banner: string;
footer: string;
intro: string;
outro: string;
}

const concatSep = (out: string, next: string) => (next ? `${out}\n${next}` : out);
Expand Down
14 changes: 7 additions & 7 deletions src/utils/exportNames.ts
Expand Up @@ -4,39 +4,39 @@ import { toBase64 } from './base64';

export function assignExportsToMangledNames(
exports: ReadonlySet<Variable>,
exportsByName: Record<string, Variable>,
exportsByName: Map<string, Variable>,
exportNamesByVariable: Map<Variable, string[]>
): void {
let nameIndex = 0;
for (const variable of exports) {
let [exportName] = variable.name;
if (exportsByName[exportName]) {
if (exportsByName.has(exportName)) {
do {
exportName = toBase64(++nameIndex);
// skip past leading number identifiers
if (exportName.charCodeAt(0) === 49 /* '1' */) {
nameIndex += 9 * 64 ** (exportName.length - 1);
exportName = toBase64(nameIndex);
}
} while (RESERVED_NAMES.has(exportName) || exportsByName[exportName]);
} while (RESERVED_NAMES.has(exportName) || exportsByName.has(exportName));
}
exportsByName[exportName] = variable;
exportsByName.set(exportName, variable);
exportNamesByVariable.set(variable, [exportName]);
}
}

export function assignExportsToNames(
exports: ReadonlySet<Variable>,
exportsByName: Record<string, Variable>,
exportsByName: Map<string, Variable>,
exportNamesByVariable: Map<Variable, string[]>
): void {
for (const variable of exports) {
let nameIndex = 0;
let exportName = variable.name;
while (exportsByName[exportName]) {
while (exportsByName.has(exportName)) {
exportName = variable.name + '$' + ++nameIndex;
}
exportsByName[exportName] = variable;
exportsByName.set(exportName, variable);
exportNamesByVariable.set(variable, [exportName]);
}
}

0 comments on commit f30e6f0

Please sign in to comment.