Skip to content

Commit d7dc41d

Browse files
committed
Remove fs read for MDX transform
1 parent 20936a9 commit d7dc41d

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

.changeset/large-glasses-jam.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@astrojs/mdx": patch
3+
---
4+
5+
Allows Vite plugins to transform `.mdx` files before the MDX plugin transforms it

packages/integrations/mdx/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export default function mdx(partialMdxOptions: Partial<MdxOptions> = {}): AstroI
7070

7171
updateConfig({
7272
vite: {
73-
plugins: [vitePluginMdx(config, mdxOptions), vitePluginMdxPostprocess(config)],
73+
plugins: [vitePluginMdx(mdxOptions), vitePluginMdxPostprocess(config)],
7474
},
7575
});
7676
},

packages/integrations/mdx/src/vite-plugin-mdx.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import fs from 'node:fs/promises';
21
import { setVfileFrontmatter } from '@astrojs/markdown-remark';
3-
import type { AstroConfig, SSRError } from 'astro';
2+
import type { SSRError } from 'astro';
43
import { VFile } from 'vfile';
54
import type { Plugin } from 'vite';
65
import type { MdxOptions } from './index.js';
76
import { createMdxProcessor } from './plugins.js';
8-
import { getFileInfo, parseFrontmatter } from './utils.js';
7+
import { parseFrontmatter } from './utils.js';
98

10-
export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions): Plugin {
9+
export function vitePluginMdx(mdxOptions: MdxOptions): Plugin {
1110
let processor: ReturnType<typeof createMdxProcessor> | undefined;
1211

1312
return {
@@ -43,13 +42,9 @@ export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions):
4342
},
4443
// Override transform to alter code before MDX compilation
4544
// ex. inject layouts
46-
async transform(_, id) {
45+
async transform(code, id) {
4746
if (!id.endsWith('.mdx')) return;
4847

49-
// Read code from file manually to prevent Vite from parsing `import.meta.env` expressions
50-
const { fileId } = getFileInfo(id, astroConfig);
51-
const code = await fs.readFile(fileId, 'utf-8');
52-
5348
const { data: frontmatter, content: pageContent } = parseFrontmatter(code, id);
5449

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

7772
// For some reason MDX puts the error location in the error's name, not very useful for us.
7873
err.name = 'MDXError';
79-
err.loc = { file: fileId, line: e.line, column: e.column };
74+
err.loc = { file: id, line: e.line, column: e.column };
8075

8176
// For another some reason, MDX doesn't include a stack trace. Weird
8277
Error.captureStackTrace(err);

0 commit comments

Comments
 (0)