summaryrefslogtreecommitdiff
path: root/packages/integrations/mdx/src/index.ts
blob: bd3d50c862d5b1d6b39d72e7ba64f1dd3bcf5b0e (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 mdxPlugin from '@mdx-js/rollup';
import type { AstroIntegration } from 'astro';

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();
										}`;
								},
							},
						],
					},
				});
			},
		},
	};
}