Skip to content

Commit

Permalink
Merge pull request #11759 from webpack/fix-external-this-concatenation
Browse files Browse the repository at this point in the history
Make external modules of type 'this' work with module concatenation
  • Loading branch information
sokra committed Oct 20, 2020
2 parents fb21e54 + 7e88ff6 commit ffb11b8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/ExternalModule.js
Expand Up @@ -293,7 +293,7 @@ class ExternalModule extends Module {
exportsType: undefined
};
this.buildInfo = {
strict: true
strict: this.externalType !== "this"
};
this.buildMeta.exportsType = "dynamic";
let canMangle = false;
Expand Down Expand Up @@ -412,18 +412,19 @@ class ExternalModule extends Module {
chunkGraph
);

let sourceString;
let sourceString = sourceData.expression;
if (sourceData.iife)
sourceString = `(function() { return ${sourceString}; }())`;
if (concatenationScope) {
sourceString = `${runtimeTemplate.supportsConst() ? "const" : "var"} ${
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
} = ${sourceData.expression};`;
} = ${sourceString};`;
concatenationScope.registerNamespaceExport(
ConcatenationScope.NAMESPACE_OBJECT_EXPORT
);
} else {
sourceString = `module.exports = ${sourceData.expression};`;
sourceString = `module.exports = ${sourceString};`;
}
if (sourceData.iife) sourceString = `(function() { ${sourceString} })();`;
if (sourceData.init) sourceString = `${sourceData.init}\n${sourceString}`;

const sources = new Map();
Expand Down
11 changes: 11 additions & 0 deletions test/configCases/externals/this/index.js
@@ -0,0 +1,11 @@
afterEach(done => {
(function() { delete this.EXTERNAL_TEST_GLOBAL; })();
done();
});

it("should import an external value assigned to global this", function() {
(function() { this.EXTERNAL_TEST_GLOBAL = 42; })();
// eslint-disable-next-line node/no-missing-require
const result = require("external");
expect(result).toBe(42);
});
9 changes: 9 additions & 0 deletions test/configCases/externals/this/webpack.config.js
@@ -0,0 +1,9 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
optimization: {
concatenateModules: true
},
externals: {
external: "this EXTERNAL_TEST_GLOBAL"
}
};

0 comments on commit ffb11b8

Please sign in to comment.