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

[@vinxi/plugin-mdx] MDX v3 compat #266

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

birkskyum
Copy link
Contributor

@birkskyum birkskyum commented Mar 24, 2024

Add MDX v3 as a possible peerDependency.

The React SPA/MDX example is updated to demonstrate this functionality.

Copy link

codesandbox bot commented Mar 24, 2024

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

Copy link

changeset-bot bot commented Mar 24, 2024

🦋 Changeset detected

Latest commit: 2e50739

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@vinxi/plugin-mdx Minor
react-spa-mdx Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Mar 24, 2024

Someone is attempting to deploy a commit to a Personal Account owned by @nksaraf on Vercel.

@nksaraf first needs to authorize it.

@birkskyum birkskyum changed the title MDX v3 compat [@vinxi/plugin-mdx] MDX v3 compat Mar 24, 2024
@milomg
Copy link

milomg commented Jun 2, 2024

I wonder if a plugin like the following would make sense as a replacement? This seems to be the recommendation for how to use MDX in vite these days: vite-plugin-mdx.

I had to slightly tweak https://github.com/mdx-js/mdx/blob/main/packages/rollup/lib/index.js to trim the query param but then everything basically seemed to work: path = path.replace(/\?[^/.]*$/, '')

import {createFormatAwareProcessors} from '@mdx-js/mdx/internal-create-format-aware-processors'
import {createFilter} from '@rollup/pluginutils'
import {SourceMapGenerator} from 'source-map'
import {VFile} from 'vfile'

/**
 * Plugin to compile MDX w/ rollup.
 *
 * @param {Readonly<Options> | null | undefined} [options]
 *   Configuration (optional).
 * @return {Plugin}
 *   Rollup plugin.
 */
export function rollup(options) {
  const {exclude, include, ...rest} = options || {}
  /** @type {FormatAwareProcessors} */
  let formatAwareProcessors
  const filter = createFilter(include, exclude)

  return {
    name: '@mdx-js/rollup',
    enforce: 'pre',
    config(config, env) {
      formatAwareProcessors = createFormatAwareProcessors({
        SourceMapGenerator,
        development: env.mode === 'development',
        ...rest
      })
    },
    async transform(value, path) {
      if (!formatAwareProcessors) {
        formatAwareProcessors = createFormatAwareProcessors({
          SourceMapGenerator,
          ...rest
        })
      }


      // Strip the queryparameters that are part of the file extension
      path = path.split("?")[0]

      const file = new VFile({path, value})

      if (
        file.extname &&
        filter(file.path) &&
        formatAwareProcessors.extnames.includes(file.extname)
      ) {
        const compiled = await formatAwareProcessors.process(file)
        const code = String(compiled.value)
        /** @type {SourceDescription} */
        const result = {
          code,
          // @ts-expect-error: `rollup` is not compiled with `exactOptionalPropertyTypes`,
          // so it does not allow `sourceRoot` in `file.map` to be `undefined` here.
          map: compiled.map
        }
        return result
      }
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants