Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Only use dotenv-webpack when a user has a dotenv file #15365

Merged
merged 2 commits into from Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/builder-webpack5/src/preview/iframe-webpack.config.ts
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 @@ -175,7 +176,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
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
@@ -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
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