Skip to content

Commit

Permalink
Merge branch 'master' into remove-descriptor-spread
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Feb 14, 2022
2 parents 95bfc13 + 61d6820 commit 86797b5
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 298 deletions.
2 changes: 1 addition & 1 deletion src/Graph.ts
Expand Up @@ -240,7 +240,7 @@ export default class Graph {

private warnForMissingExports(): void {
for (const module of this.modules) {
for (const importDescription of Object.values(module.importDescriptions)) {
for (const importDescription of module.importDescriptions.values()) {
if (
importDescription.name !== '*' &&
!importDescription.module.getVariableForExportName(importDescription.name)[0]
Expand Down
84 changes: 38 additions & 46 deletions src/Module.ts
Expand Up @@ -103,7 +103,7 @@ export interface AstContext {
getModuleName: () => string;
getNodeConstructor: (name: string) => typeof NodeBase;
getReexports: () => string[];
importDescriptions: { [name: string]: ImportDescription };
importDescriptions: Map<string, ImportDescription>;
includeAllExports: () => void;
includeDynamicImport: (node: ImportExpression) => void;
includeVariableInModule: (variable: Variable) => void;
Expand Down Expand Up @@ -187,7 +187,6 @@ function getAndExtendSideEffectModules(variable: Variable, module: Module): Set<

export default class Module {
readonly alternativeReexportModules = new Map<Variable, Module>();
ast: Program | null = null;
readonly chunkFileNames = new Set<string>();
chunkNames: {
isUserDefined: boolean;
Expand All @@ -201,11 +200,9 @@ export default class Module {
readonly dynamicImports: DynamicImport[] = [];
excludeFromSourcemap: boolean;
execIndex = Infinity;
readonly exportAllSources = new Set<string>();
readonly exports: { [name: string]: ExportDescription } = Object.create(null);
readonly implicitlyLoadedAfter = new Set<Module>();
readonly implicitlyLoadedBefore = new Set<Module>();
readonly importDescriptions: { [name: string]: ImportDescription } = Object.create(null);
readonly importDescriptions = new Map<string, ImportDescription>();
readonly importMetas: MetaProperty[] = [];
importedFromNotTreeshaken = false;
readonly importers: string[] = [];
Expand All @@ -219,7 +216,6 @@ export default class Module {
declare originalCode: string;
declare originalSourcemap: ExistingDecodedSourceMap | null;
preserveSignature: PreserveEntrySignaturesOption;
readonly reexportDescriptions: { [name: string]: ReexportDescription } = Object.create(null);
declare resolvedIds: ResolvedIdMap;
declare scope: ModuleScope;
readonly sideEffectDependenciesByVariable = new Map<Variable, Set<Module>>();
Expand All @@ -229,17 +225,21 @@ export default class Module {
usesTopLevelAwait = false;

private allExportNames: Set<string> | null = null;
private ast: Program | null = null;
private declare astContext: AstContext;
private readonly context: string;
private declare customTransformCache: boolean;
private readonly exportAllModules: (Module | ExternalModule)[] = [];
private readonly exportAllSources = new Set<string>();
private exportNamesByVariable: Map<Variable, string[]> | null = null;
private readonly exportShimVariable: ExportShimVariable = new ExportShimVariable(this);
private readonly exports = new Map<string, ExportDescription>();
private declare magicString: MagicString;
private namespaceReexportsByName: Record<
string,
[variable: Variable | null, indirectExternal?: boolean]
> = Object.create(null);
private readonly reexportDescriptions = new Map<string, ReexportDescription>();
private relevantDependencies: Set<Module | ExternalModule> | null = null;
private readonly syntheticExports = new Map<string, SyntheticNamedExportVariable>();
private syntheticNamespace: Variable | null | undefined = null;
Expand Down Expand Up @@ -295,7 +295,7 @@ export default class Module {
if (!module.ast) {
return null;
}
return 'default' in module.exports || 'default' in reexportDescriptions;
return module.exports.has('default') || reexportDescriptions.has('default');
},
get hasModuleSideEffects() {
warnDeprecation(
Expand Down Expand Up @@ -359,29 +359,23 @@ export default class Module {
if (this.allExportNames) {
return this.allExportNames;
}
const allExportNames = (this.allExportNames = new Set<string>());
for (const name of this.getExports()) {
allExportNames.add(name);
}
for (const name of Object.keys(this.reexportDescriptions)) {
allExportNames.add(name);
}
this.allExportNames = new Set([...this.exports.keys(), ...this.reexportDescriptions.keys()]);
for (const module of this.exportAllModules) {
if (module instanceof ExternalModule) {
allExportNames.add(`*${module.id}`);
this.allExportNames.add(`*${module.id}`);
continue;
}

for (const name of module.getAllExportNames()) {
if (name !== 'default') allExportNames.add(name);
if (name !== 'default') this.allExportNames.add(name);
}
}
// We do not count the synthetic namespace as a regular export to hide it
// from entry signatures and namespace objects
if (typeof this.info.syntheticNamedExports === 'string') {
allExportNames.delete(this.info.syntheticNamedExports);
this.allExportNames.delete(this.info.syntheticNamedExports);
}
return allExportNames;
return this.allExportNames;
}

getDependenciesToBeIncluded(): Set<Module | ExternalModule> {
Expand Down Expand Up @@ -462,7 +456,7 @@ export default class Module {
}

getExports(): string[] {
return Object.keys(this.exports);
return Array.from(this.exports.keys());
}

getReexports(): string[] {
Expand All @@ -472,10 +466,8 @@ export default class Module {
// to avoid infinite recursion when using circular `export * from X`
this.transitiveReexports = [];

const reexports = new Set<string>();
for (const name in this.reexportDescriptions) {
reexports.add(name);
}
const reexports = new Set(this.reexportDescriptions.keys());

for (const module of this.exportAllModules) {
if (module instanceof ExternalModule) {
reexports.add(`*${module.id}`);
Expand All @@ -492,7 +484,7 @@ export default class Module {
// only direct exports are counted here, not reexports at all
const renderedExports: string[] = [];
const removedExports: string[] = [];
for (const exportName in this.exports) {
for (const exportName of this.exports.keys()) {
const [variable] = this.getVariableForExportName(exportName);
(variable && variable.included ? renderedExports : removedExports).push(exportName);
}
Expand Down Expand Up @@ -543,7 +535,7 @@ export default class Module {
}

// export { foo } from './other'
const reexportDeclaration = this.reexportDescriptions[name];
const reexportDeclaration = this.reexportDescriptions.get(name);
if (reexportDeclaration) {
const [variable] = getVariableForExportNameRecursive(
reexportDeclaration.module,
Expand All @@ -564,7 +556,7 @@ export default class Module {
return [variable];
}

const exportDeclaration = this.exports[name];
const exportDeclaration = this.exports.get(name);
if (exportDeclaration) {
if (exportDeclaration === MISSING_EXPORT_SHIM_DESCRIPTION) {
return [this.exportShimVariable];
Expand Down Expand Up @@ -641,7 +633,7 @@ export default class Module {
this.graph.needsTreeshakingPass = true;
}

for (const exportName of this.getExports()) {
for (const exportName of this.exports.keys()) {
if (includeNamespaceMembers || exportName !== this.info.syntheticNamedExports) {
const variable = this.getVariableForExportName(exportName)[0]!;
variable.deoptimizePath(UNKNOWN_PATH);
Expand Down Expand Up @@ -808,8 +800,8 @@ export default class Module {
return localVariable;
}

if (name in this.importDescriptions) {
const importDeclaration = this.importDescriptions[name];
const importDeclaration = this.importDescriptions.get(name);
if (importDeclaration) {
const otherModule = importDeclaration.module;

if (otherModule instanceof Module && importDeclaration.name === '*') {
Expand Down Expand Up @@ -893,23 +885,23 @@ export default class Module {
if (node instanceof ExportDefaultDeclaration) {
// export default foo;

this.exports.default = {
this.exports.set('default', {
identifier: node.variable.getAssignedVariableName(),
localName: 'default'
};
});
} else if (node instanceof ExportAllDeclaration) {
const source = node.source.value;
this.sources.add(source);
if (node.exported) {
// export * as name from './other'

const name = node.exported.name;
this.reexportDescriptions[name] = {
this.reexportDescriptions.set(name, {
localName: '*',
module: null as never, // filled in later,
source,
start: node.start
};
});
} else {
// export * from './other'

Expand All @@ -922,12 +914,12 @@ export default class Module {
this.sources.add(source);
for (const specifier of node.specifiers) {
const name = specifier.exported.name;
this.reexportDescriptions[name] = {
this.reexportDescriptions.set(name, {
localName: specifier.local.name,
module: null as never, // filled in later,
source,
start: specifier.start
};
});
}
} else if (node.declaration) {
const declaration = node.declaration;
Expand All @@ -937,22 +929,22 @@ export default class Module {

for (const declarator of declaration.declarations) {
for (const localName of extractAssignedNames(declarator.id)) {
this.exports[localName] = { identifier: null, localName };
this.exports.set(localName, { identifier: null, localName });
}
}
} else {
// export function foo () {}

const localName = (declaration.id as Identifier).name;
this.exports[localName] = { identifier: null, localName };
this.exports.set(localName, { identifier: null, localName });
}
} else {
// export { foo, bar, baz }

for (const specifier of node.specifiers) {
const localName = specifier.local.name;
const exportedName = specifier.exported.name;
this.exports[exportedName] = { identifier: null, localName };
this.exports.set(exportedName, { identifier: null, localName });
}
}
}
Expand All @@ -965,12 +957,12 @@ export default class Module {
const isNamespace = specifier.type === NodeType.ImportNamespaceSpecifier;

const name = isDefault ? 'default' : isNamespace ? '*' : specifier.imported.name;
this.importDescriptions[specifier.local.name] = {
this.importDescriptions.set(specifier.local.name, {
module: null as never, // filled in later
name,
source,
start: specifier.start
};
});
}
}

Expand Down Expand Up @@ -1005,11 +997,11 @@ export default class Module {
}
}

private addModulesToImportDescriptions(importDescription: {
[name: string]: ImportDescription | ReexportDescription;
}): void {
for (const specifier of Object.values(importDescription)) {
const id = this.resolvedIds[specifier.source].id;
private addModulesToImportDescriptions(
importDescription: ReadonlyMap<string, ImportDescription | ReexportDescription>
): void {
for (const specifier of importDescription.values()) {
const { id } = this.resolvedIds[specifier.source];
specifier.module = this.graph.modulesById.get(id)!;
}
}
Expand Down Expand Up @@ -1183,7 +1175,7 @@ export default class Module {
exportName: name,
message: `Missing export "${name}" has been shimmed in module ${relativeId(this.id)}.`
});
this.exports[name] = MISSING_EXPORT_SHIM_DESCRIPTION;
this.exports.set(name, MISSING_EXPORT_SHIM_DESCRIPTION);
}
}

Expand Down
37 changes: 18 additions & 19 deletions test/function/samples/custom-module-options/_config.js
@@ -1,4 +1,5 @@
const assert = require('assert');
const { getObject } = require('../../../utils');

function getTestPlugin(index) {
const pluginName = `test-${index}`;
Expand Down Expand Up @@ -40,28 +41,26 @@ module.exports = {
name: 'wrap-up',
buildEnd() {
assert.deepStrictEqual(
[...this.getModuleIds()]
.filter(id => id.includes('resolve'))
.sort()
.map(id => ({ id, meta: this.getModuleInfo(id).meta })),
[
{
id: 'resolve1-load2-transform3',
meta: {
'test-1': { resolved: 1 },
'test-2': { loaded: 2 },
'test-3': { transformed: 3 }
}
getObject(
[...this.getModuleIds()]
.filter(id => id.includes('resolve'))
.map(id => [id, this.getModuleInfo(id).meta])
),
{
'resolve1-load2-transform3': {
'test-1': { resolved: 1 },
'test-2': { loaded: 2 },
'test-3': { transformed: 3 }
},
{
id: 'resolve2-load2-transform3',
meta: { 'test-2': { loaded: 2 }, 'test-3': { transformed: 3 } }
'resolve2-load2-transform3': {
'test-2': { loaded: 2 },
'test-3': { transformed: 3 }
},
{
id: 'resolve3-load3-transform1-transform3',
meta: { 'test-3': { transformed: 3 }, 'test-1': { transformed: 1 } }
'resolve3-load3-transform1-transform3': {
'test-3': { transformed: 3 },
'test-1': { transformed: 1 }
}
]
}
);
}
}
Expand Down

0 comments on commit 86797b5

Please sign in to comment.