MDX: Export postInstall() function from /vite

This commit is contained in:
Fuma Nama
2025-09-17 20:15:45 +08:00
parent 07414cae61
commit 854d4ef4b6
9 changed files with 78 additions and 63 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-mdx': patch
---
Export `postInstall()` function from `fumadocs-mdx/vite`
-5
View File
@@ -1,5 +0,0 @@
#!/usr/bin/env node
import { postInstall } from './dist/next/index.js';
void postInstall(process.argv[2], process.argv[3]);
+2 -3
View File
@@ -58,11 +58,10 @@
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"bin": "./bin.js",
"bin": "./dist/bin.js",
"files": [
"dist/*",
"loader-mdx.cjs",
"bin.js"
"loader-mdx.cjs"
],
"scripts": {
"build": "tsup",
+21
View File
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import { existsSync } from 'node:fs';
async function start() {
const args = process.argv.slice(2);
const isNext =
existsSync('next.config.js') ||
existsSync('next.config.mjs') ||
existsSync('next.config.ts');
if (isNext) {
const { postInstall } = await import('./next');
await postInstall(...args);
} else {
const { postInstall } = await import('./vite/postinstall');
await postInstall(...args);
}
}
void start();
+24 -1
View File
@@ -1,2 +1,25 @@
import { findConfigFile, getConfigHash, loadConfig } from '@/utils/config';
import path from 'node:path';
import fs from 'node:fs/promises';
import { generateJS } from '@/map/generate';
export * from './create';
export * from '../postinstall';
export async function postInstall(
configPath = findConfigFile(),
outDir = '.source',
) {
const config = await loadConfig(configPath, outDir, undefined, true);
const outPath = path.join(outDir, 'index.ts');
// clean past results
await fs.rm(outDir, { recursive: true });
await fs.mkdir(outDir, { recursive: true });
const hash = await getConfigHash(configPath);
await fs.writeFile(
outPath,
await generateJS(configPath, config, { relativeTo: outDir }, hash),
);
console.log('[MDX] types generated');
}
-53
View File
@@ -1,53 +0,0 @@
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import { findConfigFile, getConfigHash, loadConfig } from '@/utils/config';
import { generateJS } from '@/map/generate';
import { existsSync } from 'node:fs';
import { entry } from '@/vite/generate';
export async function postInstall(
configPath = findConfigFile(),
outDir?: string,
): Promise<void> {
const isNext =
existsSync('next.config.js') ||
existsSync('next.config.mjs') ||
existsSync('next.config.ts');
if (isNext) {
await onNext(configPath, outDir ?? '.source');
} else {
await onVite(configPath, outDir ?? process.cwd());
}
console.log('[MDX] types generated');
}
async function onNext(configPath: string, outDir: string) {
const config = await loadConfig(configPath, outDir, undefined, true);
const outPath = path.join(outDir, 'index.ts');
// clean past results
await fs.rm(outDir, { recursive: true });
await fs.mkdir(outDir, { recursive: true });
const hash = await getConfigHash(configPath);
await fs.writeFile(
outPath,
await generateJS(configPath, config, { relativeTo: outDir }, hash),
);
}
async function onVite(
configPath: string,
outDir: string,
addJsExtension?: boolean,
) {
const config = await loadConfig(configPath, 'node_modules', undefined, true);
const outFile = 'source.generated.ts';
await fs.writeFile(
path.join(outDir, outFile),
entry(configPath, config, outDir, addJsExtension),
);
}
+2
View File
@@ -38,6 +38,8 @@ export interface PluginOptions {
configPath?: string;
}
export * from './postinstall';
export default function mdx(
config: Record<string, unknown>,
options: PluginOptions = {},
+23
View File
@@ -0,0 +1,23 @@
import { findConfigFile, loadConfig } from '@/utils/config';
import fs from 'node:fs/promises';
import path from 'node:path';
import { entry } from '@/vite/generate';
export async function postInstall(
configPath = findConfigFile(),
outDir?: string,
addJsExtension = false,
) {
const config = await loadConfig(configPath, 'node_modules', undefined, true);
const outFile = 'source.generated.ts';
if (outDir) {
await fs.mkdir(outDir, { recursive: true });
}
await fs.writeFile(
outDir ? path.join(outDir, outFile) : outFile,
entry(configPath, config, outDir ?? process.cwd(), addJsExtension),
);
console.log('[MDX] types generated');
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { defineConfig } from 'tsup';
export default defineConfig({
entry: [
'./src/{index,loader-mdx}.ts',
'./src/{index,loader-mdx,bin}.ts',
'./src/{config,next,vite,bun}/index.ts',
'./src/node/loader.ts',
'./src/config/zod-3.ts',