MDX: support Vite 8 moduleType option

This commit is contained in:
Fuma Nama
2025-12-25 13:11:17 +08:00
parent 90b0e2a14d
commit 3a8b9b0f23
3 changed files with 20 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
support Vite 8 `moduleType` option
+9 -1
View File
@@ -22,6 +22,13 @@ export interface LoaderInput {
export interface LoaderOutput {
code: string;
map?: unknown;
/**
* Only supported in Vite 8.
*
* Explicitly define the transformed module type, for unsupported environments, you need to consider the differences between each bundler.
*/
moduleType?: 'js' | 'json';
}
type Awaitable<T> = T | Promise<T>;
@@ -119,6 +126,7 @@ export function toVite(loader: Loader): ViteLoader {
return {
code: result.code,
map: result.map as SourceMap,
moduleType: result.moduleType,
};
},
};
@@ -172,7 +180,7 @@ export function toBun(loader: Loader) {
return {
contents: output.code,
loader: 'js',
loader: output.moduleType ?? 'js',
};
}
+6 -7
View File
@@ -1,6 +1,6 @@
import type { Loader, LoaderInput } from '@/loaders/adapter';
import type { ConfigLoader } from '@/loaders/config';
import { dump, load } from 'js-yaml';
import { load } from 'js-yaml';
import { z } from 'zod';
import { metaLoaderGlob } from '.';
import type { MetaCollectionItem } from '@/config/build';
@@ -19,10 +19,10 @@ export function createMetaLoader(
{ getCore }: ConfigLoader,
resolve: {
json?: 'json' | 'js';
yaml?: 'yaml' | 'js';
yaml?: 'js';
} = {},
): Loader {
const { json: resolveJson = 'js', yaml: resolveYaml = 'js' } = resolve;
const { json: resolveJson = 'js' } = resolve;
function parse(filePath: string, source: string) {
try {
@@ -81,6 +81,7 @@ export function createMetaLoader(
if (input.filePath.endsWith('.json')) {
return {
moduleType: resolveJson,
code:
resolveJson === 'json'
? JSON.stringify(data)
@@ -88,10 +89,8 @@ export function createMetaLoader(
};
} else {
return {
code:
resolveYaml === 'yaml'
? dump(data)
: `export default ${JSON.stringify(data)}`,
moduleType: 'js',
code: `export default ${JSON.stringify(data)}`,
};
}
},