Skip to content

Commit

Permalink
Merge pull request #7922 from webpack/bugfix/concat-globals
Browse files Browse the repository at this point in the history
fix exporting globals in scope-hoisted modules
  • Loading branch information
sokra committed Aug 20, 2018
2 parents 0e60343 + 9315ce2 commit c2e0395
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/optimize/ConcatenatedModule.js
Expand Up @@ -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(
Expand Down Expand Up @@ -562,6 +565,7 @@ class ConcatenatedModule extends Module {
globalScope: undefined,
moduleScope: undefined,
internalNames: new Map(),
globalExports: new Set(),
exportMap: exportMap,
reexportMap: reexportMap,
hasNamespaceObject: false,
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/configCases/scope-hoisting/export-global/index.js
@@ -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);
});
2 changes: 2 additions & 0 deletions test/configCases/scope-hoisting/export-global/module.js
@@ -0,0 +1,2 @@
const process = 42;
export { process };
1 change: 1 addition & 0 deletions test/configCases/scope-hoisting/export-global/module2.js
@@ -0,0 +1 @@
export { process };
@@ -0,0 +1,5 @@
module.exports = {
optimization: {
concatenateModules: true
}
};

0 comments on commit c2e0395

Please sign in to comment.