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

feat: add exports and exportedBindings to Module class #4731

Merged
merged 12 commits into from Dec 5, 2022
2 changes: 2 additions & 0 deletions src/ExternalModule.ts
Expand Up @@ -38,6 +38,8 @@ export default class ExternalModule {
get dynamicImporters() {
return dynamicImporters.sort();
},
exportedBindings: {},
exports: [],
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved
hasDefaultExport: null,
get hasModuleSideEffects() {
warnDeprecation(
Expand Down
16 changes: 16 additions & 0 deletions src/Module.ts
Expand Up @@ -298,6 +298,8 @@ export default class Module {
get dynamicImporters() {
return dynamicImporters.sort();
},
exportedBindings: {},
exports: [],
get hasDefaultExport() {
// This information is only valid after parsing
if (!module.ast) {
Expand Down Expand Up @@ -923,6 +925,7 @@ export default class Module {
identifier: node.variable.getAssignedVariableName(),
localName: 'default'
});
this.addExportsInfoToModuleInfo('default');
} else if (node instanceof ExportAllDeclaration) {
const source = node.source.value;
this.addSource(source, node);
Expand All @@ -936,10 +939,12 @@ export default class Module {
source,
start: node.start
});
this.addExportsInfoToModuleInfo(name, source);
} else {
// export * from './other'

this.exportAllSources.add(source);
this.addExportsInfoToModuleInfo('*', source);
}
} else if (node.source instanceof Literal) {
// export { name } from './other'
Expand All @@ -954,6 +959,7 @@ export default class Module {
source,
start: specifier.start
});
this.addExportsInfoToModuleInfo(name, source);
}
} else if (node.declaration) {
const declaration = node.declaration;
Expand All @@ -964,13 +970,15 @@ export default class Module {
for (const declarator of declaration.declarations) {
for (const localName of extractAssignedNames(declarator.id)) {
this.exports.set(localName, { identifier: null, localName });
this.addExportsInfoToModuleInfo(localName);
}
}
} else {
// export function foo () {}

const localName = (declaration.id as Identifier).name;
this.exports.set(localName, { identifier: null, localName });
this.addExportsInfoToModuleInfo(localName);
}
} else {
// export { foo, bar, baz }
Expand All @@ -979,10 +987,18 @@ export default class Module {
const localName = specifier.local.name;
const exportedName = specifier.exported.name;
this.exports.set(exportedName, { identifier: null, localName });
this.addExportsInfoToModuleInfo(exportedName);
}
}
}

private addExportsInfoToModuleInfo(id: string, path = '.') {
const { exports, exportedBindings } = this.info;
exports.push(id);
exportedBindings[path] = exportedBindings[path] || [];
exportedBindings[path].push(id);
}

private addImport(node: ImportDeclaration): void {
const source = node.source.value;
this.addSource(source, node);
Expand Down
2 changes: 2 additions & 0 deletions src/rollup/types.d.ts
Expand Up @@ -157,6 +157,8 @@ interface ModuleInfo extends ModuleOptions {
dynamicImporters: readonly string[];
dynamicallyImportedIdResolutions: readonly ResolvedId[];
dynamicallyImportedIds: readonly string[];
exportedBindings: Record<string, string[]>;
exports: string[];
hasDefaultExport: boolean | null;
/** @deprecated Use `moduleSideEffects` instead */
hasModuleSideEffects: boolean | 'no-treeshake';
Expand Down
Expand Up @@ -75,6 +75,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -148,6 +150,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
Expand Up @@ -71,6 +71,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -144,6 +146,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exports: [],
exportedBindings: {},
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
Expand Up @@ -119,6 +119,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -243,6 +245,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -366,6 +370,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [ID_MAIN1, ID_MAIN2],
Expand Down
Expand Up @@ -70,6 +70,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -143,6 +145,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [ID_MAIN],
Expand Down
Expand Up @@ -91,6 +91,8 @@ module.exports = {
],
dynamicallyImportedIds: ['external'],
dynamicImporters: [getId('main')],
exports: ['promise', 'internal'],
exportedBindings: { '.': ['promise'], './lib': ['internal'] },
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -134,6 +136,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: { '.': ['default'] },
exports: ['default'],
hasDefaultExport: true,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -242,6 +246,8 @@ module.exports = {
],
dynamicallyImportedIds: [getId('dynamic')],
dynamicImporters: [],
exportedBindings: { '.': ['promise'], './lib': ['value'], external: ['external'] },
exports: ['promise', 'value', 'external'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -280,6 +286,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [getId('dynamic')],
exportedBindings: {},
exports: [],
hasDefaultExport: null,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
12 changes: 12 additions & 0 deletions test/function/samples/manual-chunks-info/_config.js
Expand Up @@ -92,6 +92,8 @@ module.exports = {
],
dynamicallyImportedIds: ['external'],
dynamicImporters: [getId('main')],
exportedBindings: { '.': ['promise'], './lib': ['internal'] },
exports: ['promise', 'internal'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -135,6 +137,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: { '.': ['default'] },
exports: ['default'],
hasDefaultExport: true,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -243,6 +247,12 @@ module.exports = {
],
dynamicallyImportedIds: [getId('dynamic')],
dynamicImporters: [],
exportedBindings: {
'.': ['promise'],
'./lib': ['value'],
external: ['external']
},
exports: ['promise', 'value', 'external'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -281,6 +291,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [getId('dynamic')],
exportedBindings: {},
exports: [],
hasDefaultExport: null,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
4 changes: 4 additions & 0 deletions test/function/samples/module-parsed-hook/_config.js
Expand Up @@ -53,6 +53,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: { './dep.js': ['value'] },
exports: ['value'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -112,6 +114,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: { '.': ['value'] },
exports: ['value'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
13 changes: 13 additions & 0 deletions test/function/samples/plugin-module-information/_config.js
Expand Up @@ -20,6 +20,8 @@ module.exports = {
ast: null,
code: null,
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: null,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
Expand Down Expand Up @@ -113,6 +115,8 @@ module.exports = {
},
code: "import path from 'path';\n\nexport const foo = path.resolve('foo');\n",
dynamicallyImportedIdResolutions: [],
exportedBindings: { '.': ['foo'] },
exports: ['foo'],
dynamicallyImportedIds: [],
dynamicImporters: [],
hasDefaultExport: false,
Expand Down Expand Up @@ -284,6 +288,11 @@ module.exports = {
],
dynamicallyImportedIds: [ID_NESTED, ID_PATH],
dynamicImporters: [],
exportedBindings: {
'.': ['nested', 'path', 'pathAgain'],
'./foo.js': ['foo']
},
exports: ['foo', 'nested', 'path', 'pathAgain'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -377,6 +386,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [ID_MAIN],
exports: ['nested'],
exportedBindings: { '.': ['nested'] },
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -407,6 +418,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [ID_MAIN],
exportedBindings: {},
exports: [],
hasDefaultExport: null,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
4 changes: 4 additions & 0 deletions test/function/samples/preload-module/_config.js
Expand Up @@ -35,6 +35,8 @@ module.exports = {
assertions: {},
code: "import './dep';\nassert.ok(true);\n",
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
Expand Down Expand Up @@ -74,6 +76,8 @@ module.exports = {
assertions: {},
code: 'assert.ok(true);\n',
dynamicImporters: [],
exportedBindings: {},
exports: [],
hasDefaultExport: false,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
Expand Down