fumadocs-mdx
Version:
The built-in source for Fumadocs
152 lines (150 loc) • 4.14 kB
JavaScript
import "../fuma-matter-CHgJa_-B.js";
import { n as metaLoaderGlob, t as mdxLoaderGlob } from "../loaders-BVwYfelL.js";
import "../preset-gmDZnBcg.js";
import "../build-BTTNEFmV.js";
import { n as createCore, t as _Defaults } from "../core-DjldE3H9.js";
import "../codegen-DleOVLNr.js";
import { t as loadConfig } from "../load-from-file-Doc98oEB.js";
import { t as indexFile } from "../index-file-D9HsrWU_.js";
import * as path$1 from "node:path";
//#region src/next/index.ts
const defaultPageExtensions = [
"mdx",
"md",
"jsx",
"js",
"tsx",
"ts"
];
function createMDX(createOptions = {}) {
const core = createNextCore(applyDefaults(createOptions));
const isDev = process.env.NODE_ENV === "development";
if (process.env._FUMADOCS_MDX !== "1") {
process.env._FUMADOCS_MDX = "1";
init(isDev, core);
}
return (nextConfig = {}) => {
const { configPath, outDir } = core.getOptions();
const loaderOptions = {
configPath,
outDir,
absoluteCompiledConfigPath: path$1.resolve(core.getCompiledConfigPath()),
isDev
};
const turbopack = {
...nextConfig.turbopack,
rules: {
...nextConfig.turbopack?.rules,
"*.{md,mdx}": {
loaders: [{
loader: "fumadocs-mdx/loader-mdx",
options: loaderOptions
}],
as: "*.js"
},
"*.json": {
loaders: [{
loader: "fumadocs-mdx/loader-meta",
options: loaderOptions
}],
as: "*.json"
},
"*.yaml": {
loaders: [{
loader: "fumadocs-mdx/loader-meta",
options: loaderOptions
}],
as: "*.js"
}
}
};
return {
...nextConfig,
turbopack,
pageExtensions: nextConfig.pageExtensions ?? defaultPageExtensions,
webpack: (config, options) => {
config.resolve ||= {};
config.module ||= {};
config.module.rules ||= [];
config.module.rules.push({
test: mdxLoaderGlob,
use: [options.defaultLoaders.babel, {
loader: "fumadocs-mdx/loader-mdx",
options: loaderOptions
}]
}, {
test: metaLoaderGlob,
enforce: "pre",
use: [{
loader: "fumadocs-mdx/loader-meta",
options: loaderOptions
}]
});
config.plugins ||= [];
return nextConfig.webpack?.(config, options) ?? config;
}
};
};
}
async function init(dev, core) {
async function initOrReload() {
await core.init({ config: loadConfig(core, true) });
await core.emit({ write: true });
}
async function devServer() {
const { FSWatcher } = await import("chokidar");
const { configPath, outDir } = core.getOptions();
const watcher = new FSWatcher({
ignoreInitial: true,
persistent: true,
ignored: [outDir]
});
watcher.add(configPath);
for (const collection of core.getCollections()) watcher.add(collection.dir);
for (const workspace of core.getWorkspaces().values()) for (const collection of workspace.getCollections()) watcher.add(collection.dir);
watcher.on("ready", () => {
console.log("[MDX] started dev server");
});
const absoluteConfigPath = path$1.resolve(configPath);
watcher.on("all", async (_event, file) => {
if (path$1.resolve(file) === absoluteConfigPath) {
watcher.removeAllListeners();
await watcher.close();
await initOrReload();
console.log("[MDX] restarting dev server");
await devServer();
}
});
process.on("exit", () => {
if (watcher.closed) return;
console.log("[MDX] closing dev server");
watcher.close();
});
await core.initServer({ watcher });
}
await initOrReload();
if (dev) await devServer();
}
async function postInstall(options) {
const core = createNextCore(applyDefaults(options));
await core.init({ config: loadConfig(core, true) });
await core.emit({ write: true });
}
function applyDefaults(options) {
return {
index: {},
outDir: options.outDir ?? _Defaults.outDir,
configPath: options.configPath ?? _Defaults.configPath
};
}
function createNextCore(options) {
return createCore({
environment: "next",
outDir: options.outDir,
configPath: options.configPath,
plugins: [options.index && indexFile(options.index)]
});
}
//#endregion
export { createMDX, postInstall };
//# sourceMappingURL=index.js.map