add indexers config, deprecate storyIndexers

This commit is contained in:
Jeppe Reinhold 2023-07-24 11:59:32 +02:00
parent 6850a8f640
commit 380ee68b5c
11 changed files with 39 additions and 32 deletions

View File

@ -135,7 +135,7 @@ async function webpack(
return result;
}
const storyIndexers = (indexers: StoryIndexer[] | null) => {
const indexers = (existingIndexers: StoryIndexer[] | null) => {
const mdxIndexer = async (fileName: string, opts: IndexerOptions) => {
let code = (await fs.readFile(fileName, 'utf-8')).toString();
const { compile } = global.FEATURES?.legacyMdx1
@ -149,7 +149,7 @@ const storyIndexers = (indexers: StoryIndexer[] | null) => {
test: /(stories|story)\.mdx$/,
indexer: mdxIndexer,
},
...(indexers || []),
...(existingIndexers || []),
];
};
@ -170,9 +170,9 @@ export const addons: StorybookConfig['addons'] = [
* something down the dependency chain is using typescript namespaces, which are not supported by rollup-plugin-dts
*/
const webpackX = webpack as any;
const storyIndexersX = storyIndexers as any;
const indexersX = indexers as any;
const docsX = docs as any;
ensureReactPeerDeps();
export { webpackX as webpack, storyIndexersX as storyIndexers, docsX as docs };
export { webpackX as webpack, indexersX as indexers, docsX as docs };

View File

@ -105,14 +105,16 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
...options,
});
const [features, core, staticDirs, storyIndexers, stories, docsOptions] = await Promise.all([
presets.apply<StorybookConfig['features']>('features'),
presets.apply<CoreConfig>('core'),
presets.apply<StorybookConfig['staticDirs']>('staticDirs'),
presets.apply('storyIndexers', []),
presets.apply('stories'),
presets.apply<DocsOptions>('docs', {}),
]);
const [features, core, staticDirs, indexers, deprecatedStoryIndexers, stories, docsOptions] =
await Promise.all([
presets.apply<StorybookConfig['features']>('features'),
presets.apply<CoreConfig>('core'),
presets.apply<StorybookConfig['staticDirs']>('staticDirs'),
presets.apply('indexers', []),
presets.apply('storyIndexers', []),
presets.apply('stories'),
presets.apply<DocsOptions>('docs', {}),
]);
const fullOptions: Options = {
...options,
@ -161,7 +163,7 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
const normalizedStories = normalizeStories(stories, directories);
const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers,
indexers: deprecatedStoryIndexers.concat(indexers),
docs: docsOptions,
storiesV2Compatibility: !features?.storyStoreV7,
storyStoreV7: !!features?.storyStoreV7,

View File

@ -194,7 +194,7 @@ export const features = async (
legacyDecoratorFileOrder: false,
});
export const storyIndexers = async (indexers?: StoryIndexer[]) => {
export const indexers = async (existingIndexers?: StoryIndexer[]) => {
const csfIndexer = async (fileName: string, opts: IndexerOptions) => {
const code = (await readFile(fileName, 'utf-8')).toString();
return loadCsf(code, { ...opts, fileName }).parse();
@ -204,7 +204,7 @@ export const storyIndexers = async (indexers?: StoryIndexer[]) => {
test: /(stories|story)\.(m?js|ts)x?$/,
indexer: csfIndexer,
},
...(indexers || []),
...(existingIndexers || []),
];
};

View File

@ -47,7 +47,7 @@ const storiesMdxIndexer = async (fileName: string, opts: any) => {
const options = {
configDir: path.join(__dirname, '__mockdata__'),
workingDir: path.join(__dirname, '__mockdata__'),
storyIndexers: [
indexers: [
{ test: /\.stories\.mdx$/, indexer: storiesMdxIndexer },
{ test: /\.stories\.(m?js|ts)x?$/, indexer: csfIndexer },
] as StoryIndexer[],

View File

@ -106,7 +106,7 @@ export class StoryIndexGenerator {
configDir: Path;
storiesV2Compatibility: boolean;
storyStoreV7: boolean;
storyIndexers: StoryIndexer[];
indexers: StoryIndexer[];
docs: DocsOptions;
}
) {
@ -248,13 +248,11 @@ export class StoryIndexGenerator {
return userOrAutoTitleFromSpecifier(importPath, specifier, userTitle);
};
const storyIndexer = this.options.storyIndexers.find((indexer) =>
indexer.test.exec(absolutePath)
);
if (!storyIndexer) {
throw new Error(`No matching story indexer found for ${absolutePath}`);
const indexer = this.options.indexers.find((ind) => ind.test.exec(absolutePath));
if (!indexer) {
throw new Error(`No matching indexer found for ${absolutePath}`);
}
const csf = await storyIndexer.indexer(absolutePath, { makeTitle });
const csf = await indexer.indexer(absolutePath, { makeTitle });
const componentTags = csf.meta.tags || [];
csf.stories.forEach(({ id, name, tags: storyTags, parameters }) => {

View File

@ -23,13 +23,14 @@ export async function getStoryIndexGenerator(
workingDir,
};
const stories = options.presets.apply('stories');
const storyIndexers = options.presets.apply('storyIndexers', []);
const deprecatedStoryIndexers = options.presets.apply('storyIndexers', []);
const indexers = options.presets.apply('indexers', []);
const docsOptions = options.presets.apply<DocsOptions>('docs', {});
const normalizedStories = normalizeStories(await stories, directories);
const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers: await storyIndexers,
indexers: (await deprecatedStoryIndexers).concat(await indexers),
docs: await docsOptions,
workingDir,
storiesV2Compatibility: !features?.storyStoreV7,

View File

@ -55,7 +55,7 @@ const getInitializedStoryIndexGenerator = async (
inputNormalizedStories = normalizedStories
) => {
const generator = new StoryIndexGenerator(inputNormalizedStories, {
storyIndexers: [
indexers: [
{ test: /\.stories\.mdx$/, indexer: storiesMdxIndexer },
{ test: /\.stories\.(m?js|ts)x?$/, indexer: csfIndexer },
] as StoryIndexer[],

View File

@ -369,9 +369,15 @@ export interface StorybookConfig {
/**
* Process CSF files for the story index.
* @deprecated use {@link indexers} instead
*/
storyIndexers?: PresetValue<StoryIndexer[]>;
/**
* Process CSF files for the story index.
*/
indexers?: PresetValue<StoryIndexer[]>;
/**
* Docs related features in index generation
*/

View File

@ -4,7 +4,7 @@ import { toId } from '@storybook/csf';
import type { StaticMeta } from '@storybook/csf-tools';
import type { IndexerOptions, IndexedStory, StoryIndexer } from '@storybook/types';
export const storyIndexers = (indexers: StoryIndexer[] | null) => {
export const indexers = (existingIndexers: StoryIndexer[] | null) => {
const serverIndexer = async (fileName: string, opts: IndexerOptions) => {
const json = fileName.endsWith('.json')
? await fs.readJson(fileName, 'utf-8')
@ -31,6 +31,6 @@ export const storyIndexers = (indexers: StoryIndexer[] | null) => {
test: /(stories|story)\.(json|ya?ml)$/,
indexer: serverIndexer,
},
...(indexers || []),
...(existingIndexers || []),
];
};

View File

@ -5,7 +5,7 @@ import { readFileSync } from 'fs';
import { loadCsf } from '@storybook/csf-tools';
export default {
storyIndexers = (indexers) => {
indexers = (existingIndexers) => {
const indexer = async (fileName, opts) => {
const code = readFileSync(fileName, { encoding: 'utf-8' });
return loadCsf(code, { ...opts, fileName }).parse();
@ -16,7 +16,7 @@ export default {
test: /(stories|story)\.[tj]sx?$/,
indexer,
},
...(indexers || []),
...(existingIndexers || []),
];
},
};

View File

@ -9,7 +9,7 @@ import type { StorybookConfig } from '@storybook/your-framework';
import { parseCode } from './parseCode';
const config: StorybookConfig = {
storyIndexers: (indexers, addonOptions) => {
indexers: (existingIndexers, addonOptions) => {
const indexer = async (fileName, compilationOptions) => {
const code = parseCode(fileName, addonOptions);
const makeTitle = (userTitle) => {
@ -26,7 +26,7 @@ const config: StorybookConfig = {
test: /\.(md|html)$/,
indexer,
},
...(indexers || []),
...(existingIndexers || []),
];
},
};