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 31, 2023
1 parent 02d5ba5 commit 4ea443c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
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 Down Expand Up @@ -84,4 +85,4 @@
"platform": "node"
},
"gitHead": "91302a6818a7794e1e8bbff01c68513b8516cb53"
}
}
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;
};
8 changes: 3 additions & 5 deletions code/yarn.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 6
cacheKey: 8c0
Expand Down Expand Up @@ -6062,8 +6059,9 @@ __metadata:
resolution: "@storybook/core-common@workspace:lib/core-common"
dependencies:
"@babel/core": ^7.20.2
"@storybook/node-logger": 7.0.0-beta.38
"@storybook/types": 7.0.0-beta.38
"@storybook/csf-tools": 7.0.0-beta.36
"@storybook/node-logger": 7.0.0-beta.36
"@storybook/types": 7.0.0-beta.36
"@types/babel__core": ^7.1.20
"@types/express": ^4.7.0
"@types/mock-fs": ^4.13.1
Expand Down

0 comments on commit 4ea443c

Please sign in to comment.