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

Sandboxes: Fix up stories entry to allow HMR #19750

Merged
merged 6 commits into from
Nov 7, 2022
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
2 changes: 1 addition & 1 deletion code/addons/docs/template/stories/docs2/Title.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meta } from '@storybook/addon-docs';

<Meta title="addons/docs/docs2/Yabbadabbadooo" />
<Meta title="Yabbadabbadooo" />

# Docs with title

Expand Down
12 changes: 3 additions & 9 deletions code/addons/docs/template/stories/stories-mdx/basic.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
import globalThis from 'global';

<Meta title="addons/docs/stories-mdx/basic" component={globalThis.Components.Button} />
<Meta component={globalThis.Components.Button} />

# MDX Stories

This file demonstrates defining stories inside MDX.

<Canvas>
<Story
name="Primary"
args={{ label: 'Primary' }}
/>
<Story name="Primary" args={{ label: 'Primary' }} />
</Canvas>

<ArgsTable story="^" />

<Canvas>
<Story
name="Secondary"
args={{ label: 'Secondary' }}
/>
<Story name="Secondary" args={{ label: 'Secondary' }} />
</Canvas>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Meta, Story, Canvas } from '@storybook/addon-docs';
import globalThis from 'global';
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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
import globalThis from 'global';

<Meta title="addons/docs/stories-mdx/iframe" component={globalThis.Components.Button} />
<Meta component={globalThis.Components.Button} />

# MDX Stories

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { Meta, Story, Canvas, ArgsTable } from '@storybook/addon-docs';
import globalThis from 'global';

<Meta
title="addons/docs/stories-mdx/play functions"
component={globalThis.Components.Button}
play={() => console.log('component play')}
/>
<Meta component={globalThis.Components.Button} play={() => console.log('component play')} />

# MDX Play function Stories

Expand Down
2 changes: 1 addition & 1 deletion code/lib/store/template/stories/title.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { PlayFunctionContext } from '@storybook/types';

export default {
component: globalThis.Components.Pre,
title: 'lib/store/manual title',
title: 'manual title',
args: { text: 'No content' },
};

Expand Down
69 changes: 42 additions & 27 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
async function linkPackageStories(
packageDir: string,
Expand All @@ -247,6 +277,8 @@ async function linkPackageStories(
: resolve(cwd, 'template-stories', packageDir);
await ensureSymlink(source, target);

if (!linkInDir) addStoriesEntry(mainConfig, packageDir)

// Add `previewAnnotation` entries of the form
// './template-stories/lib/store/preview.[tj]s'
// if the file <code>/lib/store/template/stories/preview.[jt]s exists
Expand All @@ -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({
cwd,
dryRun,
Expand All @@ -306,6 +319,7 @@ function addExtraDependencies({
}
}


export const addStories: Task['run'] = async (
{ sandboxDir, template },
{ addon: extraAddons, dryRun, debug }
Expand All @@ -315,14 +329,22 @@ export const addStories: Task['run'] = async (

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
const rendererPath = await workspacePath('renderer', template.expected.renderer);
await ensureSymlink(
join(codeDir, rendererPath, 'template', 'components'),
resolve(cwd, storiesPath, 'components')
);
addPreviewAnnotations(mainConfig, [`.${sep}${join(storiesPath, 'components')}`]);

// Add stories for the renderer. NOTE: these *do* need to be processed by the framework build system
await linkPackageStories(rendererPath, {
mainConfig,
Expand Down Expand Up @@ -369,13 +391,6 @@ export const addStories: Task['run'] = async (
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)
if (template.expected.builder === '@storybook/builder-webpack5')
addEsbuildLoaderToStories(mainConfig);
Expand Down