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

Upgrade docs-mdx to fix yarn pnp #19835

Merged
merged 8 commits into from
Nov 21, 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/lib/core-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@storybook/core-events": "7.0.0-alpha.49",
"@storybook/csf": "0.0.2-next.5",
"@storybook/csf-tools": "7.0.0-alpha.49",
"@storybook/docs-mdx": "0.0.1-canary.12433cf.0",
"@storybook/docs-mdx": "next",
"@storybook/node-logger": "7.0.0-alpha.49",
"@storybook/store": "7.0.0-alpha.49",
"@storybook/telemetry": "7.0.0-alpha.49",
Expand Down
23 changes: 0 additions & 23 deletions code/lib/core-server/src/utils/StoryIndexGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@ jest.mock('@storybook/csf', () => {
};
});

// FIXME: can't figure out how to import ESM
jest.mock('@storybook/docs-mdx', async () => ({
analyze(content: string) {
const importMatches = content.matchAll(/'(.[^']*\.stories)'/g);
const imports = Array.from(importMatches).map((match) => match[1]);
const title = content.match(/title=['"](.*)['"]/)?.[1];
const name = content.match(/name=['"](.*)['"]/)?.[1];
const ofMatch = content.match(/of=\{(.*)\}/)?.[1];
const isTemplate = content.match(/isTemplate/);
const tags = ['mdx'];
return { title, name, tags, imports, of: ofMatch && imports.length && imports[0], isTemplate };
},
}));

jest.mock('@storybook/node-logger');

const toIdMock = toId as jest.Mock<ReturnType<typeof toId>>;
Expand Down Expand Up @@ -504,7 +490,6 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"mdx",
"docs",
],
"title": "A",
Expand Down Expand Up @@ -618,7 +603,6 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"mdx",
"docs",
],
"title": "A",
Expand All @@ -633,7 +617,6 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"mdx",
"docs",
],
"title": "A",
Expand All @@ -657,7 +640,6 @@ describe('StoryIndexGenerator', () => {
"standalone": true,
"storiesImports": Array [],
"tags": Array [
"mdx",
"docs",
],
"title": "docs2/Yabbadabbadooo",
Expand All @@ -670,7 +652,6 @@ describe('StoryIndexGenerator', () => {
"standalone": true,
"storiesImports": Array [],
"tags": Array [
"mdx",
shilman marked this conversation as resolved.
Show resolved Hide resolved
"docs",
],
"title": "NoTitle",
Expand Down Expand Up @@ -761,7 +742,6 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"mdx",
"docs",
],
"title": "A",
Expand All @@ -776,7 +756,6 @@ describe('StoryIndexGenerator', () => {
"./src/A.stories.js",
],
"tags": Array [
"mdx",
"docs",
],
"title": "A",
Expand All @@ -800,7 +779,6 @@ describe('StoryIndexGenerator', () => {
"standalone": true,
"storiesImports": Array [],
"tags": Array [
"mdx",
"docs",
],
"title": "docs2/Yabbadabbadooo",
Expand All @@ -813,7 +791,6 @@ describe('StoryIndexGenerator', () => {
"standalone": true,
"storiesImports": Array [],
"tags": Array [
"mdx",
"docs",
],
"title": "NoTitle",
Expand Down
9 changes: 1 addition & 8 deletions code/lib/core-server/src/utils/StoryIndexGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { normalizeStoryPath } from '@storybook/core-common';
import { logger } from '@storybook/node-logger';
import { getStorySortParameter, NoMetaError } from '@storybook/csf-tools';
import { toId } from '@storybook/csf';
import { analyze } from '@storybook/docs-mdx';

/** A .mdx file will produce a "standalone" docs entry */
type DocsCacheEntry = Addon_StandaloneDocsIndexEntry;
Expand Down Expand Up @@ -266,14 +267,6 @@ export class StoryIndexGenerator {
const normalizedPath = normalizeStoryPath(relativePath);
const importPath = slash(normalizedPath);

// This `await require(...)` is a bit of a hack. It's necessary because
// `docs-mdx` depends on ESM code, which must be asynchronously imported
// to be used in CJS. Unfortunately, we cannot use `import()` here, because
// it will be transpiled down to `require()` by Babel. So instead, we require
// a CJS export from `@storybook/docs-mdx` that does the `async import` for us.

// eslint-disable-next-line global-require
const { analyze } = await require('@storybook/docs-mdx');
const content = await fs.readFile(absolutePath, 'utf8');
const result: {
title?: ComponentTitle;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as AStories from '../src/A.stories';

<!-- This is the same name as a story -->
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is invalid MDX but we never tested it because of the mocking

{/* This is the same name as a story */}

<Meta of={AStories} name="Story One" />

Expand Down
75 changes: 34 additions & 41 deletions code/lib/core-server/src/utils/stories-json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,6 @@ import { StoryIndexGenerator } from './StoryIndexGenerator';
jest.mock('watchpack');
jest.mock('lodash/debounce');

// FIXME: can't figure out how to import ESM
jest.mock('@storybook/docs-mdx', async () => ({
analyze(content: string) {
const importMatches = content.matchAll(/'(.[^']*\.stories)'/g);
const imports = Array.from(importMatches).map((match) => match[1]);
const title = content.match(/title=['"](.*)['"]/)?.[1];
const ofMatch = content.match(/of=\{(.*)\}/)?.[1];
return { title, imports, of: ofMatch && imports.length && imports[0] };
},
}));

const workingDir = path.join(__dirname, '__mockdata__');
const normalizedStories = [
normalizeStoriesEntry(
Expand Down Expand Up @@ -142,6 +131,20 @@ describe('useStoriesJson', () => {
"title": "A",
"type": "docs",
},
"a--second-docs": Object {
"id": "a--second-docs",
"importPath": "./src/docs2/SecondMetaOf.mdx",
"name": "Second Docs",
"standalone": true,
"storiesImports": Array [
"./src/A.stories.js",
],
"tags": Array [
"docs",
],
"title": "A",
"type": "docs",
},
"a--story-one": Object {
"id": "a--story-one",
"importPath": "./src/A.stories.js",
Expand Down Expand Up @@ -187,18 +190,6 @@ describe('useStoriesJson', () => {
"title": "docs2/NoTitle",
"type": "docs",
},
"docs2-template--docs": Object {
"id": "docs2-template--docs",
"importPath": "./src/docs2/Template.mdx",
"name": "docs",
"standalone": true,
"storiesImports": Array [],
"tags": Array [
"docs",
],
"title": "docs2/Template",
"type": "docs",
},
"docs2-yabbadabbadooo--docs": Object {
"id": "docs2-yabbadabbadooo--docs",
"importPath": "./src/docs2/Title.mdx",
Expand Down Expand Up @@ -311,6 +302,26 @@ describe('useStoriesJson', () => {
],
"title": "A",
},
"a--second-docs": Object {
"id": "a--second-docs",
"importPath": "./src/docs2/SecondMetaOf.mdx",
"kind": "A",
"name": "Second Docs",
"parameters": Object {
"__id": "a--second-docs",
"docsOnly": true,
"fileName": "./src/docs2/SecondMetaOf.mdx",
},
"standalone": true,
"storiesImports": Array [
"./src/A.stories.js",
],
"story": "Second Docs",
"tags": Array [
"docs",
],
"title": "A",
},
"a--story-one": Object {
"id": "a--story-one",
"importPath": "./src/A.stories.js",
Expand Down Expand Up @@ -380,24 +391,6 @@ describe('useStoriesJson', () => {
],
"title": "docs2/NoTitle",
},
"docs2-template--docs": Object {
"id": "docs2-template--docs",
"importPath": "./src/docs2/Template.mdx",
"kind": "docs2/Template",
"name": "docs",
"parameters": Object {
"__id": "docs2-template--docs",
"docsOnly": true,
"fileName": "./src/docs2/Template.mdx",
},
"standalone": true,
"storiesImports": Array [],
"story": "docs",
"tags": Array [
"docs",
],
"title": "docs2/Template",
},
"docs2-yabbadabbadooo--docs": Object {
"id": "docs2-yabbadabbadooo--docs",
"importPath": "./src/docs2/Title.mdx",
Expand Down
Loading