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
26 changes: 26 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,30 @@ export default class Module {
get dynamicImporters() {
return dynamicImporters.sort();
},
get exportedBindings() {
const reexportBindings: Record<string, string[]> = {};
for (const [name, { source }] of reexportDescriptions) {
reexportBindings[source] ??= [];
reexportBindings[source].push(name);
}

const exportAllSourceBindings: Record<string, ['*']> = {};
for (const source of exportAllSources) {
exportAllSourceBindings[source] = ['*'];
}

const exportBindings: Record<string, string[]> =
exports.size > 0 ? { '.': [...exports.keys()] } : {};

return { ...exportBindings, ...reexportBindings, ...exportAllSourceBindings };
lukastaegert 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,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
@@ -0,0 +1,28 @@
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 expectedResult = {
[ID_MAIN]: {
exports: ['*'],
exportedBindings: { './module.js': ['*'] }
},
[ID_MODULE]: {
exports: ['default'],
exportedBindings: { '.': ['default'] }
}
};

module.exports = {
description: 'check exports and exportedBindings in moduleParsed when exporting all source',
options: {
plugins: {
moduleParsed(moduleInfo) {
const { exports, exportedBindings, id } = moduleInfo;
assert.deepStrictEqual({ exports, exportedBindings }, expectedResult[id]);
}
}
}
};
@@ -0,0 +1,2 @@
export * from './module.js';
assert.ok(true);
lukastaegert marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1 @@
export default 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
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: ['nested', 'path', 'pathAgain', 'foo'],
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: null,
exports: null,
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