diff --git a/lib/builder-webpack5/src/preview/iframe-webpack.config.ts b/lib/builder-webpack5/src/preview/iframe-webpack.config.ts index 4e1421f7d65b..abc20147766d 100644 --- a/lib/builder-webpack5/src/preview/iframe-webpack.config.ts +++ b/lib/builder-webpack5/src/preview/iframe-webpack.config.ts @@ -18,6 +18,7 @@ import { nodeModulesPaths, interpolate, Options, + hasDotenv, } from '@storybook/core-common'; import { createBabelLoader } from './babel-loader-preview'; @@ -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: { diff --git a/lib/core-common/src/index.ts b/lib/core-common/src/index.ts index c5c06e65f168..d2251f02d60f 100644 --- a/lib/core-common/src/index.ts +++ b/lib/core-common/src/index.ts @@ -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'; diff --git a/lib/core-common/src/utils/has-dotenv.ts b/lib/core-common/src/utils/has-dotenv.ts new file mode 100644 index 000000000000..41e209e14a88 --- /dev/null +++ b/lib/core-common/src/utils/has-dotenv.ts @@ -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')); diff --git a/lib/manager-webpack5/src/manager-webpack.config.ts b/lib/manager-webpack5/src/manager-webpack.config.ts index 1f9cc74437cf..376e42930c68 100644 --- a/lib/manager-webpack5/src/manager-webpack.config.ts +++ b/lib/manager-webpack5/src/manager-webpack.config.ts @@ -19,6 +19,7 @@ import { getManagerMainTemplate, Options, ManagerWebpackOptions, + hasDotenv, } from '@storybook/core-common'; import { babelLoader } from './babel-loader-manager'; @@ -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),