File tree Expand file tree Collapse file tree
test/watchCases/plugins/dotenv-cached-expansion Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " webpack " : patch
3+ ---
4+
5+ Preserve raw dotenv values when reusing cached environment files.
Original file line number Diff line number Diff 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 } ;
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 1+ module . exports = 0 ;
Original file line number Diff line number Diff line change 1+ module . exports = 1 ;
Original file line number Diff line number Diff line change 1+ module . exports = 2 ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments