From 7466889ddb0ac99d03d9397d5321818ac7eb0394 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 22 Jan 2022 06:59:19 +0100 Subject: [PATCH] Make export information available after loading, detect reexports --- src/Module.ts | 5 ++- .../samples/has-default-export/_config.js | 45 +++++++++++++++++++ .../samples/has-default-export/direct.js | 1 + .../samples/has-default-export/indirect.js | 2 + .../samples/has-default-export/main.js | 9 ++++ .../samples/has-default-export/other.js | 2 + .../samples/has-default-export/reexport1.js | 1 + .../samples/has-default-export/reexport2.js | 1 + .../samples/preload-module/_config.js | 2 +- 9 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 test/function/samples/has-default-export/_config.js create mode 100644 test/function/samples/has-default-export/direct.js create mode 100644 test/function/samples/has-default-export/indirect.js create mode 100644 test/function/samples/has-default-export/main.js create mode 100644 test/function/samples/has-default-export/other.js create mode 100644 test/function/samples/has-default-export/reexport1.js create mode 100644 test/function/samples/has-default-export/reexport2.js diff --git a/src/Module.ts b/src/Module.ts index c0f9aac2122..93db11d47d6 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -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, diff --git a/test/function/samples/has-default-export/_config.js b/test/function/samples/has-default-export/_config.js new file mode 100644 index 00000000000..daea5948706 --- /dev/null +++ b/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}`); + } + } + ] + } +}; diff --git a/test/function/samples/has-default-export/direct.js b/test/function/samples/has-default-export/direct.js new file mode 100644 index 00000000000..f2f6d5c381b --- /dev/null +++ b/test/function/samples/has-default-export/direct.js @@ -0,0 +1 @@ +export default 'direct'; diff --git a/test/function/samples/has-default-export/indirect.js b/test/function/samples/has-default-export/indirect.js new file mode 100644 index 00000000000..28786796a99 --- /dev/null +++ b/test/function/samples/has-default-export/indirect.js @@ -0,0 +1,2 @@ +const indirect = 'indirect'; +export { indirect as default }; diff --git a/test/function/samples/has-default-export/main.js b/test/function/samples/has-default-export/main.js new file mode 100644 index 00000000000..28d6ee09c9c --- /dev/null +++ b/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'); diff --git a/test/function/samples/has-default-export/other.js b/test/function/samples/has-default-export/other.js new file mode 100644 index 00000000000..a99ac05aa02 --- /dev/null +++ b/test/function/samples/has-default-export/other.js @@ -0,0 +1,2 @@ +export default 'default'; +export const foo = 'foo'; diff --git a/test/function/samples/has-default-export/reexport1.js b/test/function/samples/has-default-export/reexport1.js new file mode 100644 index 00000000000..b98cf73cdb9 --- /dev/null +++ b/test/function/samples/has-default-export/reexport1.js @@ -0,0 +1 @@ +export { default } from './other.js'; diff --git a/test/function/samples/has-default-export/reexport2.js b/test/function/samples/has-default-export/reexport2.js new file mode 100644 index 00000000000..08facccd528 --- /dev/null +++ b/test/function/samples/has-default-export/reexport2.js @@ -0,0 +1 @@ +export { foo as default } from './other.js'; diff --git a/test/function/samples/preload-module/_config.js b/test/function/samples/preload-module/_config.js index 4454921c9d6..51ec568846e 100644 --- a/test/function/samples/preload-module/_config.js +++ b/test/function/samples/preload-module/_config.js @@ -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,