From d82fb47bb1b5545c09c71fbb5a4c8fe63d9e451e Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 9 Feb 2024 10:23:42 +0100 Subject: [PATCH 01/11] remove stories.mdx reporting in telemetry --- .../src/utils/summarizeIndex.test.ts | 63 ------------------- .../core-server/src/utils/summarizeIndex.ts | 6 +- docs/configure/telemetry.md | 1 - 3 files changed, 1 insertion(+), 69 deletions(-) diff --git a/code/lib/core-server/src/utils/summarizeIndex.test.ts b/code/lib/core-server/src/utils/summarizeIndex.test.ts index 8647c007463b..4370503d89ac 100644 --- a/code/lib/core-server/src/utils/summarizeIndex.test.ts +++ b/code/lib/core-server/src/utils/summarizeIndex.test.ts @@ -144,7 +144,6 @@ describe('summarizeIndex', () => { "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, - "storiesMdxCount": 0, "storyCount": 0, "version": 4, } @@ -202,7 +201,6 @@ describe('summarizeIndex', () => { "onboardingStoryCount": 1, "pageStoryCount": 0, "playStoryCount": 0, - "storiesMdxCount": 0, "storyCount": 0, "version": 4, } @@ -258,7 +256,6 @@ describe('summarizeIndex', () => { "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, - "storiesMdxCount": 0, "storyCount": 4, "version": 4, } @@ -315,69 +312,11 @@ describe('summarizeIndex', () => { "onboardingStoryCount": 0, "pageStoryCount": 1, "playStoryCount": 1, - "storiesMdxCount": 0, "storyCount": 1, "version": 4, } `); }); - it('storiesMdx', () => { - expect( - summarizeIndex({ - v: 4, - entries: { - 'stories-renderers-react-react-mdx--docs': { - id: 'stories-renderers-react-react-mdx--docs', - title: 'stories/renderers/react/react-mdx', - name: 'Docs', - importPath: './src/stories/renderers/react/react-mdx.stories.mdx', - type: 'docs', - tags: ['stories-mdx', 'docs'], - storiesImports: [], - }, - 'stories-renderers-react-react-mdx--primary': { - id: 'stories-renderers-react-react-mdx--primary', - title: 'stories/renderers/react/react-mdx', - name: 'Primary', - importPath: './src/stories/renderers/react/react-mdx.stories.mdx', - tags: ['stories-mdx', 'story'], - type: 'story', - }, - 'stories-renderers-react-react-mdx--secondary': { - id: 'stories-renderers-react-react-mdx--secondary', - title: 'stories/renderers/react/react-mdx', - name: 'Secondary', - importPath: './src/stories/renderers/react/react-mdx.stories.mdx', - tags: ['stories-mdx', 'story'], - type: 'story', - }, - 'stories-renderers-react-react-mdx--from-template': { - id: 'stories-renderers-react-react-mdx--from-template', - title: 'stories/renderers/react/react-mdx', - name: 'From Template', - importPath: './src/stories/renderers/react/react-mdx.stories.mdx', - tags: ['stories-mdx', 'story'], - type: 'story', - }, - }, - }) - ).toMatchInlineSnapshot(` - { - "autodocsCount": 0, - "componentCount": 1, - "exampleDocsCount": 0, - "exampleStoryCount": 0, - "mdxCount": 0, - "onboardingDocsCount": 0, - "onboardingStoryCount": 0, - "pageStoryCount": 0, - "playStoryCount": 0, - "storiesMdxCount": 1, - "storyCount": 3, - "version": 4, - } - `); - }); it('autodocs', () => { expect( summarizeIndex({ @@ -430,7 +369,6 @@ describe('summarizeIndex', () => { "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, - "storiesMdxCount": 0, "storyCount": 0, "version": 4, } @@ -481,7 +419,6 @@ describe('summarizeIndex', () => { "onboardingStoryCount": 0, "pageStoryCount": 0, "playStoryCount": 0, - "storiesMdxCount": 0, "storyCount": 0, "version": 4, } diff --git a/code/lib/core-server/src/utils/summarizeIndex.ts b/code/lib/core-server/src/utils/summarizeIndex.ts index 091038cdaccf..8b74ad8b7f5f 100644 --- a/code/lib/core-server/src/utils/summarizeIndex.ts +++ b/code/lib/core-server/src/utils/summarizeIndex.ts @@ -1,6 +1,6 @@ import type { IndexEntry, StoryIndex } from '@storybook/types'; -import { STORIES_MDX_TAG, isMdxEntry, AUTODOCS_TAG, PLAY_FN_TAG } from './StoryIndexGenerator'; +import { isMdxEntry, AUTODOCS_TAG, PLAY_FN_TAG } from './StoryIndexGenerator'; const PAGE_REGEX = /(page|screen)/i; @@ -44,7 +44,6 @@ export function summarizeIndex(storyIndex: StoryIndex) { let pageStoryCount = 0; let playStoryCount = 0; let autodocsCount = 0; - let storiesMdxCount = 0; let mdxCount = 0; Object.values(storyIndex.entries).forEach((entry) => { if (isCLIExampleEntry(entry)) { @@ -65,8 +64,6 @@ export function summarizeIndex(storyIndex: StoryIndex) { } else if (entry.type === 'docs') { if (isMdxEntry(entry)) { mdxCount += 1; - } else if (entry.tags?.includes(STORIES_MDX_TAG)) { - storiesMdxCount += 1; } else if (entry.tags?.includes(AUTODOCS_TAG)) { autodocsCount += 1; } @@ -79,7 +76,6 @@ export function summarizeIndex(storyIndex: StoryIndex) { pageStoryCount, playStoryCount, autodocsCount, - storiesMdxCount, mdxCount, exampleStoryCount, exampleDocsCount, diff --git a/docs/configure/telemetry.md b/docs/configure/telemetry.md index ff3298ac86fd..136cb48422ba 100644 --- a/docs/configure/telemetry.md +++ b/docs/configure/telemetry.md @@ -70,7 +70,6 @@ Will generate the following output: "pageStoryCount": 0, "playStoryCount": 0, "autodocsCount": 0, - "storiesMdxCount": 0, "mdxCount": 0, "exampleStoryCount": 8, "exampleDocsCount": 3, From 656c71828097ca91f273c71e53641fcd78ab0317 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 9 Feb 2024 10:24:59 +0100 Subject: [PATCH 02/11] remove stories.mdx from tests --- .../src/utils/__tests__/autoName.test.ts | 4 +- .../src/utils/watch-story-specifiers.test.ts | 28 +++++----- .../src/to-require-context.test.ts | 54 +++++++++---------- code/lib/csf-tools/src/CsfFile.test.ts | 4 +- .../src/tests/mockStoriesEntries.ts | 1 - .../preview-web/render/CsfDocsRender.ts | 2 - 6 files changed, 42 insertions(+), 51 deletions(-) diff --git a/code/lib/core-server/src/utils/__tests__/autoName.test.ts b/code/lib/core-server/src/utils/__tests__/autoName.test.ts index 9aefb7ec3eed..64954fc55d17 100644 --- a/code/lib/core-server/src/utils/__tests__/autoName.test.ts +++ b/code/lib/core-server/src/utils/__tests__/autoName.test.ts @@ -2,9 +2,9 @@ import { it, expect } from 'vitest'; import { autoName } from '../autoName'; it('pulls name from named MDX files', () => { - expect(autoName('Conventions.mdx', 'Button.stories.mdx', 'Docs')).toEqual('Conventions'); + expect(autoName('Conventions.mdx', 'Button.mdx', 'Docs')).toEqual('Conventions'); }); it('falls back for default named MDX files', () => { - expect(autoName('Button.mdx', 'Button.stories.mdx', 'Docs')).toEqual('Docs'); + expect(autoName('Button.mdx', 'Button.stories.jsx', 'Docs')).toEqual('Docs'); }); diff --git a/code/lib/core-server/src/utils/watch-story-specifiers.test.ts b/code/lib/core-server/src/utils/watch-story-specifiers.test.ts index 22d3e0901de8..4026de15fa11 100644 --- a/code/lib/core-server/src/utils/watch-story-specifiers.test.ts +++ b/code/lib/core-server/src/utils/watch-story-specifiers.test.ts @@ -83,7 +83,7 @@ describe('watchStorySpecifiers', () => { }); it('watches single file globs', async () => { - const specifier = normalizeStoriesEntry('../src/nested/Button.stories.mdx', options); + const specifier = normalizeStoriesEntry('../src/nested/Button.mdx', options); const onInvalidate = vi.fn(); close = watchStorySpecifiers([specifier], { workingDir }, onInvalidate); @@ -98,33 +98,33 @@ describe('watchStorySpecifiers', () => { // File changed, matching onInvalidate.mockClear(); - await onChange('src/nested/Button.stories.mdx', 1234); - expect(onInvalidate).toHaveBeenCalledWith(specifier, `./src/nested/Button.stories.mdx`, false); + await onChange('src/nested/Button.mdx', 1234); + expect(onInvalidate).toHaveBeenCalledWith(specifier, `./src/nested/Button.mdx`, false); // File changed, NOT matching onInvalidate.mockClear(); - await onChange('src/nested/Button.mdx', 1234); + await onChange('src/nested/Button.tsx', 1234); expect(onInvalidate).not.toHaveBeenCalled(); // File removed, matching onInvalidate.mockClear(); - await onRemove('src/nested/Button.stories.mdx'); - expect(onInvalidate).toHaveBeenCalledWith(specifier, `./src/nested/Button.stories.mdx`, true); + await onRemove('src/nested/Button.mdx'); + expect(onInvalidate).toHaveBeenCalledWith(specifier, `./src/nested/Button.mdx`, true); // File removed, NOT matching onInvalidate.mockClear(); - await onRemove('src/nested/Button.mdx'); + await onRemove('src/nested/Button.tsx'); expect(onInvalidate).not.toHaveBeenCalled(); // File moved out, matching onInvalidate.mockClear(); - await onChange('src/nested/Button.stories.mdx', null); - expect(onInvalidate).toHaveBeenCalledWith(specifier, `./src/nested/Button.stories.mdx`, true); + await onChange('src/nested/Button.mdx', null); + expect(onInvalidate).toHaveBeenCalledWith(specifier, `./src/nested/Button.mdx`, true); }); it('multiplexes between two specifiers on the same directory', async () => { const globSpecifier = normalizeStoriesEntry('../src/**/*.stories.@(ts|js)', options); - const fileSpecifier = normalizeStoriesEntry('../src/nested/Button.stories.mdx', options); + const fileSpecifier = normalizeStoriesEntry('../src/nested/Button.mdx', options); const onInvalidate = vi.fn(); close = watchStorySpecifiers([globSpecifier, fileSpecifier], { workingDir }, onInvalidate); @@ -145,11 +145,7 @@ describe('watchStorySpecifiers', () => { ); onInvalidate.mockClear(); - await onChange('src/nested/Button.stories.mdx', 1234); - expect(onInvalidate).toHaveBeenCalledWith( - fileSpecifier, - `./src/nested/Button.stories.mdx`, - false - ); + await onChange('src/nested/Button.mdx', 1234); + expect(onInvalidate).toHaveBeenCalledWith(fileSpecifier, `./src/nested/Button.mdx`, false); }); }); diff --git a/code/lib/core-webpack/src/to-require-context.test.ts b/code/lib/core-webpack/src/to-require-context.test.ts index 90fab1d15f97..e6dbd3c8a90a 100644 --- a/code/lib/core-webpack/src/to-require-context.test.ts +++ b/code/lib/core-webpack/src/to-require-context.test.ts @@ -61,21 +61,21 @@ const testCases = [ }, // INVALID GLOB { - glob: '../src/stories/**/*.stories.(js|mdx)', + glob: '../src/stories/**/*.stories.(js|ts)', recursive: true, validPaths: [ '../src/stories/components/Icon.stories.js', '../src/stories/Icon.stories.js', - '../src/stories/Icon.stories.mdx', + '../src/stories/Icon.stories.ts', '../src/stories/components/Icon/Icon.stories.js', - '../src/stories/components/Icon.stories/Icon.stories.mdx', + '../src/stories/components/Icon.stories/Icon.stories.ts', ], invalidPaths: [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', - '../src/stories/components/Icon/Icon.stories.ts', + '../src/Icon.stories.ts', + '../src/stories/components/Icon/Icon.stories.tsx', '../src/stories/components/Icon/Icon.mdx', ], }, @@ -89,59 +89,59 @@ const testCases = [ invalidPaths: ['./dirname/../stories.js', './dirname/../App.stories.js'], }, { - glob: '../src/stories/**/@(*.stories.js|*.stories.mdx)', + glob: '../src/stories/**/@(*.stories.js|*.stories.ts)', recursive: true, validPaths: [ '../src/stories/components/Icon.stories.js', '../src/stories/Icon.stories.js', - '../src/stories/Icon.stories.mdx', + '../src/stories/Icon.stories.ts', '../src/stories/components/Icon/Icon.stories.js', - '../src/stories/components/Icon.stories/Icon.stories.mdx', + '../src/stories/components/Icon.stories/Icon.stories.ts', ], invalidPaths: [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', - '../src/stories/components/Icon/Icon.stories.ts', + '../src/Icon.stories.ts', + '../src/stories/components/Icon/Icon.stories.tsx', '../src/stories/components/Icon/Icon.mdx', ], }, { - glob: '../src/stories/**/*.stories.+(js|mdx)', + glob: '../src/stories/**/*.stories.+(js|ts)', recursive: true, validPaths: [ '../src/stories/components/Icon.stories.js', '../src/stories/Icon.stories.js', - '../src/stories/Icon.stories.mdx', + '../src/stories/Icon.stories.ts', '../src/stories/components/Icon/Icon.stories.js', - '../src/stories/components/Icon.stories/Icon.stories.mdx', + '../src/stories/components/Icon.stories/Icon.stories.ts', ], invalidPaths: [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', - '../src/stories/components/Icon/Icon.stories.ts', + '../src/Icon.stories.tsx', + '../src/stories/components/Icon/Icon.stories.tsx', '../src/stories/components/Icon/Icon.mdx', ], }, { - glob: '../src/stories/**/*.stories.*(js|mdx)', + glob: '../src/stories/**/*.stories.*(js|ts)', recursive: true, validPaths: [ '../src/stories/components/Icon.stories.js', '../src/stories/Icon.stories.js', - '../src/stories/Icon.stories.mdx', + '../src/stories/Icon.stories.ts', '../src/stories/components/Icon/Icon.stories.js', - '../src/stories/components/Icon.stories/Icon.stories.mdx', + '../src/stories/components/Icon.stories/Icon.stories.ts', ], invalidPaths: [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', - '../src/stories/components/Icon/Icon.stories.ts', + '../src/Icon.stories.ts', + '../src/stories/components/Icon/Icon.stories.tsx', '../src/stories/components/Icon/Icon.mdx', ], }, @@ -150,13 +150,13 @@ const testCases = [ recursive: false, validPaths: ['../src/stories/components/Icon.stories.js'], invalidPaths: [ - '../src/Icon.stories.mdx', - '../src/stories/components/Icon.stories/Icon.stories.mdx', + '../src/Icon.stories.tsx', + '../src/stories/components/Icon.stories/Icon.stories.tsx', '../src/stories/components/Icon/Icon.mdx', '../src/stories/components/Icon/Icon.stories.js', '../src/stories/components/Icon/Icon.stories.ts', '../src/stories/Icon.stories.js', - '../src/stories/Icon.stories.mdx', + '../src/stories/Icon.stories.tsx', './Icon.stories.js', './src/stories/Icon.stories.js', './stories.js', @@ -191,7 +191,7 @@ const testCases = [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', + '../src/Icon.stories.tsx', '../src/stories/components/Icon/Icon.stories.ts', '../src/stories/components/Icon/Icon.mdx', ], @@ -205,7 +205,7 @@ const testCases = [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', + '../src/Icon.stories.tsx', '../src/stories/components/Icon/Icon.stories.ts', '../src/stories/components/Icon/Icon.mdx', ], @@ -219,7 +219,7 @@ const testCases = [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', + '../src/Icon.stories.tsx', '../components/icon/Icon.stories.js', '../components/basics/simple/icon/Icon.stories.js', '../src/stories/components/Icon/Icon.stories.ts', @@ -235,7 +235,7 @@ const testCases = [ './stories.js', './src/stories/Icon.stories.js', './Icon.stories.js', - '../src/Icon.stories.mdx', + '../src/Icon.stories.tsx', '../src/stories/components/Icon/Icon.stories.ts', '../src/stories/components/Icon/Icon.mdx', ], diff --git a/code/lib/csf-tools/src/CsfFile.test.ts b/code/lib/csf-tools/src/CsfFile.test.ts index 7e0d223a23af..b773b026646a 100644 --- a/code/lib/csf-tools/src/CsfFile.test.ts +++ b/code/lib/csf-tools/src/CsfFile.test.ts @@ -442,7 +442,7 @@ describe('CsfFile', () => { export const TestControl = () => _jsx("p", { children: "Hello" }); - export default { title: 'foo/bar', tags: ['stories-mdx'], includeStories: ["__page"] }; + export default { title: 'foo/bar', includeStories: ["__page"] }; export const __page = () => {}; __page.parameters = { docsOnly: true }; `, @@ -451,8 +451,6 @@ describe('CsfFile', () => { ).toMatchInlineSnapshot(` meta: title: foo/bar - tags: - - stories-mdx includeStories: - __page stories: diff --git a/code/lib/manager-api/src/tests/mockStoriesEntries.ts b/code/lib/manager-api/src/tests/mockStoriesEntries.ts index 703b6e6efb76..a820307f4ef2 100644 --- a/code/lib/manager-api/src/tests/mockStoriesEntries.ts +++ b/code/lib/manager-api/src/tests/mockStoriesEntries.ts @@ -53,7 +53,6 @@ export const docsEntries: StoryIndex['entries'] = { name: 'Docs', importPath: './path/to/component-b.ts', storiesImports: [], - tags: ['stories-mdx'], }, 'component-c--story-4': { type: 'story', diff --git a/code/lib/preview-api/src/modules/preview-web/render/CsfDocsRender.ts b/code/lib/preview-api/src/modules/preview-web/render/CsfDocsRender.ts index 019d43d97893..0be1ce5821c3 100644 --- a/code/lib/preview-api/src/modules/preview-web/render/CsfDocsRender.ts +++ b/code/lib/preview-api/src/modules/preview-web/render/CsfDocsRender.ts @@ -25,8 +25,6 @@ import { DocsContext } from '../docs-context/DocsContext'; * * Use cases: * - Autodocs, where there is no story, and we fall back to the globally defined template. - * - *.stories.mdx files, where the MDX compiler produces a CSF file with a `.parameter.docs.page` - * parameter containing the compiled content of the MDX file. */ export class CsfDocsRender implements Render { public readonly type: RenderType = 'docs'; From 2021951ef50a09bbdfc9f41416ace3b8366f81fb Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 9 Feb 2024 10:25:27 +0100 Subject: [PATCH 03/11] remove stories.mdx from index creation --- .../src/utils/StoryIndexGenerator.ts | 21 ++----- .../utils/__tests__/index-extraction.test.ts | 62 +------------------ 2 files changed, 7 insertions(+), 76 deletions(-) diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.ts index a650f04c1ffa..20b0f4d70046 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.ts @@ -55,12 +55,11 @@ export type StoryIndexGeneratorOptions = { }; export const AUTODOCS_TAG = 'autodocs'; -export const STORIES_MDX_TAG = 'stories-mdx'; export const PLAY_FN_TAG = 'play-fn'; /** Was this docs entry generated by a .mdx file? (see discussion below) */ export function isMdxEntry({ tags }: DocsIndexEntry) { - return !tags?.includes(AUTODOCS_TAG) && !tags?.includes(STORIES_MDX_TAG); + return !tags?.includes(AUTODOCS_TAG); } const makeAbsolute = (otherImport: Path, normalizedPath: Path, workingDir: Path) => @@ -83,12 +82,11 @@ const makeAbsolute = (otherImport: Path, normalizedPath: Path, workingDir: Path) * * A stories file is indexed by an indexer (passed in), which produces a list of stories. * - If the stories have the `parameters.docsOnly` setting, they are disregarded. - * - If the stories have the 'stories-mdx' tag (i.e. were generated by a .stories.mdx file), - * OR autodocs is enabled, a docs entry is added pointing to the story file. + * - If the stories have autodocs enabled, a docs entry is added pointing to the story file. * * A (modern) docs (.mdx) file is indexed, a docs entry is added. * - * In the preview, a docs entry with either the `autodocs` or `stories-mdx` tags will be rendered + * In the preview, a docs entry with the `autodocs` tag will be rendered * as a CSF file that exports an MDX template on the `docs.page` parameter, whereas * other docs entries are rendered as MDX files directly. * @@ -308,11 +306,8 @@ export class StoryIndexGenerator { // We need a docs entry attached to the CSF file if either: // a) autodocs is globally enabled // b) we have autodocs enabled for this file - // c) it is a stories.mdx transpiled to CSF const hasAutodocsTag = entries.some((entry) => entry.tags.includes(AUTODOCS_TAG)); - const isStoriesMdx = entries.some((entry) => entry.tags.includes(STORIES_MDX_TAG)); - const createDocEntry = - autodocs === true || (autodocs === 'tag' && hasAutodocsTag) || isStoriesMdx; + const createDocEntry = autodocs === true || (autodocs === 'tag' && hasAutodocsTag); if (createDocEntry && this.options.build?.test?.disableAutoDocs !== true) { const name = this.options.docs.defaultName ?? 'Docs'; @@ -326,17 +321,13 @@ export class StoryIndexGenerator { name, importPath, type: 'docs', - tags: [...metaTags, 'docs', ...(!hasAutodocsTag && !isStoriesMdx ? [AUTODOCS_TAG] : [])], + tags: [...metaTags, 'docs', ...(!hasAutodocsTag ? [AUTODOCS_TAG] : [])], storiesImports: [], }); } - const entriesWithoutDocsOnlyStories = entries.filter( - (entry) => !(entry.type === 'story' && entry.tags.includes('stories-mdx-docsOnly')) - ); - return { - entries: entriesWithoutDocsOnlyStories, + entries, dependents: [], type: 'stories', }; diff --git a/code/lib/core-server/src/utils/__tests__/index-extraction.test.ts b/code/lib/core-server/src/utils/__tests__/index-extraction.test.ts index a1936c6719dd..b6e603f8b86a 100644 --- a/code/lib/core-server/src/utils/__tests__/index-extraction.test.ts +++ b/code/lib/core-server/src/utils/__tests__/index-extraction.test.ts @@ -8,7 +8,7 @@ import { normalizeStoriesEntry } from '@storybook/core-common'; import type { NormalizedStoriesSpecifier } from '@storybook/types'; import type { StoryIndexGeneratorOptions } from '../StoryIndexGenerator'; -import { AUTODOCS_TAG, STORIES_MDX_TAG, StoryIndexGenerator } from '../StoryIndexGenerator'; +import { AUTODOCS_TAG, StoryIndexGenerator } from '../StoryIndexGenerator'; vi.mock('@storybook/node-logger'); @@ -537,64 +537,4 @@ describe('docs entries from story extraction', () => { } `); }); - it(`adds docs entry when an entry has the "${STORIES_MDX_TAG}" tag`, async () => { - const relativePath = './src/A.stories.js'; - const absolutePath = path.join(options.workingDir, relativePath); - const specifier: NormalizedStoriesSpecifier = normalizeStoriesEntry(relativePath, options); - - const generator = new StoryIndexGenerator([specifier], { - ...options, - docs: { defaultName: 'docs', autodocs: false }, - indexers: [ - { - test: /\.stories\.(m?js|ts)x?$/, - createIndex: async (fileName) => [ - { - exportName: 'StoryOne', - __id: 'a--story-one', - name: 'Story One', - title: 'A', - tags: [STORIES_MDX_TAG, 'story-tag-from-indexer'], - importPath: fileName, - type: 'story', - }, - ], - }, - ], - }); - const result = await generator.extractStories(specifier, absolutePath); - - expect(result).toMatchInlineSnapshot(` - { - "dependents": [], - "entries": [ - { - "id": "a--docs", - "importPath": "./src/A.stories.js", - "name": "docs", - "storiesImports": [], - "tags": [ - "docs", - ], - "title": "A", - "type": "docs", - }, - { - "id": "a--story-one", - "importPath": "./src/A.stories.js", - "metaId": undefined, - "name": "Story One", - "tags": [ - "stories-mdx", - "story-tag-from-indexer", - "story", - ], - "title": "A", - "type": "story", - }, - ], - "type": "stories", - } - `); - }); }); From a0268b8c7f91ff1affe2297bc87bea9da84491a8 Mon Sep 17 00:00:00 2001 From: Jeppe Reinhold Date: Fri, 9 Feb 2024 10:25:45 +0100 Subject: [PATCH 04/11] remove stories.mdx from client apis --- .../utils/__tests__/normalize-stories.test.ts | 124 +++++++++--------- code/lib/manager-api/src/lib/stories.test.ts | 3 - code/lib/manager-api/src/lib/stories.ts | 3 +- .../preview-web/PreviewWithSelection.tsx | 3 +- 4 files changed, 64 insertions(+), 69 deletions(-) diff --git a/code/lib/core-common/src/utils/__tests__/normalize-stories.test.ts b/code/lib/core-common/src/utils/__tests__/normalize-stories.test.ts index 8bf64a548ed5..9cefd9052bf4 100644 --- a/code/lib/core-common/src/utils/__tests__/normalize-stories.test.ts +++ b/code/lib/core-common/src/utils/__tests__/normalize-stories.test.ts @@ -42,172 +42,172 @@ const options = { describe('normalizeStoriesEntry', () => { it('direct file path', () => { - const specifier = normalizeStoriesEntry('../path/to/file.stories.mdx', options); + const specifier = normalizeStoriesEntry('../path/to/file.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": "./path/to", - "files": "file.stories.mdx", + "files": "file.stories.jsx", "importPathMatcher": {} } `); - expect(specifier.importPathMatcher).toMatchPaths(['./path/to/file.stories.mdx']); + expect(specifier.importPathMatcher).toMatchPaths(['./path/to/file.stories.jsx']); expect(specifier.importPathMatcher).not.toMatchPaths([ './path/to/file.stories.js', - './file.stories.mdx', - '../file.stories.mdx', + './file.stories.jsx', + '../file.stories.jsx', ]); }); it('story in config dir', () => { - const specifier = normalizeStoriesEntry('./file.stories.mdx', options); + const specifier = normalizeStoriesEntry('./file.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": "./.storybook", - "files": "file.stories.mdx", + "files": "file.stories.jsx", "importPathMatcher": {} } `); - expect(specifier.importPathMatcher).toMatchPaths(['./.storybook/file.stories.mdx']); + expect(specifier.importPathMatcher).toMatchPaths(['./.storybook/file.stories.jsx']); expect(specifier.importPathMatcher).not.toMatchPaths([ - '.storybook/file.stories.mdx', - './file.stories.mdx', - '../file.stories.mdx', + '.storybook/file.stories.jsx', + './file.stories.jsx', + '../file.stories.jsx', ]); }); it('non-recursive files glob', () => { - const specifier = normalizeStoriesEntry('../*/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../*/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": ".", - "files": "*/*.stories.mdx", + "files": "*/*.stories.jsx", "importPathMatcher": {} } `); expect(specifier.importPathMatcher).toMatchPaths([ - './path/file.stories.mdx', - './second-path/file.stories.mdx', + './path/file.stories.jsx', + './second-path/file.stories.jsx', ]); expect(specifier.importPathMatcher).not.toMatchPaths([ './path/file.stories.js', - './path/to/file.stories.mdx', - './file.stories.mdx', - '../file.stories.mdx', + './path/to/file.stories.jsx', + './file.stories.jsx', + '../file.stories.jsx', ]); }); it('double non-recursive directory/files glob', () => { - const specifier = normalizeStoriesEntry('../*/*/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../*/*/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": ".", - "files": "*/*/*.stories.mdx", + "files": "*/*/*.stories.jsx", "importPathMatcher": {} } `); expect(specifier.importPathMatcher).toMatchPaths([ - './path/to/file.stories.mdx', - './second-path/to/file.stories.mdx', + './path/to/file.stories.jsx', + './second-path/to/file.stories.jsx', ]); expect(specifier.importPathMatcher).not.toMatchPaths([ - './file.stories.mdx', - './path/file.stories.mdx', - './path/to/third/file.stories.mdx', + './file.stories.jsx', + './path/file.stories.jsx', + './path/to/third/file.stories.jsx', './path/to/file.stories.js', - '../file.stories.mdx', + '../file.stories.jsx', ]); }); it('directory/files glob', () => { - const specifier = normalizeStoriesEntry('../**/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../**/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": ".", - "files": "**/*.stories.mdx", + "files": "**/*.stories.jsx", "importPathMatcher": {} } `); expect(specifier.importPathMatcher).toMatchPaths([ - './file.stories.mdx', - './path/file.stories.mdx', - './path/to/file.stories.mdx', - './path/to/third/file.stories.mdx', + './file.stories.jsx', + './path/file.stories.jsx', + './path/to/file.stories.jsx', + './path/to/third/file.stories.jsx', ]); expect(specifier.importPathMatcher).not.toMatchPaths([ './file.stories.js', - '../file.stories.mdx', + '../file.stories.jsx', ]); }); it('double stars glob', () => { - const specifier = normalizeStoriesEntry('../**/foo/**/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../**/foo/**/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": ".", - "files": "**/foo/**/*.stories.mdx", + "files": "**/foo/**/*.stories.jsx", "importPathMatcher": {} } `); expect(specifier.importPathMatcher).toMatchPaths([ - './foo/file.stories.mdx', - './path/to/foo/file.stories.mdx', - './path/to/foo/third/fourth/file.stories.mdx', + './foo/file.stories.jsx', + './path/to/foo/file.stories.jsx', + './path/to/foo/third/fourth/file.stories.jsx', ]); expect(specifier.importPathMatcher).not.toMatchPaths([ - './file.stories.mdx', + './file.stories.jsx', './file.stories.js', - '../file.stories.mdx', + '../file.stories.jsx', ]); }); it('intermediate directory glob', () => { - const specifier = normalizeStoriesEntry('../**/foo/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../**/foo/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": ".", - "files": "**/foo/*.stories.mdx", + "files": "**/foo/*.stories.jsx", "importPathMatcher": {} } `); expect(specifier.importPathMatcher).toMatchPaths([ - './path/to/foo/file.stories.mdx', - './foo/file.stories.mdx', + './path/to/foo/file.stories.jsx', + './foo/file.stories.jsx', ]); expect(specifier.importPathMatcher).not.toMatchPaths([ - './file.stories.mdx', + './file.stories.jsx', './file.stories.js', - './path/to/foo/third/fourth/file.stories.mdx', - '../file.stories.mdx', + './path/to/foo/third/fourth/file.stories.jsx', + '../file.stories.jsx', ]); }); it('directory outside of working dir', () => { - const specifier = normalizeStoriesEntry('../../src/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../../src/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": "../src", - "files": "*.stories.mdx", + "files": "*.stories.jsx", "importPathMatcher": {} } `); - expect(specifier.importPathMatcher).toMatchPaths(['../src/file.stories.mdx']); + expect(specifier.importPathMatcher).toMatchPaths(['../src/file.stories.jsx']); expect(specifier.importPathMatcher).not.toMatchPaths([ - './src/file.stories.mdx', + './src/file.stories.jsx', '../src/file.stories.js', ]); }); @@ -237,11 +237,11 @@ describe('normalizeStoriesEntry', () => { }); it('directory/files specifier', () => { - const specifier = normalizeStoriesEntry({ directory: '..', files: '*.stories.mdx' }, options); + const specifier = normalizeStoriesEntry({ directory: '..', files: '*.stories.jsx' }, options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", - "files": "*.stories.mdx", + "files": "*.stories.jsx", "directory": ".", "importPathMatcher": {} } @@ -262,13 +262,13 @@ describe('normalizeStoriesEntry', () => { it('directory/titlePrefix/files specifier', () => { const specifier = normalizeStoriesEntry( - { directory: '..', titlePrefix: 'atoms', files: '*.stories.mdx' }, + { directory: '..', titlePrefix: 'atoms', files: '*.stories.jsx' }, options ); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "atoms", - "files": "*.stories.mdx", + "files": "*.stories.jsx", "directory": ".", "importPathMatcher": {} } @@ -276,25 +276,25 @@ describe('normalizeStoriesEntry', () => { }); it('globs with negation', () => { - const specifier = normalizeStoriesEntry('../!(negation)/*.stories.mdx', options); + const specifier = normalizeStoriesEntry('../!(negation)/*.stories.jsx', options); expect(specifier).toMatchInlineSnapshot(` { "titlePrefix": "", "directory": ".", - "files": "!(negation)/*.stories.mdx", + "files": "!(negation)/*.stories.jsx", "importPathMatcher": {} } `); expect(specifier.importPathMatcher).toMatchPaths([ - './path/file.stories.mdx', - './second-path/file.stories.mdx', + './path/file.stories.jsx', + './second-path/file.stories.jsx', ]); expect(specifier.importPathMatcher).not.toMatchPaths([ './path/file.stories.js', - './path/to/file.stories.mdx', - './file.stories.mdx', - '../file.stories.mdx', + './path/to/file.stories.jsx', + './file.stories.jsx', + '../file.stories.jsx', ]); }); }); diff --git a/code/lib/manager-api/src/lib/stories.test.ts b/code/lib/manager-api/src/lib/stories.test.ts index fcce4a94d874..3968bb88080d 100644 --- a/code/lib/manager-api/src/lib/stories.test.ts +++ b/code/lib/manager-api/src/lib/stories.test.ts @@ -115,9 +115,6 @@ describe('transformStoryIndexV3toV4', () => { "docsOnly": true, }, "storiesImports": [], - "tags": [ - "stories-mdx", - ], "title": "Story 1", "type": "docs", }, diff --git a/code/lib/manager-api/src/lib/stories.ts b/code/lib/manager-api/src/lib/stories.ts index 9d6b1817e677..79b3098d04e4 100644 --- a/code/lib/manager-api/src/lib/stories.ts +++ b/code/lib/manager-api/src/lib/stories.ts @@ -66,7 +66,6 @@ export const transformSetStoriesStoryDataToPreparedStoryIndex = ( if (docsOnly) { acc[id] = { type: 'docs', - tags: ['stories-mdx'], storiesImports: [], ...base, }; @@ -123,7 +122,7 @@ export const transformStoryIndexV3toV4 = (index: StoryIndexV3): API_PreparedStor } acc[entry.id] = { type, - ...(type === 'docs' && { tags: ['stories-mdx'], storiesImports: [] }), + ...(type === 'docs' && { storiesImports: [] }), ...entry, }; diff --git a/code/lib/preview-api/src/modules/preview-web/PreviewWithSelection.tsx b/code/lib/preview-api/src/modules/preview-web/PreviewWithSelection.tsx index 76f17e2419f8..50575dbbdbc3 100644 --- a/code/lib/preview-api/src/modules/preview-web/PreviewWithSelection.tsx +++ b/code/lib/preview-api/src/modules/preview-web/PreviewWithSelection.tsx @@ -54,12 +54,11 @@ function focusInInput(event: Event) { } export const AUTODOCS_TAG = 'autodocs'; -export const STORIES_MDX_TAG = 'stories-mdx'; export const ATTACHED_MDX_TAG = 'attached-mdx'; /** Was this docs entry generated by a .mdx file? (see discussion below) */ export function isMdxEntry({ tags }: DocsIndexEntry) { - return !tags?.includes(AUTODOCS_TAG) && !tags?.includes(STORIES_MDX_TAG); + return !tags?.includes(AUTODOCS_TAG); } type PossibleRender = From 51f31672d48064f2a499d58a61ee04de6479a5b4 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:50:26 +0000 Subject: [PATCH 05/11] feat: remove doctrine This removes doctrine and `assert` from the docs-tools package. The `type` of a parsed JSDoc comment will now be a `RootResult` from the `jsdoc-type-pratt-parser` package rather than a doctrine `Type`. Other than that, existing behaviour should remain. --- code/lib/docs-tools/package.json | 5 +- .../src/argTypes/jsdocParser.test.ts | 22 +- .../docs-tools/src/argTypes/jsdocParser.ts | 203 +++++++++--------- .../docgen-components/jsdoc/argTypes.snapshot | 126 ++++++----- .../jsdoc/properties.snapshot | 125 ++++++----- code/yarn.lock | 28 ++- 6 files changed, 263 insertions(+), 246 deletions(-) diff --git a/code/lib/docs-tools/package.json b/code/lib/docs-tools/package.json index 06e103124ee1..3025df49aa29 100644 --- a/code/lib/docs-tools/package.json +++ b/code/lib/docs-tools/package.json @@ -47,9 +47,8 @@ "@storybook/core-common": "workspace:*", "@storybook/preview-api": "workspace:*", "@storybook/types": "workspace:*", - "@types/doctrine": "^0.0.3", - "assert": "^2.1.0", - "doctrine": "^3.0.0", + "comment-parser": "^1.4.1", + "jsdoc-type-pratt-parser": "^4.0.0", "lodash": "^4.17.21" }, "devDependencies": { diff --git a/code/lib/docs-tools/src/argTypes/jsdocParser.test.ts b/code/lib/docs-tools/src/argTypes/jsdocParser.test.ts index 1460f70bf813..e0f83617605d 100644 --- a/code/lib/docs-tools/src/argTypes/jsdocParser.test.ts +++ b/code/lib/docs-tools/src/argTypes/jsdocParser.test.ts @@ -10,7 +10,7 @@ describe('parseJsDoc', () => { expect(extractedTags).toBeUndefined(); }); - it('should set includesJsDocto to false when the value dont contains JSDoc', () => { + it('should set includesJsDoc to false when the value dont contains JSDoc', () => { const { includesJsDoc, description, extractedTags } = parseJsDoc('Hey!'); expect(includesJsDoc).toBeFalsy(); @@ -68,7 +68,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.params).not.toBeNull(); expect(extractedTags?.params?.[0].name).toBe('event'); expect(extractedTags?.params?.[0].type).not.toBeNull(); - expect(extractedTags?.params?.[0].type.name).toBe('SyntheticEvent'); + expect(extractedTags?.params?.[0].type.value).toBe('SyntheticEvent'); expect(extractedTags?.params?.[0].description).toBeNull(); }); @@ -78,7 +78,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.params).not.toBeNull(); expect(extractedTags?.params?.[0].name).toBe('event'); expect(extractedTags?.params?.[0].type).not.toBeNull(); - expect(extractedTags?.params?.[0].type.name).toBe('SyntheticEvent'); + expect(extractedTags?.params?.[0].type.value).toBe('SyntheticEvent'); expect(extractedTags?.params?.[0].description).toBe('React event'); }); @@ -90,7 +90,7 @@ describe('parseJsDoc', () => { ['event1', 'event2', 'event3'].forEach((x, i) => { expect(extractedTags?.params?.[i].name).toBe(x); expect(extractedTags?.params?.[i].type).not.toBeNull(); - expect(extractedTags?.params?.[i].type.name).toBe('SyntheticEvent'); + expect(extractedTags?.params?.[i].type.value).toBe('SyntheticEvent'); expect(extractedTags?.params?.[i].description).toBe('React event'); }); }); @@ -129,7 +129,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.params).not.toBeNull(); expect(extractedTags?.params?.[0].name).toBe('event'); expect(extractedTags?.params?.[0].type).not.toBeNull(); - expect(extractedTags?.params?.[0].type.name).toBe('SyntheticEvent'); + expect(extractedTags?.params?.[0].type.value).toBe('SyntheticEvent'); expect(extractedTags?.params?.[0].description).toBe('React event'); }); }); @@ -251,7 +251,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.returns).not.toBeNull(); expect(extractedTags?.returns?.type).not.toBeNull(); - expect(extractedTags?.returns?.type.name).toBe('string'); + expect(extractedTags?.returns?.type.value).toBe('string'); }); it('should return a @returns with a type and a description', () => { @@ -259,7 +259,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.returns).not.toBeNull(); expect(extractedTags?.returns?.type).not.toBeNull(); - expect(extractedTags?.returns?.type.name).toBe('string'); + expect(extractedTags?.returns?.type.value).toBe('string'); expect(extractedTags?.returns?.description).toBe('A bar description'); }); @@ -270,7 +270,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.returns).not.toBeNull(); expect(extractedTags?.returns?.type).not.toBeNull(); - expect(extractedTags?.returns?.type.name).toBe('string'); + expect(extractedTags?.returns?.type.value).toBe('string'); expect(extractedTags?.returns?.description).toBe('This is\na multiline\ndescription'); }); @@ -279,7 +279,7 @@ describe('parseJsDoc', () => { expect(extractedTags?.returns).not.toBeNull(); expect(extractedTags?.returns?.type).not.toBeNull(); - expect(extractedTags?.returns?.type.name).toBe('number'); + expect(extractedTags?.returns?.type.value).toBe('number'); }); describe('getTypeName', () => { @@ -353,9 +353,9 @@ describe('parseJsDoc', () => { expect(extractedTags?.params).not.toBeNull(); expect(Object.keys(extractedTags?.params ?? []).length).toBe(1); expect(extractedTags?.params?.[0].name).toBe('event'); - expect(extractedTags?.params?.[0].type.name).toBe('SyntheticEvent'); + expect(extractedTags?.params?.[0].type.value).toBe('SyntheticEvent'); expect(extractedTags?.params?.[0].description).toBe('Original event.'); expect(extractedTags?.returns).not.toBeNull(); - expect(extractedTags?.returns?.type.name).toBe('string'); + expect(extractedTags?.returns?.type.value).toBe('string'); }); }); diff --git a/code/lib/docs-tools/src/argTypes/jsdocParser.ts b/code/lib/docs-tools/src/argTypes/jsdocParser.ts index 5a2b6258fc64..0990865a14c4 100644 --- a/code/lib/docs-tools/src/argTypes/jsdocParser.ts +++ b/code/lib/docs-tools/src/argTypes/jsdocParser.ts @@ -1,5 +1,11 @@ -import type { Annotation } from 'doctrine'; -import doctrine from 'doctrine'; +import type { Block, Spec } from 'comment-parser'; +import type { RootResult as JSDocType } from 'jsdoc-type-pratt-parser'; +import { parse as parseJSDoc } from 'comment-parser'; +import { + parse as parseJSDocType, + transform as transformJSDocType, + stringifyRules, +} from 'jsdoc-type-pratt-parser'; import type { JsDocParam, JsDocReturns } from './docgen'; export interface ExtractedJsDocParam extends JsDocParam { @@ -40,21 +46,25 @@ function containsJsDoc(value?: string | null): boolean { return value != null && value.includes('@'); } -function parse(content: string | null, tags?: string[]): Annotation { - let ast; +function parse(content: string | null): Block { + const contentString = content ?? ''; + const mappedLines = contentString + .split('\n') + .map((line) => ` * ${line}`) + .join('\n'); + const normalisedContent = '/**\n' + mappedLines + '\n*/'; - try { - ast = doctrine.parse(content ?? '', { - tags, - sloppy: true, - }); - } catch (e) { - console.error(e); + const ast = parseJSDoc(normalisedContent, { + spacing: 'preserve', + }); + if (!ast || ast.length === 0) { throw new Error('Cannot parse JSDoc tags.'); } - return ast; + // Return the first block, since we shouldn't ever really encounter + // multiple blocks of JSDoc + return ast[0]; } const DEFAULT_OPTIONS = { @@ -69,9 +79,9 @@ export const parseJsDoc: ParseJsDoc = (value, options = DEFAULT_OPTIONS) => { }; } - const jsDocAst = parse(value, options.tags); + const jsDocAst = parse(value); - const extractedTags = extractJsDocTags(jsDocAst); + const extractedTags = extractJsDocTags(jsDocAst, options.tags); if (extractedTags.ignore) { // There is no point in doing other stuff since this prop will not be rendered. @@ -85,12 +95,12 @@ export const parseJsDoc: ParseJsDoc = (value, options = DEFAULT_OPTIONS) => { includesJsDoc: true, ignore: false, // Always use the parsed description to ensure JSDoc is removed from the description. - description: jsDocAst.description, + description: jsDocAst.description.trim(), extractedTags, }; }; -function extractJsDocTags(ast: doctrine.Annotation): ExtractedJsDoc { +function extractJsDocTags(ast: Block, tags?: string[]): ExtractedJsDoc { const extractedTags: ExtractedJsDoc = { params: null, deprecated: null, @@ -98,20 +108,23 @@ function extractJsDocTags(ast: doctrine.Annotation): ExtractedJsDoc { ignore: false, }; - for (let i = 0; i < ast.tags.length; i += 1) { - const tag = ast.tags[i]; + for (const tagSpec of ast.tags) { + // Skip any tags we don't care about + if (tags !== undefined && !tags.includes(tagSpec.tag)) { + continue; + } - if (tag.title === 'ignore') { + if (tagSpec.tag === 'ignore') { extractedTags.ignore = true; // Once we reach an @ignore tag, there is no point in parsing the other tags since we will not render the prop. break; } else { - switch (tag.title) { + switch (tagSpec.tag) { // arg & argument are aliases for param. case 'param': case 'arg': case 'argument': { - const paramTag = extractParam(tag); + const paramTag = extractParam(tagSpec); if (paramTag != null) { if (extractedTags.params == null) { extractedTags.params = []; @@ -121,14 +134,14 @@ function extractJsDocTags(ast: doctrine.Annotation): ExtractedJsDoc { break; } case 'deprecated': { - const deprecatedTag = extractDeprecated(tag); + const deprecatedTag = extractDeprecated(tagSpec); if (deprecatedTag != null) { extractedTags.deprecated = deprecatedTag; } break; } case 'returns': { - const returnsTag = extractReturns(tag); + const returnsTag = extractReturns(tagSpec); if (returnsTag != null) { extractedTags.returns = returnsTag; } @@ -143,49 +156,61 @@ function extractJsDocTags(ast: doctrine.Annotation): ExtractedJsDoc { return extractedTags; } -function extractParam(tag: doctrine.Tag): ExtractedJsDocParam | null { - const paramName = tag.name; - - // When the @param doesn't have a name but have a type and a description, "null-null" is returned. - if (paramName != null && paramName !== 'null-null') { - return { - name: tag.name, - type: tag.type, - description: tag.description, - getPrettyName: () => { - if (paramName.includes('null')) { - // There is a few cases in which the returned param name contains "null". - // - @param {SyntheticEvent} event- Original SyntheticEvent - // - @param {SyntheticEvent} event.\n@returns {string} - return paramName.replace('-null', '').replace('.null', ''); - } +function normaliseParamName(name: string): string { + return name.replace(/[\.-]$/, ''); +} - return tag.name; - }, - getTypeName: () => { - return tag.type != null ? extractTypeName(tag.type) : null; - }, - }; +function extractParam(tag: Spec): ExtractedJsDocParam | null { + // Ignore tags with empty names or `-`. + // We ignore `-` since it means a comment was likely missing a name but + // using separators. For example: `@param {foo} - description` + if (!tag.name || tag.name === '-') { + return null; } - return null; + const type = extractType(tag.type); + + return { + name: tag.name, + type: type, + description: normaliseDescription(tag.description), + getPrettyName: () => { + return normaliseParamName(tag.name); + }, + getTypeName: () => { + return type ? extractTypeName(type) : null; + }, + }; } -function extractDeprecated(tag: doctrine.Tag): string | null { - if (tag.title != null) { - return tag.description; +function extractDeprecated(tag: Spec): string | null { + if (tag.name) { + return joinNameAndDescription(tag.name, tag.description); } return null; } -function extractReturns(tag: doctrine.Tag): ExtractedJsDocReturns | null { - if (tag.type != null) { +function joinNameAndDescription(name: string, desc: string): string | null { + const joined = name === '' ? desc : `${name} ${desc}`; + return normaliseDescription(joined); +} + +function normaliseDescription(text: string): string | null { + const normalised = text.replace(/^- /g, '').trim(); + + return normalised === '' ? null : normalised; +} + +function extractReturns(tag: Spec): ExtractedJsDocReturns | null { + const type = extractType(tag.type); + + if (type) { return { - type: tag.type, - description: tag.description, + type: type, + description: joinNameAndDescription(tag.name, tag.description), getTypeName: () => { - return extractTypeName(tag.type); + return extractTypeName(type); }, }; } @@ -193,57 +218,25 @@ function extractReturns(tag: doctrine.Tag): ExtractedJsDocReturns | null { return null; } -function extractTypeName(type?: doctrine.Type | null): string | null { - if (type?.type === 'NameExpression') { - return type.name; - } - - if (type?.type === 'RecordType') { - const recordFields = type.fields.map((field: doctrine.Type) => { - if (field.type === 'FieldType' && field.value != null) { - const valueTypeName = extractTypeName(field.value); - - return `${field.key}: ${valueTypeName}`; - } - - return (field as doctrine.type.FieldType).key; - }); - - return `({${recordFields.join(', ')}})`; - } - - if (type?.type === 'UnionType') { - const unionElements = type.elements.map(extractTypeName); - - return `(${unionElements.join('|')})`; - } - - // Only support untyped array: []. Might add more support later if required. - if (type?.type === 'ArrayType') { - return '[]'; - } - - if (type?.type === 'TypeApplication') { - if (type.expression != null) { - if ((type.expression as doctrine.type.NameExpression).name === 'Array') { - const arrayType = extractTypeName(type.applications[0]); - - return `${arrayType}[]`; - } - } - } - - if ( - type?.type === 'NullableType' || - type?.type === 'NonNullableType' || - type?.type === 'OptionalType' - ) { - return extractTypeName(type.expression); - } - - if (type?.type === 'AllLiteral') { - return 'any'; +const jsdocStringifyRules = stringifyRules(); +const originalJsdocStringifyObject = jsdocStringifyRules.JsdocTypeObject; +jsdocStringifyRules.JsdocTypeAny = () => 'any'; +jsdocStringifyRules.JsdocTypeObject = (result, transform) => + `(${originalJsdocStringifyObject(result, transform)})`; +jsdocStringifyRules.JsdocTypeOptional = (result, transform) => transform(result.element); +jsdocStringifyRules.JsdocTypeNullable = (result, transform) => transform(result.element); +jsdocStringifyRules.JsdocTypeNotNullable = (result, transform) => transform(result.element); +jsdocStringifyRules.JsdocTypeUnion = (result, transform) => + result.elements.map(transform).join('|'); + +function extractType(typeString: string): JSDocType | null { + try { + return parseJSDocType(typeString, 'typescript'); + } catch (_err) { + return null; } +} - return null; +function extractTypeName(type: JSDocType): string { + return transformJSDocType(jsdocStringifyRules, type); } diff --git a/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot b/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot index c37916b55746..693188a97e8f 100644 --- a/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot +++ b/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot @@ -101,20 +101,10 @@ "name": "case13", "table": { "defaultValue": null, - "jsDocTags": { - "deprecated": null, - "ignore": false, - "params": [ - { - "description": null, - "name": "SyntheticEvent", - }, - ], - "returns": null, - }, + "jsDocTags": undefined, "type": { "detail": undefined, - "summary": "(SyntheticEvent)", + "summary": "func", }, }, "type": { @@ -208,8 +198,8 @@ "description": null, "getTypeName": [Function], "type": { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, }, }, @@ -391,8 +381,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -419,8 +409,8 @@ description", "description": "React's original event", "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -452,8 +442,8 @@ description", "description": "React's original event", "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -489,8 +479,8 @@ description", "description": "React's original event", "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -517,8 +507,8 @@ description", "description": "Second returns", "getTypeName": [Function], "type": { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, }, }, @@ -699,7 +689,7 @@ description", }, "type": { "detail": undefined, - "summary": "(myType)", + "summary": "(myType: ...number)", }, }, "type": { @@ -798,25 +788,38 @@ description", "description": null, "getTypeName": [Function], "type": { - "fields": [ + "elements": [ { "key": "a", - "type": "FieldType", - "value": { - "name": "number", - "type": "NameExpression", + "meta": { + "quote": undefined, + }, + "optional": false, + "readonly": false, + "right": { + "type": "JsdocTypeName", + "value": "number", }, + "type": "JsdocTypeObjectField", }, { "key": "b", - "type": "FieldType", - "value": { - "name": "string", - "type": "NameExpression", + "meta": { + "quote": undefined, }, + "optional": false, + "readonly": false, + "right": { + "type": "JsdocTypeName", + "value": "string", + }, + "type": "JsdocTypeObjectField", }, ], - "type": "RecordType", + "meta": { + "separator": "comma", + }, + "type": "JsdocTypeObject", }, }, }, @@ -843,17 +846,21 @@ description", "description": null, "getTypeName": [Function], "type": { - "applications": [ + "elements": [ { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, ], - "expression": { - "name": "Array", - "type": "NameExpression", + "left": { + "type": "JsdocTypeName", + "value": "Array", + }, + "meta": { + "brackets": "square", + "dot": false, }, - "type": "TypeApplication", + "type": "JsdocTypeGeneric", }, }, }, @@ -909,17 +916,20 @@ description", "description": null, "getTypeName": [Function], "type": { - "elements": [ - { - "name": "number", - "type": "NameExpression", - }, - { - "name": "boolean", - "type": "NameExpression", - }, - ], - "type": "UnionType", + "element": { + "elements": [ + { + "type": "JsdocTypeName", + "value": "number", + }, + { + "type": "JsdocTypeName", + "value": "boolean", + }, + ], + "type": "JsdocTypeUnion", + }, + "type": "JsdocTypeParenthesis", }, }, }, @@ -946,7 +956,7 @@ description", "description": null, "getTypeName": [Function], "type": { - "type": "AllLiteral", + "type": "JsdocTypeAny", }, }, }, @@ -973,8 +983,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, }, }, @@ -1001,8 +1011,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "void", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "void", }, }, }, diff --git a/code/renderers/react/template/stories/docgen-components/jsdoc/properties.snapshot b/code/renderers/react/template/stories/docgen-components/jsdoc/properties.snapshot index cf7888853878..cef2829d44dc 100644 --- a/code/renderers/react/template/stories/docgen-components/jsdoc/properties.snapshot +++ b/code/renderers/react/template/stories/docgen-components/jsdoc/properties.snapshot @@ -227,17 +227,6 @@ description", { "defaultValue": null, "description": "param with type", - "jsDocTags": { - "deprecated": null, - "ignore": false, - "params": [ - { - "description": null, - "name": "SyntheticEvent", - }, - ], - "returns": null, - }, "name": "case13", "required": false, "sbType": { @@ -245,7 +234,7 @@ description", }, "type": { "detail": undefined, - "summary": "(SyntheticEvent)", + "summary": "func", }, }, { @@ -325,8 +314,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, }, }, @@ -470,8 +459,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -496,8 +485,8 @@ description", "description": "React's original event", "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -527,8 +516,8 @@ description", "description": "React's original event", "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -562,8 +551,8 @@ description", "description": "React's original event", "getTypeName": [Function], "type": { - "name": "SyntheticEvent", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "SyntheticEvent", }, }, }, @@ -588,8 +577,8 @@ description", "description": "Second returns", "getTypeName": [Function], "type": { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, }, }, @@ -744,7 +733,7 @@ description", }, "type": { "detail": undefined, - "summary": "(myType)", + "summary": "(myType: ...number)", }, }, { @@ -830,25 +819,38 @@ description", "description": null, "getTypeName": [Function], "type": { - "fields": [ + "elements": [ { "key": "a", - "type": "FieldType", - "value": { - "name": "number", - "type": "NameExpression", + "meta": { + "quote": undefined, + }, + "optional": false, + "readonly": false, + "right": { + "type": "JsdocTypeName", + "value": "number", }, + "type": "JsdocTypeObjectField", }, { "key": "b", - "type": "FieldType", - "value": { - "name": "string", - "type": "NameExpression", + "meta": { + "quote": undefined, }, + "optional": false, + "readonly": false, + "right": { + "type": "JsdocTypeName", + "value": "string", + }, + "type": "JsdocTypeObjectField", }, ], - "type": "RecordType", + "meta": { + "separator": "comma", + }, + "type": "JsdocTypeObject", }, }, }, @@ -873,17 +875,21 @@ description", "description": null, "getTypeName": [Function], "type": { - "applications": [ + "elements": [ { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, ], - "expression": { - "name": "Array", - "type": "NameExpression", + "left": { + "type": "JsdocTypeName", + "value": "Array", + }, + "meta": { + "brackets": "square", + "dot": false, }, - "type": "TypeApplication", + "type": "JsdocTypeGeneric", }, }, }, @@ -908,17 +914,20 @@ description", "description": null, "getTypeName": [Function], "type": { - "elements": [ - { - "name": "number", - "type": "NameExpression", - }, - { - "name": "boolean", - "type": "NameExpression", - }, - ], - "type": "UnionType", + "element": { + "elements": [ + { + "type": "JsdocTypeName", + "value": "number", + }, + { + "type": "JsdocTypeName", + "value": "boolean", + }, + ], + "type": "JsdocTypeUnion", + }, + "type": "JsdocTypeParenthesis", }, }, }, @@ -943,7 +952,7 @@ description", "description": null, "getTypeName": [Function], "type": { - "type": "AllLiteral", + "type": "JsdocTypeAny", }, }, }, @@ -968,8 +977,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "string", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "string", }, }, }, @@ -994,8 +1003,8 @@ description", "description": null, "getTypeName": [Function], "type": { - "name": "void", - "type": "NameExpression", + "type": "JsdocTypeName", + "value": "void", }, }, }, diff --git a/code/yarn.lock b/code/yarn.lock index 5e19c4ce9845..b9fe9698d8c4 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -5937,10 +5937,9 @@ __metadata: "@storybook/core-common": "workspace:*" "@storybook/preview-api": "workspace:*" "@storybook/types": "workspace:*" - "@types/doctrine": "npm:^0.0.3" - assert: "npm:^2.1.0" babel-plugin-react-docgen: "npm:4.2.1" - doctrine: "npm:^3.0.0" + comment-parser: "npm:^1.4.1" + jsdoc-type-pratt-parser: "npm:^4.0.0" lodash: "npm:^4.17.21" require-from-string: "npm:^2.0.2" typescript: "npm:^5.3.2" @@ -7530,13 +7529,6 @@ __metadata: languageName: node linkType: hard -"@types/doctrine@npm:^0.0.3": - version: 0.0.3 - resolution: "@types/doctrine@npm:0.0.3" - checksum: 566dcdc988c97ff01d14493ceb2223643347f07cf0a88c86cd7cb7c2821cfc837fd39295e6809a29614fdfdc6c4e981408155ca909b2e5da5d947af939b6c966 - languageName: node - linkType: hard - "@types/doctrine@npm:^0.0.9": version: 0.0.9 resolution: "@types/doctrine@npm:0.0.9" @@ -10122,7 +10114,7 @@ __metadata: languageName: node linkType: hard -"assert@npm:^2.0.0, assert@npm:^2.1.0": +"assert@npm:^2.0.0": version: 2.1.0 resolution: "assert@npm:2.1.0" dependencies: @@ -12135,6 +12127,13 @@ __metadata: languageName: node linkType: hard +"comment-parser@npm:^1.4.1": + version: 1.4.1 + resolution: "comment-parser@npm:1.4.1" + checksum: d6c4be3f5be058f98b24f2d557f745d8fe1cc9eb75bebbdccabd404a0e1ed41563171b16285f593011f8b6a5ec81f564fb1f2121418ac5cbf0f49255bf0840dd + languageName: node + linkType: hard + "common-path-prefix@npm:^3.0.0": version: 3.0.0 resolution: "common-path-prefix@npm:3.0.0" @@ -18891,6 +18890,13 @@ __metadata: languageName: node linkType: hard +"jsdoc-type-pratt-parser@npm:^4.0.0": + version: 4.0.0 + resolution: "jsdoc-type-pratt-parser@npm:4.0.0" + checksum: b23ef7bbbe2f56d72630d1c5a233dc9fecaff399063d373c57bef136908c1b05e723dac107177303c03ccf8d75aa51507510b282aa567600477479c5ea0c36d1 + languageName: node + linkType: hard + "jsdom@npm:^23.0.1": version: 23.0.1 resolution: "jsdom@npm:23.0.1" From 849ad4db25510a451af6829a818a70ecc686f9a5 Mon Sep 17 00:00:00 2001 From: Norbert de Langen Date: Tue, 26 Mar 2024 10:45:25 +0100 Subject: [PATCH 06/11] Disable YARN_ENABLE_IMMUTABLE_INSTALLS in CircleCI config --- .circleci/config.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.circleci/config.yml b/.circleci/config.yml index 1cd268cbbe1b..7c0de536a9e7 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -594,6 +594,8 @@ jobs: name: Install dependencies command: yarn install working_directory: test-storybooks/portable-stories-kitchen-sink/<< parameters.directory >> + environment: + YARN_ENABLE_IMMUTABLE_INSTALLS: false - run: name: Run Jest tests command: yarn jest From 395405a71f0394fad166bca184926b1977dc8fc9 Mon Sep 17 00:00:00 2001 From: 43081j <43081j@users.noreply.github.com> Date: Mon, 6 May 2024 17:11:50 +0100 Subject: [PATCH 07/11] test: update argTypes snapshot --- .../stories/docgen-components/jsdoc/argTypes.snapshot | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot b/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot index f3580400f7fb..09c821398dad 100644 --- a/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot +++ b/code/renderers/react/template/stories/docgen-components/jsdoc/argTypes.snapshot @@ -100,7 +100,7 @@ "description": "param with type", "name": "case13", "table": { - "defaultValue": null, + "defaultValue": undefined, "jsDocTags": undefined, "type": { "detail": undefined, @@ -1132,4 +1132,4 @@ description", "required": false, }, }, -} +} \ No newline at end of file From 6f19ef586e7bb21a447352dd033a0663333d5771 Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Fri, 7 Jun 2024 14:09:25 +0200 Subject: [PATCH 08/11] Angular: Add preserveSymlinks option to Storybook builders --- .../frameworks/angular/src/builders/build-storybook/index.ts | 3 +++ .../angular/src/builders/build-storybook/schema.json | 5 +++++ .../frameworks/angular/src/builders/start-storybook/index.ts | 3 +++ .../angular/src/builders/start-storybook/schema.json | 5 +++++ .../angular/src/builders/utils/standalone-options.ts | 1 + 5 files changed, 17 insertions(+) diff --git a/code/frameworks/angular/src/builders/build-storybook/index.ts b/code/frameworks/angular/src/builders/build-storybook/index.ts index 25f7faeb5268..892547aabe80 100644 --- a/code/frameworks/angular/src/builders/build-storybook/index.ts +++ b/code/frameworks/angular/src/builders/build-storybook/index.ts @@ -40,6 +40,7 @@ export type StorybookBuilderOptions = JsonObject & { enableProdMode?: boolean; styles?: StyleElement[]; stylePreprocessorOptions?: StylePreprocessorOptions; + preserveSymlinks?: boolean; assets?: AssetPattern[]; sourceMap?: SourceMapUnion; } & Pick< @@ -102,6 +103,7 @@ const commandBuilder: BuilderHandlerFn = ( assets, previewUrl, sourceMap = false, + preserveSymlinks = false, } = options; const standaloneOptions: StandaloneBuildOptions = { @@ -121,6 +123,7 @@ const commandBuilder: BuilderHandlerFn = ( ...(styles ? { styles } : {}), ...(assets ? { assets } : {}), sourceMap, + preserveSymlinks, }, tsConfig, webpackStatsJson, diff --git a/code/frameworks/angular/src/builders/build-storybook/schema.json b/code/frameworks/angular/src/builders/build-storybook/schema.json index 6b614e574172..dbc2a734417e 100644 --- a/code/frameworks/angular/src/builders/build-storybook/schema.json +++ b/code/frameworks/angular/src/builders/build-storybook/schema.json @@ -19,6 +19,11 @@ "description": "Directory where to store built files.", "default": "storybook-static" }, + "preserveSymlinks": { + "type": "boolean", + "description": "Do not use the real path when resolving modules. If true, symlinks are resolved to their real path, if false, symlinks are resolved to their symlinked path.", + "default": false + }, "configDir": { "type": "string", "description": "Directory where to load Storybook configurations from.", diff --git a/code/frameworks/angular/src/builders/start-storybook/index.ts b/code/frameworks/angular/src/builders/start-storybook/index.ts index 2ecbb63c8a0f..7700cc3acdbc 100644 --- a/code/frameworks/angular/src/builders/start-storybook/index.ts +++ b/code/frameworks/angular/src/builders/start-storybook/index.ts @@ -37,6 +37,7 @@ export type StorybookBuilderOptions = JsonObject & { styles?: StyleElement[]; stylePreprocessorOptions?: StylePreprocessorOptions; assets?: AssetPattern[]; + preserveSymlinks?: boolean; sourceMap?: SourceMapUnion; } & Pick< // makes sure the option exists @@ -118,6 +119,7 @@ const commandBuilder: BuilderHandlerFn = (options, cont statsJson, previewUrl, sourceMap = false, + preserveSymlinks = false, } = options; const standaloneOptions: StandaloneOptions = { @@ -141,6 +143,7 @@ const commandBuilder: BuilderHandlerFn = (options, cont ...(stylePreprocessorOptions ? { stylePreprocessorOptions } : {}), ...(styles ? { styles } : {}), ...(assets ? { assets } : {}), + preserveSymlinks, sourceMap, }, tsConfig, diff --git a/code/frameworks/angular/src/builders/start-storybook/schema.json b/code/frameworks/angular/src/builders/start-storybook/schema.json index 2b98bab9039c..a2eef03e4b08 100644 --- a/code/frameworks/angular/src/builders/start-storybook/schema.json +++ b/code/frameworks/angular/src/builders/start-storybook/schema.json @@ -19,6 +19,11 @@ "type": "string", "description": "The full path for the TypeScript configuration file, relative to the current workspace." }, + "preserveSymlinks": { + "type": "boolean", + "description": "Do not use the real path when resolving modules. If true, symlinks are resolved to their real path, if false, symlinks are resolved to their symlinked path.", + "default": false + }, "port": { "type": "number", "description": "Port to listen on.", diff --git a/code/frameworks/angular/src/builders/utils/standalone-options.ts b/code/frameworks/angular/src/builders/utils/standalone-options.ts index ef73d78f01b5..83fc0090985c 100644 --- a/code/frameworks/angular/src/builders/utils/standalone-options.ts +++ b/code/frameworks/angular/src/builders/utils/standalone-options.ts @@ -18,6 +18,7 @@ export type StandaloneOptions = CLIOptions & stylePreprocessorOptions?: StylePreprocessorOptions; assets?: AssetPattern[]; sourceMap?: SourceMapUnion; + preserveSymlinks?: boolean; }; angularBuilderContext?: BuilderContext | null; tsConfig?: string; From 096c612f93e5ea6bdefef6d2f079fe9690e2e87e Mon Sep 17 00:00:00 2001 From: Valentin Palkovic Date: Fri, 7 Jun 2024 14:47:39 +0200 Subject: [PATCH 09/11] Build: Add --prod flag to be able to create Angular-specific sandboxes in linked mode --- CONTRIBUTING.md | 13 +++++++++++++ scripts/task.ts | 5 +++++ scripts/tasks/compile.ts | 4 ++-- scripts/tasks/sandbox-parts.ts | 18 ++++++++++++------ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index f8ae3a42c13c..cab252f7d26e 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -61,6 +61,19 @@ yarn build --watch react core-server api addon-docs yarn task --task dev --template --start-from=publish ``` +### Making code changes when working on Angular-specific code + +If you are working on Angular-specific code, you will need to append `--prod` to the above mentioned commands to ensure that the Angular compiler is able to pick up the changes appropriately and doesn't fail. This will build all the packages in production mode. + +```sh +yarn task --prod +``` + +```bash +cd code +yarn build --prod --watch angular core-server api addon-docs +``` + ## Contributing to Storybook For further advice on how to contribute, please refer to our [NEW contributing guide on the Storybook website](https://storybook.js.org/docs/contribute). diff --git a/scripts/task.ts b/scripts/task.ts index 1247cc89351a..2d1225ddbe6c 100644 --- a/scripts/task.ts +++ b/scripts/task.ts @@ -157,6 +157,11 @@ export const options = createOptions({ inverse: true, promptType: false, }, + prod: { + type: 'boolean', + description: 'Build code for production', + promptType: false, + }, dryRun: { type: 'boolean', description: "Don't execute commands, just list them (dry run)?", diff --git a/scripts/tasks/compile.ts b/scripts/tasks/compile.ts index af220b9f27a4..0275a363e222 100644 --- a/scripts/tasks/compile.ts +++ b/scripts/tasks/compile.ts @@ -33,9 +33,9 @@ export const compile: Task = { return false; } }, - async run({ codeDir }, { link, dryRun, debug }) { + async run({ codeDir }, { link, dryRun, debug, prod }) { return exec( - link ? linkCommand : noLinkCommand, + link && !prod ? linkCommand : noLinkCommand, { cwd: codeDir }, { startMessage: '🥾 Bootstrapping', diff --git a/scripts/tasks/sandbox-parts.ts b/scripts/tasks/sandbox-parts.ts index b80c1d5e734b..8bc2566a21a7 100644 --- a/scripts/tasks/sandbox-parts.ts +++ b/scripts/tasks/sandbox-parts.ts @@ -620,18 +620,24 @@ export async function setImportMap(cwd: string) { await writeJson(join(cwd, 'package.json'), packageJson, { spaces: 2 }); } -/** - * Sets compodoc option in angular.json projects to false. We have to generate compodoc - * manually to avoid symlink issues related to the template-stories folder. - * In a second step a docs:json script is placed into the package.json to generate the - * Compodoc documentation.json, which respects symlinks - * */ async function prepareAngularSandbox(cwd: string, templateName: string) { const angularJson = await readJson(join(cwd, 'angular.json')); Object.keys(angularJson.projects).forEach((projectName: string) => { + /** + * Sets compodoc option in angular.json projects to false. We have to generate compodoc + * manually to avoid symlink issues related to the template-stories folder. + * In a second step a docs:json script is placed into the package.json to generate the + * Compodoc documentation.json, which respects symlinks + */ angularJson.projects[projectName].architect.storybook.options.compodoc = false; angularJson.projects[projectName].architect['build-storybook'].options.compodoc = false; + /** + * Sets preserveSymlinks option in angular.json projects to true. This is necessary to + * respect symlinks so that Angular doesn't complain about wrong types in @storybook/* packages + */ + angularJson.projects[projectName].architect.storybook.options.preserveSymlinks = true; + angularJson.projects[projectName].architect['build-storybook'].options.preserveSymlinks = true; }); await writeJson(join(cwd, 'angular.json'), angularJson, { spaces: 2 }); From e20977a8ead8a27551150fab56a086c76b9ce66f Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 10 Jun 2024 08:21:08 +0800 Subject: [PATCH 10/11] Update yarn.lock --- code/yarn.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/yarn.lock b/code/yarn.lock index d825a7b40354..6e6ecb7b69a6 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -501,7 +501,7 @@ __metadata: languageName: node linkType: hard -"@babel/helper-create-class-features-plugin@npm:^7.18.6, @babel/helper-create-class-features-plugin@npm:^7.21.0, @babel/helper-create-class-features-plugin@npm:^7.24.0, @babel/helper-create-class-features-plugin@npm:^7.24.1, @babel/helper-create-class-features-plugin@npm:^7.24.4": +"@babel/helper-create-class-features-plugin@npm:^7.18.6, @babel/helper-create-class-features-plugin@npm:^7.21.0, @babel/helper-create-class-features-plugin@npm:^7.24.1, @babel/helper-create-class-features-plugin@npm:^7.24.4": version: 7.24.4 resolution: "@babel/helper-create-class-features-plugin@npm:7.24.4" dependencies: From c388109586094350ccea098a81a566b2a737c672 Mon Sep 17 00:00:00 2001 From: Michael Shilman Date: Mon, 10 Jun 2024 16:21:29 +0800 Subject: [PATCH 11/11] Restore back-compat code --- code/lib/manager-api/src/lib/stories.test.ts | 3 +++ code/lib/manager-api/src/lib/stories.ts | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/code/lib/manager-api/src/lib/stories.test.ts b/code/lib/manager-api/src/lib/stories.test.ts index 1107f23bb8d4..f8a7c5882cfb 100644 --- a/code/lib/manager-api/src/lib/stories.test.ts +++ b/code/lib/manager-api/src/lib/stories.test.ts @@ -120,6 +120,9 @@ describe('transformStoryIndexV3toV4', () => { "docsOnly": true, }, "storiesImports": [], + "tags": [ + "stories-mdx", + ], "title": "Story 1", "type": "docs", }, diff --git a/code/lib/manager-api/src/lib/stories.ts b/code/lib/manager-api/src/lib/stories.ts index fc8f866c575a..db8cc4c1efb3 100644 --- a/code/lib/manager-api/src/lib/stories.ts +++ b/code/lib/manager-api/src/lib/stories.ts @@ -66,6 +66,7 @@ export const transformSetStoriesStoryDataToPreparedStoryIndex = ( if (docsOnly) { acc[id] = { type: 'docs', + tags: ['stories-mdx'], storiesImports: [], ...base, }; @@ -122,7 +123,7 @@ export const transformStoryIndexV3toV4 = (index: StoryIndexV3): API_PreparedStor } acc[entry.id] = { type, - ...(type === 'docs' && { storiesImports: [] }), + ...(type === 'docs' && { tags: ['stories-mdx'], storiesImports: [] }), ...entry, };