Skip to content

Commit 9ed3b7b

Browse files
authored
fix(dotenv): preserve cached values before expansion (#22787)
Signed-off-by: Eshiv-Pandey <[email protected]> Co-authored-by: Eshiv-Pandey <[email protected]>
1 parent 9de749d commit 9ed3b7b

8 files changed

Lines changed: 66 additions & 0 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+
Preserve raw dotenv values when reusing cached environment files.

‎lib/define/DotenvPlugin.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ class DotenvPlugin {
495495
* @returns {Env} env variables
496496
*/
497497
_getEnv(prefixes, parsed) {
498+
// Expansion mutates its input, but the cache must retain the raw file values.
499+
parsed = Object.assign(/** @type {Env} */ (Object.create(null)), parsed);
498500
// Always expand environment variables (like Vite does)
499501
// Make a copy of process.env so that dotenv-expand doesn't modify global process.env
500502
const processEnv = { ...process.env };
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# cspell:ignore DOTENV
2+
WEBPACK_DOTENV_CACHE_BASE=fallback
3+
WEBPACK_DOTENV_CACHE_URL=${WEBPACK_DOTENV_CACHE_BASE}/api
4+
WEBPACK_DOTENV_CACHE_ESCAPED=\$WEBPACK_DOTENV_CACHE_BASE
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const step = require("./step");
2+
3+
it("should preserve escaped variables when reusing the dotenv cache", () => {
4+
expect(process.env.WEBPACK_DOTENV_CACHE_ESCAPED).toBe(
5+
"$WEBPACK_DOTENV_CACHE_BASE"
6+
);
7+
});
8+
9+
it("should expand cached values with the current environment", () => {
10+
expect(process.env.WEBPACK_DOTENV_CACHE_URL).toBe(
11+
["first/api", "second/api", "fallback/api"][step]
12+
);
13+
});
14+
15+
it("should restore the file value when an environment override is removed", () => {
16+
expect(process.env.WEBPACK_DOTENV_CACHE_BASE).toBe(
17+
["first", "second", "fallback"][step]
18+
);
19+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 0;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 1;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = 2;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict";
2+
3+
const currentWatchStep = require("../../../helpers/currentWatchStep");
4+
5+
/** @type {(env: Env, options: TestOptions) => import("../../../../").Configuration} */
6+
module.exports = (env, { srcPath }) => ({
7+
mode: "development",
8+
devtool: false,
9+
dotenv: {
10+
dir: srcPath,
11+
template: [".env"]
12+
},
13+
plugins: [
14+
(compiler) => {
15+
const original = process.env.WEBPACK_DOTENV_CACHE_BASE;
16+
compiler.hooks.watchRun.tap("DotenvCachedExpansion", () => {
17+
const step = Number(currentWatchStep.step);
18+
if (step < 2) {
19+
process.env.WEBPACK_DOTENV_CACHE_BASE = ["first", "second"][step];
20+
} else {
21+
delete process.env.WEBPACK_DOTENV_CACHE_BASE;
22+
}
23+
});
24+
compiler.hooks.watchClose.tap("DotenvCachedExpansion", () => {
25+
if (original === undefined) {
26+
delete process.env.WEBPACK_DOTENV_CACHE_BASE;
27+
} else {
28+
process.env.WEBPACK_DOTENV_CACHE_BASE = original;
29+
}
30+
});
31+
}
32+
]
33+
});

0 commit comments

Comments
 (0)