Skip to content

Commit

Permalink
Merge pull request #15925 from vdh/fix-define-plugin
Browse files Browse the repository at this point in the history
Core: Replaced `process.env` override in `DefinePlugin` config
  • Loading branch information
shilman committed Sep 7, 2021
2 parents 8a56e94 + 17cbc66 commit 06450c4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 11 deletions.
4 changes: 1 addition & 3 deletions examples/vue-kitchen-sink/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ if (process.env.NODE_ENV === 'production') {
// http://vue-loader.vuejs.org/en/workflow/production.html
module.exports.plugins = (module.exports.plugins || []).concat([
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: '"production"',
},
'process.env.NODE_ENV': '"production"',
}),
new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
Expand Down
4 changes: 2 additions & 2 deletions lib/builder-webpack4/src/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import themingPaths from '@storybook/theming/paths';

import {
toRequireContextString,
stringifyEnvs,
stringifyProcessEnvs,
es6Transpiler,
interpolate,
nodeModulesPaths,
Expand Down Expand Up @@ -178,7 +178,7 @@ export default async ({
template,
}),
new DefinePlugin({
'process.env': stringifyEnvs(envs),
...stringifyProcessEnvs(envs),
NODE_ENV: JSON.stringify(envs.NODE_ENV),
}),
isProd ? null : new WatchMissingNodeModulesPlugin(nodeModulesPaths),
Expand Down
4 changes: 2 additions & 2 deletions lib/builder-webpack5/src/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import themingPaths from '@storybook/theming/paths';
import {
toRequireContextString,
es6Transpiler,
stringifyEnvs,
stringifyProcessEnvs,
nodeModulesPaths,
interpolate,
Options,
Expand Down Expand Up @@ -178,7 +178,7 @@ export default async ({
template,
}),
new DefinePlugin({
'process.env': stringifyEnvs(envs),
...stringifyProcessEnvs(envs),
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
}),
isProd ? null : new WatchMissingNodeModulesPlugin(nodeModulesPaths),
Expand Down
13 changes: 13 additions & 0 deletions lib/core-common/src/utils/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,21 @@ export function loadEnvs(
};
}

/** @deprecated use `stringifyProcessEnvs` */
export const stringifyEnvs = (raw: Record<string, string>): Record<string, string> =>
Object.entries(raw).reduce<Record<string, string>>((acc, [key, value]) => {
acc[key] = JSON.stringify(value);
return acc;
}, {});

export const stringifyProcessEnvs = (raw: Record<string, string>): Record<string, string> =>
Object.entries(raw).reduce<Record<string, string>>(
(acc, [key, value]) => {
acc[`process.env.${key}`] = JSON.stringify(value);
return acc;
},
{
// Default fallback
'process.env.XSTORYBOOK_EXAMPLE_APP': '""',
}
);
4 changes: 2 additions & 2 deletions lib/manager-webpack4/src/presets/manager-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import readPackage from 'read-pkg-up';
import {
loadManagerOrAddonsFile,
resolvePathInStorybookCache,
stringifyEnvs,
stringifyProcessEnvs,
es6Transpiler,
getManagerHeadTemplate,
getManagerMainTemplate,
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function managerWebpack(
(new Dotenv({ silent: true }) as any) as WebpackPluginInstance,
// graphql sources check process variable
new DefinePlugin({
'process.env': stringifyEnvs(envs),
...stringifyProcessEnvs(envs),
NODE_ENV: JSON.stringify(envs.NODE_ENV),
}) as WebpackPluginInstance,
// isProd &&
Expand Down
4 changes: 2 additions & 2 deletions lib/manager-webpack5/src/presets/manager-preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import readPackage from 'read-pkg-up';
import {
loadManagerOrAddonsFile,
resolvePathInStorybookCache,
stringifyEnvs,
stringifyProcessEnvs,
es6Transpiler,
getManagerHeadTemplate,
getManagerMainTemplate,
Expand Down Expand Up @@ -113,7 +113,7 @@ export async function managerWebpack(
hasDotenv() ? new Dotenv({ silent: true }) : null,
// graphql sources check process variable
new DefinePlugin({
'process.env': stringifyEnvs(envs),
...stringifyProcessEnvs(envs),
NODE_ENV: JSON.stringify(envs.NODE_ENV),
}) as WebpackPluginInstance,
// isProd &&
Expand Down

0 comments on commit 06450c4

Please sign in to comment.