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

CLI: add warning when main config does not use default exports #20802

Merged
merged 3 commits into from
Jan 31, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion code/lib/core-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
},
"dependencies": {
"@babel/core": "^7.20.2",
"@storybook/csf-tools": "7.0.0-beta.38",
"@storybook/node-logger": "7.0.0-beta.38",
"@storybook/types": "7.0.0-beta.38",
"@types/babel__core": "^7.1.20",
Expand All @@ -57,7 +58,7 @@
"file-system-cache": "^2.0.0",
"find-up": "^5.0.0",
"fs-extra": "^11.1.0",
"glob": "^7.1.6",
"glob-promise": "^4.2.0",
"handlebars": "^4.7.7",
"lazy-universal-dotenv": "^4.0.0",
"picomatch": "^2.3.0",
Expand Down
8 changes: 6 additions & 2 deletions code/lib/core-common/src/utils/load-main-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import type { StorybookConfig } from '@storybook/types';
import { serverRequire } from './interpret-require';
import { validateConfigurationFiles } from './validate-configuration-files';

export function loadMainConfig({ configDir }: { configDir: string }): StorybookConfig {
validateConfigurationFiles(configDir);
export async function loadMainConfig({
configDir,
}: {
configDir: string;
}): Promise<StorybookConfig> {
await validateConfigurationFiles(configDir);

return serverRequire(path.resolve(configDir, 'main'));
}
30 changes: 23 additions & 7 deletions code/lib/core-common/src/utils/validate-configuration-files.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import { dedent } from 'ts-dedent';
import glob from 'glob';
import { promise as glob } from 'glob-promise';
import path from 'path';
import { readConfig } from '@storybook/csf-tools';
import { once } from '@storybook/node-logger';

import { boost } from './interpret-files';

export function validateConfigurationFiles(configDir: string) {
export async function validateConfigurationFiles(configDir: string) {
const extensionsPattern = `{${Array.from(boost).join(',')}}`;
const exists = (file: string) =>
!!glob.sync(path.resolve(configDir, `${file}${extensionsPattern}`)).length;
const mainConfigMatches = await glob(path.resolve(configDir, `main${extensionsPattern}`));

const main = exists('main');
const [mainConfigPath] = mainConfigMatches;

if (!main) {
if (mainConfigMatches.length > 1) {
once.warn(dedent`
Multiple main files found in your configDir (${path.resolve(configDir)}).
Storybook will use the first one found and ignore the others. Please remove the extra files.
`);
}

if (!mainConfigPath) {
throw new Error(dedent`
No configuration files have been found in your configDir (${path.resolve(configDir)}).
Storybook needs either a "main" or "config" file.
Storybook needs "main.js" file, please add it.
`);
} else {
const main = await readConfig(mainConfigPath);
if (!main.hasDefaultExport) {
once.warn(dedent`
Your main.js is not using a default export, which is the recommended format. Please update it.
For more info: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#esm-format-in-mainjs
`);
}
}
}
2 changes: 1 addition & 1 deletion code/lib/core-server/src/build-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export async function buildDevStandalone(
options.serverChannelUrl = getServerChannelUrl(port, options);
/* eslint-enable no-param-reassign */

const { framework } = loadMainConfig(options);
const { framework } = await loadMainConfig(options);
const corePresets = [];

const frameworkName = typeof framework === 'string' ? framework : framework?.name;
Expand Down
2 changes: 1 addition & 1 deletion code/lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
await emptyDir(options.outputDir);
await ensureDir(options.outputDir);

const { framework } = loadMainConfig(options);
const { framework } = await loadMainConfig(options);
const corePresets = [];

const frameworkName = typeof framework === 'string' ? framework : framework?.name;
Expand Down
3 changes: 3 additions & 0 deletions code/lib/csf-tools/src/ConfigFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ export class ConfigFile {

fileName?: string;

hasDefaultExport = false;

constructor(ast: t.File, code: string, fileName?: string) {
this._ast = ast;
this._code = code;
Expand All @@ -131,6 +133,7 @@ export class ConfigFile {
traverse.default(this._ast, {
ExportDefaultDeclaration: {
enter({ node, parent }) {
self.hasDefaultExport = true;
const decl =
t.isIdentifier(node.declaration) && t.isProgram(parent)
? _findVarInitialization(node.declaration.name, parent)
Expand Down
2 changes: 1 addition & 1 deletion code/lib/telemetry/src/storybook-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export const getStorybookMetadata = async (_configDir?: string) => {
'--config-dir'
) as string)) ??
'.storybook';
const mainConfig = loadMainConfig({ configDir });
const mainConfig = await loadMainConfig({ configDir });
cachedMetadata = await computeStorybookMetadata({ mainConfig, packageJson });
return cachedMetadata;
};
3 changes: 2 additions & 1 deletion code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6062,6 +6062,7 @@ __metadata:
resolution: "@storybook/core-common@workspace:lib/core-common"
dependencies:
"@babel/core": ^7.20.2
"@storybook/csf-tools": 7.0.0-beta.38
"@storybook/node-logger": 7.0.0-beta.38
"@storybook/types": 7.0.0-beta.38
"@types/babel__core": ^7.1.20
Expand All @@ -6077,7 +6078,7 @@ __metadata:
file-system-cache: ^2.0.0
find-up: ^5.0.0
fs-extra: ^11.1.0
glob: ^7.1.6
glob-promise: ^4.2.0
handlebars: ^4.7.7
lazy-universal-dotenv: ^4.0.0
mock-fs: ^5.2.0
Expand Down