fix(commonjs-static): export unprovided variables (#19303)

This commit is contained in:
Wei 2025-03-11 14:18:11 +08:00 committed by GitHub
parent c282c96ca1
commit 1b7f0e1e95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 126 additions and 39 deletions

View File

@ -302,13 +302,42 @@ class AssignLibraryPlugin extends AbstractLibraryPlugin {
this._getPrefix(compilation).length, this._getPrefix(compilation).length,
true true
); );
/** @type {string[]} */
const provided = [];
for (const exportInfo of exportsInfo.orderedExports) { for (const exportInfo of exportsInfo.orderedExports) {
if (!exportInfo.provided) continue; if (!exportInfo.provided) continue;
const nameAccess = propertyAccess([exportInfo.name]); const nameAccess = propertyAccess([exportInfo.name]);
result.add( result.add(
`${exportTarget}${nameAccess} = ${RuntimeGlobals.exports}${exportAccess}${nameAccess};\n` `${exportTarget}${nameAccess} = ${RuntimeGlobals.exports}${exportAccess}${nameAccess};\n`
); );
provided.push(exportInfo.name);
} }
const webpackExportTarget = accessWithInit(
fullNameResolved,
this._getPrefix(compilation).length,
true
);
/** @type {string} */
let exports = RuntimeGlobals.exports;
if (exportAccess) {
result.add(
`var __webpack_exports_export__ = ${RuntimeGlobals.exports}${exportAccess};\n`
);
exports = "__webpack_exports_export__";
}
result.add(`for(var __webpack_i__ in ${exports}) {\n`);
const hasProvided = provided.length > 0;
if (hasProvided) {
result.add(
` if (${JSON.stringify(provided)}.indexOf(__webpack_i__) === -1) {\n`
);
}
result.add(
` ${hasProvided ? " " : ""}${webpackExportTarget}[__webpack_i__] = ${exports}[__webpack_i__];\n`
);
result.add(hasProvided ? " }\n}\n" : "\n");
result.add( result.add(
`Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n` `Object.defineProperty(${exportTarget}, "__esModule", { value: true });\n`
); );

View File

@ -2,6 +2,7 @@ export * from "./a";
export default "default-value"; export default "default-value";
export var b = "b"; export var b = "b";
export { default as external } from "external"; export { default as external } from "external";
export * from "external-named";
var module = "should not conflict", var module = "should not conflict",
define = "should not conflict", define = "should not conflict",

View File

@ -0,0 +1 @@
export const nonExternalA = "non-external-a";

View File

@ -11,7 +11,8 @@ module.exports = (env, { testPath }) => [
target: "node14", target: "node14",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
experiments: { experiments: {
@ -27,7 +28,8 @@ module.exports = (env, { testPath }) => [
target: "node14", target: "node14",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
experiments: { experiments: {
@ -43,7 +45,8 @@ module.exports = (env, { testPath }) => [
target: "node14", target: "node14",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -62,7 +65,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -75,7 +79,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -88,7 +93,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -101,7 +107,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -116,7 +123,8 @@ module.exports = (env, { testPath }) => [
target: "web", target: "web",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -134,7 +142,8 @@ module.exports = (env, { testPath }) => [
target: "web", target: "web",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -149,7 +158,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -164,7 +174,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -179,7 +190,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
ignoreWarnings: [error => error.name === "FalseIIFEUmdWarning"] ignoreWarnings: [error => error.name === "FalseIIFEUmdWarning"]
@ -195,7 +207,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
ignoreWarnings: [error => error.name === "FalseIIFEUmdWarning"] ignoreWarnings: [error => error.name === "FalseIIFEUmdWarning"]
@ -209,7 +222,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -222,7 +236,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -235,7 +250,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -248,7 +264,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
plugins: [ plugins: [
@ -267,7 +284,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
plugins: [ plugins: [
@ -288,7 +306,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -303,7 +322,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -314,7 +334,7 @@ module.exports = (env, { testPath }) => [
libraryTarget: "commonjs2", libraryTarget: "commonjs2",
iife: false iife: false
}, },
externals: ["external"] externals: ["external", "external-named"]
}, },
{ {
output: { output: {
@ -326,7 +346,7 @@ module.exports = (env, { testPath }) => [
optimization: { optimization: {
concatenateModules: false concatenateModules: false
}, },
externals: ["external"] externals: ["external", "external-named"]
}, },
{ {
output: { output: {
@ -335,7 +355,7 @@ module.exports = (env, { testPath }) => [
libraryTarget: "commonjs2", libraryTarget: "commonjs2",
iife: true iife: true
}, },
externals: ["external"] externals: ["external", "external-named"]
}, },
{ {
mode: "development", mode: "development",
@ -344,7 +364,7 @@ module.exports = (env, { testPath }) => [
filename: "commonjs2-external-eval.js", filename: "commonjs2-external-eval.js",
libraryTarget: "commonjs2" libraryTarget: "commonjs2"
}, },
externals: ["external"] externals: ["external", "external-named"]
}, },
{ {
mode: "development", mode: "development",
@ -354,7 +374,7 @@ module.exports = (env, { testPath }) => [
libraryTarget: "commonjs2" libraryTarget: "commonjs2"
}, },
devtool: "eval-source-map", devtool: "eval-source-map",
externals: ["external"] externals: ["external", "external-named"]
}, },
{ {
output: { output: {
@ -363,7 +383,7 @@ module.exports = (env, { testPath }) => [
libraryTarget: "commonjs-static", libraryTarget: "commonjs-static",
iife: false iife: false
}, },
externals: ["external"] externals: ["external", "external-named"]
}, },
{ {
output: { output: {
@ -387,7 +407,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
}, },
@ -400,7 +421,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -416,7 +438,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -434,7 +457,8 @@ module.exports = (env, { testPath }) => [
target: "web", target: "web",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -452,7 +476,8 @@ module.exports = (env, { testPath }) => [
target: "web", target: "web",
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
}, },
optimization: { optimization: {
@ -487,7 +512,8 @@ module.exports = (env, { testPath }) => [
}, },
resolve: { resolve: {
alias: { alias: {
external: "./non-external" external: "./non-external",
"external-named": "./non-external-named"
} }
} }
} }

View File

@ -1,5 +1,6 @@
import d from "library"; import d from "library";
import { a, b, external } from "library"; import { a, b, external } from "library";
import * as imoprtStar from "library";
it( it(
"should be able to import harmony exports from library (" + NAME + ")", "should be able to import harmony exports from library (" + NAME + ")",
@ -10,8 +11,12 @@ it(
if (typeof TEST_EXTERNAL !== "undefined" && TEST_EXTERNAL) { if (typeof TEST_EXTERNAL !== "undefined" && TEST_EXTERNAL) {
expect(external).toEqual(["external"]); expect(external).toEqual(["external"]);
expect(external).toBe(require("external")); expect(external).toBe(require("external"));
const { externalA } = imoprtStar
expect(externalA).toEqual(["external-a"]);
} else { } else {
expect(external).toBe("non-external"); expect(external).toBe("non-external");
const { nonExternalA } = imoprtStar;
expect(nonExternalA).toBe("non-external-a");
} }
} }
); );

View File

@ -0,0 +1 @@
module.exports['externalA'] = ["external-a"];

View File

@ -293,7 +293,11 @@ module.exports = (env, { testPath }) => [
testPath, testPath,
"../0-create-library/commonjs2-external.js" "../0-create-library/commonjs2-external.js"
), ),
external: path.resolve(__dirname, "node_modules/external.js") external: path.resolve(__dirname, "node_modules/external.js"),
"external-named": path.resolve(
__dirname,
"node_modules/external-named.js"
)
} }
}, },
plugins: [ plugins: [
@ -310,7 +314,11 @@ module.exports = (env, { testPath }) => [
testPath, testPath,
"../0-create-library/commonjs2-iife-external.js" "../0-create-library/commonjs2-iife-external.js"
), ),
external: path.resolve(__dirname, "node_modules/external.js") external: path.resolve(__dirname, "node_modules/external.js"),
"external-named": path.resolve(
__dirname,
"node_modules/external-named.js"
)
} }
}, },
plugins: [ plugins: [
@ -327,7 +335,11 @@ module.exports = (env, { testPath }) => [
testPath, testPath,
"../0-create-library/commonjs2-external-eval.js" "../0-create-library/commonjs2-external-eval.js"
), ),
external: path.resolve(__dirname, "node_modules/external.js") external: path.resolve(__dirname, "node_modules/external.js"),
"external-named": path.resolve(
__dirname,
"node_modules/external-named.js"
)
} }
}, },
plugins: [ plugins: [
@ -344,7 +356,11 @@ module.exports = (env, { testPath }) => [
testPath, testPath,
"../0-create-library/commonjs2-external-eval-source-map.js" "../0-create-library/commonjs2-external-eval-source-map.js"
), ),
external: path.resolve(__dirname, "node_modules/external.js") external: path.resolve(__dirname, "node_modules/external.js"),
"external-named": path.resolve(
__dirname,
"node_modules/external-named.js"
)
} }
}, },
plugins: [ plugins: [
@ -363,7 +379,11 @@ module.exports = (env, { testPath }) => [
testPath, testPath,
"../0-create-library/commonjs-static-external.js" "../0-create-library/commonjs-static-external.js"
), ),
external: path.resolve(__dirname, "node_modules/external.js") external: path.resolve(__dirname, "node_modules/external.js"),
"external-named": path.resolve(
__dirname,
"node_modules/external-named.js"
)
} }
}, },
plugins: [ plugins: [
@ -380,7 +400,11 @@ module.exports = (env, { testPath }) => [
testPath, testPath,
"../0-create-library/commonjs2-split-chunks/" "../0-create-library/commonjs2-split-chunks/"
), ),
external: path.resolve(__dirname, "node_modules/external.js") external: path.resolve(__dirname, "node_modules/external.js"),
"external-named": path.resolve(
__dirname,
"node_modules/external-named.js"
)
} }
}, },
plugins: [ plugins: [

View File

@ -6,5 +6,5 @@ export default bar
it("should success compile and work",()=>{ it("should success compile and work",()=>{
const output = fs.readFileSync(__filename).toString(); const output = fs.readFileSync(__filename).toString();
expect(output.match(/exports(\[|\.)/g).length).toBe(3) expect(output.match(/exports(\[|\.)/g).length).toBe(4)
}) })