Skip to content

Commit

Permalink
Merge pull request #2701 from modernweb-dev/feat/storybook-builder-mdx
Browse files Browse the repository at this point in the history
feat(storybook-builder): support MDX and autodocs
  • Loading branch information
bashmish committed Apr 16, 2024
2 parents 3032b41 + e30da4a commit e257d08
Show file tree
Hide file tree
Showing 16 changed files with 265 additions and 113 deletions.
5 changes: 5 additions & 0 deletions .changeset/eighty-chicken-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@web/storybook-builder': patch
---

support MDX and autodocs
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions packages/storybook-builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@rollup/plugin-node-resolve": "^15.1.0",
"@rollup/pluginutils": "^5.0.2",
"@storybook/core-common": "^7.0.0",
"@storybook/mdx2-csf": "^1.0.0",
"@storybook/node-logger": "^7.0.0",
"@storybook/preview": "^7.0.0",
"@web/dev-server": "^0.4.0",
Expand All @@ -65,6 +66,8 @@
"glob-promise": "^6.0.3",
"lodash-es": "^4.17.21",
"path-browserify": "^1.0.1",
"remark-external-links": "^8.0.0",
"remark-slug": "^6.0.0",
"rollup": "^4.4.1",
"rollup-plugin-external-globals": "^0.9.0",
"slash": "^5.1.0"
Expand Down
8 changes: 7 additions & 1 deletion packages/storybook-builder/src/generate-stories-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ async function toImportFn(stories: string[]) {
logger.warn(`Cannot process ${ext} file with storyStoreV7: ${relativePath}`);
}

return ` '${toImportPath(relativePath)}': () => import('${process.cwd()}/${relativePath}')`;
const importPath = toImportPath(relativePath);
let actualPath = `${process.cwd()}/${relativePath}`;
if (actualPath.endsWith('.mdx')) {
actualPath = `${actualPath}.js`;
}

return ` '${importPath}': () => import('${actualPath}')`;
});

return `
Expand Down
4 changes: 4 additions & 0 deletions packages/storybook-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ import rollupPluginExternalGlobals from 'rollup-plugin-external-globals';
import { generateIframeHtml } from './generate-iframe-html.js';
import { getNodeModuleDir } from './get-node-module-dir.js';
import { readFileConfig } from './read-file-config.js';
import { rollupPluginMdx } from './rollup-plugin-mdx.js';
import {
PREBUNDLED_MODULES_DIR,
rollupPluginPrebundleModules,
} from './rollup-plugin-prebundle-modules.js';
import { rollupPluginStorybookBuilder } from './rollup-plugin-storybook-builder.js';

const wdsPluginExternalGlobals = fromRollup(rollupPluginExternalGlobals);
const wdsPluginMdx = fromRollup(rollupPluginMdx);
const wdsPluginPrebundleModules = fromRollup(rollupPluginPrebundleModules);
const wdsPluginStorybookBuilder = fromRollup(rollupPluginStorybookBuilder);

Expand Down Expand Up @@ -76,6 +78,7 @@ export const start: WdsBuilder['start'] = async ({ startTime, options, router, s
},
wdsPluginPrebundleModules(env),
wdsPluginStorybookBuilder(options),
wdsPluginMdx(options),
wdsPluginExternalGlobals(globalsNameReferenceMap || globals),
],
};
Expand Down Expand Up @@ -149,6 +152,7 @@ export const build: WdsBuilder['build'] = async ({ startTime, options }) => {
rollupPluginNodeResolve(),
rollupPluginPrebundleModules(env),
rollupPluginStorybookBuilder(options),
rollupPluginMdx(options),
rollupPluginExternalGlobals(globalsNameReferenceMap || globals),
],
};
Expand Down
54 changes: 54 additions & 0 deletions packages/storybook-builder/src/rollup-plugin-mdx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { compile } from '@storybook/mdx2-csf';
import type { Options } from '@storybook/types';
import { readFile } from 'fs-extra';
import remarkExternalLinks from 'remark-external-links';
import remarkSlug from 'remark-slug';
import type { Plugin } from 'rollup';

export function rollupPluginMdx(storybookOptions: Options): Plugin {
let mdxPluginOptions: Record<string, any>;
let jsxOptions: Record<string, any>;

return {
name: 'rollup-plugin-mdx',

async buildStart() {
({ mdxPluginOptions, jsxOptions } = await storybookOptions.presets.apply<Record<string, any>>(
'options',
{},
));
},

async resolveId(id) {
if (id.endsWith('.mdx.js')) {
return id;
}
},

async load(id) {
if (!id.endsWith('.mdx.js')) return;

const mdxPath = id.replace(/\.js$/, '');
const mdxCode = await readFile(mdxPath, 'utf8');

const mdxLoaderOptions = await storybookOptions.presets.apply('mdxLoaderOptions', {
...mdxPluginOptions,
mdxCompileOptions: {
providerImportSource: '@mdx-js/react',
...mdxPluginOptions?.mdxCompileOptions,
remarkPlugins: [remarkSlug, remarkExternalLinks].concat(
mdxPluginOptions?.mdxCompileOptions?.remarkPlugins ?? [],
),
},
jsxOptions,
});

const jsCode = compile(mdxCode, {
skipCsf: true,
...mdxLoaderOptions,
});

return jsCode;
},
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export function rollupPluginPrebundleModules(env: Record<string, string>): Plugi
// builder-vite bundles different dependencies for performance reasons
// we aim only at browserifying NodeJS dependencies (CommonJS/process.env/...)
export const CANDIDATES = [
/* for different addons built with React and for MDX */
'@storybook/react-dom-shim', // needs special resolution
'react',
process.env.NODE_ENV === 'production' ? 'react/jsx-runtime' : 'react/jsx-dev-runtime',
'react-dom',

/* for different packages */
'tiny-invariant',

Expand All @@ -92,7 +98,6 @@ export const CANDIDATES = [
'@testing-library/user-event',

/* for @storybook/addon-docs */
'@storybook/react-dom-shim', // needs special resolution
'color-convert',
'doctrine',
'lodash/cloneDeep.js',
Expand All @@ -101,8 +106,6 @@ export const CANDIDATES = [
'lodash/throttle.js',
'lodash/uniq.js',
'memoizerific',
'react',
'react-dom',
'tocbot',

/* for @storybook/addon-a11y */
Expand Down
1 change: 1 addition & 0 deletions packages/storybook-framework-web-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@storybook/addon-essentials": "^7.0.0",
"@storybook/addon-interactions": "^7.0.0",
"@storybook/addon-links": "^7.0.0",
"@storybook/blocks": "^7.0.0",
"@storybook/types": "^7.0.0",
"@web/dev-server": "^0.4.0",
"storybook": "^7.0.0"
Expand Down
Loading

0 comments on commit e257d08

Please sign in to comment.