Skip to content
Permalink
Browse files
Merge pull request #7922 from webpack/bugfix/concat-globals
fix exporting globals in scope-hoisted modules
  • Loading branch information
sokra committed Aug 20, 2018
2 parents 0e60343 + 9315ce2 commit c2e03951f46bf56397a9b2039309a0c7bbc1991f
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 c2e0395

Please sign in to comment.