Skip to content
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

Core: Remove more .stories.mdx handling #25973

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
124 changes: 62 additions & 62 deletions code/lib/core-common/src/utils/__tests__/normalize-stories.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,172 +42,172 @@ const options = {

describe('normalizeStoriesEntry', () => {
it('direct file path', () => {
const specifier = normalizeStoriesEntry('../path/to/file.stories.mdx', options);
const specifier = normalizeStoriesEntry('../path/to/file.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": "./path/to",
"files": "file.stories.mdx",
"files": "file.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths(['./path/to/file.stories.mdx']);
expect(specifier.importPathMatcher).toMatchPaths(['./path/to/file.stories.jsx']);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./path/to/file.stories.js',
'./file.stories.mdx',
'../file.stories.mdx',
'./file.stories.jsx',
'../file.stories.jsx',
]);
});

it('story in config dir', () => {
const specifier = normalizeStoriesEntry('./file.stories.mdx', options);
const specifier = normalizeStoriesEntry('./file.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": "./.storybook",
"files": "file.stories.mdx",
"files": "file.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths(['./.storybook/file.stories.mdx']);
expect(specifier.importPathMatcher).toMatchPaths(['./.storybook/file.stories.jsx']);
expect(specifier.importPathMatcher).not.toMatchPaths([
'.storybook/file.stories.mdx',
'./file.stories.mdx',
'../file.stories.mdx',
'.storybook/file.stories.jsx',
'./file.stories.jsx',
'../file.stories.jsx',
]);
});

it('non-recursive files glob', () => {
const specifier = normalizeStoriesEntry('../*/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../*/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "*/*.stories.mdx",
"files": "*/*.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths([
'./path/file.stories.mdx',
'./second-path/file.stories.mdx',
'./path/file.stories.jsx',
'./second-path/file.stories.jsx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./path/file.stories.js',
'./path/to/file.stories.mdx',
'./file.stories.mdx',
'../file.stories.mdx',
'./path/to/file.stories.jsx',
'./file.stories.jsx',
'../file.stories.jsx',
]);
});

it('double non-recursive directory/files glob', () => {
const specifier = normalizeStoriesEntry('../*/*/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../*/*/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "*/*/*.stories.mdx",
"files": "*/*/*.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths([
'./path/to/file.stories.mdx',
'./second-path/to/file.stories.mdx',
'./path/to/file.stories.jsx',
'./second-path/to/file.stories.jsx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./file.stories.mdx',
'./path/file.stories.mdx',
'./path/to/third/file.stories.mdx',
'./file.stories.jsx',
'./path/file.stories.jsx',
'./path/to/third/file.stories.jsx',
'./path/to/file.stories.js',
'../file.stories.mdx',
'../file.stories.jsx',
]);
});

it('directory/files glob', () => {
const specifier = normalizeStoriesEntry('../**/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../**/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "**/*.stories.mdx",
"files": "**/*.stories.jsx",
"importPathMatcher": {}
}
`);
expect(specifier.importPathMatcher).toMatchPaths([
'./file.stories.mdx',
'./path/file.stories.mdx',
'./path/to/file.stories.mdx',
'./path/to/third/file.stories.mdx',
'./file.stories.jsx',
'./path/file.stories.jsx',
'./path/to/file.stories.jsx',
'./path/to/third/file.stories.jsx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./file.stories.js',
'../file.stories.mdx',
'../file.stories.jsx',
]);
});

it('double stars glob', () => {
const specifier = normalizeStoriesEntry('../**/foo/**/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../**/foo/**/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "**/foo/**/*.stories.mdx",
"files": "**/foo/**/*.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths([
'./foo/file.stories.mdx',
'./path/to/foo/file.stories.mdx',
'./path/to/foo/third/fourth/file.stories.mdx',
'./foo/file.stories.jsx',
'./path/to/foo/file.stories.jsx',
'./path/to/foo/third/fourth/file.stories.jsx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./file.stories.mdx',
'./file.stories.jsx',
'./file.stories.js',
'../file.stories.mdx',
'../file.stories.jsx',
]);
});

it('intermediate directory glob', () => {
const specifier = normalizeStoriesEntry('../**/foo/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../**/foo/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "**/foo/*.stories.mdx",
"files": "**/foo/*.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths([
'./path/to/foo/file.stories.mdx',
'./foo/file.stories.mdx',
'./path/to/foo/file.stories.jsx',
'./foo/file.stories.jsx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./file.stories.mdx',
'./file.stories.jsx',
'./file.stories.js',
'./path/to/foo/third/fourth/file.stories.mdx',
'../file.stories.mdx',
'./path/to/foo/third/fourth/file.stories.jsx',
'../file.stories.jsx',
]);
});

it('directory outside of working dir', () => {
const specifier = normalizeStoriesEntry('../../src/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../../src/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": "../src",
"files": "*.stories.mdx",
"files": "*.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths(['../src/file.stories.mdx']);
expect(specifier.importPathMatcher).toMatchPaths(['../src/file.stories.jsx']);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./src/file.stories.mdx',
'./src/file.stories.jsx',
'../src/file.stories.js',
]);
});
Expand Down Expand Up @@ -237,11 +237,11 @@ describe('normalizeStoriesEntry', () => {
});

it('directory/files specifier', () => {
const specifier = normalizeStoriesEntry({ directory: '..', files: '*.stories.mdx' }, options);
const specifier = normalizeStoriesEntry({ directory: '..', files: '*.stories.jsx' }, options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"files": "*.stories.mdx",
"files": "*.stories.jsx",
"directory": ".",
"importPathMatcher": {}
}
Expand All @@ -262,39 +262,39 @@ describe('normalizeStoriesEntry', () => {

it('directory/titlePrefix/files specifier', () => {
const specifier = normalizeStoriesEntry(
{ directory: '..', titlePrefix: 'atoms', files: '*.stories.mdx' },
{ directory: '..', titlePrefix: 'atoms', files: '*.stories.jsx' },
options
);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "atoms",
"files": "*.stories.mdx",
"files": "*.stories.jsx",
"directory": ".",
"importPathMatcher": {}
}
`);
});

it('globs with negation', () => {
const specifier = normalizeStoriesEntry('../!(negation)/*.stories.mdx', options);
const specifier = normalizeStoriesEntry('../!(negation)/*.stories.jsx', options);
expect(specifier).toMatchInlineSnapshot(`
{
"titlePrefix": "",
"directory": ".",
"files": "!(negation)/*.stories.mdx",
"files": "!(negation)/*.stories.jsx",
"importPathMatcher": {}
}
`);

expect(specifier.importPathMatcher).toMatchPaths([
'./path/file.stories.mdx',
'./second-path/file.stories.mdx',
'./path/file.stories.jsx',
'./second-path/file.stories.jsx',
]);
expect(specifier.importPathMatcher).not.toMatchPaths([
'./path/file.stories.js',
'./path/to/file.stories.mdx',
'./file.stories.mdx',
'../file.stories.mdx',
'./path/to/file.stories.jsx',
'./file.stories.jsx',
'../file.stories.jsx',
]);
});
});
Expand Down
21 changes: 6 additions & 15 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ export type StoryIndexGeneratorOptions = {
};

export const AUTODOCS_TAG = 'autodocs';
export const STORIES_MDX_TAG = 'stories-mdx';
export const PLAY_FN_TAG = 'play-fn';

/** Was this docs entry generated by a .mdx file? (see discussion below) */
export function isMdxEntry({ tags }: DocsIndexEntry) {
return !tags?.includes(AUTODOCS_TAG) && !tags?.includes(STORIES_MDX_TAG);
return !tags?.includes(AUTODOCS_TAG);
}

const makeAbsolute = (otherImport: Path, normalizedPath: Path, workingDir: Path) =>
Expand All @@ -83,12 +82,11 @@ const makeAbsolute = (otherImport: Path, normalizedPath: Path, workingDir: Path)
*
* A stories file is indexed by an indexer (passed in), which produces a list of stories.
* - If the stories have the `parameters.docsOnly` setting, they are disregarded.
* - If the stories have the 'stories-mdx' tag (i.e. were generated by a .stories.mdx file),
* OR autodocs is enabled, a docs entry is added pointing to the story file.
* - If the stories have autodocs enabled, a docs entry is added pointing to the story file.
*
* A (modern) docs (.mdx) file is indexed, a docs entry is added.
*
* In the preview, a docs entry with either the `autodocs` or `stories-mdx` tags will be rendered
* In the preview, a docs entry with the `autodocs` tag will be rendered
* as a CSF file that exports an MDX template on the `docs.page` parameter, whereas
* other docs entries are rendered as MDX files directly.
*
Expand Down Expand Up @@ -308,11 +306,8 @@ export class StoryIndexGenerator {
// We need a docs entry attached to the CSF file if either:
// a) autodocs is globally enabled
// b) we have autodocs enabled for this file
// c) it is a stories.mdx transpiled to CSF
const hasAutodocsTag = entries.some((entry) => entry.tags.includes(AUTODOCS_TAG));
const isStoriesMdx = entries.some((entry) => entry.tags.includes(STORIES_MDX_TAG));
const createDocEntry =
autodocs === true || (autodocs === 'tag' && hasAutodocsTag) || isStoriesMdx;
const createDocEntry = autodocs === true || (autodocs === 'tag' && hasAutodocsTag);

if (createDocEntry && this.options.build?.test?.disableAutoDocs !== true) {
const name = this.options.docs.defaultName ?? 'Docs';
Expand All @@ -326,17 +321,13 @@ export class StoryIndexGenerator {
name,
importPath,
type: 'docs',
tags: [...metaTags, 'docs', ...(!hasAutodocsTag && !isStoriesMdx ? [AUTODOCS_TAG] : [])],
tags: [...metaTags, 'docs', ...(!hasAutodocsTag ? [AUTODOCS_TAG] : [])],
storiesImports: [],
});
}

const entriesWithoutDocsOnlyStories = entries.filter(
(entry) => !(entry.type === 'story' && entry.tags.includes('stories-mdx-docsOnly'))
);

return {
entries: entriesWithoutDocsOnlyStories,
entries,
dependents: [],
type: 'stories',
};
Expand Down
4 changes: 2 additions & 2 deletions code/lib/core-server/src/utils/__tests__/autoName.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { it, expect } from 'vitest';
import { autoName } from '../autoName';

it('pulls name from named MDX files', () => {
expect(autoName('Conventions.mdx', 'Button.stories.mdx', 'Docs')).toEqual('Conventions');
expect(autoName('Conventions.mdx', 'Button.mdx', 'Docs')).toEqual('Conventions');
});

it('falls back for default named MDX files', () => {
expect(autoName('Button.mdx', 'Button.stories.mdx', 'Docs')).toEqual('Docs');
expect(autoName('Button.mdx', 'Button.stories.jsx', 'Docs')).toEqual('Docs');
});
Loading
Loading