Skip to content

Commit

Permalink
Support MDXv2
Browse files Browse the repository at this point in the history
  • Loading branch information
joshwooding committed May 14, 2022
1 parent 7e5af52 commit a5e1cc4
Show file tree
Hide file tree
Showing 7 changed files with 1,269 additions and 391 deletions.
202 changes: 101 additions & 101 deletions examples/react-ts/stories/Introduction.stories.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,109 +10,109 @@ import StackAlt from './assets/stackalt.svg';

<Meta title="Introduction" />

<style>{`
.subheading {
--mediumdark: '#999999';
font-weight: 900;
font-size: 13px;
color: #999;
letter-spacing: 6px;
line-height: 24px;
text-transform: uppercase;
margin-bottom: 12px;
margin-top: 40px;
}
.link-list {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
row-gap: 10px;
}
@media (min-width: 620px) {
.link-list {
row-gap: 20px;
column-gap: 20px;
grid-template-columns: 1fr 1fr;
<style>
{`
.subheading {
--mediumdark: '#999999';
font-weight: 900;
font-size: 13px;
color: #999;
letter-spacing: 6px;
line-height: 24px;
text-transform: uppercase;
margin-bottom: 12px;
margin-top: 40px;
}
}
@media all and (-ms-high-contrast:none) {
.link-list {
display: -ms-grid;
-ms-grid-columns: 1fr 1fr;
-ms-grid-rows: 1fr 1fr;
}
}
.link-item {
display: block;
padding: 20px 30px 20px 15px;
border: 1px solid #00000010;
border-radius: 5px;
transition: background 150ms ease-out, border 150ms ease-out, transform 150ms ease-out;
color: #333333;
display: flex;
align-items: flex-start;
}
.link-item:hover {
border-color: #1EA7FD50;
transform: translate3d(0, -3px, 0);
box-shadow: rgba(0, 0, 0, 0.08) 0 3px 10px 0;
}
.link-item:active {
border-color: #1EA7FD;
transform: translate3d(0, 0, 0);
}
.link-item strong {
font-weight: 700;
display: block;
margin-bottom: 2px;
}
.link-item img {
height: 40px;
width: 40px;
margin-right: 15px;
flex: none;
}
.link-item span {
font-size: 14px;
line-height: 20px;
}
.tip {
display: inline-block;
border-radius: 1em;
font-size: 11px;
line-height: 12px;
font-weight: 700;
background: #E7FDD8;
color: #66BF3C;
padding: 4px 12px;
margin-right: 10px;
vertical-align: top;
}
.tip-wrapper {
font-size: 13px;
line-height: 20px;
margin-top: 40px;
margin-bottom: 40px;
}
.tip-wrapper code {
font-size: 12px;
display: inline-block;
}
`}</style>
.link-list {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 1fr 1fr;
row-gap: 10px;
}
@media (min-width: 620px) {
.link-list {
row-gap: 20px;
column-gap: 20px;
grid-template-columns: 1fr 1fr;
}
}
@media all and (-ms-high-contrast:none) {
.link-list {
display: -ms-grid;
-ms-grid-columns: 1fr 1fr;
-ms-grid-rows: 1fr 1fr;
}
}
.link-item {
display: block;
padding: 20px 30px 20px 15px;
border: 1px solid #00000010;
border-radius: 5px;
transition: background 150ms ease-out, border 150ms ease-out, transform 150ms ease-out;
color: #333333;
display: flex;
align-items: flex-start;
}
.link-item:hover {
border-color: #1EA7FD50;
transform: translate3d(0, -3px, 0);
box-shadow: rgba(0, 0, 0, 0.08) 0 3px 10px 0;
}
.link-item:active {
border-color: #1EA7FD;
transform: translate3d(0, 0, 0);
}
.link-item strong {
font-weight: 700;
display: block;
margin-bottom: 2px;
}
.link-item img {
height: 40px;
width: 40px;
margin-right: 15px;
flex: none;
}
.link-item span {
font-size: 14px;
line-height: 20px;
}
.tip {
display: inline-block;
border-radius: 1em;
font-size: 11px;
line-height: 12px;
font-weight: 700;
background: #E7FDD8;
color: #66BF3C;
padding: 4px 12px;
margin-right: 10px;
vertical-align: top;
}
.tip-wrapper {
font-size: 13px;
line-height: 20px;
margin-top: 40px;
margin-bottom: 40px;
}
.tip-wrapper code {
font-size: 12px;
display: inline-block;
}
`}
</style>

# Welcome to Storybook

Expand Down
24 changes: 0 additions & 24 deletions packages/builder-vite/mdx-plugin.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/builder-vite/optimizeDeps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const INCLUDE_CANDIDATES = [
'react-is',
'react-textarea-autosize',
'react',
'react/jsx-runtime',
'refractor/core',
'refractor/lang/bash.js',
'refractor/lang/css.js',
Expand Down
13 changes: 9 additions & 4 deletions packages/builder-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
"homepage": "https://github.com/storybookjs/builder-vite/#readme",
"dependencies": {
"@joshwooding/vite-plugin-react-docgen-typescript": "0.0.4",
"@mdx-js/mdx": "^1.6.22",
"@storybook/csf-tools": "^6.4.3",
"@storybook/mdx1-csf": "canary",
"@storybook/source-loader": "^6.4.3",
"@vitejs/plugin-react": "^1.0.8",
"ast-types": "^0.14.2",
Expand All @@ -24,20 +23,26 @@
"glob-promise": "^4.2.0",
"magic-string": "^0.26.1",
"react-docgen": "^6.0.0-alpha.0",
"slash": "^3.0.0",
"vite-plugin-mdx": "^3.5.6"
"slash": "^3.0.0"
},
"devDependencies": {
"@storybook/mdx2-csf": "canary",
"@types/express": "^4.17.13",
"@types/node": "^17.0.23",
"vue-docgen-api": "^4.40.0"
},
"peerDependencies": {
"@storybook/core-common": ">=6.4.3 || >=6.5.0-alpha.0",
"@storybook/mdx2-csf": "*",
"@storybook/node-logger": ">=6.4.3 || >=6.5.0-alpha.0",
"@storybook/source-loader": ">=6.4.3 || >=6.5.0-alpha.0",
"vite": ">=2.6.7"
},
"peerDependenciesMeta": {
"@storybook/mdx2-csf": {
"optional": true
}
},
"publishConfig": {
"access": "public"
}
Expand Down
71 changes: 71 additions & 0 deletions packages/builder-vite/plugins/mdx-plugin.ts
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: '' } };
}
},
};
}
4 changes: 2 additions & 2 deletions packages/builder-vite/vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TypescriptConfig } from '@storybook/core-common';
import { mockCoreJs } from './mock-core-js';
import { codeGeneratorPlugin } from './code-generator-plugin';
import { injectExportOrderPlugin } from './inject-export-order-plugin';
import { mdxPlugin } from './mdx-plugin';
import { mdxPlugin } from './plugins/mdx-plugin';
import { noFouc } from './plugins/no-fouc';
import { sourceLoaderPlugin } from './source-loader-plugin';

Expand Down Expand Up @@ -58,7 +58,7 @@ export async function pluginConfig(options: ExtendedOptions, _type: PluginConfig
codeGeneratorPlugin(options),
mockCoreJs(),
sourceLoaderPlugin(options),
mdxPlugin(),
mdxPlugin(options),
noFouc(),
injectExportOrderPlugin,
] as Plugin[];
Expand Down
Loading

0 comments on commit a5e1cc4

Please sign in to comment.