@@ -12,7 +12,7 @@ export function loadEnvs(options: { production?: boolean } = {}): {
1212} {
1313 const defaultNodeEnv = options . production ? 'production' : 'development' ;
1414
15- const env : Record < string , string | undefined > = {
15+ const baseEnv : Record < string , string > = {
1616 // eslint-disable-next-line @typescript-eslint/dot-notation
1717 NODE_ENV : process . env [ 'NODE_ENV' ] || defaultNodeEnv ,
1818 NODE_PATH : process . env [ 'NODE_PATH' ] || '' ,
@@ -24,27 +24,23 @@ export function loadEnvs(options: { production?: boolean } = {}): {
2424 PUBLIC_URL : options . production ? '.' : '' ,
2525 } ;
2626
27- Object . keys ( process . env )
28- . filter ( ( name ) => / ^ S T O R Y B O O K _ / . test ( name ) )
29- . forEach ( ( name ) => {
30- env [ name ] = process . env [ name ] ;
31- } ) ;
27+ const dotenv = getEnvironment ( { nodeEnv : baseEnv [ 'NODE_ENV' ] } ) ;
3228
33- const base = Object . entries ( env ) . reduce (
34- ( acc , [ k , v ] ) => Object . assign ( acc , { [ k ] : JSON . stringify ( v ) } ) ,
35- { } as Record < string , string >
29+ const envEntries = Object . fromEntries < string > (
30+ Object . entries < string > ( {
31+ // TODO: it seems wrong that dotenv overrides process.env, but that's how it has always worked
32+ ...process . env ,
33+ ...dotenv . raw ,
34+ } ) . filter ( ( [ name ] ) => / ^ S T O R Y B O O K _ / . test ( name ) )
3635 ) ;
3736
38- const { stringified, raw } = getEnvironment ( { nodeEnv : env [ 'NODE_ENV' ] } ) ;
37+ const raw : Record < string , string > = { ...baseEnv , ...envEntries } ;
38+ ( raw as any ) . NODE_PATH = nodePathsToArray ( ( raw . NODE_PATH as string ) || '' ) ;
3939
40- const fullRaw = { ...env , ...raw } ;
41-
42- fullRaw . NODE_PATH = nodePathsToArray ( fullRaw . NODE_PATH || '' ) ;
43-
44- return {
45- stringified : { ...base , ...stringified } ,
46- raw : fullRaw ,
47- } ;
40+ const stringified = Object . fromEntries (
41+ Object . entries ( raw ) . map ( ( [ key , value ] ) => [ key , JSON . stringify ( value ) ] )
42+ ) ;
43+ return { raw, stringified } ;
4844}
4945
5046export const stringifyEnvs = ( raw : Record < string , string > ) : Record < string , string > =>
@@ -58,12 +54,5 @@ export const stringifyProcessEnvs = (raw: Record<string, string>): Record<string
5854 acc [ `process.env.${ key } ` ] = JSON . stringify ( value ) ;
5955 return acc ;
6056 } , { } ) ;
61- // FIXME: something like this is necessary to support destructuring like:
62- //
63- // const { foo } = process.env;
64- //
65- // However, it also means that process.env.foo = 'bar' will fail, so removing this:
66- //
67- // envs['process.env'] = JSON.stringify(raw);
6857 return envs ;
6958} ;
0 commit comments