Skip to content

Commit f30e6f0

Browse files
refactor: convert exportsByName object to map (#4362)
* refactor: convert exportsByName object to map * refactor: use nullish assigment, nullish coalescing * Make Addons properties non-optional Co-authored-by: Lukas Taegert-Atkinson <[email protected]> Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent 9eeb6a0 commit f30e6f0

3 files changed

Lines changed: 23 additions & 29 deletions

File tree

src/Chunk.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export default class Chunk {
143143
private dynamicName: string | null = null;
144144
private readonly exportNamesByVariable = new Map<Variable, string[]>();
145145
private readonly exports = new Set<Variable>();
146-
private readonly exportsByName: Record<string, Variable> = Object.create(null);
146+
private readonly exportsByName = new Map<string, Variable>();
147147
private fileName: string | null = null;
148148
private implicitEntryModules: Module[] = [];
149149
private readonly implicitlyLoadedBefore = new Set<Chunk>();
@@ -295,7 +295,7 @@ export default class Chunk {
295295
for (const [variable, exportNames] of exportNamesByVariable) {
296296
this.exportNamesByVariable.set(variable, [...exportNames]);
297297
for (const exportName of exportNames) {
298-
this.exportsByName[exportName] = variable;
298+
this.exportsByName.set(exportName, variable);
299299
}
300300
remainingExports.delete(variable);
301301
}
@@ -503,15 +503,11 @@ export default class Chunk {
503503
}
504504

505505
getChunkName(): string {
506-
return (
507-
this.name || (this.name = this.outputOptions.sanitizeFileName(this.getFallbackChunkName()))
508-
);
506+
return (this.name ??= this.outputOptions.sanitizeFileName(this.getFallbackChunkName()));
509507
}
510508

511509
getExportNames(): string[] {
512-
return (
513-
this.sortedExportNames || (this.sortedExportNames = Object.keys(this.exportsByName).sort())
514-
);
510+
return (this.sortedExportNames ??= Array.from(this.exportsByName.keys()).sort());
515511
}
516512

517513
getRenderedHash(): string {
@@ -533,7 +529,7 @@ export default class Chunk {
533529
hash.update(
534530
this.getExportNames()
535531
.map(exportName => {
536-
const variable = this.exportsByName[exportName];
532+
const variable = this.exportsByName.get(exportName)!;
537533
return `${relativeId((variable.module as Module).id).replace(/\\/g, '/')}:${
538534
variable.name
539535
}:${exportName}`;
@@ -741,13 +737,13 @@ export default class Chunk {
741737
hasExports,
742738
id: this.id,
743739
indent: this.indentString,
744-
intro: addons.intro!,
740+
intro: addons.intro,
745741
isEntryFacade:
746742
this.outputOptions.preserveModules ||
747743
(this.facadeModule !== null && this.facadeModule.info.isEntry),
748744
isModuleFacade: this.facadeModule !== null,
749745
namedExportsMode: this.exportMode !== 'default',
750-
outro: addons.outro!,
746+
outro: addons.outro,
751747
snippets,
752748
usesTopLevelAwait,
753749
warn: this.inputOptions.onwarn
@@ -868,14 +864,12 @@ export default class Chunk {
868864
existingNames: Record<string, unknown>
869865
): string {
870866
const hash = createHash();
871-
hash.update(
872-
[addons.intro, addons.outro, addons.banner, addons.footer].map(addon => addon || '').join(':')
873-
);
867+
hash.update([addons.intro, addons.outro, addons.banner, addons.footer].join(':'));
874868
hash.update(options.format);
875869
const dependenciesForHashing = new Set<Chunk | ExternalModule>([this]);
876870
for (const current of dependenciesForHashing) {
877871
if (current instanceof ExternalModule) {
878-
hash.update(':' + current.renderPath);
872+
hash.update(`:${current.renderPath}`);
879873
} else {
880874
hash.update(current.getRenderedHash());
881875
hash.update(current.generateId(addons, options, existingNames, false));
@@ -1013,7 +1007,7 @@ export default class Chunk {
10131007
for (const exportName of this.getExportNames()) {
10141008
if (exportName[0] === '*') continue;
10151009

1016-
const variable = this.exportsByName[exportName];
1010+
const variable = this.exportsByName.get(exportName)!;
10171011
if (!(variable instanceof SyntheticNamedExportVariable)) {
10181012
const module = variable.module;
10191013
if (module && this.chunkByModule.get(module as Module) !== this) continue;
@@ -1171,7 +1165,7 @@ export default class Chunk {
11711165
dependency = this.modulesById.get(id) as ExternalModule;
11721166
imported = exportName = '*';
11731167
} else {
1174-
const variable = this.exportsByName[exportName];
1168+
const variable = this.exportsByName.get(exportName)!;
11751169
if (variable instanceof SyntheticNamedExportVariable) continue;
11761170
const module = variable.module!;
11771171
if (module instanceof Module) {
@@ -1287,7 +1281,7 @@ export default class Chunk {
12871281
}: NormalizedOutputOptions) {
12881282
const syntheticExports = new Set<SyntheticNamedExportVariable>();
12891283
for (const exportName of this.getExportNames()) {
1290-
const exportVariable = this.exportsByName[exportName];
1284+
const exportVariable = this.exportsByName.get(exportName)!;
12911285
if (
12921286
format !== 'es' &&
12931287
format !== 'system' &&

src/utils/addons.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import { PluginDriver } from './PluginDriver';
33
import { error } from './error';
44

55
export interface Addons {
6-
banner?: string;
7-
footer?: string;
8-
intro?: string;
9-
outro?: string;
6+
banner: string;
7+
footer: string;
8+
intro: string;
9+
outro: string;
1010
}
1111

1212
const concatSep = (out: string, next: string) => (next ? `${out}\n${next}` : out);

src/utils/exportNames.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@ import { toBase64 } from './base64';
44

55
export function assignExportsToMangledNames(
66
exports: ReadonlySet<Variable>,
7-
exportsByName: Record<string, Variable>,
7+
exportsByName: Map<string, Variable>,
88
exportNamesByVariable: Map<Variable, string[]>
99
): void {
1010
let nameIndex = 0;
1111
for (const variable of exports) {
1212
let [exportName] = variable.name;
13-
if (exportsByName[exportName]) {
13+
if (exportsByName.has(exportName)) {
1414
do {
1515
exportName = toBase64(++nameIndex);
1616
// skip past leading number identifiers
1717
if (exportName.charCodeAt(0) === 49 /* '1' */) {
1818
nameIndex += 9 * 64 ** (exportName.length - 1);
1919
exportName = toBase64(nameIndex);
2020
}
21-
} while (RESERVED_NAMES.has(exportName) || exportsByName[exportName]);
21+
} while (RESERVED_NAMES.has(exportName) || exportsByName.has(exportName));
2222
}
23-
exportsByName[exportName] = variable;
23+
exportsByName.set(exportName, variable);
2424
exportNamesByVariable.set(variable, [exportName]);
2525
}
2626
}
2727

2828
export function assignExportsToNames(
2929
exports: ReadonlySet<Variable>,
30-
exportsByName: Record<string, Variable>,
30+
exportsByName: Map<string, Variable>,
3131
exportNamesByVariable: Map<Variable, string[]>
3232
): void {
3333
for (const variable of exports) {
3434
let nameIndex = 0;
3535
let exportName = variable.name;
36-
while (exportsByName[exportName]) {
36+
while (exportsByName.has(exportName)) {
3737
exportName = variable.name + '$' + ++nameIndex;
3838
}
39-
exportsByName[exportName] = variable;
39+
exportsByName.set(exportName, variable);
4040
exportNamesByVariable.set(variable, [exportName]);
4141
}
4242
}

0 commit comments

Comments
 (0)