MDX: Generate config based on Next.js version

This commit is contained in:
Fuma Nama
2025-07-25 21:28:39 +08:00
parent ec7b9edb69
commit f8000f4265
2 changed files with 37 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Generate config based on Next.js version
+32 -17
View File
@@ -3,6 +3,8 @@ import type { Configuration } from 'webpack';
import { findConfigFile } from '@/utils/config';
import { start } from '@/map';
import { type Options as MDXLoaderOptions } from '../loader-mdx';
import { version } from 'next/package.json';
import type { TurbopackOptions } from 'next/dist/server/config-shared';
export interface CreateMDXOptions {
/**
@@ -19,6 +21,10 @@ export interface CreateMDXOptions {
}
const defaultPageExtensions = ['mdx', 'md', 'jsx', 'js', 'tsx', 'ts'];
const isTurboExperimental =
version.startsWith('15.0.') ||
version.startsWith('15.1.') ||
version.startsWith('15.2.');
export { start };
@@ -43,25 +49,26 @@ export function createMDX({
configPath,
outDir,
};
return {
...nextConfig,
turbopack: {
...nextConfig?.turbopack,
rules: {
...nextConfig?.turbopack?.rules,
// @ts-expect-error -- safe
'*.{md,mdx}': {
loaders: [
{
loader: 'fumadocs-mdx/loader-mdx',
options: mdxLoaderOptions,
},
],
as: '*.js',
},
const turbo: TurbopackOptions = {
...nextConfig.experimental?.turbo,
...nextConfig.turbopack,
rules: {
...nextConfig.experimental?.turbo?.rules,
...nextConfig.turbopack?.rules,
'*.{md,mdx}': {
loaders: [
{
loader: 'fumadocs-mdx/loader-mdx',
options: mdxLoaderOptions as any,
},
],
as: '*.js',
},
},
};
const updated: NextConfig = {
...nextConfig,
pageExtensions: nextConfig.pageExtensions ?? defaultPageExtensions,
webpack: (config: Configuration, options) => {
config.resolve ||= {};
@@ -85,5 +92,13 @@ export function createMDX({
return nextConfig.webpack?.(config, options) ?? config;
},
};
if (isTurboExperimental) {
updated.experimental = { ...updated.experimental, turbo };
} else {
updated.turbopack = turbo;
}
return updated;
};
}