Skip to content

Commit 25f0fa1

Browse files
committed
solve process.env usage in Storybook MJS modules
1 parent c6323ba commit 25f0fa1

File tree

6 files changed

+49
-17
lines changed

6 files changed

+49
-17
lines changed

package-lock.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/storybook-builder/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"dependencies": {
5050
"@mdx-js/mdx": "^3.0.0",
5151
"@rollup/plugin-node-resolve": "^15.1.0",
52+
"@rollup/plugin-replace": "^6.0.2",
5253
"@rollup/pluginutils": "^5.0.2",
5354
"@storybook/core-common": "^8.5.0",
5455
"@storybook/node-logger": "^8.5.0",

packages/storybook-builder/src/index.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import rollupPluginNodeResolve from '@rollup/plugin-node-resolve';
2+
import rollupPluginReplace from '@rollup/plugin-replace';
23
import { getBuilderOptions } from '@storybook/core-common';
34
import { logger } from '@storybook/node-logger';
45
// Import both globals and globalsNameReferenceMap to prevent retrocompatibility.
@@ -23,10 +24,12 @@ import {
2324
rollupPluginPrebundleModules,
2425
} from './rollup-plugin-prebundle-modules.js';
2526
import { rollupPluginStorybookBuilder } from './rollup-plugin-storybook-builder.js';
27+
import { stringifyProcessEnvs } from './stringify-process-envs.js';
2628

2729
const wdsPluginExternalGlobals = fromRollup(rollupPluginExternalGlobals);
2830
const wdsPluginMdx = fromRollup(rollupPluginMdx);
2931
const wdsPluginPrebundleModules = fromRollup(rollupPluginPrebundleModules);
32+
const wdsPluginReplace = fromRollup(rollupPluginReplace);
3033
const wdsPluginStorybookBuilder = fromRollup(rollupPluginStorybookBuilder);
3134

3235
export type StorybookConfigWds = StorybookConfigBase & {
@@ -90,6 +93,11 @@ export const start: WdsBuilder['start'] = async ({ startTime, options, router, s
9093
wdsPluginStorybookBuilder(options),
9194
wdsPluginMdx(options),
9295
wdsPluginExternalGlobals(globalsNameReferenceMap || globals),
96+
wdsPluginReplace({
97+
...stringifyProcessEnvs(env),
98+
include: ['**/node_modules/@storybook/**/*'],
99+
preventAssignment: true,
100+
}),
93101
],
94102
};
95103

@@ -164,6 +172,11 @@ export const build: WdsBuilder['build'] = async ({ startTime, options }) => {
164172
rollupPluginStorybookBuilder(options),
165173
rollupPluginMdx(options),
166174
rollupPluginExternalGlobals(globalsNameReferenceMap || globals),
175+
rollupPluginReplace({
176+
...stringifyProcessEnvs(env),
177+
include: ['**/node_modules/@storybook/**/*'],
178+
preventAssignment: true,
179+
}),
167180
],
168181
};
169182

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import type { Options } from '@storybook/types';
2-
import { stringifyProcessEnvs } from '@storybook/core-common';
32
import { build } from 'esbuild';
43
import { rm, readFile } from 'node:fs/promises';
54
import { join, normalize, isAbsolute, dirname } from 'node:path';
65
import type { Plugin } from 'rollup';
76
import { esbuildPluginCommonjsNamedExports } from './esbuild-plugin-commonjs-named-exports.js';
8-
import { getNodeModuleDir } from './get-node-module-dir.js';
7+
import { stringifyProcessEnvs } from './stringify-process-envs.js';
98

109
export const PREBUNDLED_MODULES_DIR = normalize('node_modules/.prebundled_modules');
1110

@@ -46,15 +45,7 @@ export function rollupPluginPrebundleModules(
4645
}),
4746
},
4847
external: [...modules],
49-
define: (() => {
50-
const define = stringifyProcessEnvs(env);
51-
52-
// "NODE_PATH" pollutes the output, it's not used by prebundled modules and is not recommended in general
53-
// see more https://github.com/nodejs/node/issues/38128#issuecomment-814969356
54-
delete define['process.env.NODE_PATH'];
55-
56-
return define;
57-
})(),
48+
define: stringifyProcessEnvs(env),
5849
plugins: [esbuildPluginCommonjsNamedExports(modules)],
5950
});
6051
},
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { stringifyProcessEnvs as storybookStringifyProcessEnvs } from '@storybook/core-common';
2+
3+
export function stringifyProcessEnvs(env: Record<string, string>) {
4+
const result = storybookStringifyProcessEnvs(env);
5+
6+
// "NODE_PATH" pollutes the output, it's not really used and is not recommended in general
7+
// see more https://github.com/nodejs/node/issues/38128#issuecomment-814969356
8+
delete result['process.env.NODE_PATH'];
9+
10+
return result;
11+
}

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@
5757
// doesn't fail (it will simply be disabled)
5858
window.module = undefined;
5959
window.global = window;
60-
window.process = {
61-
env: {
62-
// TODO(storybook): unlikely this works properly in the rollup build, but somehow it wasn't failing either
63-
NODE_ENV: 'development',
64-
},
65-
};
6660
</script>
6761
<!-- [HEAD HTML SNIPPET HERE] -->
6862
</head>

0 commit comments

Comments
 (0)