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: null,
exports: null,
hasDefaultExport: null,
get hasModuleSideEffects() {
warnDeprecation(
Expand Down
24 changes: 24 additions & 0 deletions src/Module.ts
Expand Up @@ -274,6 +274,8 @@ export default class Module {
const {
dynamicImports,
dynamicImporters,
exportAllSources,
exports,
implicitlyLoadedAfter,
implicitlyLoadedBefore,
importers,
Expand All @@ -298,6 +300,28 @@ export default class Module {
get dynamicImporters() {
return dynamicImporters.sort();
},
get exportedBindings() {
const reexportBindings: Record<string, string[]> = {};

for (const [name, { source }] of reexportDescriptions) {
(reexportBindings[source] ??= []).push(name);
}

for (const source of exportAllSources) {
(reexportBindings[source] ??= []).push('*');
}

const exportBindings = { '.': [...exports.keys()] };

return { ...exportBindings, ...reexportBindings };
TrickyPi marked this conversation as resolved.
Show resolved Hide resolved
},
get exports() {
return [
...exports.keys(),
...reexportDescriptions.keys(),
...[...exportAllSources].map(() => '*')
];
},
get hasDefaultExport() {
// This information is only valid after parsing
if (!module.ast) {
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[]> | null;
exports: string[] | null;
hasDefaultExport: boolean | null;
/** @deprecated Use `moduleSideEffects` instead */
hasModuleSideEffects: boolean | 'no-treeshake';
Expand Down
Expand Up @@ -75,6 +75,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -148,6 +152,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
Expand Up @@ -71,6 +71,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -144,6 +148,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exports: [],
exportedBindings: {
'.': []
},
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
Expand Up @@ -119,6 +119,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -243,6 +247,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -366,6 +374,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [ID_MAIN1, ID_MAIN2],
Expand Down
Expand Up @@ -70,6 +70,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -143,6 +147,10 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [ID_MAIN],
Expand Down
@@ -0,0 +1,33 @@
const assert = require('node:assert');
const path = require('node:path');

const ID_MAIN = path.join(__dirname, 'main.js');
const ID_MODULE = path.join(__dirname, 'module.js');
const ID_MODULE_2 = path.join(__dirname, 'module2.js');

const expectedResult = {
[ID_MAIN]: {
exports: ['moduleAlias', '*'],
exportedBindings: { '.': [], './module.js': ['moduleAlias', '*'] }
},
[ID_MODULE]: {
exports: ['default', 'module', 'module2'],
exportedBindings: { '.': ['default', 'module'], './module2.js': ['module2'] }
},
[ID_MODULE_2]: {
exports: ['module2'],
exportedBindings: { '.': ['module2'] }
}
};

module.exports = {
description: 'check exports and exportedBindings in moduleParsed as supplementary test',
options: {
plugins: {
moduleParsed(moduleInfo) {
const { exports, exportedBindings, id } = moduleInfo;
assert.deepStrictEqual({ exports, exportedBindings }, expectedResult[id]);
}
}
}
};
@@ -0,0 +1,3 @@
export * from './module.js';
export { module as moduleAlias } from './module.js';
assert.ok(true);
@@ -0,0 +1,3 @@
export * as module2 from './module2.js';
export default 1;
export const module = 1;
@@ -0,0 +1 @@
export const module2 = 1;
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: null,
exports: null,
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: null,
exports: null,
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
15 changes: 15 additions & 0 deletions test/function/samples/plugin-module-information/_config.js
Expand Up @@ -20,6 +20,10 @@ module.exports = {
ast: null,
code: null,
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: null,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
Expand Down Expand Up @@ -113,6 +117,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 +290,11 @@ module.exports = {
],
dynamicallyImportedIds: [ID_NESTED, ID_PATH],
dynamicImporters: [],
exportedBindings: {
'.': ['nested', 'path', 'pathAgain'],
'./foo.js': ['foo']
},
exports: ['nested', 'path', 'pathAgain', 'foo'],
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -377,6 +388,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [ID_MAIN],
exports: ['nested'],
exportedBindings: { '.': ['nested'] },
hasDefaultExport: false,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down Expand Up @@ -407,6 +420,8 @@ module.exports = {
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
dynamicImporters: [ID_MAIN],
exportedBindings: null,
exports: null,
hasDefaultExport: null,
moduleSideEffects: true,
implicitlyLoadedAfterOneOf: [],
Expand Down
8 changes: 8 additions & 0 deletions test/function/samples/preload-module/_config.js
Expand Up @@ -35,6 +35,10 @@ module.exports = {
assertions: {},
code: "import './dep';\nassert.ok(true);\n",
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
Expand Down Expand Up @@ -74,6 +78,10 @@ module.exports = {
assertions: {},
code: 'assert.ok(true);\n',
dynamicImporters: [],
exportedBindings: {
'.': []
},
exports: [],
hasDefaultExport: false,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
Expand Down