fumadocs-mdx15.6.45.4 kB
import { t as loadConfig } from "../load-from-file-Bt3g8VEm.js";
import { t as createCore } from "../core-r1WHKfaI.js";
import { i as metaLoaderQueryGlob, n as metaLoaderFileGlob, t as mdxLoaderGlob } from "../loaders-3R0gLmlP.js";
import { t as indexFile } from "../index-file-DHU9QZX_.js";
import { i as resolveMacroOptions, r as createMacroMatcher } from "../options-DZSjmVhY.js";
import * as path from "node:path";
// src/next/index.ts
const defaultPageExtensions = [
"mdx",
"md",
"jsx",
"js",
"tsx",
"ts"
];
function createMDX(options = {}) {
const core = createNextCore(options);
const isDev = process.env.NODE_ENV === "development";
const macro = resolveMacroOptions(options.macro);
let pending;
if (process.env._FUMADOCS_MDX !== "1") {
process.env._FUMADOCS_MDX = "1";
pending = init(isDev, core);
pending.catch((err) => {
console.error("[MDX] failed to generate files:", err);
});
}
function onLoaderOptions(type) {
return {
type,
configPath: core.configPath,
outDir: core.outDir,
compiledConfigPath: core.getCompiledConfigPath(),
isDev,
macro: macro !== void 0
};
}
return (nextConfig = {}) => {
const turbopackLoaderOptions = onLoaderOptions("turbopack");
const turbopack = {
...nextConfig.turbopack,
rules: {
...nextConfig.turbopack?.rules,
...macro ? Object.fromEntries(macro.include.map((pattern) => [pattern, {
condition: { all: [{ not: "foreign" }, { content: /['"]fumadocs-mdx\/macro['"]/ }] },
loaders: [{
loader: "fumadocs-mdx/webpack/macro",
options: turbopackLoaderOptions
}]
}])) : void 0,
"*.{md,mdx}": {
loaders: [{
loader: "fumadocs-mdx/webpack/mdx",
options: turbopackLoaderOptions
}],
as: "*.js"
},
"*.json": {
condition: { query: metaLoaderQueryGlob },
loaders: [{
loader: "fumadocs-mdx/webpack/meta",
options: turbopackLoaderOptions
}],
as: "*.js"
},
"*.yaml": {
condition: { query: metaLoaderQueryGlob },
loaders: [{
loader: "fumadocs-mdx/webpack/meta",
options: turbopackLoaderOptions
}],
as: "*.js"
},
"*.{js,ts}": {
condition: { all: [{ query: metaLoaderQueryGlob }, { path: metaLoaderFileGlob }] },
loaders: [{
loader: "fumadocs-mdx/webpack/meta",
options: turbopackLoaderOptions
}],
as: "*.js"
}
}
};
const out = {
...nextConfig,
turbopack,
pageExtensions: nextConfig.pageExtensions ?? defaultPageExtensions,
webpack: (config, options) => {
config.resolve ||= {};
config.module ||= {};
config.module.rules ||= [];
const loaderOptions = onLoaderOptions("webpack");
if (macro) {
const matcher = createMacroMatcher(process.cwd(), macro);
config.module.rules.push({
test: matcher,
enforce: "pre",
use: [{
loader: "fumadocs-mdx/webpack/macro",
options: loaderOptions
}]
});
}
config.module.rules.push({
test: mdxLoaderGlob,
use: [options.defaultLoaders.babel, {
loader: "fumadocs-mdx/webpack/mdx",
options: loaderOptions
}]
}, {
test: metaLoaderFileGlob,
resourceQuery: metaLoaderQueryGlob,
enforce: "pre",
use: [{
loader: "fumadocs-mdx/webpack/meta",
options: loaderOptions
}]
});
config.plugins ||= [];
return nextConfig.webpack?.(config, options) ?? config;
}
};
const ready = pending ?? Promise.resolve();
return Object.defineProperty(out, "then", {
enumerable: false,
configurable: true,
writable: true,
value(onFulfilled, onRejected) {
return ready.then(() => {
const { ...clean } = out;
return clean;
}).then(onFulfilled, onRejected);
}
});
};
}
function init(dev, core) {
async function initOrReload() {
const { config, fromFile } = await loadConfig(core, true);
await core.init({ config });
if (fromFile) await core.emit({ write: true });
}
async function devServer() {
const { FSWatcher } = await import("chokidar");
const watcher = new FSWatcher({
ignoreInitial: true,
persistent: true,
ignored: [core.outDir]
});
watcher.add(core.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");
});
watcher.on("all", async (_event, file) => {
if (path.resolve(file) === core.configPath) {
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 });
}
const ready = initOrReload();
if (dev) ready.then(devServer, () => {}).catch((err) => {
console.error("[MDX] failed to start dev server:", err);
});
return ready;
}
async function postInstall(options = {}) {
const core = createNextCore(options);
const { config, fromFile } = await loadConfig(core, true);
await core.init({ config });
if (fromFile) await core.emit({ write: true });
}
function createNextCore({ outDir, configPath, index = {} }) {
return createCore({
environment: "next",
outDir,
configPath,
plugins: [index && indexFile(index)]
});
}
export { createMDX, postInstall };
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.