From 3bcdf0c23ba77cfa15f76690af939b6ae9591a3b Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Wed, 3 May 2023 06:54:10 +0200 Subject: [PATCH] Handle shimming missing exports when preserving modules --- src/Module.ts | 6 +++++- .../_config.js | 19 +++++++++++++++++++ .../missing-export-preserve-modules/foo.js | 1 + .../missing-export-preserve-modules/main.js | 2 ++ 4 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 test/function/samples/missing-export-preserve-modules/_config.js create mode 100644 test/function/samples/missing-export-preserve-modules/foo.js create mode 100644 test/function/samples/missing-export-preserve-modules/main.js diff --git a/src/Module.ts b/src/Module.ts index 8d539ad2c90..55cec222fe9 100644 --- a/src/Module.ts +++ b/src/Module.ts @@ -747,7 +747,11 @@ export default class Module { // Modules where this.ast is missing have been loaded via this.load and are // not yet fully processed, hence they cannot be included. return ( - this.ast && (this.ast.included || this.namespace.included || this.importedFromNotTreeshaken) + this.ast && + (this.ast.included || + this.namespace.included || + this.importedFromNotTreeshaken || + this.exportShimVariable.included) ); } diff --git a/test/function/samples/missing-export-preserve-modules/_config.js b/test/function/samples/missing-export-preserve-modules/_config.js new file mode 100644 index 00000000000..cd10b25726d --- /dev/null +++ b/test/function/samples/missing-export-preserve-modules/_config.js @@ -0,0 +1,19 @@ +const path = require('node:path'); + +module.exports = defineTest({ + description: 'supports shimming missing exports when preserving modules', + options: { + shimMissingExports: true, + output: { + preserveModules: true + } + }, + warnings: [ + { + binding: 'bar', + code: 'SHIMMED_EXPORT', + exporter: path.join(__dirname, 'foo.js'), + message: 'Missing export "bar" has been shimmed in module "foo.js".' + } + ] +}); diff --git a/test/function/samples/missing-export-preserve-modules/foo.js b/test/function/samples/missing-export-preserve-modules/foo.js new file mode 100644 index 00000000000..3329a7d972f --- /dev/null +++ b/test/function/samples/missing-export-preserve-modules/foo.js @@ -0,0 +1 @@ +export const foo = 'foo'; diff --git a/test/function/samples/missing-export-preserve-modules/main.js b/test/function/samples/missing-export-preserve-modules/main.js new file mode 100644 index 00000000000..352d9782b70 --- /dev/null +++ b/test/function/samples/missing-export-preserve-modules/main.js @@ -0,0 +1,2 @@ +import { bar } from './foo'; +assert.strictEqual(bar, undefined);