Skip to content
Permalink
Browse files
fix exporting globals in scope-hoisted modules
fixes #7905
  • Loading branch information
sokra committed Aug 20, 2018
1 parent a02bf99 commit 9315ce29aa41d05252a43036cc9f2ff15cad59ef
Showing 5 changed files with 32 additions and 0 deletions.
@@ -136,6 +136,9 @@ const getFinalName = (
} else if (!info.module.isUsed(exportName)) {
return "/* unused export */ undefined";
}
if (info.globalExports.has(directExport)) {
return directExport;
}
const name = info.internalNames.get(directExport);
if (!name) {
throw new Error(
@@ -562,6 +565,7 @@ class ConcatenatedModule extends Module {
globalScope: undefined,
moduleScope: undefined,
internalNames: new Map(),
globalExports: new Set(),
exportMap: exportMap,
reexportMap: reexportMap,
hasNamespaceObject: false,
@@ -941,6 +945,19 @@ class ConcatenatedModule extends Module {
}
}
}

// add exported globals
if (info.type === "concatenated") {
const variables = new Set();
for (const variable of info.moduleScope.variables) {
variables.add(variable.name);
}
for (const [, variable] of info.exportMap) {
if (!variables.has(variable)) {
info.globalExports.add(variable);
}
}
}
}

// generate names for symbols
@@ -0,0 +1,7 @@
import { process as p } from "./module";
import { process as p2 } from "./module2";

it("should export globals correctly", () => {
expect(p).toBe(42);
expect(p2).toBe(process);
});
@@ -0,0 +1,2 @@
const process = 42;
export { process };
@@ -0,0 +1 @@
export { process };
@@ -0,0 +1,5 @@
module.exports = {
optimization: {
concatenateModules: true
}
};

0 comments on commit 9315ce2

Please sign in to comment.