Skip to content

Commit

Permalink
Merge pull request #24890 from storybookjs/norbert/improve-mdx-filtering
Browse files Browse the repository at this point in the history
TestBuild: Fix indexer bug
  • Loading branch information
ndelangen authored Nov 17, 2023
2 parents 0d9a95c + ffb9003 commit d11044a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 170 deletions.
9 changes: 5 additions & 4 deletions code/lib/core-common/src/utils/normalize-stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { normalizeStoryPath } from './paths';
import { globToRegexp } from './glob-to-regexp';

const DEFAULT_TITLE_PREFIX = '';
const DEFAULT_FILES = '**/*.@(mdx|stories.@(js|jsx|mjs|ts|tsx))';
const DEFAULT_FILES_PATTERN = '**/*.@(mdx|stories.@(js|jsx|mjs|ts|tsx))';

const isDirectory = (configDir: string, entry: string) => {
try {
Expand All @@ -34,7 +34,7 @@ export const getDirectoryFromWorkingDir = ({

export const normalizeStoriesEntry = (
entry: StoriesEntry,
{ configDir, workingDir }: NormalizeOptions
{ configDir, workingDir, default_files_pattern = DEFAULT_FILES_PATTERN }: NormalizeOptions
): NormalizedStoriesSpecifier => {
let specifierWithoutMatcher: Omit<NormalizedStoriesSpecifier, 'importPathMatcher'>;

Expand All @@ -53,7 +53,7 @@ export const normalizeStoriesEntry = (
specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
directory: entry,
files: DEFAULT_FILES,
files: default_files_pattern,
};
} else {
specifierWithoutMatcher = {
Expand All @@ -65,7 +65,7 @@ export const normalizeStoriesEntry = (
} else {
specifierWithoutMatcher = {
titlePrefix: DEFAULT_TITLE_PREFIX,
files: DEFAULT_FILES,
files: default_files_pattern,
...entry,
};
}
Expand Down Expand Up @@ -99,6 +99,7 @@ export const normalizeStoriesEntry = (
interface NormalizeOptions {
configDir: string;
workingDir: string;
default_files_pattern?: string;
}

export const normalizeStories = (entries: StoriesEntry[], options: NormalizeOptions) => {
Expand Down
57 changes: 41 additions & 16 deletions code/lib/core-server/src/presets/common-override-preset.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { Options, PresetProperty, StorybookConfig, TestBuildFlags } from '@storybook/types';
import type {
Options,
PresetProperty,
StoriesEntry,
StorybookConfig,
TestBuildFlags,
} from '@storybook/types';
import { normalizeStories, commonGlobOptions } from '@storybook/core-common';
import { isAbsolute, join } from 'path';
import { isAbsolute, join, relative } from 'path';
import slash from 'slash';
import { glob } from 'glob';

Expand All @@ -19,30 +25,49 @@ export const framework: PresetProperty<'framework', StorybookConfig> = async (co

export const stories: PresetProperty<'stories', StorybookConfig> = async (entries, options) => {
if (options?.build?.test?.disableMDXEntries) {
return (
const list = normalizeStories(entries, {
configDir: options.configDir,
workingDir: options.configDir,
default_files_pattern: '**/*.@(stories.@(js|jsx|mjs|ts|tsx))',
});
const result = (
await Promise.all(
normalizeStories(entries, {
configDir: options.configDir,
workingDir: options.configDir,
}).map(({ directory, files }) => {
list.map(async ({ directory, files, titlePrefix }) => {
const pattern = join(directory, files);
const absolutePattern = isAbsolute(pattern) ? pattern : join(options.configDir, pattern);
const absoluteDirectory = isAbsolute(directory)
? directory
: join(options.configDir, directory);

return glob(slash(absolutePattern), {
...commonGlobOptions(absolutePattern),
follow: true,
});
return {
files: (
await glob(slash(absolutePattern), {
...commonGlobOptions(absolutePattern),
follow: true,
})
).map((f) => relative(absoluteDirectory, f)),
directory,
titlePrefix,
};
})
)
).flatMap((expanded, i) => {
const filteredEntries = expanded.filter((s) => !s.endsWith('.mdx'));
).flatMap<StoriesEntry>((expanded, i) => {
const filteredEntries = expanded.files.filter((s) => !s.endsWith('.mdx'));
// only return the filtered entries when there is something to filter
// as webpack is faster with unexpanded globs
if (filteredEntries.length < expanded.length) {
return filteredEntries;
let items = [];
if (filteredEntries.length < expanded.files.length) {
items = filteredEntries.map((k) => ({
...expanded,
files: `**/${k}`,
}));
} else {
items = [list[i]];
}
return entries[i];

return items;
});
return result;
}
return entries;
};
Expand Down
6 changes: 6 additions & 0 deletions code/ui/.storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const isBlocksOnly = process.env.STORYBOOK_BLOCKS_ONLY === 'true';
const allStories = [
{
directory: '../manager/src',
files: '**/*.stories.@(js|jsx|mjs|ts|tsx|mdx)',
titlePrefix: '@manager',
},
{
Expand Down Expand Up @@ -53,6 +54,11 @@ const config: StorybookConfig = {
'@storybook/addon-designs',
'@chromaui/addon-visual-tests',
],
build: {
test: {
disableBlocks: false,
},
},
framework: {
name: '@storybook/react-vite',
options: {},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import React from 'react';

import { DocumentWrapper } from './DocumentWrapper';
import MarkdownSample from './DocumentFormattingSample.mdx';

export default {
component: DocumentWrapper,
decorators: [(storyFn: any) => <div style={{ width: '600px' }}>{storyFn()}</div>],
};

export const WithMarkdown = () => (
<DocumentWrapper>
<MarkdownSample />
</DocumentWrapper>
);

export const WithDOM = () => (
<DocumentWrapper>
<h1>h1 Heading</h1>
Expand Down

0 comments on commit d11044a

Please sign in to comment.