Skip to content

Commit

Permalink
Addon Docs: Dynamically import rehype
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinpalkovic committed Nov 5, 2024
1 parent c1a8779 commit fd7f82f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
5 changes: 3 additions & 2 deletions code/addons/docs/src/plugins/mdx-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { dirname, join } from 'node:path';
import type { Options } from 'storybook/internal/types';

import { createFilter } from '@rollup/pluginutils';
import rehypeExternalLinks from 'rehype-external-links';
import rehypeSlug from 'rehype-slug';
import type { Plugin } from 'vite';

import type { CompileOptions } from '../compiler';
Expand All @@ -24,6 +22,9 @@ export async function mdxPlugin(options: Options): Promise<Plugin> {
const presetOptions = await presets.apply<Record<string, any>>('options', {});
const mdxPluginOptions = presetOptions?.mdxPluginOptions as CompileOptions;

const rehypeSlug = (await import('rehype-slug')).default;
const rehypeExternalLinks = (await import('rehype-external-links')).default;

return {
name: 'storybook:mdx-plugin',
enforce: 'pre',
Expand Down
9 changes: 6 additions & 3 deletions code/addons/docs/src/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import type { DocsOptions, Options, PresetProperty } from 'storybook/internal/ty

import type { CsfPluginOptions } from '@storybook/csf-plugin';

import rehypeExternalLinks from 'rehype-external-links';
import rehypeSlug from 'rehype-slug';

import type { CompileOptions } from './compiler';

/**
Expand Down Expand Up @@ -42,6 +39,9 @@ async function webpack(

const { csfPluginOptions = {}, mdxPluginOptions = {} } = options;

const rehypeSlug = (await import('rehype-slug')).default;
const rehypeExternalLinks = (await import('rehype-external-links')).default;

const mdxLoaderOptions: CompileOptions = await options.presets.apply('mdxLoaderOptions', {
...mdxPluginOptions,
mdxCompileOptions: {
Expand Down Expand Up @@ -175,6 +175,9 @@ export const viteFinal = async (config: any, options: Options) => {
const { plugins = [] } = config;
const { mdxPlugin } = await import('./plugins/mdx-plugin');

const rehypeSlug = (await import('rehype-slug')).default;
const rehypeExternalLinks = (await import('rehype-external-links')).default;

// Use the resolvedReact preset to alias react and react-dom to either the users version or the version shipped with addon-docs
const { react, reactDom, mdx } = await getResolvedReact(options);

Expand Down

0 comments on commit fd7f82f

Please sign in to comment.