Skip to content

Commit

Permalink
Warn users to use default export in main.js
Browse files Browse the repository at this point in the history
- involves changes in csf-tools to define a flag that tells us there is (or not) a default export
- involves changes in affected packages now that loadMainConfig is async
  • Loading branch information
yannbf committed Jan 26, 2023
1 parent b6d7fcc commit 1b1db9f
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions 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.34",
"@storybook/node-logger": "7.0.0-beta.34",
"@storybook/types": "7.0.0-beta.34",
"@types/babel__core": "^7.1.20",
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'));
}
21 changes: 15 additions & 6 deletions code/lib/core-common/src/utils/validate-configuration-files.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { dedent } from 'ts-dedent';
import glob from 'glob';
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 = glob.sync(path.resolve(configDir, `main${extensionsPattern}`));

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

if (!main) {
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;
};
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6050,6 +6050,7 @@ __metadata:
resolution: "@storybook/core-common@workspace:lib/core-common"
dependencies:
"@babel/core": ^7.20.2
"@storybook/csf-tools": 7.0.0-beta.34
"@storybook/node-logger": 7.0.0-beta.34
"@storybook/types": 7.0.0-beta.34
"@types/babel__core": ^7.1.20
Expand Down

0 comments on commit 1b1db9f

Please sign in to comment.