diff --git a/lib/ExternalModule.js b/lib/ExternalModule.js index 3cb8d1900..12840537b 100644 --- a/lib/ExternalModule.js +++ b/lib/ExternalModule.js @@ -443,10 +443,6 @@ const getSourceForModuleExternal = ( dependencyMeta, concatenationScope ) => { - if (!Array.isArray(moduleAndSpecifiers)) { - moduleAndSpecifiers = [moduleAndSpecifiers]; - } - /** @type {Imported} */ let imported = true; if (concatenationScope) { @@ -466,6 +462,15 @@ const getSourceForModuleExternal = ( } } + if (!Array.isArray(moduleAndSpecifiers)) { + moduleAndSpecifiers = [moduleAndSpecifiers]; + } + + // Return to `namespace` when the external request includes a specific export + if (moduleAndSpecifiers.length > 1) { + imported = true; + } + const initFragment = new ModuleExternalInitFragment( moduleAndSpecifiers[0], imported, diff --git a/test/configCases/library/module-external-array-request/external.mjs b/test/configCases/library/module-external-array-request/external.mjs new file mode 100644 index 000000000..397fba070 --- /dev/null +++ b/test/configCases/library/module-external-array-request/external.mjs @@ -0,0 +1,4 @@ +export const inner = { + foo: "foo", + bar: "bar" +}; diff --git a/test/configCases/library/module-external-array-request/index.js b/test/configCases/library/module-external-array-request/index.js new file mode 100644 index 000000000..6c77c3713 --- /dev/null +++ b/test/configCases/library/module-external-array-request/index.js @@ -0,0 +1,6 @@ +import { foo, bar } from "external"; + +it("should have the correct export for array-style external request (e.g. external: ['./external.mjs', 'inner'])", function () { + expect(foo).toBe("foo"); + expect(bar).toBe("bar"); +}); diff --git a/test/configCases/library/module-external-array-request/webpack.config.js b/test/configCases/library/module-external-array-request/webpack.config.js new file mode 100644 index 000000000..f7f8ceb61 --- /dev/null +++ b/test/configCases/library/module-external-array-request/webpack.config.js @@ -0,0 +1,48 @@ +"use strict"; + +const fs = require("fs"); +const path = require("path"); +const webpack = require("../../../../"); + +/** @type {webpack.Configuration} */ +module.exports = { + mode: "production", + entry: "./index.js", + output: { + module: true, + library: { + type: "module" + } + }, + experiments: { + outputModule: true + }, + externals: { + external: ["./external.mjs", "inner"] + }, + externalsType: "module", + plugins: [ + { + apply(compiler) { + compiler.hooks.compilation.tap("Test", (compilation) => { + compilation.hooks.processAssets.tap( + { + name: "copy-webpack-plugin", + stage: + compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL + }, + () => { + const data = fs.readFileSync( + path.resolve(__dirname, "./external.mjs") + ); + compilation.emitAsset( + "external.mjs", + new webpack.sources.RawSource(data) + ); + } + ); + }); + } + } + ] +};