Skip to content

Commit

Permalink
Merge pull request #15365 from storybookjs/14257-conditional-dotenv
Browse files Browse the repository at this point in the history
Core: Only use dotenv-webpack when a user has a dotenv file
  • Loading branch information
shilman committed Jun 28, 2021
1 parent 2d41219 commit dcc3461
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/builder-webpack5/src/preview/iframe-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
nodeModulesPaths,
interpolate,
Options,
hasDotenv,
} from '@storybook/core-common';
import { createBabelLoader } from './babel-loader-preview';

Expand Down Expand Up @@ -177,7 +178,7 @@ export default async ({
isProd ? null : new HotModuleReplacementPlugin(),
new CaseSensitivePathsPlugin(),
quiet ? null : new ProgressPlugin({}),
new Dotenv({ silent: true }),
hasDotenv() ? new Dotenv({ silent: true }) : null,
shouldCheckTs ? new ForkTsCheckerWebpackPlugin(tsCheckOptions) : null,
].filter(Boolean),
module: {
Expand Down
1 change: 1 addition & 0 deletions lib/core-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export * from './utils/template';
export * from './utils/interpolate';
export * from './utils/validate-configuration-files';
export * from './utils/to-require-context';
export * from './utils/has-dotenv';

export * from './types';
10 changes: 10 additions & 0 deletions lib/core-common/src/utils/has-dotenv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fs from 'fs';
import path from 'path';

/**
* Is there a .env file in the current directory?
*
* This is the default behavior of `dotenv-webpack-plugin`
* https://github.com/mrsteele/dotenv-webpack/blob/master/src/index.js#L34
*/
export const hasDotenv = () => fs.existsSync(path.join('.', '.env'));
3 changes: 2 additions & 1 deletion lib/manager-webpack5/src/manager-webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
getManagerMainTemplate,
Options,
ManagerWebpackOptions,
hasDotenv,
} from '@storybook/core-common';

import { babelLoader } from './babel-loader-manager';
Expand Down Expand Up @@ -103,7 +104,7 @@ export default async ({
template,
}) as any) as WebpackPluginInstance,
(new CaseSensitivePathsPlugin() as any) as WebpackPluginInstance,
new Dotenv({ silent: true }),
hasDotenv() ? new Dotenv({ silent: true }) : null,
// graphql sources check process variable
new DefinePlugin({
'process.env': stringifyEnvs(envs),
Expand Down

0 comments on commit dcc3461

Please sign in to comment.