fumadocs-mdx 15.4.4 → 15.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,213 @@
1
+ import fsSync from "node:fs";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { createRequire } from "node:module";
5
+ //#region ../vite/dist/index.js
6
+ let pnp;
7
+ if (process.versions.pnp) try {
8
+ pnp = createRequire(import.meta.url)("pnpapi");
9
+ } catch {}
10
+ /**
11
+ * Breadth-first, so each `package.json` is read once however many chains reach it, and the chain
12
+ * recorded for a CommonJS package is the shortest one (ties broken by name): Vite hashes
13
+ * `optimizeDeps.include`, the output must not depend on I/O order.
14
+ */
15
+ async function crawlFrameworkPkgs(root, isFrameworkPkg) {
16
+ const rootPkgJsonPath = await findClosestPkgJsonPath(root);
17
+ if (!rootPkgJsonPath) return {
18
+ framework: [],
19
+ include: []
20
+ };
21
+ const framework = /* @__PURE__ */ new Set();
22
+ const include = [];
23
+ const visited = /* @__PURE__ */ new Set([rootPkgJsonPath]);
24
+ let discovered = /* @__PURE__ */ new Map();
25
+ let level = [{
26
+ pkgJsonPath: rootPkgJsonPath,
27
+ pkgJson: await readJson(rootPkgJsonPath),
28
+ chain: []
29
+ }];
30
+ async function visit(parent, dep) {
31
+ const pkgJsonPath = await findDepPkgJsonPath(dep, parent.pkgJsonPath);
32
+ if (!pkgJsonPath) return;
33
+ if (isFrameworkPkg(dep)) framework.add(dep);
34
+ if (visited.has(pkgJsonPath)) return;
35
+ const chain = [...parent.chain, dep];
36
+ const found = discovered.get(pkgJsonPath);
37
+ if (found) {
38
+ if (compareChains(chain, found.chain) < 0) found.chain = chain;
39
+ return;
40
+ }
41
+ const node = {
42
+ pkgJsonPath,
43
+ chain
44
+ };
45
+ discovered.set(pkgJsonPath, node);
46
+ node.pkgJson = await readJson(pkgJsonPath).catch(() => void 0);
47
+ }
48
+ while (level.length > 0) {
49
+ const tasks = [];
50
+ discovered = /* @__PURE__ */ new Map();
51
+ for (const node of level) {
52
+ const isRoot = node.chain.length === 0;
53
+ const deps = Object.keys(node.pkgJson.dependencies ?? {});
54
+ if (isRoot) deps.push(...Object.keys(node.pkgJson.devDependencies ?? {}));
55
+ for (const dep of deps) {
56
+ const flag = isFrameworkPkg(dep);
57
+ if (flag === false || isRoot && !flag) continue;
58
+ tasks.push(visit(node, dep));
59
+ }
60
+ }
61
+ await Promise.all(tasks);
62
+ level = [];
63
+ tasks.length = 0;
64
+ for (const node of discovered.values()) {
65
+ visited.add(node.pkgJsonPath);
66
+ if (!node.pkgJson) continue;
67
+ tasks.push(pkgNeedsOptimization(node.pkgJson, node.pkgJsonPath).then((needed) => {
68
+ if (!needed) {
69
+ level.push(node);
70
+ return;
71
+ }
72
+ const chain = node.chain.join(" > ");
73
+ for (const subpath of getExportsSubpaths(node.pkgJson.exports)) include.push(subpath === "." ? chain : chain + subpath.slice(1));
74
+ }));
75
+ }
76
+ await Promise.all(tasks);
77
+ }
78
+ return {
79
+ framework: [...framework].sort(),
80
+ include: include.sort()
81
+ };
82
+ }
83
+ function compareChains(left, right) {
84
+ for (let i = 0; i < left.length; i++) if (left[i] !== right[i]) return left[i] < right[i] ? -1 : 1;
85
+ return 0;
86
+ }
87
+ async function findClosestPkgJsonPath(dir) {
88
+ for (let current = dir;; current = path.dirname(current)) {
89
+ const pkg = path.join(current, "package.json");
90
+ if ((await fs.stat(pkg).catch(() => void 0))?.isFile()) return pkg;
91
+ if (path.dirname(current) === current) return;
92
+ }
93
+ }
94
+ async function pkgNeedsOptimization(pkgJson, pkgJsonPath) {
95
+ if (pkgJson.module || pkgJson.type === "module") return false;
96
+ if (pkgJson.exports) return !exportsHasEsmEntry(pkgJson.exports) && exportsHasJsEntry(pkgJson.exports);
97
+ if (pkgJson.main) {
98
+ const entryExt = path.extname(pkgJson.main);
99
+ return !entryExt || entryExt === ".js" || entryExt === ".cjs";
100
+ }
101
+ try {
102
+ await fs.access(path.join(path.dirname(pkgJsonPath), "index.js"));
103
+ return true;
104
+ } catch {
105
+ return false;
106
+ }
107
+ }
108
+ function exportsHasEsmEntry(exportsField) {
109
+ if (typeof exportsField === "string") return exportsField.endsWith(".mjs");
110
+ if (Array.isArray(exportsField)) return exportsField.some(exportsHasEsmEntry);
111
+ if (exportsField && typeof exportsField === "object") return Object.entries(exportsField).some(([key, value]) => key === "import" || key === "module" || exportsHasEsmEntry(value));
112
+ return false;
113
+ }
114
+ function exportsHasJsEntry(exportsField) {
115
+ if (typeof exportsField === "string") return /\.[cm]?js$/.test(exportsField);
116
+ if (Array.isArray(exportsField)) return exportsField.some(exportsHasJsEntry);
117
+ if (exportsField && typeof exportsField === "object") return Object.entries(exportsField).some(([key, value]) => key !== "types" && exportsHasJsEntry(value));
118
+ return false;
119
+ }
120
+ function getExportsSubpaths(exportsField) {
121
+ if (!exportsField || typeof exportsField !== "object" || Array.isArray(exportsField)) return ["."];
122
+ const subpaths = [];
123
+ let hasSubpathKeys = false;
124
+ for (const [key, value] of Object.entries(exportsField)) {
125
+ if (!key.startsWith(".")) continue;
126
+ hasSubpathKeys = true;
127
+ if ((key === "." || /^\.\/[^*.]+$/.test(key)) && exportsHasJsEntry(value)) subpaths.push(key);
128
+ }
129
+ return hasSubpathKeys ? subpaths : ["."];
130
+ }
131
+ async function findDepPkgJsonPath(dep, parent) {
132
+ if (pnp) try {
133
+ const depRoot = pnp.resolveToUnqualified(dep, parent);
134
+ return depRoot ? path.join(depRoot, "package.json") : void 0;
135
+ } catch {
136
+ return;
137
+ }
138
+ for (let root = parent;; root = path.dirname(root)) {
139
+ const pkg = path.join(root, "node_modules", dep, "package.json");
140
+ try {
141
+ await fs.access(pkg);
142
+ return fsSync.realpathSync(pkg);
143
+ } catch {
144
+ if (path.dirname(root) === root) return;
145
+ }
146
+ }
147
+ }
148
+ async function readJson(pkgJsonPath) {
149
+ return JSON.parse(await fs.readFile(pkgJsonPath, "utf8"));
150
+ }
151
+ /**
152
+ * Vite config for a project using Fumadocs, derived from its installed dependencies.
153
+ *
154
+ * Our packages are served as source rather than pre-bundled, which means Vite also serves their
155
+ * dependencies raw and no longer discovers the CommonJS ones on its own. The crawl walks the
156
+ * project's tree to name them explicitly:
157
+ * https://vite.dev/config/dep-optimization-options#optimizedeps-exclude
158
+ */
159
+ async function getConfig(options) {
160
+ const { framework, include } = await crawlCached(options.root);
161
+ return {
162
+ optimizeDeps: {
163
+ include,
164
+ exclude: framework
165
+ },
166
+ ssr: { noExternal: framework },
167
+ resolve: options.isBuild ? { alias: buildAlias } : {}
168
+ };
169
+ }
170
+ const buildAlias = [{
171
+ find: /^use-sync-external-store\/shim$/,
172
+ replacement: "react"
173
+ }];
174
+ const cache = /* @__PURE__ */ new Map();
175
+ async function crawlCached(root) {
176
+ const key = await getInstallKey(root);
177
+ const cached = key && cache.get(root);
178
+ if (cached && cached.key === key) return cached.result;
179
+ const result = crawlFrameworkPkgs(root, (name) => {
180
+ if (name.startsWith("@fumadocs/") || name.startsWith("fumadocs-")) return true;
181
+ switch (name) {
182
+ case "vite":
183
+ case "waku":
184
+ case "shiki": return false;
185
+ }
186
+ });
187
+ if (key) {
188
+ cache.set(root, {
189
+ key,
190
+ result
191
+ });
192
+ result.catch(() => cache.delete(root));
193
+ }
194
+ return result;
195
+ }
196
+ const installStateFiles = [
197
+ "node_modules/.pnpm/lock.yaml",
198
+ "node_modules/.package-lock.json",
199
+ "node_modules/.yarn-state.yml",
200
+ "bun.lock"
201
+ ];
202
+ async function getInstallKey(root) {
203
+ for (let dir = root;; dir = path.dirname(dir)) {
204
+ for (const file of installStateFiles) {
205
+ const filePath = path.join(dir, file);
206
+ const stat = await fs.stat(filePath).catch(() => void 0);
207
+ if (stat) return `${filePath}:${stat.mtimeMs}:${stat.size}`;
208
+ }
209
+ if (path.dirname(dir) === dir) return;
210
+ }
211
+ }
212
+ //#endregion
213
+ export { getConfig };
@@ -6,7 +6,7 @@ function server() {
6
6
  if (file.startsWith("./")) file = file.slice(2);
7
7
  return {
8
8
  path: file,
9
- fullPath: PathUtils.joinPath(base, file)
9
+ fullPath: !base || base.endsWith("/") ? base + file : `${base}/${file}`
10
10
  };
11
11
  }
12
12
  function mapDocData(entry) {
@@ -109,7 +109,7 @@ function mdx(forcedConfig, pluginOptions = {}) {
109
109
  }
110
110
  if ("_fumadocs_skipViteConfig" in config && config._fumadocs_skipViteConfig) return;
111
111
  if (!updateViteConfig) return;
112
- const { getConfig } = await import("../dist-ReDy_RCL.js");
112
+ const { getConfig } = await import("../dist-DrAxpp76.js");
113
113
  return getConfig({
114
114
  root: core.root,
115
115
  isBuild: env.command === "build"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fumadocs-mdx",
3
- "version": "15.4.4",
3
+ "version": "15.4.6",
4
4
  "description": "The built-in source for Fumadocs",
5
5
  "keywords": [
6
6
  "Docs",
@@ -83,7 +83,7 @@
83
83
  "@types/node": "^26.6.1",
84
84
  "@types/picomatch": "^4.0.3",
85
85
  "@types/react": "^19.3.0",
86
- "fumadocs-core": "16.15.14",
86
+ "fumadocs-core": "16.15.18",
87
87
  "mdast-util-directive": "^3.1.0",
88
88
  "next": "^16.3.5",
89
89
  "react": "^19.3.0",
@@ -1,231 +0,0 @@
1
- import fsSync from "node:fs";
2
- import fs from "node:fs/promises";
3
- import path from "node:path";
4
- import { createRequire } from "node:module";
5
- //#region ../vite/dist/index.js
6
- let pnp;
7
- let pnpWorkspaceLocators = [];
8
- if (process.versions.pnp) try {
9
- pnp = createRequire(import.meta.url)("pnpapi");
10
- pnpWorkspaceLocators = pnp.getDependencyTreeRoots();
11
- } catch {}
12
- async function crawlFrameworkPkgs(options) {
13
- const pkgJsonPath = await findClosestPkgJsonPath(options.root);
14
- if (!pkgJsonPath) return {
15
- optimizeDeps: {
16
- include: [],
17
- exclude: []
18
- },
19
- ssr: {
20
- noExternal: [],
21
- external: []
22
- }
23
- };
24
- const pkgJson = await readJson(pkgJsonPath).catch((e) => {
25
- throw new Error(`Unable to read ${pkgJsonPath}`, { cause: e });
26
- });
27
- const optimizeDepsIncludeByPkgJsonPath = /* @__PURE__ */ new Map();
28
- const optimizeDepsSubpathsByPkgJsonPath = /* @__PURE__ */ new Map();
29
- let optimizeDepsInclude = [];
30
- let optimizeDepsExclude = [];
31
- let ssrNoExternal = [];
32
- let ssrExternal = [];
33
- await crawl(pkgJsonPath, pkgJson);
34
- optimizeDepsInclude = [...optimizeDepsIncludeByPkgJsonPath.entries()].flatMap(([depPkgJsonPath, chain]) => (optimizeDepsSubpathsByPkgJsonPath.get(depPkgJsonPath) ?? ["."]).map((subpath) => subpath === "." ? chain : chain + subpath.slice(1)));
35
- if (options.viteUserConfig) {
36
- const userOptimizeDepsExclude = options.viteUserConfig.optimizeDeps?.exclude;
37
- if (userOptimizeDepsExclude) optimizeDepsInclude = optimizeDepsInclude.filter((dep) => !isDepExcluded(dep, userOptimizeDepsExclude));
38
- const userOptimizeDepsInclude = options.viteUserConfig.optimizeDeps?.include;
39
- if (userOptimizeDepsInclude) optimizeDepsExclude = optimizeDepsExclude.filter((dep) => !isDepIncluded(dep, userOptimizeDepsInclude));
40
- const userSsrExternal = options.viteUserConfig.ssr?.external;
41
- if (userSsrExternal) ssrNoExternal = ssrNoExternal.filter((dep) => !isDepExternaled(dep, userSsrExternal));
42
- const userSsrNoExternal = options.viteUserConfig.ssr?.noExternal;
43
- if (userSsrNoExternal) ssrExternal = ssrExternal.filter((dep) => !isDepNoExternaled(dep, userSsrNoExternal));
44
- }
45
- optimizeDepsInclude.sort();
46
- optimizeDepsExclude.sort();
47
- ssrExternal.sort();
48
- ssrNoExternal.sort();
49
- return {
50
- optimizeDeps: {
51
- include: optimizeDepsInclude,
52
- exclude: optimizeDepsExclude
53
- },
54
- ssr: {
55
- noExternal: ssrNoExternal,
56
- external: ssrExternal
57
- }
58
- };
59
- async function crawl(currentPkgJsonPath, currentPkgJson, parentDepNames = [], parentIsFrameworkPkg = false, hasFrameworkAncestor = false) {
60
- const crawlDevDependencies = parentDepNames.length === 0 || isPrivateWorkspacePackage(currentPkgJsonPath, currentPkgJson, options.workspaceRoot);
61
- const deps = [...Object.keys(currentPkgJson.dependencies ?? {}), ...crawlDevDependencies ? Object.keys(currentPkgJson.devDependencies ?? {}) : []].filter((dep) => !parentDepNames.includes(dep));
62
- await Promise.all(deps.map(async (dep) => {
63
- const frameworkByName = options.isFrameworkPkgByName?.(dep);
64
- const semiFrameworkByName = options.isSemiFrameworkPkgByName?.(dep);
65
- if (frameworkByName === false || semiFrameworkByName === false) return;
66
- const depPkgJsonPath = await findDepPkgJsonPath(dep, currentPkgJsonPath, !!options.workspaceRoot);
67
- if (!depPkgJsonPath) return;
68
- const depPkgJson = await readJson(depPkgJsonPath).catch(() => {});
69
- if (!depPkgJson) return;
70
- const isFrameworkPkg = frameworkByName === true || options.isFrameworkPkgByJson?.(depPkgJson) === true;
71
- const isSemiFrameworkPkg = semiFrameworkByName === true || options.isSemiFrameworkPkgByJson?.(depPkgJson) === true;
72
- const depChain = parentDepNames.concat(dep);
73
- if (isFrameworkPkg || isSemiFrameworkPkg) {
74
- if (isFrameworkPkg) {
75
- pushUnique(optimizeDepsExclude, dep);
76
- pushUnique(ssrNoExternal, dep);
77
- } else pushUnique(ssrNoExternal, dep);
78
- await crawl(depPkgJsonPath, depPkgJson, depChain, true, true);
79
- return;
80
- }
81
- if (!hasFrameworkAncestor) return;
82
- if (await pkgNeedsOptimization(depPkgJson, depPkgJsonPath)) addOptimizedDep(depPkgJsonPath, depChain, depPkgJson);
83
- else await crawl(depPkgJsonPath, depPkgJson, depChain, false, true);
84
- if (!options.isBuild && parentIsFrameworkPkg) pushUnique(ssrExternal, dep);
85
- }));
86
- }
87
- function addOptimizedDep(depPkgJsonPath, depChain, depPkgJson) {
88
- const includePath = depChain.join(" > ");
89
- const current = optimizeDepsIncludeByPkgJsonPath.get(depPkgJsonPath);
90
- if (!current || compareDepChains(includePath, current) < 0) optimizeDepsIncludeByPkgJsonPath.set(depPkgJsonPath, includePath);
91
- if (!optimizeDepsSubpathsByPkgJsonPath.has(depPkgJsonPath)) optimizeDepsSubpathsByPkgJsonPath.set(depPkgJsonPath, getExportsSubpaths(depPkgJson.exports));
92
- }
93
- }
94
- async function findClosestPkgJsonPath(dir, predicate) {
95
- let currentDir = dir.endsWith("package.json") ? path.dirname(dir) : dir;
96
- while (currentDir) {
97
- const pkg = path.join(currentDir, "package.json");
98
- try {
99
- if ((await fs.stat(pkg)).isFile() && (!predicate || await predicate(pkg))) return pkg;
100
- } catch {}
101
- const nextDir = path.dirname(currentDir);
102
- if (nextDir === currentDir) break;
103
- currentDir = nextDir;
104
- }
105
- }
106
- async function pkgNeedsOptimization(pkgJson, pkgJsonPath) {
107
- if (pkgJson.module || pkgJson.type === "module") return false;
108
- if (pkgJson.exports) return !exportsHasEsmEntry(pkgJson.exports) && exportsHasJsEntry(pkgJson.exports);
109
- if (pkgJson.main) {
110
- const entryExt = path.extname(pkgJson.main);
111
- return !entryExt || entryExt === ".js" || entryExt === ".cjs";
112
- }
113
- try {
114
- await fs.access(path.join(path.dirname(pkgJsonPath), "index.js"));
115
- return true;
116
- } catch {
117
- return false;
118
- }
119
- }
120
- function exportsHasEsmEntry(exportsField) {
121
- if (typeof exportsField === "string") return exportsField.endsWith(".mjs");
122
- if (Array.isArray(exportsField)) return exportsField.some(exportsHasEsmEntry);
123
- if (exportsField && typeof exportsField === "object") return Object.entries(exportsField).some(([key, value]) => key === "import" || key === "module" || exportsHasEsmEntry(value));
124
- return false;
125
- }
126
- function exportsHasJsEntry(exportsField) {
127
- if (typeof exportsField === "string") return /\.[cm]?js$/.test(exportsField);
128
- if (Array.isArray(exportsField)) return exportsField.some(exportsHasJsEntry);
129
- if (exportsField && typeof exportsField === "object") return Object.entries(exportsField).some(([key, value]) => key !== "types" && exportsHasJsEntry(value));
130
- return false;
131
- }
132
- function getExportsSubpaths(exportsField) {
133
- if (!exportsField || typeof exportsField !== "object" || Array.isArray(exportsField)) return ["."];
134
- const subpathKeys = Object.keys(exportsField).filter((key) => key.startsWith("."));
135
- if (subpathKeys.length === 0) return ["."];
136
- return subpathKeys.filter((key) => (key === "." || /^\.\/[^*.]+$/.test(key)) && exportsHasJsEntry(exportsField[key]));
137
- }
138
- async function findDepPkgJsonPath(dep, parent, usePnpWorkspaceLocators) {
139
- if (pnp) {
140
- if (usePnpWorkspaceLocators) try {
141
- const locator = pnpWorkspaceLocators.find((root) => root.name === dep);
142
- if (locator) {
143
- const pkgPath = pnp.getPackageInformation(locator).packageLocation;
144
- return path.resolve(pkgPath, "package.json");
145
- }
146
- } catch {}
147
- try {
148
- const depRoot = pnp.resolveToUnqualified(dep, parent);
149
- if (!depRoot) return;
150
- return path.join(depRoot, "package.json");
151
- } catch {
152
- return;
153
- }
154
- }
155
- let root = parent;
156
- while (root) {
157
- const pkg = path.join(root, "node_modules", dep, "package.json");
158
- try {
159
- await fs.access(pkg);
160
- return fsSync.realpathSync(pkg);
161
- } catch {}
162
- const nextRoot = path.dirname(root);
163
- if (nextRoot === root) break;
164
- root = nextRoot;
165
- }
166
- }
167
- async function readJson(pkgJsonPath) {
168
- return JSON.parse(await fs.readFile(pkgJsonPath, "utf8"));
169
- }
170
- function isPrivateWorkspacePackage(pkgJsonPath, pkgJson, workspaceRoot) {
171
- return !!(workspaceRoot && pkgJson.private && !pkgJsonPath.match(/[/\\]node_modules[/\\]/) && !path.relative(workspaceRoot, pkgJsonPath).startsWith(".."));
172
- }
173
- function pushUnique(array, value) {
174
- if (!array.includes(value)) array.push(value);
175
- }
176
- function compareDepChains(left, right) {
177
- const leftDepth = left.split(" > ").length;
178
- const rightDepth = right.split(" > ").length;
179
- if (leftDepth !== rightDepth) return leftDepth - rightDepth;
180
- return left.localeCompare(right);
181
- }
182
- function isDepIncluded(dep, optimizeDepsInclude) {
183
- return optimizeDepsInclude.some((include) => dep === include);
184
- }
185
- function isDepExcluded(dep, optimizeDepsExclude) {
186
- return optimizeDepsExclude.some((exclude) => dep === exclude || dep.startsWith(`${exclude} > `));
187
- }
188
- function isDepNoExternaled(dep, ssrNoExternal) {
189
- if (typeof ssrNoExternal === "boolean") return ssrNoExternal;
190
- return (Array.isArray(ssrNoExternal) ? ssrNoExternal : [ssrNoExternal]).some((noExternal) => {
191
- if (typeof noExternal === "string") return dep === noExternal;
192
- return noExternal.test(dep);
193
- });
194
- }
195
- function isDepExternaled(dep, ssrExternal) {
196
- if (typeof ssrExternal === "boolean") return ssrExternal;
197
- return ssrExternal.some((external) => dep === external);
198
- }
199
- /**
200
- * Vite config for a project using Fumadocs, derived from its installed dependencies.
201
- *
202
- * Our packages are served as source rather than pre-bundled, which means Vite also serves their
203
- * dependencies raw and no longer discovers the CommonJS ones on its own. The crawl walks the
204
- * project's tree to name them explicitly:
205
- * https://vite.dev/config/dep-optimization-options#optimizedeps-exclude
206
- */
207
- async function getConfig(options) {
208
- const out = await crawlFrameworkPkgs({
209
- root: options.root,
210
- isBuild: options.isBuild,
211
- isFrameworkPkgByName(pkgName) {
212
- if (pkgName.startsWith("@fumadocs/") || pkgName.startsWith("fumadocs-")) return true;
213
- switch (pkgName) {
214
- case "vite":
215
- case "waku":
216
- case "shiki": return false;
217
- }
218
- }
219
- });
220
- return {
221
- optimizeDeps: out.optimizeDeps,
222
- ssr: { noExternal: out.ssr.noExternal },
223
- resolve: options.isBuild ? { alias: buildAlias } : {}
224
- };
225
- }
226
- const buildAlias = [{
227
- find: /^use-sync-external-store\/shim$/,
228
- replacement: "react"
229
- }];
230
- //#endregion
231
- export { getConfig };