Skip to content

Commit 6d18ec7

Browse files
fix: keep empty process env variables in DotenvPlugin
Co-authored-by: shuaiyafei <[email protected]>
1 parent 701dab6 commit 6d18ec7

5 files changed

Lines changed: 75 additions & 5 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+
Keep empty process environment variables that `.env` files don't define in `DotenvPlugin`.

‎lib/define/DotenvPlugin.js‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,9 @@ class DotenvPlugin {
507507
// Prioritize actual env variables from `process.env`, fallback to parsed
508508
for (const key of keys) {
509509
if (prefixes.some((prefix) => key.startsWith(prefix))) {
510-
env[key] =
511-
Object.prototype.hasOwnProperty.call(process.env, key) &&
512-
process.env[key]
513-
? process.env[key]
514-
: parsed[key];
510+
env[key] = Object.prototype.hasOwnProperty.call(process.env, key)
511+
? /** @type {string} */ (process.env[key])
512+
: parsed[key];
515513
}
516514
}
517515

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
WEBPACK_DOTENV_EMPTY_OVERRIDE=from-file
2+
WEBPACK_DOTENV_NONEMPTY=from-file
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
it("should preserve empty process variables without a file default", () => {
4+
expect(process.env.WEBPACK_DOTENV_EMPTY_PROCESS_ONLY).toBe("");
5+
expect(import.meta.env.WEBPACK_DOTENV_EMPTY_PROCESS_ONLY).toBe("");
6+
expect(
7+
Object.prototype.hasOwnProperty.call(
8+
import.meta.env,
9+
"WEBPACK_DOTENV_EMPTY_PROCESS_ONLY"
10+
)
11+
).toBe(true);
12+
});
13+
14+
it("should prefer empty process variables over file values", () => {
15+
expect(process.env.WEBPACK_DOTENV_EMPTY_OVERRIDE).toBe("");
16+
expect(import.meta.env.WEBPACK_DOTENV_EMPTY_OVERRIDE).toBe("");
17+
});
18+
19+
it("should preserve nonempty process variables", () => {
20+
expect(process.env.WEBPACK_DOTENV_NONEMPTY).toBe("from-process");
21+
expect(import.meta.env.WEBPACK_DOTENV_NONEMPTY).toBe("from-process");
22+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use strict";
2+
3+
/**
4+
* @param {import("../../../../").Compiler} compiler compiler
5+
* @returns {void}
6+
*/
7+
const setProcessEnv = (compiler) => {
8+
const values = {
9+
WEBPACK_DOTENV_EMPTY_PROCESS_ONLY: "",
10+
WEBPACK_DOTENV_EMPTY_OVERRIDE: "",
11+
WEBPACK_DOTENV_NONEMPTY: "from-process"
12+
};
13+
/** @type {Map<string, string | undefined>} */
14+
const previousValues = new Map();
15+
16+
compiler.hooks.beforeCompile.tap("TestEmptyProcessEnv", () => {
17+
for (const [key, value] of Object.entries(values)) {
18+
previousValues.set(key, process.env[key]);
19+
process.env[key] = value;
20+
}
21+
});
22+
compiler.hooks.afterCompile.tap("TestEmptyProcessEnv", () => {
23+
for (const [key, value] of previousValues) {
24+
if (value === undefined) {
25+
delete process.env[key];
26+
} else {
27+
process.env[key] = value;
28+
}
29+
}
30+
});
31+
};
32+
33+
/** @type {import("../../../../").Configuration[]} */
34+
module.exports = [
35+
{
36+
dotenv: true,
37+
plugins: [setProcessEnv]
38+
},
39+
{
40+
dotenv: { dir: false },
41+
plugins: [setProcessEnv]
42+
}
43+
];

0 commit comments

Comments
 (0)