Skip to content

Init: Exclude mdx stories when docs feature isn't selected during init #32142

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

Merged
merged 2 commits into from
Jul 29, 2025
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
6 changes: 5 additions & 1 deletion code/lib/create-storybook/src/generators/NUXT/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { baseGenerator } from '../baseGenerator';
import type { Generator } from '../types';

const generator: Generator = async (packageManager, npmOptions, options) => {
const extraStories = options.features.includes('docs') ? ['../components/**/*.mdx'] : [];

extraStories.push('../components/**/*.stories.@(js|jsx|ts|tsx|mdx)');

await baseGenerator(
packageManager,
{
Expand All @@ -16,7 +20,7 @@ const generator: Generator = async (packageManager, npmOptions, options) => {
installFrameworkPackages: false,
componentsDestinationPath: './components',
extraMain: {
stories: ['../components/**/*.mdx', '../components/**/*.stories.@(js|jsx|ts|tsx|mdx)'],
stories: extraStories,
},
},
'nuxt'
Expand Down
1 change: 1 addition & 0 deletions code/lib/create-storybook/src/generators/baseGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ export async function baseGenerator(
name: frameworkPackagePath,
options: options.framework || {},
},
features,
frameworkPackage,
prefixes,
storybookConfigFolder,
Expand Down
39 changes: 37 additions & 2 deletions code/lib/create-storybook/src/generators/configure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe('configureMain', () => {
name: '@storybook/react-vite',
},
frameworkPackage: '@storybook/react-vite',
features: [],
});

const { calls } = vi.mocked(fsp.writeFile).mock;
Expand All @@ -38,7 +39,6 @@ describe('configureMain', () => {
/** @type { import('@storybook/react-vite').StorybookConfig } */
const config = {
"stories": [
"../stories/**/*.mdx",
"../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)"
],
"addons": [],
Expand All @@ -50,7 +50,7 @@ describe('configureMain', () => {
`);
});

it('should generate main.ts', async () => {
it('should generate main.ts with docs feature', async () => {
await configureMain({
language: SupportedLanguage.TYPESCRIPT,
addons: [],
Expand All @@ -60,6 +60,7 @@ describe('configureMain', () => {
name: '@storybook/react-vite',
},
frameworkPackage: '@storybook/react-vite',
features: ['docs'],
});

const { calls } = vi.mocked(fsp.writeFile).mock;
Expand All @@ -83,6 +84,39 @@ describe('configureMain', () => {
`);
});

it('should generate main.ts without docs feature', async () => {
await configureMain({
language: SupportedLanguage.TYPESCRIPT,
addons: [],
prefixes: [],
storybookConfigFolder: '.storybook',
framework: {
name: '@storybook/react-vite',
},
frameworkPackage: '@storybook/react-vite',
features: [],
});

const { calls } = vi.mocked(fsp.writeFile).mock;
const [mainConfigPath, mainConfigContent] = calls[0];

expect(mainConfigPath).toEqual('./.storybook/main.ts');
expect(mainConfigContent).toMatchInlineSnapshot(`
"import type { StorybookConfig } from '@storybook/react-vite';

const config: StorybookConfig = {
"stories": [
"../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)"
],
"addons": [],
"framework": {
"name": "@storybook/react-vite"
}
};
export default config;"
`);
});

it('should handle resolved paths in pnp', async () => {
await configureMain({
language: SupportedLanguage.JAVASCRIPT,
Expand All @@ -96,6 +130,7 @@ describe('configureMain', () => {
name: "%%path.dirname(require.resolve(path.join('@storybook/react-webpack5', 'package.json')))%%",
},
frameworkPackage: '@storybook/react-webpack5',
features: ['docs'],
});

const { calls } = vi.mocked(fsp.writeFile).mock;
Expand Down
24 changes: 8 additions & 16 deletions code/lib/create-storybook/src/generators/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { dedent } from 'ts-dedent';

import { SupportedLanguage } from '../../../../core/src/cli/project_types';
import { logger } from '../../../../core/src/node-logger';
import type { GeneratorFeature } from './types';

interface ConfigureMainOptions {
addons: string[];
Expand All @@ -14,6 +15,7 @@ interface ConfigureMainOptions {
language: SupportedLanguage;
prefixes: string[];
frameworkPackage: string;
features: GeneratorFeature[];
/**
* Extra values for main.js
*
Expand Down Expand Up @@ -42,34 +44,24 @@ const pathExists = async (path: string) => {
.catch(() => false);
};

/**
* We need to clean up the paths in case of pnp input:
* `path.dirname(require.resolve(path.join('@storybook/react-webpack5', 'package.json')))` output:
* `@storybook/react-webpack5`
*/
const sanitizeFramework = (framework: string) => {
// extract either @storybook/<framework> or storybook-<framework>
const matches = framework.match(/(@storybook\/\w+(?:-\w+)*)|(storybook-(\w+(?:-\w+)*))/g);
if (!matches) {
return undefined;
}

return matches[0];
};

export async function configureMain({
addons,
extensions = ['js', 'jsx', 'mjs', 'ts', 'tsx'],
storybookConfigFolder,
language,
frameworkPackage,
prefixes = [],
features = [],
...custom
}: ConfigureMainOptions) {
const srcPath = resolve(storybookConfigFolder, '../src');
const prefix = (await pathExists(srcPath)) ? '../src' : '../stories';
const stories = features.includes('docs') ? [`${prefix}/**/*.mdx`] : [];

stories.push(`${prefix}/**/*.stories.@(${extensions.join('|')})`);

const config = {
stories: [`${prefix}/**/*.mdx`, `${prefix}/**/*.stories.@(${extensions.join('|')})`],
stories,
addons,
...custom,
};
Expand Down
2 changes: 1 addition & 1 deletion code/lib/create-storybook/src/generators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type GeneratorOptions = {
frameworkPreviewParts?: FrameworkPreviewParts;
// skip prompting the user
yes: boolean;
features: string[];
features: Array<GeneratorFeature>;
};

export interface FrameworkOptions {
Expand Down