fumadocs-mdx 12.0.3 → 13.0.1

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.
Files changed (80) hide show
  1. package/dist/bin.cjs +1116 -359
  2. package/dist/bin.js +4 -4
  3. package/dist/build-mdx-CCNr86q6.d.ts +53 -0
  4. package/dist/build-mdx-D-r3_eQL.d.cts +53 -0
  5. package/dist/bun/index.cjs +196 -52
  6. package/dist/bun/index.d.cts +8 -3
  7. package/dist/bun/index.d.ts +8 -3
  8. package/dist/bun/index.js +19 -10
  9. package/dist/{chunk-QAUWMR5D.js → chunk-3J3WL7WN.js} +23 -5
  10. package/dist/{chunk-6Y5JDZHD.js → chunk-CXA4JO4Z.js} +1 -21
  11. package/dist/chunk-EELYB2XC.js +207 -0
  12. package/dist/{chunk-46UPKP5R.js → chunk-II3H5ZVZ.js} +5 -5
  13. package/dist/{chunk-LGYVNESJ.js → chunk-JVZFH6ND.js} +6 -22
  14. package/dist/{chunk-LMG6UWCL.js → chunk-K5ZLPEIQ.js} +56 -16
  15. package/dist/{chunk-OMAMTKDE.js → chunk-KILFIBVW.js} +3 -12
  16. package/dist/chunk-NVRDCY6Z.js +30 -0
  17. package/dist/{chunk-RMDXSZYE.js → chunk-XQ5O7IPO.js} +31 -24
  18. package/dist/chunk-XZY2AWJI.js +81 -0
  19. package/dist/{chunk-VXEBLM4X.js → chunk-YVCR6FUH.js} +1 -1
  20. package/dist/config/index.cjs +56 -16
  21. package/dist/config/index.d.cts +2 -1
  22. package/dist/config/index.d.ts +2 -1
  23. package/dist/config/index.js +1 -1
  24. package/dist/{define-DJbJduHy.d.ts → core-B6j6Fxse.d.cts} +89 -2
  25. package/dist/{define-DJbJduHy.d.cts → core-B6j6Fxse.d.ts} +89 -2
  26. package/dist/index.cjs +0 -109
  27. package/dist/index.d.cts +75 -9
  28. package/dist/index.d.ts +75 -9
  29. package/dist/index.js +0 -11
  30. package/dist/{load-UUXLUBHL.js → load-MNG3CLET.js} +1 -3
  31. package/dist/next/index.cjs +298 -234
  32. package/dist/next/index.d.cts +2 -11
  33. package/dist/next/index.d.ts +2 -11
  34. package/dist/next/index.js +177 -141
  35. package/dist/node/loader.cjs +228 -85
  36. package/dist/node/loader.js +19 -9
  37. package/dist/plugins/json-schema.cjs +162 -0
  38. package/dist/plugins/json-schema.d.cts +24 -0
  39. package/dist/plugins/json-schema.d.ts +24 -0
  40. package/dist/plugins/json-schema.js +78 -0
  41. package/dist/runtime/next/async.cjs +108 -70
  42. package/dist/runtime/next/async.d.cts +9 -6
  43. package/dist/runtime/next/async.d.ts +9 -6
  44. package/dist/runtime/next/async.js +8 -18
  45. package/dist/runtime/next/index.cjs +25 -14
  46. package/dist/runtime/next/index.d.cts +11 -8
  47. package/dist/runtime/next/index.d.ts +11 -8
  48. package/dist/runtime/next/index.js +2 -2
  49. package/dist/runtime/vite/browser.cjs +7 -3
  50. package/dist/runtime/vite/browser.d.cts +56 -7
  51. package/dist/runtime/vite/browser.d.ts +56 -7
  52. package/dist/runtime/vite/browser.js +2 -1
  53. package/dist/runtime/vite/server.cjs +40 -34
  54. package/dist/runtime/vite/server.d.cts +13 -10
  55. package/dist/runtime/vite/server.d.ts +13 -10
  56. package/dist/runtime/vite/server.js +8 -23
  57. package/dist/{types-TeHjsmja.d.ts → types-AGzTfBmf.d.ts} +3 -10
  58. package/dist/{types-BRx1QsIJ.d.cts → types-DKGMoay5.d.cts} +3 -10
  59. package/dist/vite/index.cjs +443 -249
  60. package/dist/vite/index.d.cts +23 -10
  61. package/dist/vite/index.d.ts +23 -10
  62. package/dist/vite/index.js +213 -36
  63. package/dist/{loader-mdx.cjs → webpack/index.cjs} +268 -82
  64. package/dist/{loader-mdx.d.ts → webpack/index.d.cts} +1 -0
  65. package/dist/{loader-mdx.d.cts → webpack/index.d.ts} +1 -0
  66. package/dist/webpack/index.js +44 -0
  67. package/loader-mdx.cjs +1 -1
  68. package/package.json +30 -16
  69. package/dist/browser-BupUnhpC.d.ts +0 -98
  70. package/dist/browser-R0x9IPaQ.d.cts +0 -98
  71. package/dist/chunk-ADR6R7HM.js +0 -29
  72. package/dist/chunk-IQAEAI4P.js +0 -66
  73. package/dist/chunk-XMFLD5J6.js +0 -30
  74. package/dist/chunk-ZLCSVXCD.js +0 -10
  75. package/dist/chunk-ZX7TM4AR.js +0 -127
  76. package/dist/loader-mdx.js +0 -25
  77. package/dist/postinstall-SCSXM4IM.js +0 -10
  78. package/dist/shared-CfiiRctw.d.ts +0 -70
  79. package/dist/shared-fFqiuWJC.d.cts +0 -70
  80. package/dist/watcher-HGOH3APP.js +0 -22
@@ -1,6 +1,19 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
- declare function postInstall(configPath?: string, outDir?: string, addJsExtension?: boolean): Promise<void>;
3
+ interface IndexFileOptions {
4
+ /**
5
+ * Runtime compat fallbacks for Vite specific APIs
6
+ *
7
+ * - `bun`: use Bun-specific APIs.
8
+ * - `node`: use Node.js APIs.
9
+ * - `false` (default): no fallback.
10
+ */
11
+ runtime?: 'bun' | 'node' | false;
12
+ /**
13
+ * add `.js` extensions to imports, needed for ESM without bundler resolution
14
+ */
15
+ addJsExtension?: boolean;
16
+ }
4
17
 
5
18
  interface PluginOptions {
6
19
  /**
@@ -8,13 +21,7 @@ interface PluginOptions {
8
21
  *
9
22
  * @defaultValue true
10
23
  */
11
- generateIndexFile?: boolean | {
12
- out?: string;
13
- /**
14
- * add `.js` extensions to imports, needed for ESM without bundler resolution
15
- */
16
- addJsExtension?: boolean;
17
- };
24
+ generateIndexFile?: boolean | IndexFileOptions;
18
25
  /**
19
26
  * @defaultValue source.config.ts
20
27
  */
@@ -25,8 +32,14 @@ interface PluginOptions {
25
32
  * @defaultValue true
26
33
  */
27
34
  updateViteConfig?: boolean;
35
+ /**
36
+ * Output directory of generated files
37
+ *
38
+ * @defaultValue '.source'
39
+ */
40
+ outDir?: string;
28
41
  }
29
-
30
- declare function mdx(config: Record<string, unknown>, options?: PluginOptions): Plugin;
42
+ declare function mdx(config: Record<string, unknown>, pluginOptions?: PluginOptions): Promise<Plugin>;
43
+ declare function postInstall(configPath?: string, pluginOptions?: PluginOptions): Promise<void>;
31
44
 
32
45
  export { type PluginOptions, mdx as default, postInstall };
@@ -1,6 +1,19 @@
1
1
  import { Plugin } from 'vite';
2
2
 
3
- declare function postInstall(configPath?: string, outDir?: string, addJsExtension?: boolean): Promise<void>;
3
+ interface IndexFileOptions {
4
+ /**
5
+ * Runtime compat fallbacks for Vite specific APIs
6
+ *
7
+ * - `bun`: use Bun-specific APIs.
8
+ * - `node`: use Node.js APIs.
9
+ * - `false` (default): no fallback.
10
+ */
11
+ runtime?: 'bun' | 'node' | false;
12
+ /**
13
+ * add `.js` extensions to imports, needed for ESM without bundler resolution
14
+ */
15
+ addJsExtension?: boolean;
16
+ }
4
17
 
5
18
  interface PluginOptions {
6
19
  /**
@@ -8,13 +21,7 @@ interface PluginOptions {
8
21
  *
9
22
  * @defaultValue true
10
23
  */
11
- generateIndexFile?: boolean | {
12
- out?: string;
13
- /**
14
- * add `.js` extensions to imports, needed for ESM without bundler resolution
15
- */
16
- addJsExtension?: boolean;
17
- };
24
+ generateIndexFile?: boolean | IndexFileOptions;
18
25
  /**
19
26
  * @defaultValue source.config.ts
20
27
  */
@@ -25,8 +32,14 @@ interface PluginOptions {
25
32
  * @defaultValue true
26
33
  */
27
34
  updateViteConfig?: boolean;
35
+ /**
36
+ * Output directory of generated files
37
+ *
38
+ * @defaultValue '.source'
39
+ */
40
+ outDir?: string;
28
41
  }
29
-
30
- declare function mdx(config: Record<string, unknown>, options?: PluginOptions): Plugin;
42
+ declare function mdx(config: Record<string, unknown>, pluginOptions?: PluginOptions): Promise<Plugin>;
43
+ declare function postInstall(configPath?: string, pluginOptions?: PluginOptions): Promise<void>;
31
44
 
32
45
  export { type PluginOptions, mdx as default, postInstall };
@@ -1,50 +1,195 @@
1
1
  import {
2
- entry,
3
- postInstall
4
- } from "../chunk-ZX7TM4AR.js";
5
- import "../chunk-6Y5JDZHD.js";
6
- import "../chunk-LGYVNESJ.js";
2
+ ident,
3
+ toImportPath
4
+ } from "../chunk-CXA4JO4Z.js";
7
5
  import {
8
6
  buildConfig
9
7
  } from "../chunk-U4MQ44TS.js";
10
8
  import {
11
9
  toVite
12
- } from "../chunk-VXEBLM4X.js";
10
+ } from "../chunk-YVCR6FUH.js";
13
11
  import {
14
12
  createMdxLoader
15
- } from "../chunk-RMDXSZYE.js";
16
- import "../chunk-QAUWMR5D.js";
17
- import "../chunk-LMG6UWCL.js";
13
+ } from "../chunk-XQ5O7IPO.js";
14
+ import "../chunk-3J3WL7WN.js";
15
+ import "../chunk-K5ZLPEIQ.js";
18
16
  import {
19
17
  ValidationError,
18
+ createCore,
19
+ findConfigFile,
20
20
  validate
21
- } from "../chunk-IQAEAI4P.js";
22
- import {
23
- resolvedConfig
24
- } from "../chunk-XMFLD5J6.js";
21
+ } from "../chunk-EELYB2XC.js";
25
22
  import "../chunk-VWJKRQZR.js";
23
+ import {
24
+ getGlobPatterns
25
+ } from "../chunk-XZY2AWJI.js";
26
26
 
27
27
  // src/vite/index.ts
28
28
  import {
29
29
  mergeConfig
30
30
  } from "vite";
31
31
  import { parse } from "querystring";
32
- import * as fs from "fs/promises";
33
- import * as path from "path";
32
+ import * as path3 from "path";
34
33
  import { load } from "js-yaml";
34
+
35
+ // src/utils/glob-import.ts
36
+ import { globSync } from "tinyglobby";
37
+ import path from "path";
38
+ import { pathToFileURL } from "url";
39
+ function generateGlobImport(patterns, options) {
40
+ let code = "{";
41
+ const result = globSync(patterns, {
42
+ cwd: options.base
43
+ });
44
+ for (const item of result) {
45
+ const fullPath = path.join(options.base, item);
46
+ const url = pathToFileURL(fullPath);
47
+ for (const [k, v] of Object.entries(options.query ?? {})) {
48
+ url.searchParams.set(k, v);
49
+ }
50
+ let line = `${JSON.stringify(item)}: () => import(${JSON.stringify(url.href)})`;
51
+ if (options.import) {
52
+ line += `.then(mod => mod[${JSON.stringify(options.import)}])`;
53
+ }
54
+ code += `${line}, `;
55
+ }
56
+ code += "}";
57
+ return code;
58
+ }
59
+
60
+ // src/plugins/vite.ts
61
+ import path2 from "path";
62
+ function vite(options) {
63
+ let config;
64
+ return {
65
+ config(v) {
66
+ config = v;
67
+ },
68
+ emit() {
69
+ return [
70
+ {
71
+ path: "index.ts",
72
+ content: indexFile(this.configPath, this.outDir, config, options)
73
+ }
74
+ ];
75
+ }
76
+ };
77
+ }
78
+ function indexFile(configPath, outDir, config, options) {
79
+ const { addJsExtension = false, runtime } = options;
80
+ const lines = [
81
+ '/// <reference types="vite/client" />',
82
+ `import { fromConfig } from 'fumadocs-mdx/runtime/vite';`,
83
+ `import type * as Config from '${toImportPath(configPath, {
84
+ relativeTo: outDir,
85
+ jsExtension: addJsExtension
86
+ })}';`,
87
+ "",
88
+ `export const create = fromConfig<typeof Config>();`
89
+ ];
90
+ function docs(name, collection) {
91
+ const obj = [
92
+ ident(`doc: ${doc(name, collection.docs)}`),
93
+ ident(`meta: ${meta(name, collection.meta)}`)
94
+ ].join(",\n");
95
+ return `{
96
+ ${obj}
97
+ }`;
98
+ }
99
+ function doc(name, collection) {
100
+ const patterns = getGlobPatterns(collection);
101
+ const base = getGlobBase(collection);
102
+ const docGlob = generateGlob(patterns, {
103
+ query: {
104
+ collection: name
105
+ },
106
+ base
107
+ });
108
+ if (collection.async) {
109
+ const headBlob = generateGlob(patterns, {
110
+ query: {
111
+ only: "frontmatter",
112
+ collection: name
113
+ },
114
+ import: "frontmatter",
115
+ base
116
+ });
117
+ return `create.docLazy("${name}", "${base}", ${headBlob}, ${docGlob})`;
118
+ }
119
+ return `create.doc("${name}", "${base}", ${docGlob})`;
120
+ }
121
+ function meta(name, collection) {
122
+ const patterns = getGlobPatterns(collection);
123
+ const base = getGlobBase(collection);
124
+ return `create.meta("${name}", "${base}", ${generateGlob(patterns, {
125
+ import: "default",
126
+ base,
127
+ query: {
128
+ collection: name
129
+ }
130
+ })})`;
131
+ }
132
+ function generateGlob(patterns, options2) {
133
+ patterns = mapGlobPatterns(patterns);
134
+ if (runtime === "node" || runtime === "bun") {
135
+ return generateGlobImport(patterns, options2);
136
+ } else {
137
+ return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
138
+ {
139
+ ...options2,
140
+ base: path2.relative(outDir, options2.base)
141
+ },
142
+ null,
143
+ 2
144
+ )})`;
145
+ }
146
+ }
147
+ for (const [name, collection] of config.collections.entries()) {
148
+ let body;
149
+ if (collection.type === "docs") {
150
+ body = docs(name, collection);
151
+ } else if (collection.type === "meta") {
152
+ body = meta(name, collection);
153
+ } else {
154
+ body = doc(name, collection);
155
+ }
156
+ lines.push("");
157
+ lines.push(`export const ${name} = ${body};`);
158
+ }
159
+ return lines.join("\n");
160
+ }
161
+ function mapGlobPatterns(patterns) {
162
+ return patterns.map(enforceRelative);
163
+ }
164
+ function enforceRelative(file) {
165
+ if (file.startsWith("./")) return file;
166
+ if (file.startsWith("/")) return `.${file}`;
167
+ return `./${file}`;
168
+ }
169
+ function getGlobBase(collection) {
170
+ let dir = collection.dir;
171
+ if (Array.isArray(dir)) {
172
+ if (dir.length !== 1)
173
+ throw new Error(
174
+ `[Fumadocs MDX] Vite Plugin doesn't support multiple \`dir\` for a collection at the moment.`
175
+ );
176
+ dir = dir[0];
177
+ }
178
+ return enforceRelative(dir);
179
+ }
180
+
181
+ // src/vite/index.ts
35
182
  var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
36
- function mdx(config, options = {}) {
37
- const {
38
- generateIndexFile = true,
39
- updateViteConfig = true,
40
- configPath = "source.config.ts"
41
- } = options;
42
- const loaded = buildConfig(config);
43
- const mdxLoader = toVite(createMdxLoader(resolvedConfig(loaded)));
44
- async function transformMeta(path2, query, value) {
45
- const isJson = path2.endsWith(".json");
183
+ async function mdx(config, pluginOptions = {}) {
184
+ const options = applyDefaults(pluginOptions);
185
+ const core = await createViteCore(options).init({
186
+ config: buildConfig(config)
187
+ });
188
+ const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
189
+ async function transformMeta(path4, query, value) {
190
+ const isJson = path4.endsWith(".json");
46
191
  const parsed = parse(query);
47
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
192
+ const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
48
193
  if (!collection) return null;
49
194
  let schema;
50
195
  switch (collection.type) {
@@ -65,8 +210,8 @@ function mdx(config, options = {}) {
65
210
  const out = await validate(
66
211
  schema,
67
212
  data,
68
- { path: path2, source: value },
69
- `invalid data in ${path2}`
213
+ { path: path4, source: value },
214
+ `invalid data in ${path4}`
70
215
  );
71
216
  return {
72
217
  code: isJson ? JSON.stringify(out) : `export default ${JSON.stringify(out)}`,
@@ -78,7 +223,7 @@ function mdx(config, options = {}) {
78
223
  // needed, otherwise other plugins will be executed before our `transform`.
79
224
  enforce: "pre",
80
225
  config(config2) {
81
- if (!updateViteConfig) return config2;
226
+ if (!options.updateViteConfig) return config2;
82
227
  return mergeConfig(config2, {
83
228
  optimizeDeps: {
84
229
  exclude: FumadocsDeps
@@ -90,16 +235,16 @@ function mdx(config, options = {}) {
90
235
  });
91
236
  },
92
237
  async buildStart() {
93
- if (!generateIndexFile) return;
94
- const { out = "source.generated.ts", addJsExtension } = typeof generateIndexFile === "object" ? generateIndexFile : {};
95
- console.log("[Fumadocs MDX] Generating index files");
96
- const dir = path.dirname(out);
97
- await fs.mkdir(dir, { recursive: true });
98
- await fs.writeFile(out, entry(configPath, loaded, dir, addJsExtension));
238
+ await core.emitAndWrite();
239
+ },
240
+ async configureServer(server) {
241
+ await core.initServer({
242
+ watcher: server.watcher
243
+ });
99
244
  },
100
245
  async transform(value, id) {
101
246
  const [file, query = ""] = id.split("?");
102
- const ext = path.extname(file);
247
+ const ext = path3.extname(file);
103
248
  try {
104
249
  if ([".yaml", ".json"].includes(ext))
105
250
  return await transformMeta(file, query, value);
@@ -114,6 +259,38 @@ function mdx(config, options = {}) {
114
259
  }
115
260
  };
116
261
  }
262
+ async function postInstall(configPath = findConfigFile(), pluginOptions = {}) {
263
+ const { loadConfig } = await import("../load-MNG3CLET.js");
264
+ const options = applyDefaults(pluginOptions);
265
+ const core = await createViteCore(options).init({
266
+ config: loadConfig(configPath, options.outDir, true)
267
+ });
268
+ await core.emitAndWrite();
269
+ }
270
+ function createViteCore({
271
+ configPath,
272
+ outDir,
273
+ generateIndexFile
274
+ }) {
275
+ return createCore(
276
+ {
277
+ environment: "vite",
278
+ configPath,
279
+ outDir
280
+ },
281
+ [
282
+ generateIndexFile !== false && vite(typeof generateIndexFile === "object" ? generateIndexFile : {})
283
+ ]
284
+ );
285
+ }
286
+ function applyDefaults(options) {
287
+ return {
288
+ updateViteConfig: options.updateViteConfig ?? true,
289
+ generateIndexFile: options.generateIndexFile ?? true,
290
+ configPath: options.configPath ?? "source.config.ts",
291
+ outDir: options.outDir ?? ".source"
292
+ };
293
+ }
117
294
  export {
118
295
  mdx as default,
119
296
  postInstall