MDX: Support outDir option on createMDX()

This commit is contained in:
Fuma Nama
2025-06-02 21:22:04 +08:00
parent b4916d2f5e
commit a5c283f7e1
7 changed files with 27 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Support `outDir` option on `createMDX()`
+1 -1
View File
@@ -2,4 +2,4 @@
import { postInstall } from './dist/next/index.js';
void postInstall(process.argv[2]);
void postInstall(process.argv[2], process.argv[3]);
+3 -2
View File
@@ -9,6 +9,7 @@ import { validate, ValidationError } from '@/utils/schema';
export interface Options {
configPath: string;
outDir: string;
}
/**
@@ -24,7 +25,7 @@ export default async function loader(
this.cacheable(true);
const context = this.context;
const filePath = this.resourcePath;
const { configPath } = this.getOptions();
const { configPath, outDir } = this.getOptions();
const matter = grayMatter(source);
// notice that `resourceQuery` can be missing (e.g. `page.mdx`)
@@ -35,7 +36,7 @@ export default async function loader(
hash?: string;
collection?: string;
};
const config = await loadConfig(configPath, configHash);
const config = await loadConfig(configPath, outDir, configHash);
let collection =
collectionId !== undefined
+2 -2
View File
@@ -17,7 +17,7 @@ export async function start(
): Promise<void> {
// init
let configHash = await getConfigHash(configPath);
let config = await loadConfig(configPath, configHash, true);
let config = await loadConfig(configPath, outDir, configHash, true);
const outPath = path.resolve(outDir, `index.ts`);
async function updateMapFile() {
@@ -58,7 +58,7 @@ export async function start(
if (isConfigFile) {
configHash = await getConfigHash(configPath);
config = await loadConfig(configPath, configHash, true);
config = await loadConfig(configPath, outDir, configHash, true);
}
if (event === 'change') fileCache.removeCache(absolutePath);
+9 -2
View File
@@ -1,4 +1,3 @@
import path from 'node:path';
import type { NextConfig } from 'next';
import type { Configuration } from 'webpack';
import { findConfigFile } from '@/utils/config';
@@ -10,15 +9,22 @@ export interface CreateMDXOptions {
* Path to source configuration file
*/
configPath?: string;
/**
* Directory for output files
*
* @defaultValue '.source'
*/
outDir?: string;
}
const outDir = path.resolve('.source');
const defaultPageExtensions = ['mdx', 'md', 'jsx', 'js', 'tsx', 'ts'];
export { start };
export function createMDX({
configPath = findConfigFile(),
outDir = '.source',
}: CreateMDXOptions = {}) {
// Next.js performs multiple iteration on the `next.config.js` file
// the first time contains the original arguments of `next dev`
@@ -35,6 +41,7 @@ export function createMDX({
return (nextConfig: NextConfig = {}): NextConfig => {
const mdxLoaderOptions: MDXLoaderOptions = {
configPath,
outDir,
};
return {
+3 -2
View File
@@ -5,10 +5,11 @@ import { generateJS } from '@/map/generate';
export async function postInstall(
configPath = findConfigFile(),
outDir = '.source',
): Promise<void> {
const jsOut = path.resolve('.source/index.ts');
const jsOut = path.resolve(outDir, 'index.ts');
const hash = await getConfigHash(configPath);
const config = await loadConfig(configPath, hash, true);
const config = await loadConfig(configPath, outDir, hash, true);
// clean past results
await fs.rm(path.dirname(jsOut), { recursive: true });
+4 -2
View File
@@ -61,11 +61,13 @@ async function compileConfig(configPath: string, outDir: string) {
* Load config
*
* @param configPath - config path
* @param outDir - output directory
* @param hash - hash of config content
* @param build - By default, it assumes the TypeScript config has been compiled to `.source/source.config.mjs`. Set this `true` to compile the config first.
*/
export async function loadConfig(
configPath: string,
outDir: string,
hash: string,
build = false,
): Promise<LoadedConfig> {
@@ -73,9 +75,9 @@ export async function loadConfig(
return await cache.config;
}
if (build) await compileConfig(configPath, '.source');
if (build) await compileConfig(configPath, outDir);
const url = pathToFileURL(path.resolve('.source/source.config.mjs'));
const url = pathToFileURL(path.resolve(outDir, 'source.config.mjs'));
const config = import(`${url.href}?hash=${hash}`).then((loaded) => {
const [err, config] = buildConfig(