MDX: improve performance

This commit is contained in:
Fuma Nama
2025-04-19 14:38:01 +08:00
parent ebb3699779
commit 434ccb2cfc
4 changed files with 20 additions and 20 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Improve performance
+3 -8
View File
@@ -15,18 +15,13 @@ export async function start(
configPath: string,
outDir: string,
): Promise<void> {
// delete previous output
void fs.rm(path.resolve(outDir, `index.js`), { force: true });
void fs.rm(path.resolve(outDir, `index.d.ts`), { force: true });
// init
await fs.mkdir(outDir, { recursive: true });
let configHash = await getConfigHash(configPath);
let config = await loadConfig(configPath, configHash, true);
const outPath = path.resolve(outDir, `index.ts`);
async function updateMapFile() {
console.time(`[MDX] update map file`);
const start = Date.now();
try {
await fs.writeFile(
@@ -41,14 +36,14 @@ export async function start(
}
}
console.timeEnd(`[MDX] update map file`);
console.log(`[MDX] updated map file in ${Date.now() - start}ms`);
}
await updateMapFile();
if (dev) {
const { watcher } = await import('@/map/watcher');
const instance = watcher(configPath, config);
const instance = watcher(configPath, config, [outPath]);
instance.on('ready', () => {
console.log('[MDX] started dev server');
+6 -1
View File
@@ -1,7 +1,11 @@
import { type FSWatcher, watch } from 'chokidar';
import { type LoadedConfig } from '@/utils/config';
export function watcher(configPath: string, config: LoadedConfig): FSWatcher {
export function watcher(
configPath: string,
config: LoadedConfig,
ignored: string[],
): FSWatcher {
const deps: string[] = [configPath];
function add(dir: string | string[]) {
@@ -19,6 +23,7 @@ export function watcher(configPath: string, config: LoadedConfig): FSWatcher {
return watch(deps, {
ignoreInitial: true,
ignored,
persistent: true,
});
}
+6 -11
View File
@@ -1,5 +1,5 @@
import * as path from 'node:path';
import * as fs from 'node:fs';
import * as fs from 'node:fs/promises';
import { findConfigFile, getConfigHash, loadConfig } from '@/utils/config';
import { generateJS } from '@/map/generate';
@@ -10,15 +10,10 @@ export async function postInstall(
const hash = await getConfigHash(configPath);
const config = await loadConfig(configPath, hash, true);
fs.mkdirSync(path.dirname(jsOut), { recursive: true });
fs.writeFileSync(
jsOut,
await generateJS(
configPath,
config,
path.resolve('.source/index.ts'),
hash,
),
);
// clean past results
await fs.rm(path.dirname(jsOut), { recursive: true });
await fs.mkdir(path.dirname(jsOut), { recursive: true });
await fs.writeFile(jsOut, await generateJS(configPath, config, jsOut, hash));
console.log('[MDX] types generated');
}