Skip to content

Commit

Permalink
Merge pull request #19563 from joshwooding/tidy-up-vite-mdx-plugin
Browse files Browse the repository at this point in the history
Tidy up vite mdx-plugin
IanVS authored Oct 24, 2022
2 parents 01e1e34 + 32df5e2 commit 26973f2
Showing 2 changed files with 5 additions and 17 deletions.
20 changes: 4 additions & 16 deletions code/lib/builder-vite/src/plugins/mdx-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import type { Options } from '@storybook/core-common';
import type { Plugin } from 'vite';
import { createFilter } from 'vite';

const isStorybookMdx = (id: string) => id.endsWith('stories.mdx') || id.endsWith('story.mdx');

function injectRenderer(code: string, mdx2: boolean) {
if (mdx2) {
return `
function injectRenderer(code: string) {
return `
import React from 'react';
${code}
`;
}

return `
/* @jsx mdx */
import React from 'react';
import { mdx } from '@mdx-js/react';
${code}
`;
}

/**
@@ -28,9 +18,7 @@ function injectRenderer(code: string, mdx2: boolean) {
*
* @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;

export function mdxPlugin(): Plugin {
let reactRefresh: Plugin | undefined;
const include = /\.mdx?$/;
const filter = createFilter(include);
@@ -60,7 +48,7 @@ export function mdxPlugin(options: Options): Plugin {

const mdxCode = String(await compile(src, { skipCsf: !isStorybookMdx(id) }));

const modifiedCode = injectRenderer(mdxCode, true);
const modifiedCode = injectRenderer(mdxCode);

// Hooks in recent rollup versions can be functions or objects, and though react hasn't changed, the typescript defs have
const rTransform = reactRefresh?.transform;
2 changes: 1 addition & 1 deletion code/lib/builder-vite/src/vite-config.ts
Original file line number Diff line number Diff line change
@@ -79,7 +79,7 @@ export async function pluginConfig(options: ExtendedOptions) {
const plugins = [
codeGeneratorPlugin(options),
// sourceLoaderPlugin(options),
mdxPlugin(options),
mdxPlugin(),
injectExportOrderPlugin,
stripStoryHMRBoundary(),
{

0 comments on commit 26973f2

Please sign in to comment.