fix: return to namespace import when the external request includes a specific export (#20126)

This commit is contained in:
hai-x 2025-11-17 22:53:19 +08:00 committed by GitHub
parent 067cc60bdb
commit 27c05c7c39
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 4 deletions

View File

@ -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,

View File

@ -0,0 +1,4 @@
export const inner = {
foo: "foo",
bar: "bar"
};

View File

@ -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");
});

View File

@ -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)
);
}
);
});
}
}
]
};