Skip to content

Commit 8a404c6

Browse files
fix(DefinePlugin): destructure a dotted key of any value shape (#22215)
* fix(DefinePlugin): destructure a dotted key of any value shape Reading `const { X } = process.env` substituted a dotted define only when its value was a truthy primitive. Two guards read the value where they meant to ask its shape: the merged view skipped anything `typeof "object"`, which a RuntimeValue and a RegExp are, and anything falsy; the destructuring handler then re-tested the value for truthiness. One unusable id abandons the whole destructuring, so a single `0` among the defines silently disabled substitution for its siblings too. An empty string stays out: it renders to no code, which the member path tolerates and an object literal cannot hold. * chore: keep the new comment within two lines
1 parent a09da3b commit 8a404c6

4 files changed

Lines changed: 67 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Destructure a dotted `DefinePlugin` key whose value is falsy or a runtimeValue.

‎lib/DefinePlugin.js‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,9 @@ const getMergedDefinitions = (compilation) => {
610610
keysByPrefix.set(prefix, [key]);
611611
}
612612
}
613-
if (!code || typeof code === "object") continue;
613+
// a nested object is walked separately and an empty string renders to
614+
// no code; every other shape is a destructurable leaf, falsy included
615+
if (isObjectDefinition(code) || code === "") continue;
614616
const idx = key.lastIndexOf(".");
615617
if (idx <= 0 || idx >= key.length - 1) continue;
616618
const nested = key.slice(0, idx);
@@ -1261,7 +1263,10 @@ class DefinePlugin {
12611263
if (
12621264
!finalSet ||
12631265
!finalSet.has(id) ||
1264-
!mergedDefinitions.definitions[fullKey]
1266+
!Object.prototype.hasOwnProperty.call(
1267+
mergedDefinitions.definitions,
1268+
fullKey
1269+
)
12651270
) {
12661271
return;
12671272
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { TRUTHY, ZERO, FALSE, NULL, PATTERN, SYNC, ASYNC } = process.env;
2+
3+
it("should destructure a dotted key of every value shape", () => {
4+
// a real `process.env` carries none of these, so reading them back is what
5+
// says the whole object was substituted at build time
6+
expect(TRUTHY).toBe("truthy");
7+
expect(ZERO).toBe(0);
8+
expect(FALSE).toBe(false);
9+
expect(NULL).toBe(null);
10+
expect(PATTERN).toEqual(/^a$/);
11+
expect(SYNC).toBe("sync");
12+
expect(ASYNC).toBe("async");
13+
});
14+
15+
it("should still fold a member read of the same keys", () => {
16+
// a member read is folded, so these branches are pruned and the requests
17+
// they hold never resolve
18+
if (process.env.ZERO !== 0) require("./this-file-does-not-exist");
19+
if (process.env.ASYNC !== "async") require("./this-file-does-not-exist");
20+
expect(process.env.ZERO).toBe(0);
21+
});
22+
23+
it("should leave a key that renders to no code unsubstituted", () => {
24+
// its own statement: one unusable id abandons the whole destructuring
25+
const { EMPTY } = process.env;
26+
expect(EMPTY).toBe(undefined);
27+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
const webpack = require("../../../../");
4+
5+
const { DefinePlugin } = webpack;
6+
7+
/** @type {import("../../../../").Configuration} */
8+
module.exports = {
9+
plugins: [
10+
new DefinePlugin({
11+
// every value shape a dotted key can carry, read by destructuring
12+
"process.env.TRUTHY": JSON.stringify("truthy"),
13+
"process.env.ZERO": 0,
14+
"process.env.FALSE": false,
15+
"process.env.NULL": null,
16+
"process.env.PATTERN": /^a$/,
17+
"process.env.SYNC": DefinePlugin.runtimeValue(() =>
18+
JSON.stringify("sync")
19+
),
20+
"process.env.ASYNC": DefinePlugin.runtimeValue(async () =>
21+
JSON.stringify("async")
22+
),
23+
// renders to no code, so it is left out rather than emitted into an
24+
// object literal that cannot hold it
25+
"process.env.EMPTY": ""
26+
})
27+
]
28+
};

0 commit comments

Comments
 (0)