Skip to content
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
1 change: 1 addition & 0 deletions code/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ const config = defineMain({
developmentModeForBuild: true,
experimentalTestSyntax: true,
experimentalComponentsManifest: true,
experimentalCodeExamples: true,
},
staticDirs: [{ from: './bench/bundle-analyzer', to: '/bundle-analyzer' }],
viteFinal: async (viteConfig, { configType }) => {
Expand Down
10 changes: 9 additions & 1 deletion code/addons/docs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { fileURLToPath } from 'node:url';

import { logger } from 'storybook/internal/node-logger';
import type { Options, PresetProperty, StorybookConfigRaw } from 'storybook/internal/types';
import { type CsfEnricher } from 'storybook/internal/types';

import type { CsfPluginOptions } from '@storybook/csf-plugin';

Expand Down Expand Up @@ -41,6 +42,8 @@ async function webpack(

const { csfPluginOptions = {}, mdxPluginOptions = {} } = options;

const enrichCsf = await options.presets.apply('experimental_enrichCsf');

const rehypeSlug = (await import('rehype-slug')).default;
const rehypeExternalLinks = (await import('rehype-external-links')).default;

Expand Down Expand Up @@ -100,7 +103,12 @@ async function webpack(
...(webpackConfig.plugins || []),

...(csfPluginOptions
? [(await import('@storybook/csf-plugin')).webpack(csfPluginOptions)]
? [
(await import('@storybook/csf-plugin')).webpack({
...csfPluginOptions,
enrichCsf,
}),
]
: []),
],
resolve: {
Expand Down
7 changes: 6 additions & 1 deletion code/builders/builder-vite/src/plugins/csf-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export async function csfPlugin(config: Options): Promise<Plugin> {
// @ts-expect-error - not sure what type to use here
addons.find((a) => [a, a.name].includes('@storybook/addon-docs'))?.options ?? {};

const enrichCsf = await presets.apply('experimental_enrichCsf');

// TODO: looks like unplugin can return an array of plugins
return vite(docsOptions?.csfPluginOptions) as Plugin;
return vite({
...docsOptions?.csfPluginOptions,
enrichCsf,
}) as Plugin;
}
4 changes: 2 additions & 2 deletions code/core/src/common/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { getInterpretedFile } from './utils/interpret-files';
import { stripAbsNodeModulesPath } from './utils/strip-abs-node-modules-path';
import { validateConfigurationFiles } from './utils/validate-configuration-files';

type InterPresetOptions = Omit<
export type InterPresetOptions = Omit<
CLIOptions &
LoadOptions &
BuilderOptions & { isCritical?: boolean; build?: StorybookConfigRaw['build'] },
Expand Down Expand Up @@ -321,7 +321,7 @@ export async function getPresets(
const loadedPresets: LoadedPreset[] = await loadPresets(presets, 0, storybookOptions);

return {
apply: async (extension: string, config: any, args = {}) =>
apply: async (extension: string, config?: any, args = {}) =>
applyPresets(loadedPresets, extension, config, args, storybookOptions),
};
}
Expand Down
2 changes: 1 addition & 1 deletion code/core/src/common/utils/formatter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
async function getPrettier() {
export async function getPrettier() {
return import('prettier').catch((e) => ({
resolveConfig: async () => null,
format: (content: string) => content,
Expand Down
3 changes: 2 additions & 1 deletion code/core/src/core-server/presets/favicon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dirname, join } from 'node:path';
import { expect, it, vi } from 'vitest';

import { logger } from 'storybook/internal/node-logger';
import { type Presets } from 'storybook/internal/types';

import * as m from './common-preset';

Expand Down Expand Up @@ -41,7 +42,7 @@ const createOptions = (locations: string[]): Parameters<typeof m.favicon>[1] =>
}
}
},
},
} as Presets,
});

vi.mock('storybook/internal/node-logger', () => {
Expand Down
88 changes: 44 additions & 44 deletions code/core/src/csf-tools/enrichCsf.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ expect.addSnapshotSerializer({
test: () => true,
});

const enrich = (code: string, originalCode: string, options?: EnrichCsfOptions) => {
const enrich = async (code: string, originalCode: string, options?: EnrichCsfOptions) => {
// we don't actually care about the title

const csf = loadCsf(code, {
Expand All @@ -20,15 +20,15 @@ const enrich = (code: string, originalCode: string, options?: EnrichCsfOptions)
const csfSource = loadCsf(originalCode, {
makeTitle: (userTitle) => userTitle || 'default',
}).parse();
enrichCsf(csf, csfSource, options);
await enrichCsf(csf, csfSource, options);
return formatCsf(csf);
};

describe('enrichCsf', () => {
describe('source', () => {
it('csf1', () => {
it('csf1', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -62,9 +62,9 @@ describe('enrichCsf', () => {
};
`);
});
it('csf2', () => {
it('csf2', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -106,9 +106,9 @@ describe('enrichCsf', () => {
};
`);
});
it('csf3', () => {
it('csf3', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -148,9 +148,9 @@ describe('enrichCsf', () => {
};
`);
});
it('csf factories', () => {
it('csf factories', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
import {config} from "/.storybook/preview.ts";
Expand Down Expand Up @@ -193,9 +193,9 @@ describe('enrichCsf', () => {
};
`);
});
it('multiple stories', () => {
it('multiple stories', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -245,9 +245,9 @@ describe('enrichCsf', () => {
});

describe('story descriptions', () => {
it('skips inline comments', () => {
it('skips inline comments', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -285,9 +285,9 @@ describe('enrichCsf', () => {
`);
});

it('skips blocks without jsdoc', () => {
it('skips blocks without jsdoc', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -323,9 +323,9 @@ describe('enrichCsf', () => {
`);
});

it('JSDoc single-line', () => {
it('JSDoc single-line', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -365,9 +365,9 @@ describe('enrichCsf', () => {
`);
});

it('JSDoc multi-line', () => {
it('JSDoc multi-line', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -411,9 +411,9 @@ describe('enrichCsf', () => {
`);
});

it('preserves indentation', () => {
it('preserves indentation', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -459,9 +459,9 @@ describe('enrichCsf', () => {
});

describe('meta descriptions', () => {
it('skips inline comments', () => {
it('skips inline comments', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -497,9 +497,9 @@ describe('enrichCsf', () => {
`);
});

it('skips blocks without jsdoc', () => {
it('skips blocks without jsdoc', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -535,9 +535,9 @@ describe('enrichCsf', () => {
`);
});

it('JSDoc single-line', () => {
it('JSDoc single-line', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -580,9 +580,9 @@ describe('enrichCsf', () => {
`);
});

it('JSDoc multi-line', () => {
it('JSDoc multi-line', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -629,9 +629,9 @@ describe('enrichCsf', () => {
`);
});

it('preserves indentation', () => {
it('preserves indentation', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -678,9 +678,9 @@ describe('enrichCsf', () => {
`);
});

it('correctly interleaves parameters', () => {
it('correctly interleaves parameters', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -734,9 +734,9 @@ describe('enrichCsf', () => {
`);
});

it('respects user component description', () => {
it('respects user component description', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -793,9 +793,9 @@ describe('enrichCsf', () => {
`);
});

it('respects meta variables', () => {
it('respects meta variables', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
const meta = {
Expand Down Expand Up @@ -844,9 +844,9 @@ describe('enrichCsf', () => {
});

describe('options', () => {
it('disableSource', () => {
it('disableSource', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -883,9 +883,9 @@ describe('enrichCsf', () => {
`);
});

it('disableDescription', () => {
it('disableDescription', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down Expand Up @@ -922,9 +922,9 @@ describe('enrichCsf', () => {
`);
});

it('disable all', () => {
it('disable all', async () => {
expect(
enrich(
await enrich(
dedent`
// compiled code
export default {
Expand Down
5 changes: 4 additions & 1 deletion code/core/src/csf-tools/enrichCsf.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { generate, types as t } from 'storybook/internal/babel';
import { type CsfEnricher } from 'storybook/internal/types';

import type { CsfFile } from './CsfFile';

export interface EnrichCsfOptions {
disableSource?: boolean;
disableDescription?: boolean;
enrichCsf?: CsfEnricher;
}

export const enrichCsfStory = (
Expand Down Expand Up @@ -139,8 +141,9 @@ export const enrichCsfMeta = (csf: CsfFile, csfSource: CsfFile, options?: Enrich
}
};

export const enrichCsf = (csf: CsfFile, csfSource: CsfFile, options?: EnrichCsfOptions) => {
export const enrichCsf = async (csf: CsfFile, csfSource: CsfFile, options?: EnrichCsfOptions) => {
enrichCsfMeta(csf, csfSource, options);
await options?.enrichCsf?.(csf, csfSource);
Object.keys(csf._storyExports).forEach((key) => {
enrichCsfStory(csf, csfSource, key, options);
});
Expand Down
Loading