Skip to content

Commit

Permalink
Make export information available after loading, detect reexports
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jan 22, 2022
1 parent 09e15b6 commit c2f0e1c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/Module.ts
Expand Up @@ -278,10 +278,11 @@ export default class Module {
return module.dynamicImporters.sort();
},
get hasDefaultExport() {
if (module.graph.phase !== BuildPhase.GENERATE) {
// This information is only valid after parsing
if (!module.ast) {
return null;
}
return 'default' in module.exports;
return 'default' in module.exports || 'default' in module.reexportDescriptions;
},
hasModuleSideEffects,
id,
Expand Down
45 changes: 45 additions & 0 deletions test/function/samples/has-default-export/_config.js
@@ -0,0 +1,45 @@
const assert = require('assert');
const path = require('path');

module.exports = {
description: 'reports if a module has a default export',
options: {
plugins: [
{
async buildStart() {
const ID_MAIN = path.join(__dirname, 'main.js');
const loadMain = this.load({ id: ID_MAIN });
assert.strictEqual(this.getModuleInfo(ID_MAIN).hasDefaultExport, null);
assert.strictEqual((await loadMain).hasDefaultExport, false);

assert.strictEqual(
(await this.load({ id: path.join(__dirname, 'direct.js') })).hasDefaultExport,
true,
'direct'
);
assert.strictEqual(
(await this.load({ id: path.join(__dirname, 'indirect.js') })).hasDefaultExport,
true,
'indirect'
);
assert.strictEqual(
(await this.load({ id: path.join(__dirname, 'reexport1.js') })).hasDefaultExport,
true,
'reexport'
);
assert.strictEqual(
(await this.load({ id: path.join(__dirname, 'reexport2.js') })).hasDefaultExport,
true,
'renamed reexport'
);
},
load(id) {
assert.strictEqual(this.getModuleInfo(id).hasDefaultExport, null, `load ${id}`);
},
transform(code, id) {
assert.strictEqual(this.getModuleInfo(id).hasDefaultExport, null, `transform ${id}`);
}
}
]
}
};
1 change: 1 addition & 0 deletions test/function/samples/has-default-export/direct.js
@@ -0,0 +1 @@
export default 'direct';
2 changes: 2 additions & 0 deletions test/function/samples/has-default-export/indirect.js
@@ -0,0 +1,2 @@
const indirect = 'indirect';
export { indirect as default };
9 changes: 9 additions & 0 deletions test/function/samples/has-default-export/main.js
@@ -0,0 +1,9 @@
import direct from './direct.js';
import indirect from './indirect.js';
import reexport1 from './reexport1.js';
import reexport2 from './reexport2.js';

assert.strictEqual(direct, 'direct');
assert.strictEqual(indirect, 'indirect');
assert.strictEqual(reexport1, 'default');
assert.strictEqual(reexport2, 'foo');
2 changes: 2 additions & 0 deletions test/function/samples/has-default-export/other.js
@@ -0,0 +1,2 @@
export default 'default';
export const foo = 'foo';
1 change: 1 addition & 0 deletions test/function/samples/has-default-export/reexport1.js
@@ -0,0 +1 @@
export { default } from './other.js';
1 change: 1 addition & 0 deletions test/function/samples/has-default-export/reexport2.js
@@ -0,0 +1 @@
export { foo as default } from './other.js';
2 changes: 1 addition & 1 deletion test/function/samples/preload-module/_config.js
Expand Up @@ -33,7 +33,7 @@ module.exports = {
assert.deepStrictEqual(moduleInfo, {
code: "import './dep';\nassert.ok(true);\n",
dynamicImporters: [],
hasDefaultExport: null,
hasDefaultExport: false,
dynamicallyImportedIdResolutions: [],
dynamicallyImportedIds: [],
hasModuleSideEffects: true,
Expand Down

0 comments on commit c2f0e1c

Please sign in to comment.