vite-plugin-mdx.ts — astro Source File
Architecture documentation for vite-plugin-mdx.ts, a typescript file in the astro codebase. 7 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR 5053c6d5_f109_9970_dbe2_50bf2be7a473["vite-plugin-mdx.ts"] c9598570_76ac_7dd8_56a7_b8994dc3f0bd["./index.js"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> c9598570_76ac_7dd8_56a7_b8994dc3f0bd bcc297e6_3230_e5a8_b494_84abb5eb9c5d["./plugins.js"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> bcc297e6_3230_e5a8_b494_84abb5eb9c5d ab5f9b39_abc6_0697_d4ba_c51748add5b2["./utils.js"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> ab5f9b39_abc6_0697_d4ba_c51748add5b2 f16d8c76_2866_6150_bd14_0347b59abfe9["astro"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> f16d8c76_2866_6150_bd14_0347b59abfe9 8c9d3d30_e7fa_6898_26b0_03d93d9c96f8["rehype.js"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> 8c9d3d30_e7fa_6898_26b0_03d93d9c96f8 b909a1d2_5b96_acd8_d198_1f106f44e2c3["vfile"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> b909a1d2_5b96_acd8_d198_1f106f44e2c3 263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"] 5053c6d5_f109_9970_dbe2_50bf2be7a473 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7 style 5053c6d5_f109_9970_dbe2_50bf2be7a473 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
import type { SSRError } from 'astro';
import { getAstroMetadata } from 'astro/jsx/rehype.js';
import { VFile } from 'vfile';
import type { Plugin } from 'vite';
import type { MdxOptions } from './index.js';
import { createMdxProcessor } from './plugins.js';
import { safeParseFrontmatter } from './utils.js';
export interface VitePluginMdxOptions {
mdxOptions: MdxOptions;
srcDir: URL;
}
// NOTE: Do not destructure `opts` as we're assigning a reference that will be mutated later
export function vitePluginMdx(opts: VitePluginMdxOptions): Plugin {
let processor: ReturnType<typeof createMdxProcessor> | undefined;
let sourcemapEnabled: boolean;
return {
name: '@mdx-js/rollup',
enforce: 'pre',
buildEnd() {
processor = undefined;
},
configResolved(resolved) {
sourcemapEnabled = !!resolved.build.sourcemap;
// HACK: Remove the `astro:jsx` plugin if defined as we handle the JSX transformation ourselves
const jsxPluginIndex = resolved.plugins.findIndex((p) => p.name === 'astro:jsx');
if (jsxPluginIndex !== -1) {
// @ts-ignore-error ignore readonly annotation
resolved.plugins.splice(jsxPluginIndex, 1);
}
},
resolveId: {
filter: {
// Do not match sources that start with /
id: /^[^/]/,
},
async handler(source, importer, options) {
if (importer?.endsWith('.mdx')) {
let resolved = await this.resolve(source, importer, options);
if (!resolved) resolved = await this.resolve('./' + source, importer, options);
return resolved;
}
},
},
// Override transform to alter code before MDX compilation
// ex. inject layouts
transform: {
filter: {
id: /\.mdx$/,
},
async handler(code, id) {
const { frontmatter, content } = safeParseFrontmatter(code, id);
const vfile = new VFile({
value: content,
path: id,
data: {
astro: {
frontmatter,
},
applyFrontmatterExport: {
srcDir: opts.srcDir,
},
},
});
// Lazily initialize the MDX processor
if (!processor) {
processor = createMdxProcessor(opts.mdxOptions, {
sourcemap: sourcemapEnabled,
});
}
try {
const compiled = await processor.process(vfile);
return {
code: String(compiled.value),
map: compiled.map,
meta: getMdxMeta(vfile),
};
} catch (e: any) {
const err: SSRError = e;
// For some reason MDX puts the error location in the error's name, not very useful for us.
err.name = 'MDXError';
err.loc = { file: id, line: e.line, column: e.column };
// For another some reason, MDX doesn't include a stack trace. Weird
Error.captureStackTrace(err);
throw err;
}
},
},
};
}
function getMdxMeta(vfile: VFile): Record<string, any> {
const astroMetadata = getAstroMetadata(vfile);
if (!astroMetadata) {
throw new Error(
'Internal MDX error: Astro metadata is not set by rehype-analyze-astro-metadata',
);
}
return {
astro: astroMetadata,
vite: {
// Setting this vite metadata to `ts` causes Vite to resolve .js
// extensions to .ts files.
lang: 'ts',
},
};
}
Domain
Subdomains
Functions
Types
Dependencies
- ./index.js
- ./plugins.js
- ./utils.js
- astro
- rehype.js
- vfile
- vite
Source
Frequently Asked Questions
What does vite-plugin-mdx.ts do?
vite-plugin-mdx.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in vite-plugin-mdx.ts?
vite-plugin-mdx.ts defines 2 function(s): getMdxMeta, vitePluginMdx.
What does vite-plugin-mdx.ts depend on?
vite-plugin-mdx.ts imports 7 module(s): ./index.js, ./plugins.js, ./utils.js, astro, rehype.js, vfile, vite.
Where is vite-plugin-mdx.ts in the architecture?
vite-plugin-mdx.ts is located at packages/integrations/mdx/src/vite-plugin-mdx.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/mdx/src).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free