Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow globalObject to be expression #12927

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ExternalModule.js
Expand Up @@ -354,7 +354,7 @@ class ExternalModule extends Module {
case "global":
return getSourceForGlobalVariableExternal(
request,
runtimeTemplate.outputOptions.globalObject
`(${runtimeTemplate.outputOptions.globalObject})`
);
case "commonjs":
case "commonjs2":
Expand Down
6 changes: 3 additions & 3 deletions lib/javascript/ArrayPushCallbackChunkFormatPlugin.js
Expand Up @@ -51,7 +51,7 @@ class ArrayPushCallbackChunkFormatPlugin {
const hotUpdateGlobal =
runtimeTemplate.outputOptions.hotUpdateGlobal;
source.add(
`${globalObject}[${JSON.stringify(hotUpdateGlobal)}](`
`(${globalObject})[${JSON.stringify(hotUpdateGlobal)}](`
);
source.add(`${JSON.stringify(chunk.id)},`);
source.add(modules);
Expand All @@ -68,9 +68,9 @@ class ArrayPushCallbackChunkFormatPlugin {
const chunkLoadingGlobal =
runtimeTemplate.outputOptions.chunkLoadingGlobal;
source.add(
`(${globalObject}[${JSON.stringify(
`((${globalObject})[${JSON.stringify(
chunkLoadingGlobal
)}] = ${globalObject}[${JSON.stringify(
)}] = (${globalObject})[${JSON.stringify(
chunkLoadingGlobal
)}] || []).push([`
);
Expand Down
4 changes: 2 additions & 2 deletions lib/web/JsonpChunkLoadingRuntimeModule.js
Expand Up @@ -94,7 +94,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
const withPreload = this._runtimeRequirements.has(
RuntimeGlobals.preloadChunkHandlers
);
const chunkLoadingGlobalExpr = `${globalObject}[${JSON.stringify(
const chunkLoadingGlobalExpr = `(${globalObject})[${JSON.stringify(
chunkLoadingGlobal
)}]`;
const conditionMap = chunkGraph.getChunkConditionMap(chunk, chunkHasJs);
Expand Down Expand Up @@ -303,7 +303,7 @@ class JsonpChunkLoadingRuntimeModule extends RuntimeModule {
]),
"}",
"",
`${globalObject}[${JSON.stringify(
`(${globalObject})[${JSON.stringify(
hotUpdateGlobal
)}] = ${runtimeTemplate.basicFunction(
"chunkId, moreModules, runtime",
Expand Down
4 changes: 2 additions & 2 deletions lib/webworker/ImportScriptsChunkLoadingRuntimeModule.js
Expand Up @@ -44,7 +44,7 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
const withHmrManifest = this.runtimeRequirements.has(
RuntimeGlobals.hmrDownloadManifest
);
const chunkLoadingGlobalExpr = `${globalObject}[${JSON.stringify(
const chunkLoadingGlobalExpr = `(${globalObject})[${JSON.stringify(
chunkLoadingGlobal
)}]`;
const hasJsMatcher = compileBooleanMatcher(
Expand Down Expand Up @@ -141,7 +141,7 @@ class ImportScriptsChunkLoadingRuntimeModule extends RuntimeModule {
"function loadUpdateChunk(chunkId, updatedModulesList) {",
Template.indent([
"var success = false;",
`${globalObject}[${JSON.stringify(
`(${globalObject})[${JSON.stringify(
hotUpdateGlobal
)}] = ${runtimeTemplate.basicFunction("_, moreModules, runtime", [
"for(var moduleId in moreModules) {",
Expand Down
2 changes: 1 addition & 1 deletion test/Compiler.test.js
Expand Up @@ -166,7 +166,7 @@ describe("Compiler", () => {
expect(bundle).not.toMatch("fixtures");
expect(chunk).not.toMatch("fixtures");
expect(bundle).toMatch("webpackChunk");
expect(chunk).toMatch('self["webpackChunk"] || []).push');
expect(chunk).toMatch('(self)["webpackChunk"] || []).push');
done();
});
});
Expand Down
760 changes: 380 additions & 380 deletions test/__snapshots__/StatsTestCases.test.js.snap

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions test/configCases/externals/global-globalObject-expression/index.js
@@ -0,0 +1,11 @@
afterEach(done => {
delete global.EXTERNAL_TEST_GLOBAL;
done();
});

it("should move externals in chunks into entry chunk", function() {
global.EXTERNAL_TEST_GLOBAL = 42;
// eslint-disable-next-line node/no-missing-require
const result = require("external");
expect(result).toBe(42);
});
@@ -0,0 +1,9 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
output: {
globalObject: "typeof self !== 'undefined' ? self : global || false"
},
externals: {
external: "global EXTERNAL_TEST_GLOBAL"
}
};
1 change: 1 addition & 0 deletions test/configCases/web/globalObject-expression/chunk.js
@@ -0,0 +1 @@
export default "ok";
4 changes: 4 additions & 0 deletions test/configCases/web/globalObject-expression/index.js
@@ -0,0 +1,4 @@
it("should work", async () => {
const module = await import(/* webpackChunkName: "the-chunk" */ "./chunk");
expect(module.default).toBe("ok");
});
5 changes: 5 additions & 0 deletions test/configCases/web/globalObject-expression/test.config.js
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function (i, options) {
return ["the-chunk.js", "bundle0.js"];
}
};
@@ -0,0 +1,8 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "web",
output: {
chunkFilename: "[name].js",
globalObject: "typeof self !== 'undefined' ? self : this"
}
};
2 changes: 1 addition & 1 deletion test/configCases/web/unique-jsonp/index.js
@@ -1,7 +1,7 @@
it("should do something", () => {
const fs = require("fs");
const source = fs.readFileSync(__dirname + "/module.js", "utf-8");
expect(source).toMatch(/^\(self\[\"webpackChunksomething\"\]/);
expect(source).toMatch(/^\(\(self\)\[\"webpackChunksomething\"\]/);
});

if (Math.random() < 0) import(/* webpackChunkName: "module" */ "./module");
13 changes: 13 additions & 0 deletions test/configCases/worker/globalObject-expression/index.js
@@ -0,0 +1,13 @@
it("should work", async () => {
const worker = new Worker(new URL("./worker.js", import.meta.url), {
type: "module"
});
worker.postMessage("ok");
const result = await new Promise(resolve => {
worker.onmessage = event => {
resolve(event.data);
};
});
expect(result).toBe("data: OK, thanks");
await worker.terminate();
});
3 changes: 3 additions & 0 deletions test/configCases/worker/globalObject-expression/module.js
@@ -0,0 +1,3 @@
export function upper(str) {
return str.toUpperCase();
}
@@ -0,0 +1,5 @@
module.exports = {
findBundle: function(i, options) {
return ["main.js"];
}
};
@@ -0,0 +1,5 @@
var supportsWorker = require("../../../helpers/supportsWorker");

module.exports = function (config) {
return supportsWorker();
};
@@ -0,0 +1,7 @@
module.exports = {
output: {
filename: "[name].js",
globalObject: "typeof self !== 'undefined' ? self : this"
},
target: "web"
};
4 changes: 4 additions & 0 deletions test/configCases/worker/globalObject-expression/worker.js
@@ -0,0 +1,4 @@
onmessage = async event => {
const { upper } = await import("./module");
postMessage(`data: ${upper(event.data)}, thanks`);
};