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
Core: Remove storyIndexers in favor of experimental_indexers
  • Loading branch information
yannbf committed Jan 5, 2024
commit b50aea9b04344e51c1268f6417a8c5d37488d378
5 changes: 5 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
- [DecoratorFn, Story, ComponentStory, ComponentStoryObj, ComponentStoryFn and ComponentMeta TypeScript types](#decoratorfn-story-componentstory-componentstoryobj-componentstoryfn-and-componentmeta-typescript-types)
- ["Framework" TypeScript types](#framework-typescript-types)
- [`navigateToSettingsPage` method from Storybook's manager-api](#navigatetosettingspage-method-from-storybooks-manager-api)
- [storyIndexers](#storyindexers)
- [From version 7.5.0 to 7.6.0](#from-version-750-to-760)
- [CommonJS with Vite is deprecated](#commonjs-with-vite-is-deprecated)
- [Using implicit actions during rendering is deprecated](#using-implicit-actions-during-rendering-is-deprecated)
Expand Down Expand Up @@ -651,6 +652,10 @@ export const Component = () => {
}
```

#### storyIndexers

The Storybook's main.js configuration property `storyIndexers` is now removed in favor of `experimental_indexers`. [More info](#storyindexers-is-replaced-with-experimental_indexers).

## From version 7.5.0 to 7.6.0

#### CommonJS with Vite is deprecated
Expand Down
19 changes: 8 additions & 11 deletions code/lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,14 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
build,
});

const [features, core, staticDirs, indexers, deprecatedStoryIndexers, stories, docsOptions] =
await Promise.all([
presets.apply('features'),
presets.apply('core'),
presets.apply('staticDirs'),
presets.apply('experimental_indexers', []),
presets.apply('storyIndexers', []),
presets.apply('stories'),
presets.apply('docs', {}),
]);
const [features, core, staticDirs, indexers, stories, docsOptions] = await Promise.all([
presets.apply('features'),
presets.apply('core'),
presets.apply('staticDirs'),
presets.apply('experimental_indexers', []),
presets.apply('stories'),
presets.apply('docs', {}),
]);

if (features?.storyStoreV7 === false) {
deprecate(
Expand Down Expand Up @@ -150,7 +148,6 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption
const normalizedStories = normalizeStories(stories, directories);
const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers: deprecatedStoryIndexers,
indexers,
docs: docsOptions,
storyStoreV7: !!features?.storyStoreV7,
Expand Down
1 change: 0 additions & 1 deletion code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const getStorySortParameterMock = vi.mocked(getStorySortParameter);
const options: StoryIndexGeneratorOptions = {
configDir: path.join(__dirname, '__mockdata__'),
workingDir: path.join(__dirname, '__mockdata__'),
storyIndexers: [],
indexers: [csfIndexer],
storyStoreV7: true,
docs: { defaultName: 'docs', autodocs: false },
Expand Down
86 changes: 2 additions & 84 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ import type {
DocsIndexEntry,
ComponentTitle,
NormalizedStoriesSpecifier,
StoryIndexer,
DocsOptions,
Path,
Tag,
StoryIndex,
StoryName,
Indexer,
IndexerOptions,
DeprecatedIndexer,
StorybookConfigRaw,
} from '@storybook/types';
import { userOrAutoTitleFromSpecifier, sortStoriesV7 } from '@storybook/preview-api';
import { commonGlobOptions, normalizeStoryPath } from '@storybook/core-common';
import { deprecate, logger, once } from '@storybook/node-logger';
import { logger, once } from '@storybook/node-logger';
import { getStorySortParameter } from '@storybook/csf-tools';
import { storyNameFromExport, toId } from '@storybook/csf';
import { analyze } from '@storybook/docs-mdx';
Expand Down Expand Up @@ -53,7 +50,6 @@ export type StoryIndexGeneratorOptions = {
workingDir: Path;
configDir: Path;
storyStoreV7: boolean;
storyIndexers: StoryIndexer[];
indexers: Indexer[];
docs: DocsOptions;
build?: StorybookConfigRaw['build'];
Expand Down Expand Up @@ -284,25 +280,10 @@ export class StoryIndexGenerator {
return title;
};

const indexer = (this.options.indexers as StoryIndexer[])
.concat(this.options.storyIndexers)
.find((ind) => ind.test.exec(absolutePath));
const indexer = this.options.indexers.find((ind) => ind.test.exec(absolutePath));

invariant(indexer, `No matching indexer found for ${absolutePath}`);

if (indexer.indexer) {
deprecate(
dedent`'storyIndexers' is deprecated, please use 'experimental_indexers' instead. Found a deprecated indexer with matcher: ${indexer.test}
- Refer to the migration guide at https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#storyindexers-is-replaced-with-experimental_indexers`
);
return this.extractStoriesFromDeprecatedIndexer({
indexer: indexer.indexer,
indexerOptions: { makeTitle: defaultMakeTitle },
absolutePath,
importPath,
});
}

const indexInputs = await indexer.createIndex(absolutePath, { makeTitle: defaultMakeTitle });

const entries: ((StoryIndexEntryWithMetaId | DocsCacheEntry) & { tags: Tag[] })[] =
Expand Down Expand Up @@ -362,69 +343,6 @@ export class StoryIndexGenerator {
};
}

async extractStoriesFromDeprecatedIndexer({
indexer,
indexerOptions,
absolutePath,
importPath,
}: {
indexer: DeprecatedIndexer['indexer'];
indexerOptions: IndexerOptions;
absolutePath: Path;
importPath: Path;
}) {
const csf = await indexer(absolutePath, indexerOptions);

const entries = [];

const componentTags = csf.meta.tags || [];
csf.stories.forEach(({ id, name, tags: storyTags, parameters }) => {
if (!parameters?.docsOnly) {
const tags = (csf.meta.tags ?? []).concat(storyTags ?? [], 'story');
invariant(csf.meta.title);
entries.push({
id,
title: csf.meta.title,
name,
importPath,
tags,
type: 'story',
// We need to keep track of the csf meta id so we know the component id when referencing docs below in `extractDocs`
metaId: csf.meta.id,
});
}
});

if (csf.stories.length) {
const { autodocs } = this.options.docs;
const componentAutodocs = componentTags.includes(AUTODOCS_TAG);
const autodocsOptedIn = autodocs === true || (autodocs === 'tag' && componentAutodocs);
// We need a docs entry attached to the CSF file if either:
// a) it is a stories.mdx transpiled to CSF, OR
// b) we have docs page enabled for this file
if (componentTags.includes(STORIES_MDX_TAG) || autodocsOptedIn) {
const name = this.options.docs.defaultName ?? 'Docs';
invariant(csf.meta.title, 'expected a title property in csf.meta');
const id = toId(csf.meta.id || csf.meta.title, name);
entries.unshift({
id,
title: csf.meta.title,
name,
importPath,
type: 'docs',
tags: [
...componentTags,
'docs',
...(autodocsOptedIn && !componentAutodocs ? [AUTODOCS_TAG] : []),
],
storiesImports: [],
});
}
}

return { entries, type: 'stories', dependents: [] } as StoriesCacheEntry;
}

async extractDocs(specifier: NormalizedStoriesSpecifier, absolutePath: Path) {
const relativePath = path.relative(this.options.workingDir, absolutePath);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ vi.mock('@storybook/node-logger');
const options: StoryIndexGeneratorOptions = {
configDir: path.join(__dirname, '..', '__mockdata__'),
workingDir: path.join(__dirname, '..', '__mockdata__'),
storyIndexers: [],
indexers: [],
storyStoreV7: true,
docs: { defaultName: 'docs', autodocs: false },
Expand Down
2 changes: 0 additions & 2 deletions code/lib/core-server/src/utils/getStoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ export async function getStoryIndexGenerator(
workingDir,
};
const stories = options.presets.apply('stories');
const deprecatedStoryIndexers = options.presets.apply('storyIndexers', []);
const indexers = options.presets.apply('experimental_indexers', []);
const docsOptions = options.presets.apply<DocsOptions>('docs', {});
const normalizedStories = normalizeStories(await stories, directories);

const generator = new StoryIndexGenerator(normalizedStories, {
...directories,
storyIndexers: await deprecatedStoryIndexers,
indexers: await indexers,
docs: await docsOptions,
workingDir,
Expand Down
1 change: 0 additions & 1 deletion code/lib/core-server/src/utils/stories-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const getInitializedStoryIndexGenerator = async (
inputNormalizedStories = normalizedStories
) => {
const options: StoryIndexGeneratorOptions = {
storyIndexers: [],
indexers: [csfIndexer],
configDir: workingDir,
workingDir,
Expand Down
10 changes: 1 addition & 9 deletions code/lib/types/src/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Router } from 'express';
import type { Server } from 'http';
import type { PackageJson as PackageJsonFromTypeFest } from 'type-fest';

import type { StoriesEntry, Indexer, StoryIndexer } from './indexer';
import type { StoriesEntry, Indexer } from './indexer';

/**
* ⚠️ This file contains internal WIP types they MUST NOT be exported outside this package for now!
Expand Down Expand Up @@ -416,8 +416,6 @@ export interface StorybookConfigRaw {

previewAnnotations?: Entry[];

storyIndexers?: StoryIndexer[];

experimental_indexers?: Indexer[];

docs?: DocsOptions;
Expand Down Expand Up @@ -509,12 +507,6 @@ export interface StorybookConfig {
*/
previewAnnotations?: PresetValue<StorybookConfigRaw['previewAnnotations']>;

/**
* Process CSF files for the story index.
* @deprecated use {@link experimental_indexers} instead
*/
storyIndexers?: PresetValue<StorybookConfigRaw['storyIndexers']>;

/**
* Process CSF files for the story index.
*/
Expand Down
14 changes: 0 additions & 14 deletions code/lib/types/src/modules/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,8 @@ export type Indexer = BaseIndexer & {
* @returns A promise that resolves to an array of {@link IndexInput} objects.
*/
createIndex: (fileName: string, options: IndexerOptions) => Promise<IndexInput[]>;
/**
* @deprecated Use {@link index} instead
*/
indexer?: never;
};

export type DeprecatedIndexer = BaseIndexer & {
indexer: (fileName: string, options: IndexerOptions) => Promise<IndexedCSFFile>;
createIndex?: never;
};

/**
* @deprecated Use {@link Indexer} instead
*/
export type StoryIndexer = Indexer | DeprecatedIndexer;

export interface BaseIndexEntry {
id: StoryId;
name: StoryName;
Expand Down