-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e5af52
commit a5e1cc4
Showing
7 changed files
with
1,269 additions
and
391 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import type { Options } from '@storybook/core-common'; | ||
import { Plugin, transformWithEsbuild } from 'vite'; | ||
|
||
const isStorybookMdx = (id: string) => id.endsWith('stories.mdx') || id.endsWith('story.mdx'); | ||
|
||
/** | ||
* Storybook uses two different loaders when dealing with MDX: | ||
* | ||
* - *stories.mdx and *story.mdx are compiled with the CSF compiler | ||
* - *.mdx are compiled with the MDX compiler directly | ||
* | ||
* @see https://github.com/storybookjs/storybook/blob/next/addons/docs/docs/recipes.md#csf-stories-with-arbitrary-mdx | ||
*/ | ||
export function mdxPlugin(options: Options): Plugin { | ||
const { features } = options; | ||
|
||
if (features?.previewMdx2) { | ||
return { | ||
name: 'storybook-vite-mdx2-plugin', | ||
enforce: 'pre', | ||
async transform(code, id, ssr) { | ||
if (id.endsWith('.mdx')) { | ||
// @ts-ignore | ||
const { compile } = await import('@storybook/mdx2-csf'); | ||
let mdxCode = String(await compile(code, { skipCsf: !isStorybookMdx(id) })); | ||
|
||
if (isStorybookMdx(id)) { | ||
const transformResult = ( | ||
await transformWithEsbuild(mdxCode, id, { | ||
loader: 'jsx', | ||
target: 'es2019', | ||
}) | ||
).code; | ||
|
||
mdxCode = ` | ||
import React from 'react'; | ||
${transformResult} | ||
`; | ||
} | ||
|
||
return { code: mdxCode, map: { mappings: '' } }; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
name: 'storybook-vite-mdx1-plugin', | ||
enforce: 'pre', | ||
async transform(code, id, ssr) { | ||
if (id.endsWith('.mdx')) { | ||
const { compile } = await import('@storybook/mdx1-csf'); | ||
const mdxCode = String(await compile(code, { skipCsf: !isStorybookMdx(id) })); | ||
|
||
const modifiedCode = ` | ||
/* @jsx mdx */ | ||
import React from 'react'; | ||
import { mdx } from '@mdx-js/react'; | ||
${mdxCode} | ||
`; | ||
|
||
const result = await transformWithEsbuild(modifiedCode, id, { | ||
loader: 'jsx', | ||
target: 'es2019', | ||
}); | ||
|
||
return { code: result.code, map: { mappings: '' } }; | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.