summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/index.ts
blob: d07913ede5c8dbe4ffefb9a7aa23d3dada6261df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import type { AstroIntegration } from 'astro';
import mdxPlugin from '@mdx-js/rollup';

export default function mdx(): AstroIntegration {
	return {
			name: '@astrojs/mdx',
			hooks: {
				'astro:config:setup': ({ updateConfig, addPageExtension, command }: any) => {
					addPageExtension('.mdx');
					updateConfig({
						vite: {
							plugins: [
								{ 	
									enforce: 'pre',
									...mdxPlugin({
										jsx: true,
										jsxImportSource: 'astro',
										// Note: disable `.md` support
										format: 'mdx',
										mdExtensions: []
									})
								},
								command === 'dev' && {
									name: '@astrojs/mdx',
									transform(code: string, id: string) {
										if (!id.endsWith('.mdx')) return;
										// TODO: decline HMR updates until we have a stable approach
										return `${code}\nif (import.meta.hot) {
											import.meta.hot.decline();
										}`
									}
								}
							]
						}
					})
				}
			}
		}
}