Skip to content

Commit 4a04cb2

Browse files
committed
filter env vars from .env files
allow direct process.env call in webpack, matching manager+vite implementations
1 parent ab87178 commit 4a04cb2

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

code/builders/builder-webpack5/src/preview/iframe-webpack.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export default async (
194194
},
195195
}),
196196
new DefinePlugin({
197+
'process.env': JSON.stringify(envs),
197198
...stringifyProcessEnvs(envs),
198199
NODE_ENV: JSON.stringify(
199200
features?.developmentModeForBuild && isProd ? 'development' : process.env.NODE_ENV

code/core/src/common/utils/envs.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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) => /^STORYBOOK_/.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]) => /^STORYBOOK_/.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

5046
export 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

Comments
 (0)