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

Docs: Fix source snippets when parameters.docs.source.type = 'code' #22048

Merged
merged 6 commits into from
Apr 14, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions code/addons/docs/template/stories/docspage/basic.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ export const Disabled = {
*/
export const Another = {
args: { label: 'Another' },
parameters: {
docs: {
source: {
type: 'code',
},
},
},
play: async () => {
await new Promise((resolve) => resolve('Play function'));
},
};
37 changes: 37 additions & 0 deletions code/e2e-tests/addon-docs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable no-await-in-loop */
import { test, expect } from '@playwright/test';
import process from 'process';
import dedent from 'ts-dedent';
import { SbPage } from './util';

const storybookUrl = process.env.STORYBOOK_URL || 'http://localhost:8001';
Expand Down Expand Up @@ -36,6 +37,42 @@ test.describe('addon-docs', () => {
await expect(anotherStory).toContainText('Another button, just to show multiple stories');
});

test('should show source=code view for stories', async ({ page }) => {
const skipped = [
// SSv6 does not render stories in the correct order in our sandboxes
'internal\\/ssv6',
];
test.skip(
new RegExp(`^${skipped.join('|')}`, 'i').test(`${templateName}`),
`Skipping ${templateName}, because of wrong ordering of stories on docs page`
);

const sbPage = new SbPage(page);
await sbPage.navigateToStory('addons/docs/docspage/basic', 'docs');
const root = sbPage.previewRoot();

// Click on the third button which has the text "Show code"
const showCodeButton = (await root.locator('button', { hasText: 'Show Code' }).all())[2];
await showCodeButton.click();
const sourceCode = root.locator('pre.prismjs');
const expectedSource = dedent`{
args: {
label: 'Another'
},
parameters: {
docs: {
source: {
type: 'code'
}
}
},
play: async () => {
await new Promise(resolve => resolve('Play function'));
}
}`;
await expect(sourceCode.textContent()).resolves.toContain(expectedSource);
});

test('should render errors', async ({ page }) => {
const sbPage = new SbPage(page);
await sbPage.navigateToStory('addons/docs/docspage/error', 'docs');
Expand Down
10 changes: 5 additions & 5 deletions code/lib/csf-plugin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createUnplugin } from 'unplugin';
import fs from 'fs/promises';
import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools';
import type { EnrichCsfOptions } from '@storybook/csf-tools';

Expand All @@ -11,10 +12,12 @@ const logger = console;
export const unplugin = createUnplugin<CsfPluginOptions>((options) => {
return {
name: 'unplugin-csf',
transformInclude(id) {
enforce: 'pre',
loadInclude(id) {
return STORIES_REGEX.test(id);
},
async transform(code) {
async load(fname) {
const code = await fs.readFile(fname, 'utf-8');
try {
const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse();
enrichCsf(csf, options);
Expand All @@ -28,9 +31,6 @@ export const unplugin = createUnplugin<CsfPluginOptions>((options) => {
return code;
}
},
vite: {
enforce: 'pre',
},
};
});

Expand Down
1 change: 1 addition & 0 deletions code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
"semver": "^7.3.7",
"serve-static": "^1.14.1",
"trash": "^7.0.0",
"ts-dedent": "^2.0.0",
"ts-node": "^10.9.1",
"typescript": "~4.9.3",
"util": "^0.12.4",
Expand Down
1 change: 1 addition & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7134,6 +7134,7 @@ __metadata:
semver: ^7.3.7
serve-static: ^1.14.1
trash: ^7.0.0
ts-dedent: ^2.0.0
ts-node: ^10.9.1
typescript: ~4.9.3
util: ^0.12.4
Expand Down
5 changes: 0 additions & 5 deletions scripts/tasks/sandbox-parts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ function addEsbuildLoaderToStories(mainConfig: ConfigFile) {
{
test: [/\\/template-stories\\//],
exclude: [/\\.mdx$/],
/**
* We need to run esbuild-loader after the csf-plugin loader, so we use the "enforce: 'post'" option.
* Otherwise, the csf-plugin loader does not have any effect.
*/
enforce: 'post',
loader: '${esbuildLoaderPath}',
options: {
loader: 'tsx',
Expand Down