UNPKG

polen

Version:

A framework for delightful GraphQL developer portals

54 lines 1.93 kB
// Note: This is adapted from `@mdx-js/mdx` 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'; export const createConfig = (options) => { const { exclude, include = /\.mdx?$/, // Default to .md and .mdx files ...rest } = options ?? {}; return { exclude, include, ...rest, }; }; /** * Plugin to compile MDX. * * Uses Rolldown. */ export const VitePluginMdx = (options) => { const config = createConfig(options); const filter = createFilter(config.include, config.exclude); let formatAwareProcessors; return { name: `mdx`, // enforce: `pre`, // Run before other transforms config(__config, env) { // Initialize processors with Vite environment info formatAwareProcessors = createFormatAwareProcessors({ SourceMapGenerator, jsx: true, // emit JSX, not JS development: env.mode === `development`, ...config, }); }, async transform(value, id) { const file = new VFile({ path: id, value }); if (file.extname && filter(file.path) && formatAwareProcessors.extnames.includes(file.extname)) { const compiled = await formatAwareProcessors.process(file); const code = String(compiled.value); const sourceDescription = { code, moduleType: `jsx`, // When MDX compiles to JS (not JSX), we don't need to set moduleType map: compiled.map, }; return sourceDescription; } }, }; }; //# sourceMappingURL=vite-plugin-mdx.js.map