1
- import fs from 'node:fs/promises' ;
2
1
import { setVfileFrontmatter } from '@astrojs/markdown-remark' ;
3
- import type { AstroConfig , SSRError } from 'astro' ;
2
+ import type { SSRError } from 'astro' ;
4
3
import { VFile } from 'vfile' ;
5
4
import type { Plugin } from 'vite' ;
6
5
import type { MdxOptions } from './index.js' ;
7
6
import { createMdxProcessor } from './plugins.js' ;
8
- import { getFileInfo , parseFrontmatter } from './utils.js' ;
7
+ import { parseFrontmatter } from './utils.js' ;
9
8
10
- export function vitePluginMdx ( astroConfig : AstroConfig , mdxOptions : MdxOptions ) : Plugin {
9
+ export function vitePluginMdx ( mdxOptions : MdxOptions ) : Plugin {
11
10
let processor : ReturnType < typeof createMdxProcessor > | undefined ;
12
11
13
12
return {
@@ -43,13 +42,9 @@ export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions):
43
42
} ,
44
43
// Override transform to alter code before MDX compilation
45
44
// ex. inject layouts
46
- async transform ( _ , id ) {
45
+ async transform ( code , id ) {
47
46
if ( ! id . endsWith ( '.mdx' ) ) return ;
48
47
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
-
53
48
const { data : frontmatter , content : pageContent } = parseFrontmatter ( code , id ) ;
54
49
55
50
const vfile = new VFile ( { value : pageContent , path : id } ) ;
@@ -76,7 +71,7 @@ export function vitePluginMdx(astroConfig: AstroConfig, mdxOptions: MdxOptions):
76
71
77
72
// For some reason MDX puts the error location in the error's name, not very useful for us.
78
73
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 } ;
80
75
81
76
// For another some reason, MDX doesn't include a stack trace. Weird
82
77
Error . captureStackTrace ( err ) ;
0 commit comments