From 9ca58a5b29bc577dbb9f5c566b05a3823ba972cf Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Thu, 16 Mar 2023 12:48:38 +0100 Subject: [PATCH] Fix csf-plugin loader The CSF-Plugin loader was not applying the necessary transformations in Angular projects. Co-authored-by: Norbert de Langen --- code/e2e-tests/addon-docs.spec.ts | 13 +++++++++++++ code/lib/csf-plugin/src/index.ts | 7 ++----- scripts/tasks/sandbox-parts.ts | 5 +++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/code/e2e-tests/addon-docs.spec.ts b/code/e2e-tests/addon-docs.spec.ts index 3e6982bd6739..cf2e96028296 100644 --- a/code/e2e-tests/addon-docs.spec.ts +++ b/code/e2e-tests/addon-docs.spec.ts @@ -14,6 +14,19 @@ test.describe('addon-docs', () => { await new SbPage(page).waitUntilLoaded(); }); + test('should show descriptions for stories', async ({ page }) => { + const sbPage = new SbPage(page); + await sbPage.navigateToStory('addons/docs/docspage/basic', 'docs'); + const root = sbPage.previewRoot(); + + const basicStories = root.locator('#anchor--addons-docs-docspage-basic--basic'); + const secondBasicStory = (await basicStories.all())[1]; + await expect(secondBasicStory).toContainText('A basic button'); + + const anotherStory = root.locator('#anchor--addons-docs-docspage-basic--another'); + await expect(anotherStory).toContainText('Another button, just to show multiple stories'); + }); + test('should render errors', async ({ page }) => { const sbPage = new SbPage(page); await sbPage.navigateToStory('addons/docs/docspage/error', 'docs'); diff --git a/code/lib/csf-plugin/src/index.ts b/code/lib/csf-plugin/src/index.ts index 397daf274aab..9b7f9a322576 100644 --- a/code/lib/csf-plugin/src/index.ts +++ b/code/lib/csf-plugin/src/index.ts @@ -1,5 +1,4 @@ import { createUnplugin } from 'unplugin'; -import fs from 'fs/promises'; import { loadCsf, enrichCsf, formatCsf } from '@storybook/csf-tools'; import type { EnrichCsfOptions } from '@storybook/csf-tools'; @@ -12,12 +11,10 @@ const logger = console; export const unplugin = createUnplugin((options) => { return { name: 'unplugin-csf', - enforce: 'pre', - loadInclude(id) { + transformInclude(id) { return STORIES_REGEX.test(id); }, - async load(fname) { - const code = await fs.readFile(fname, 'utf-8'); + async transform(code) { try { const csf = loadCsf(code, { makeTitle: (userTitle) => userTitle || 'default' }).parse(); enrichCsf(csf, options); diff --git a/scripts/tasks/sandbox-parts.ts b/scripts/tasks/sandbox-parts.ts index 6b1bcbc22715..174ff2f96205 100644 --- a/scripts/tasks/sandbox-parts.ts +++ b/scripts/tasks/sandbox-parts.ts @@ -168,6 +168,11 @@ 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',