mirror of
https://github.com/withastro/astro.git
synced 2024-12-30 22:03:56 -05:00
40 lines
972 B
TypeScript
40 lines
972 B
TypeScript
|
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();
|
||
|
}`
|
||
|
}
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|