Skip to content

Commit 5a2e763

Browse files
authored
Revert "fix(storybook-builder): define only necessary process.env globally"
1 parent a9292d0 commit 5a2e763

File tree

5 files changed

+15
-20
lines changed

5 files changed

+15
-20
lines changed

.changeset/sweet-sloths-crash.md

-5
This file was deleted.

packages/storybook-builder/src/generate-iframe-html.ts

-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export async function generateIframeHtml(options: Options): Promise<string> {
1111
'utf-8',
1212
);
1313
const { configType, features, presets, serverChannelUrl } = options;
14-
const env = await presets.apply<Record<string, string>>('env');
1514
const frameworkOptions = await presets.apply<Record<string, any> | null>('frameworkOptions');
1615
const headHtmlSnippet = await presets.apply<PreviewHtml>('previewHead');
1716
const bodyHtmlSnippet = await presets.apply<PreviewHtml>('previewBody');
@@ -27,14 +26,6 @@ export async function generateIframeHtml(options: Options): Promise<string> {
2726
}));
2827
return (
2928
iframeHtmlTemplate
30-
.replace(
31-
`'[PROCESS_ENV HERE]'`,
32-
JSON.stringify({
33-
// IMPORTANT!!!
34-
// please do not include the entire "env" to prevent bundling and deploying of sensitive data
35-
NODE_ENV: env.NODE_ENV,
36-
}),
37-
)
3829
.replace('[CONFIG_TYPE HERE]', configType || '')
3930
.replace('[LOGLEVEL HERE]', logLevel || '')
4031
.replace(`'[FRAMEWORK_OPTIONS HERE]'`, JSON.stringify(frameworkOptions))

packages/storybook-builder/src/index.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ export const start: WdsBuilder['start'] = async ({ startTime, options, router, s
6060
router.use('/sb-preview', express.static(previewDirOrigin, { immutable: true, maxAge: '5m' }));
6161
router.use(`/${PREBUNDLED_MODULES_DIR}`, express.static(resolve(`./${PREBUNDLED_MODULES_DIR}`)));
6262

63+
const env = await options.presets.apply<Record<string, string>>('env');
64+
6365
const wdsStorybookConfig: DevServerConfig = {
6466
nodeResolve: true,
6567
plugins: [
@@ -72,7 +74,7 @@ export const start: WdsBuilder['start'] = async ({ startTime, options, router, s
7274
}
7375
},
7476
},
75-
wdsPluginPrebundleModules(),
77+
wdsPluginPrebundleModules(env),
7678
wdsPluginStorybookBuilder(options),
7779
wdsPluginExternalGlobals(globalsNameReferenceMap || globals),
7880
],
@@ -128,6 +130,8 @@ export const start: WdsBuilder['start'] = async ({ startTime, options, router, s
128130
};
129131

130132
export const build: WdsBuilder['build'] = async ({ startTime, options }) => {
133+
const env = await options.presets.apply<Record<string, string>>('env');
134+
131135
const rollupDefaultOutputOptions: OutputOptions = {
132136
dir: options.outputDir,
133137
};
@@ -142,7 +146,7 @@ export const build: WdsBuilder['build'] = async ({ startTime, options }) => {
142146
extractAssets: false,
143147
}),
144148
rollupPluginNodeResolve(),
145-
rollupPluginPrebundleModules(),
149+
rollupPluginPrebundleModules(env),
146150
rollupPluginStorybookBuilder(options),
147151
rollupPluginExternalGlobals(globalsNameReferenceMap || globals),
148152
],

packages/storybook-builder/src/rollup-plugin-prebundle-modules.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { stringifyProcessEnvs } from '@storybook/core-common';
12
import { build } from 'esbuild';
23
import { join } from 'path';
34
import type { Plugin } from 'rollup';
45
import { getNodeModuleDir } from './get-node-module-dir.js';
56

67
export const PREBUNDLED_MODULES_DIR = 'node_modules/.prebundled_modules';
78

8-
export function rollupPluginPrebundleModules(): Plugin {
9+
export function rollupPluginPrebundleModules(env: Record<string, string>): Plugin {
910
const modulePaths: Record<string, string> = {};
1011

1112
return {
@@ -36,6 +37,9 @@ export function rollupPluginPrebundleModules(): Plugin {
3637
lodash: getNodeModuleDir('lodash-es'), // more optimal, but also solves esbuild incorrectly compiling lodash/_nodeUtil
3738
path: require.resolve('path-browserify'),
3839
},
40+
define: {
41+
...stringifyProcessEnvs(env),
42+
},
3943
plugins: [esbuildCommonjsPlugin()],
4044
});
4145
},
@@ -60,7 +64,7 @@ function getModules() {
6064

6165
// this is different to https://github.com/storybookjs/storybook/blob/v7.0.0/code/lib/builder-vite/src/optimizeDeps.ts#L7
6266
// builder-vite bundles different dependencies for performance reasons
63-
// we aim only at browserifying dependencies which are CommonJS, or sometimes ESM with issues
67+
// we aim only at browserifying NodeJS dependencies (CommonJS/process.env/...)
6468
export const CANDIDATES = [
6569
// @testing-library has ESM, but imports/exports are not working correctly between packages
6670
// specifically "@testing-library/user-event" has "dist/esm/utils/misc/getWindow.js" (see https://cdn.jsdelivr.net/npm/@testing-library/[email protected]/dist/esm/utils/misc/getWindow.js)
@@ -78,4 +82,7 @@ export const CANDIDATES = [
7882

7983
// CommonJS module used in Storybook MJS files
8084
'lodash/mapValues.js',
85+
86+
// ESM, but uses `process.env.NODE_ENV`
87+
'tiny-invariant',
8188
];

packages/storybook-builder/static/iframe-template.html

-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@
4949
// We do this so that "module && module.hot" etc. in Storybook source code
5050
// doesn't fail (it will simply be disabled)
5151
window.module = undefined;
52-
5352
window.global = window;
54-
window.process = { env: '[PROCESS_ENV HERE]' };
5553
</script>
5654
<!-- [HEAD HTML SNIPPET HERE] -->
5755
</head>

0 commit comments

Comments
 (0)