1
- import type { TransformResult } from 'rollup' ;
2
- import { type Plugin , type ResolvedConfig , transformWithEsbuild } from 'vite' ;
3
- import type { AstroRenderer , AstroSettings } from '../@types/astro.js' ;
4
- import type { Logger } from '../core/logger/core.js' ;
5
- import type { PluginMetadata } from '../vite-plugin-astro/types.js' ;
6
-
7
- import babel from '@babel/core' ;
1
+ import { type Plugin , transformWithEsbuild } from 'vite' ;
8
2
import { CONTENT_FLAG , PROPAGATED_ASSET_FLAG } from '../content/index.js' ;
9
3
import { astroEntryPrefix } from '../core/build/plugins/plugin-component-entry.js' ;
10
4
import { removeQueryString } from '../core/path.js' ;
11
- import tagExportsPlugin from './tag.js' ;
12
-
13
- interface TransformJSXOptions {
14
- code : string ;
15
- id : string ;
16
- mode : string ;
17
- renderer : AstroRenderer ;
18
- ssr : boolean ;
19
- root : URL ;
20
- }
21
-
22
- async function transformJSX ( {
23
- code,
24
- mode,
25
- id,
26
- ssr,
27
- renderer,
28
- root,
29
- } : TransformJSXOptions ) : Promise < TransformResult > {
30
- const { jsxTransformOptions } = renderer ;
31
- const options = await jsxTransformOptions ! ( { mode, ssr } ) ;
32
- const plugins = [ ...( options . plugins || [ ] ) ] ;
33
- if ( ssr ) {
34
- plugins . push ( await tagExportsPlugin ( { rendererName : renderer . name , root } ) ) ;
35
- }
36
- const result = await babel . transformAsync ( code , {
37
- presets : options . presets ,
38
- plugins,
39
- cwd : process . cwd ( ) ,
40
- filename : id ,
41
- ast : false ,
42
- compact : false ,
43
- sourceMaps : true ,
44
- configFile : false ,
45
- babelrc : false ,
46
- inputSourceMap : options . inputSourceMap ,
47
- } ) ;
48
- // TODO: Be more strict about bad return values here.
49
- // Should we throw an error instead? Should we never return `{code: ""}`?
50
- if ( ! result ) return null ;
51
-
52
- if ( renderer . name === 'astro:jsx' ) {
53
- const { astro } = result . metadata as unknown as PluginMetadata ;
54
- return {
55
- code : result . code || '' ,
56
- map : result . map ,
57
- meta : {
58
- astro,
59
- vite : {
60
- // Setting this vite metadata to `ts` causes Vite to resolve .js
61
- // extensions to .ts files.
62
- lang : 'ts' ,
63
- } ,
64
- } ,
65
- } ;
66
- }
67
-
68
- return {
69
- code : result . code || '' ,
70
- map : result . map ,
71
- } ;
72
- }
73
-
74
- interface AstroPluginJSXOptions {
75
- settings : AstroSettings ;
76
- logger : Logger ;
77
- }
5
+ import { transformJSX } from './transform-jsx.js' ;
78
6
79
7
// Format inspired by https://github.com/vitejs/vite/blob/main/packages/vite/src/node/constants.ts#L54
80
8
const SPECIAL_QUERY_REGEX = new RegExp (
81
9
`[?&](?:worker|sharedworker|raw|url|${ CONTENT_FLAG } |${ PROPAGATED_ASSET_FLAG } )\\b`
82
10
) ;
83
11
84
- /** Use Astro config to allow for alternate or multiple JSX renderers (by default Vite will assume React) */
85
- export default function mdxVitePlugin ( { settings } : AstroPluginJSXOptions ) : Plugin {
86
- let viteConfig : ResolvedConfig ;
87
- // A reference to Astro's internal JSX renderer.
88
- let astroJSXRenderer : AstroRenderer ;
89
-
12
+ // TODO: Move this Vite plugin into `@astrojs/mdx` in Astro 5
13
+ export default function mdxVitePlugin ( ) : Plugin {
90
14
return {
91
15
name : 'astro:jsx' ,
92
16
enforce : 'pre' , // run transforms before other plugins
93
- async configResolved ( resolvedConfig ) {
94
- viteConfig = resolvedConfig ;
95
- astroJSXRenderer = settings . renderers . find ( ( r ) => r . jsxImportSource === 'astro' ) ! ;
96
- } ,
97
17
async transform ( code , id , opts ) {
98
18
// Skip special queries and astro entries. We skip astro entries here as we know it doesn't contain
99
19
// JSX code, and also because we can't detect the import source to apply JSX transforms.
@@ -117,14 +37,7 @@ export default function mdxVitePlugin({ settings }: AstroPluginJSXOptions): Plug
117
37
} ,
118
38
} ,
119
39
} ) ;
120
- return transformJSX ( {
121
- code : jsxCode ,
122
- id,
123
- renderer : astroJSXRenderer ,
124
- mode : viteConfig . mode ,
125
- ssr : Boolean ( opts ?. ssr ) ,
126
- root : settings . config . root ,
127
- } ) ;
40
+ return await transformJSX ( jsxCode , id , opts ?. ssr ) ;
128
41
} ,
129
42
} ;
130
43
}
0 commit comments