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

Tidy up vite mdx-plugin #19563

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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}
`;
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion code/lib/builder-vite/src/vite-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function pluginConfig(options: ExtendedOptions) {
const plugins = [
codeGeneratorPlugin(options),
// sourceLoaderPlugin(options),
mdxPlugin(options),
mdxPlugin(),
injectExportOrderPlugin,
stripStoryHMRBoundary(),
{
Expand Down