Skip to content

Commit 9315ce2

Browse files
committed
fix exporting globals in scope-hoisted modules
fixes webpack#7905
1 parent a02bf99 commit 9315ce2

File tree

5 files changed

+32
-0
lines changed

5 files changed

+32
-0
lines changed

lib/optimize/ConcatenatedModule.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ const getFinalName = (
136136
} else if (!info.module.isUsed(exportName)) {
137137
return "/* unused export */ undefined";
138138
}
139+
if (info.globalExports.has(directExport)) {
140+
return directExport;
141+
}
139142
const name = info.internalNames.get(directExport);
140143
if (!name) {
141144
throw new Error(
@@ -562,6 +565,7 @@ class ConcatenatedModule extends Module {
562565
globalScope: undefined,
563566
moduleScope: undefined,
564567
internalNames: new Map(),
568+
globalExports: new Set(),
565569
exportMap: exportMap,
566570
reexportMap: reexportMap,
567571
hasNamespaceObject: false,
@@ -941,6 +945,19 @@ class ConcatenatedModule extends Module {
941945
}
942946
}
943947
}
948+
949+
// add exported globals
950+
if (info.type === "concatenated") {
951+
const variables = new Set();
952+
for (const variable of info.moduleScope.variables) {
953+
variables.add(variable.name);
954+
}
955+
for (const [, variable] of info.exportMap) {
956+
if (!variables.has(variable)) {
957+
info.globalExports.add(variable);
958+
}
959+
}
960+
}
944961
}
945962

946963
// generate names for symbols
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { process as p } from "./module";
2+
import { process as p2 } from "./module2";
3+
4+
it("should export globals correctly", () => {
5+
expect(p).toBe(42);
6+
expect(p2).toBe(process);
7+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const process = 42;
2+
export { process };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { process };
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
optimization: {
3+
concatenateModules: true
4+
}
5+
};

0 commit comments

Comments
 (0)