Skip to content

Commit b91f4da

Browse files
committed
fix: preserve empty process environment variables in DotenvPlugin
1 parent f9705a6 commit b91f4da

5 files changed

Lines changed: 73 additions & 1 deletion

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+
Preserve empty process environment variables in `DotenvPlugin`.

‎lib/define/DotenvPlugin.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ class DotenvPlugin {
509509
if (prefixes.some((prefix) => key.startsWith(prefix))) {
510510
env[key] =
511511
Object.prototype.hasOwnProperty.call(process.env, key) &&
512-
process.env[key]
512+
process.env[key] !== undefined
513513
? process.env[key]
514514
: parsed[key];
515515
}
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 let empty process variables override 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)