File tree Expand file tree Collapse file tree
test/configCases/plugins/dotenv-empty-process-env Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ ---
2+ " webpack " : patch
3+ ---
4+
5+ Preserve empty process environment variables in ` DotenvPlugin ` .
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 1+ WEBPACK_DOTENV_EMPTY_OVERRIDE = from-file
2+ WEBPACK_DOTENV_NONEMPTY = from-file
Original file line number Diff line number Diff line change 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+ } ) ;
Original file line number Diff line number Diff line change 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+ ] ;
You can’t perform that action at this time.
0 commit comments