Skip to content

Commit

Permalink
Remove fs read for MDX transform (#10866)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Apr 24, 2024
1 parent 20936a9 commit dd7a113
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-glasses-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/mdx": patch
---

Allows Vite plugins to transform `.mdx` files before the MDX plugin transforms it
2 changes: 1 addition & 1 deletion packages/integrations/mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI

updateConfig({
vite: {
plugins: [vitePluginMdx(config, mdxOptions), vitePluginMdxPostprocess(config)],
plugins: [vitePluginMdx(mdxOptions), vitePluginMdxPostprocess(config)],
},
});
},
Expand Down
15 changes: 5 additions & 10 deletions packages/integrations/mdx/src/vite-plugin-mdx.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import fs from 'node:fs/promises';
import { setVfileFrontmatter } from '@astrojs/markdown-remark';
import type { AstroConfig, SSRError } from 'astro';
import type { SSRError } from 'astro';
import { VFile } from 'vfile';
import type { Plugin } from 'vite';
import type { MdxOptions } from './index.js';
import { createMdxProcessor } from './plugins.js';
import { getFileInfo, parseFrontmatter } from './utils.js';
import { parseFrontmatter } from './utils.js';

export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions): Plugin {
export function vitePluginMdx(mdxOptions: MdxOptions): Plugin {
let processor: ReturnType<typeof createMdxProcessor> | undefined;

return {
Expand Down Expand Up @@ -43,13 +42,9 @@ export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions):
},
// Override transform to alter code before MDX compilation
// ex. inject layouts
async transform(_, id) {
async transform(code, id) {
if (!id.endsWith('.mdx')) return;

// Read code from file manually to prevent Vite from parsing `import.meta.env` expressions
const { fileId } = getFileInfo(id, astroConfig);
const code = await fs.readFile(fileId, 'utf-8');

const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);

const vfile = new VFile({ value: pageContent, path: id });
Expand All @@ -76,7 +71,7 @@ export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions):

// For some reason MDX puts the error location in the error's name, not very useful for us.
err.name = 'MDXError';
err.loc = { file: fileId, line: e.line, column: e.column };
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);
Expand Down

0 comments on commit dd7a113

Please sign in to comment.