mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-09 00:19:13 +08:00
Merge pull request #19750 from storybookjs/tom/sb-754-hmr-does-not-detect-addedremoved-stories
Sandboxes: Fix up stories entry to allow HMR
This commit is contained in:
commit
8ce29a01ad
@ -1,6 +1,6 @@
|
|||||||
import { Meta } from '@storybook/addon-docs';
|
import { Meta } from '@storybook/addon-docs';
|
||||||
|
|
||||||
<Meta title="addons/docs/docs2/Yabbadabbadooo" />
|
<Meta title="Yabbadabbadooo" />
|
||||||
|
|
||||||
# Docs with title
|
# Docs with title
|
||||||
|
|
||||||
|
@ -1,24 +1,18 @@
|
|||||||
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
||||||
import globalThis from 'global';
|
import globalThis from 'global';
|
||||||
|
|
||||||
<Meta title="addons/docs/stories-mdx/basic" component={globalThis.Components.Button} />
|
<Meta component={globalThis.Components.Button} />
|
||||||
|
|
||||||
# MDX Stories
|
# MDX Stories
|
||||||
|
|
||||||
This file demonstrates defining stories inside MDX.
|
This file demonstrates defining stories inside MDX.
|
||||||
|
|
||||||
<Canvas>
|
<Canvas>
|
||||||
<Story
|
<Story name="Primary" args={{ label: 'Primary' }} />
|
||||||
name="Primary"
|
|
||||||
args={{ label: 'Primary' }}
|
|
||||||
/>
|
|
||||||
</Canvas>
|
</Canvas>
|
||||||
|
|
||||||
<ArgsTable story="^" />
|
<ArgsTable story="^" />
|
||||||
|
|
||||||
<Canvas>
|
<Canvas>
|
||||||
<Story
|
<Story name="Secondary" args={{ label: 'Secondary' }} />
|
||||||
name="Secondary"
|
|
||||||
args={{ label: 'Secondary' }}
|
|
||||||
/>
|
|
||||||
</Canvas>
|
</Canvas>
|
||||||
|
@ -2,7 +2,7 @@ import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
|||||||
import globalThis from 'global';
|
import globalThis from 'global';
|
||||||
import * as Csf from './csf-in-mdx.stories.js';
|
import * as Csf from './csf-in-mdx.stories.js';
|
||||||
|
|
||||||
<Meta title="addons/docs/stories-mdx/csf-in-mdx" component={globalThis.Components.Button} />
|
<Meta component={globalThis.Components.Button} />
|
||||||
|
|
||||||
# Legacy CSF in MDX Stories
|
# Legacy CSF in MDX Stories
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
||||||
import globalThis from 'global';
|
import globalThis from 'global';
|
||||||
|
|
||||||
<Meta title="addons/docs/stories-mdx/iframe" component={globalThis.Components.Button} />
|
<Meta component={globalThis.Components.Button} />
|
||||||
|
|
||||||
# MDX Stories
|
# MDX Stories
|
||||||
|
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
|
||||||
import globalThis from 'global';
|
import globalThis from 'global';
|
||||||
|
|
||||||
<Meta
|
<Meta component={globalThis.Components.Button} play={() => console.log('component play')} />
|
||||||
title="addons/docs/stories-mdx/play functions"
|
|
||||||
component={globalThis.Components.Button}
|
|
||||||
play={() => console.log('component play')}
|
|
||||||
/>
|
|
||||||
|
|
||||||
# MDX Play function Stories
|
# MDX Play function Stories
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ import type { PlayFunctionContext } from '@storybook/types';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
component: globalThis.Components.Pre,
|
component: globalThis.Components.Pre,
|
||||||
title: 'lib/store/manual title',
|
title: 'manual title',
|
||||||
args: { text: 'No content' },
|
args: { text: 'No content' },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -230,6 +230,36 @@ function setSandboxViteFinal(mainConfig: ConfigFile) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Update the stories field to ensure that no TS files
|
||||||
|
// that are linked from the renderer are picked up in non-TS projects
|
||||||
|
function updateStoriesField(mainConfig: ConfigFile, isJs: boolean) {
|
||||||
|
const stories = mainConfig.getFieldValue(['stories']) as string[];
|
||||||
|
|
||||||
|
// If the project is a JS project, let's make sure any linked in TS stories from the
|
||||||
|
// renderer inside src|stories are simply ignored.
|
||||||
|
const updatedStories = isJs
|
||||||
|
? stories.map((specifier) => specifier.replace('js|jsx|ts|tsx', 'js|jsx'))
|
||||||
|
: stories;
|
||||||
|
|
||||||
|
|
||||||
|
mainConfig.setFieldValue(['stories'], [...updatedStories]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add a stories field entry for the passed symlink
|
||||||
|
function addStoriesEntry(mainConfig: ConfigFile, path: string) {
|
||||||
|
const stories = mainConfig.getFieldValue(['stories']) as string[];
|
||||||
|
|
||||||
|
const entry = {
|
||||||
|
directory: join('../template-stories', path),
|
||||||
|
titlePrefix: path,
|
||||||
|
files: '**/*.@(mdx|stories.@(js|jsx|ts|tsx))',
|
||||||
|
};
|
||||||
|
|
||||||
|
mainConfig.setFieldValue(['stories'], [...stories, entry]);
|
||||||
|
}
|
||||||
|
|
||||||
// packageDir is eg 'renderers/react', 'addons/actions'
|
// packageDir is eg 'renderers/react', 'addons/actions'
|
||||||
async function linkPackageStories(
|
async function linkPackageStories(
|
||||||
packageDir: string,
|
packageDir: string,
|
||||||
@ -247,6 +277,8 @@ async function linkPackageStories(
|
|||||||
: resolve(cwd, 'template-stories', packageDir);
|
: resolve(cwd, 'template-stories', packageDir);
|
||||||
await ensureSymlink(source, target);
|
await ensureSymlink(source, target);
|
||||||
|
|
||||||
|
if (!linkInDir) addStoriesEntry(mainConfig, packageDir)
|
||||||
|
|
||||||
// Add `previewAnnotation` entries of the form
|
// Add `previewAnnotation` entries of the form
|
||||||
// './template-stories/lib/store/preview.[tj]s'
|
// './template-stories/lib/store/preview.[tj]s'
|
||||||
// if the file <code>/lib/store/template/stories/preview.[jt]s exists
|
// if the file <code>/lib/store/template/stories/preview.[jt]s exists
|
||||||
@ -265,25 +297,6 @@ async function linkPackageStories(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the stories field to ensure that:
|
|
||||||
// a) no TS files that are linked from the renderer are picked up in non-TS projects
|
|
||||||
// b) files in ./template-stories are not matched by the default glob
|
|
||||||
async function updateStoriesField(mainConfig: ConfigFile, isJs: boolean) {
|
|
||||||
const stories = mainConfig.getFieldValue(['stories']) as string[];
|
|
||||||
|
|
||||||
// If the project is a JS project, let's make sure any linked in TS stories from the
|
|
||||||
// renderer inside src|stories are simply ignored.
|
|
||||||
const updatedStories = isJs
|
|
||||||
? stories.map((specifier) => specifier.replace('js|jsx|ts|tsx', 'js|jsx'))
|
|
||||||
: stories;
|
|
||||||
|
|
||||||
// FIXME: '*.@(mdx|stories.mdx|stories.tsx|stories.ts|stories.jsx|stories.js'
|
|
||||||
const linkedStories = join('..', 'template-stories', '**', '*.stories.@(js|jsx|ts|tsx|mdx)');
|
|
||||||
const linkedMdx = join('..', 'template-stories/addons/docs/docs2', '**', '*.@(mdx)');
|
|
||||||
|
|
||||||
mainConfig.setFieldValue(['stories'], [...updatedStories, linkedStories, linkedMdx]);
|
|
||||||
}
|
|
||||||
|
|
||||||
function addExtraDependencies({
|
function addExtraDependencies({
|
||||||
cwd,
|
cwd,
|
||||||
dryRun,
|
dryRun,
|
||||||
@ -306,6 +319,7 @@ function addExtraDependencies({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const addStories: Task['run'] = async (
|
export const addStories: Task['run'] = async (
|
||||||
{ sandboxDir, template },
|
{ sandboxDir, template },
|
||||||
{ addon: extraAddons, dryRun, debug }
|
{ addon: extraAddons, dryRun, debug }
|
||||||
@ -315,6 +329,14 @@ export const addStories: Task['run'] = async (
|
|||||||
|
|
||||||
const mainConfig = await readMainConfig({ cwd });
|
const mainConfig = await readMainConfig({ cwd });
|
||||||
|
|
||||||
|
// Ensure that we match the right stories in the stories directory
|
||||||
|
const packageJson = await import(join(cwd, 'package.json'));
|
||||||
|
updateStoriesField(
|
||||||
|
mainConfig,
|
||||||
|
detectLanguage(packageJson) === SupportedLanguage.JAVASCRIPT
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
// Link in the template/components/index.js from store, the renderer and the addons
|
// Link in the template/components/index.js from store, the renderer and the addons
|
||||||
const rendererPath = await workspacePath('renderer', template.expected.renderer);
|
const rendererPath = await workspacePath('renderer', template.expected.renderer);
|
||||||
await ensureSymlink(
|
await ensureSymlink(
|
||||||
@ -369,13 +391,6 @@ export const addStories: Task['run'] = async (
|
|||||||
existingStories.map(async (packageDir) => linkPackageStories(packageDir, { mainConfig, cwd }))
|
existingStories.map(async (packageDir) => linkPackageStories(packageDir, { mainConfig, cwd }))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Ensure that we match stories from the template-stories dir
|
|
||||||
const packageJson = await import(join(cwd, 'package.json'));
|
|
||||||
await updateStoriesField(
|
|
||||||
mainConfig,
|
|
||||||
detectLanguage(packageJson) === SupportedLanguage.JAVASCRIPT
|
|
||||||
);
|
|
||||||
|
|
||||||
// Add some extra settings (see above for what these do)
|
// Add some extra settings (see above for what these do)
|
||||||
if (template.expected.builder === '@storybook/builder-webpack5')
|
if (template.expected.builder === '@storybook/builder-webpack5')
|
||||||
addEsbuildLoaderToStories(mainConfig);
|
addEsbuildLoaderToStories(mainConfig);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user