fumadocs-mdx 13.0.0 → 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 (55) hide show
  1. package/dist/bin.cjs +272 -240
  2. package/dist/{build-mdx-CzrQDBRZ.d.ts → build-mdx-CCNr86q6.d.ts} +1 -1
  3. package/dist/{build-mdx-BHG-_uxo.d.cts → build-mdx-D-r3_eQL.d.cts} +1 -1
  4. package/dist/bun/index.cjs +114 -34
  5. package/dist/bun/index.d.cts +8 -3
  6. package/dist/bun/index.d.ts +8 -3
  7. package/dist/bun/index.js +20 -10
  8. package/dist/{chunk-6Y5JDZHD.js → chunk-CXA4JO4Z.js} +1 -21
  9. package/dist/chunk-EELYB2XC.js +207 -0
  10. package/dist/{chunk-CEA6MYJU.js → chunk-XQ5O7IPO.js} +29 -27
  11. package/dist/chunk-XZY2AWJI.js +81 -0
  12. package/dist/{chunk-XV5Z4BFL.js → chunk-YVCR6FUH.js} +1 -1
  13. package/dist/config/index.d.cts +2 -2
  14. package/dist/config/index.d.ts +2 -2
  15. package/dist/{define-BCNh3n4O.d.cts → core-B6j6Fxse.d.cts} +101 -38
  16. package/dist/{define-bck_EB4t.d.ts → core-B6j6Fxse.d.ts} +101 -38
  17. package/dist/index.d.cts +7 -2
  18. package/dist/index.d.ts +7 -2
  19. package/dist/next/index.cjs +195 -163
  20. package/dist/next/index.js +79 -71
  21. package/dist/node/loader.cjs +120 -35
  22. package/dist/node/loader.js +10 -5
  23. package/dist/plugins/json-schema.cjs +103 -2
  24. package/dist/plugins/json-schema.d.cts +12 -4
  25. package/dist/plugins/json-schema.d.ts +12 -4
  26. package/dist/plugins/json-schema.js +40 -2
  27. package/dist/runtime/next/async.d.cts +4 -4
  28. package/dist/runtime/next/async.d.ts +4 -4
  29. package/dist/runtime/next/async.js +3 -3
  30. package/dist/runtime/next/index.d.cts +5 -5
  31. package/dist/runtime/next/index.d.ts +5 -5
  32. package/dist/runtime/vite/browser.d.cts +3 -3
  33. package/dist/runtime/vite/browser.d.ts +3 -3
  34. package/dist/runtime/vite/server.d.cts +3 -3
  35. package/dist/runtime/vite/server.d.ts +3 -3
  36. package/dist/{types-1cCFEzWt.d.ts → types-AGzTfBmf.d.ts} +1 -1
  37. package/dist/{types-D5NhXTJY.d.cts → types-DKGMoay5.d.cts} +1 -1
  38. package/dist/vite/index.cjs +123 -91
  39. package/dist/vite/index.js +30 -27
  40. package/dist/{loader-mdx.cjs → webpack/index.cjs} +151 -58
  41. package/dist/webpack/index.js +44 -0
  42. package/loader-mdx.cjs +1 -1
  43. package/package.json +4 -3
  44. package/dist/chunk-4MAYA5QX.js +0 -44
  45. package/dist/chunk-HI62EXSB.js +0 -127
  46. package/dist/loader-mdx.js +0 -39
  47. package/dist/plugins/index.cjs +0 -78
  48. package/dist/plugins/index.d.cts +0 -7
  49. package/dist/plugins/index.d.ts +0 -7
  50. package/dist/plugins/index.js +0 -6
  51. package/dist/remark-postprocess-K233ZVBK.d.cts +0 -22
  52. package/dist/remark-postprocess-K233ZVBK.d.ts +0 -22
  53. package/dist/watcher-WXJDWRZY.js +0 -22
  54. /package/dist/{loader-mdx.d.cts → webpack/index.d.cts} +0 -0
  55. /package/dist/{loader-mdx.d.ts → webpack/index.d.ts} +0 -0
package/dist/bin.cjs CHANGED
@@ -34,13 +34,6 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
34
34
  function findConfigFile() {
35
35
  return import_node_path.default.resolve("source.config.ts");
36
36
  }
37
- function resolvedConfig(loaded) {
38
- return {
39
- getConfig() {
40
- return loaded;
41
- }
42
- };
43
- }
44
37
  var import_node_path, import_promises;
45
38
  var init_config = __esm({
46
39
  "src/loaders/config/index.ts"() {
@@ -50,53 +43,6 @@ var init_config = __esm({
50
43
  }
51
44
  });
52
45
 
53
- // src/plugins/index.ts
54
- function createPluginHandler(context, defaultPlugins = []) {
55
- const plugins2 = [];
56
- async function write(entry) {
57
- const file = import_node_path2.default.join(context.outDir, entry.path);
58
- await import_promises2.default.mkdir(import_node_path2.default.dirname(file), { recursive: true });
59
- await import_promises2.default.writeFile(file, entry.content);
60
- }
61
- return {
62
- async init(config) {
63
- if (config.global.plugins) {
64
- defaultPlugins.push(...config.global.plugins);
65
- }
66
- for await (const option of defaultPlugins) {
67
- if (!option) continue;
68
- if (Array.isArray(option)) plugins2.push(...option);
69
- else plugins2.push(option);
70
- }
71
- for (const plugin of plugins2) {
72
- const out = await plugin.config?.call(context, config);
73
- if (out) config = out;
74
- }
75
- return config;
76
- },
77
- async emit() {
78
- const out = await Promise.all(
79
- plugins2.map((plugin) => {
80
- return plugin.emit?.call(context) ?? [];
81
- })
82
- );
83
- return out.flat();
84
- },
85
- async emitAndWrite() {
86
- const entries = await this.emit();
87
- await Promise.all(entries.map(write));
88
- }
89
- };
90
- }
91
- var import_node_path2, import_promises2;
92
- var init_plugins = __esm({
93
- "src/plugins/index.ts"() {
94
- "use strict";
95
- import_node_path2 = __toESM(require("path"), 1);
96
- import_promises2 = __toESM(require("fs/promises"), 1);
97
- }
98
- });
99
-
100
46
  // src/loaders/mdx/preset.ts
101
47
  var preset_exports = {};
102
48
  __export(preset_exports, {
@@ -266,18 +212,18 @@ async function compileConfig(configPath, outDir) {
266
212
  }
267
213
  async function loadConfig(configPath, outDir, build = false) {
268
214
  if (build) await compileConfig(configPath, outDir);
269
- const url = (0, import_node_url.pathToFileURL)(path3.resolve(outDir, "source.config.mjs"));
215
+ const url = (0, import_node_url.pathToFileURL)(path2.resolve(outDir, "source.config.mjs"));
270
216
  url.searchParams.set("hash", Date.now().toString());
271
217
  const config = import(url.href).then(
272
218
  (loaded) => buildConfig(loaded)
273
219
  );
274
220
  return await config;
275
221
  }
276
- var path3, import_node_url;
222
+ var path2, import_node_url;
277
223
  var init_load = __esm({
278
224
  "src/loaders/config/load.ts"() {
279
225
  "use strict";
280
- path3 = __toESM(require("path"), 1);
226
+ path2 = __toESM(require("path"), 1);
281
227
  import_node_url = require("url");
282
228
  init_build();
283
229
  }
@@ -285,8 +231,8 @@ var init_load = __esm({
285
231
 
286
232
  // src/next/file-cache.ts
287
233
  function toFullPath(file) {
288
- if (import_node_path3.default.isAbsolute(file)) {
289
- return import_node_path3.default.relative(process.cwd(), file);
234
+ if (import_node_path2.default.isAbsolute(file)) {
235
+ return import_node_path2.default.relative(process.cwd(), file);
290
236
  }
291
237
  return file;
292
238
  }
@@ -294,20 +240,20 @@ async function readFileWithCache(file) {
294
240
  const fullPath = toFullPath(file);
295
241
  const cached = map.get(fullPath);
296
242
  if (cached) return cached;
297
- const read = import_promises3.default.readFile(fullPath).then((s) => s.toString());
243
+ const read = import_promises2.default.readFile(fullPath).then((s) => s.toString());
298
244
  map.set(fullPath, read);
299
245
  return read;
300
246
  }
301
247
  function removeFileCache(file) {
302
248
  map.delete(toFullPath(file));
303
249
  }
304
- var import_lru_cache, import_promises3, import_node_path3, map;
250
+ var import_lru_cache, import_promises2, import_node_path2, map;
305
251
  var init_file_cache = __esm({
306
252
  "src/next/file-cache.ts"() {
307
253
  "use strict";
308
254
  import_lru_cache = require("lru-cache");
309
- import_promises3 = __toESM(require("fs/promises"), 1);
310
- import_node_path3 = __toESM(require("path"), 1);
255
+ import_promises2 = __toESM(require("fs/promises"), 1);
256
+ import_node_path2 = __toESM(require("path"), 1);
311
257
  map = new import_lru_cache.LRUCache({
312
258
  max: 100
313
259
  });
@@ -365,7 +311,7 @@ async function getGitTimestamp(file) {
365
311
  try {
366
312
  const out = await (0, import_tinyexec.x)(
367
313
  "git",
368
- ["log", "-1", '--pretty="%ai"', import_node_path4.default.relative(process.cwd(), file)],
314
+ ["log", "-1", '--pretty="%ai"', import_node_path3.default.relative(process.cwd(), file)],
369
315
  {
370
316
  throwOnError: true
371
317
  }
@@ -377,11 +323,11 @@ async function getGitTimestamp(file) {
377
323
  return;
378
324
  }
379
325
  }
380
- var import_node_path4, import_tinyexec, cache;
326
+ var import_node_path3, import_tinyexec, cache;
381
327
  var init_git_timestamp = __esm({
382
328
  "src/utils/git-timestamp.ts"() {
383
329
  "use strict";
384
- import_node_path4 = __toESM(require("path"), 1);
330
+ import_node_path3 = __toESM(require("path"), 1);
385
331
  import_tinyexec = require("tinyexec");
386
332
  cache = /* @__PURE__ */ new Map();
387
333
  }
@@ -424,7 +370,7 @@ function getImportCode(info) {
424
370
  return `import ${specifier}`;
425
371
  }
426
372
  function toImportPath(file, config) {
427
- const ext = import_node_path5.default.extname(file);
373
+ const ext = import_node_path4.default.extname(file);
428
374
  let filename;
429
375
  if (ext === ".ts" && config.jsExtension) {
430
376
  filename = file.substring(0, file.length - ext.length) + ".js";
@@ -435,55 +381,94 @@ function toImportPath(file, config) {
435
381
  }
436
382
  let importPath;
437
383
  if ("relativeTo" in config) {
438
- importPath = import_node_path5.default.relative(config.relativeTo, filename);
439
- if (!import_node_path5.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
384
+ importPath = import_node_path4.default.relative(config.relativeTo, filename);
385
+ if (!import_node_path4.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
440
386
  importPath = `./${importPath}`;
441
387
  }
442
388
  } else {
443
- importPath = import_node_path5.default.resolve(filename);
389
+ importPath = import_node_path4.default.resolve(filename);
444
390
  }
445
- return importPath.replaceAll(import_node_path5.default.sep, "/");
391
+ return importPath.replaceAll(import_node_path4.default.sep, "/");
446
392
  }
447
393
  function ident(code, tab = 1) {
448
394
  return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
449
395
  }
450
- var import_node_path5;
396
+ var import_node_path4;
451
397
  var init_import_formatter = __esm({
452
398
  "src/utils/import-formatter.ts"() {
453
399
  "use strict";
454
- import_node_path5 = __toESM(require("path"), 1);
400
+ import_node_path4 = __toESM(require("path"), 1);
455
401
  }
456
402
  });
457
403
 
458
404
  // src/utils/collections.ts
459
- function getSupportedFormats(collection) {
460
- return {
461
- doc: ["mdx", "md"],
462
- meta: ["json", "yaml"]
463
- }[collection.type];
464
- }
465
405
  function getGlobPatterns(collection) {
466
406
  if (collection.files) return collection.files;
467
- return [`**/*.{${getSupportedFormats(collection).join(",")}}`];
407
+ return [`**/*.{${SupportedFormats[collection.type].join(",")}}`];
468
408
  }
469
409
  function isFileSupported(filePath, collection) {
470
- for (const format of getSupportedFormats(collection)) {
471
- if (filePath.endsWith(`.${format}`)) return true;
472
- }
473
- return false;
410
+ return SupportedFormats[collection.type].some(
411
+ (format) => filePath.endsWith(`.${format}`)
412
+ );
474
413
  }
414
+ async function getCollectionFiles(collection) {
415
+ const files = /* @__PURE__ */ new Map();
416
+ const dirs = Array.isArray(collection.dir) ? collection.dir : [collection.dir];
417
+ const patterns = getGlobPatterns(collection);
418
+ await Promise.all(
419
+ dirs.map(async (dir) => {
420
+ const result = await (0, import_tinyglobby.glob)(patterns, {
421
+ cwd: import_node_path5.default.resolve(dir)
422
+ });
423
+ for (const item of result) {
424
+ if (!isFileSupported(item, collection)) continue;
425
+ const fullPath = import_node_path5.default.join(dir, item);
426
+ files.set(fullPath, {
427
+ path: item,
428
+ fullPath
429
+ });
430
+ }
431
+ })
432
+ );
433
+ return Array.from(files.values());
434
+ }
435
+ var import_picomatch, import_tinyglobby, import_node_path5, SupportedFormats;
475
436
  var init_collections = __esm({
476
437
  "src/utils/collections.ts"() {
477
438
  "use strict";
439
+ import_picomatch = __toESM(require("picomatch"), 1);
440
+ import_tinyglobby = require("tinyglobby");
441
+ import_node_path5 = __toESM(require("path"), 1);
442
+ SupportedFormats = {
443
+ doc: ["mdx", "md"],
444
+ meta: ["json", "yaml"]
445
+ };
478
446
  }
479
447
  });
480
448
 
481
449
  // src/plugins/next.ts
482
450
  function next() {
483
451
  let config;
452
+ let shouldEmitOnChange = false;
484
453
  return {
454
+ name: "next",
485
455
  config(v) {
486
456
  config = v;
457
+ shouldEmitOnChange = false;
458
+ for (const collection of config.collections.values()) {
459
+ if (collection.type === "doc" && collection.async || collection.type === "docs" && collection.docs.async) {
460
+ shouldEmitOnChange = true;
461
+ }
462
+ }
463
+ },
464
+ configureServer(server) {
465
+ if (!server.watcher) return;
466
+ server.watcher.on("all", async () => {
467
+ if (!shouldEmitOnChange) return;
468
+ await this.core.emitAndWrite({
469
+ filterPlugin: (plugin) => plugin.name === "next"
470
+ });
471
+ });
487
472
  },
488
473
  async emit() {
489
474
  return [
@@ -626,27 +611,6 @@ async function indexFile(configPath, config, importPath, configHash = false) {
626
611
  ...resolvedDeclares
627
612
  ].join("\n");
628
613
  }
629
- async function getCollectionFiles(collection) {
630
- const files = /* @__PURE__ */ new Map();
631
- const dirs = Array.isArray(collection.dir) ? collection.dir : [collection.dir];
632
- const patterns = getGlobPatterns(collection);
633
- await Promise.all(
634
- dirs.map(async (dir) => {
635
- const result = await (0, import_tinyglobby.glob)(patterns, {
636
- cwd: path7.resolve(dir)
637
- });
638
- for (const item of result) {
639
- if (!isFileSupported(item, collection)) continue;
640
- const fullPath = path7.join(dir, item);
641
- files.set(fullPath, {
642
- path: item,
643
- fullPath
644
- });
645
- }
646
- })
647
- );
648
- return Array.from(files.values());
649
- }
650
614
  function parseMetaEntry(file, content) {
651
615
  const extname4 = path7.extname(file);
652
616
  try {
@@ -659,13 +623,12 @@ function parseMetaEntry(file, content) {
659
623
  }
660
624
  throw new Error(`Unknown meta file format: ${extname4}, in ${file}.`);
661
625
  }
662
- var path7, import_node_crypto, import_tinyglobby, import_js_yaml2;
626
+ var path7, import_node_crypto, import_js_yaml2;
663
627
  var init_next = __esm({
664
628
  "src/plugins/next.ts"() {
665
629
  "use strict";
666
630
  path7 = __toESM(require("path"), 1);
667
631
  import_node_crypto = require("crypto");
668
- import_tinyglobby = require("tinyglobby");
669
632
  init_validation();
670
633
  init_file_cache();
671
634
  import_js_yaml2 = require("js-yaml");
@@ -676,33 +639,82 @@ var init_next = __esm({
676
639
  }
677
640
  });
678
641
 
679
- // src/next/watcher.ts
680
- var watcher_exports = {};
681
- __export(watcher_exports, {
682
- watcher: () => watcher
683
- });
684
- function watcher(configPath, config, ignored) {
685
- const watcher2 = new import_chokidar.FSWatcher({
686
- ignoreInitial: true,
687
- persistent: true,
688
- ignored
689
- });
690
- watcher2.add(configPath);
691
- for (const collection of config.collections.values()) {
692
- if (collection.type === "docs") {
693
- watcher2.add(collection.docs.dir);
694
- watcher2.add(collection.meta.dir);
695
- } else {
696
- watcher2.add(collection.dir);
642
+ // src/core.ts
643
+ function createCore(options, defaultPlugins = []) {
644
+ let config;
645
+ let plugins2;
646
+ return {
647
+ _options: options,
648
+ getPluginContext() {
649
+ return {
650
+ core: this,
651
+ ...options
652
+ };
653
+ },
654
+ /**
655
+ * Convenient cache store, reset when config changes
656
+ */
657
+ cache: /* @__PURE__ */ new Map(),
658
+ async init({ config: newConfig }) {
659
+ config = await newConfig;
660
+ this.cache.clear();
661
+ plugins2 = [];
662
+ for await (const option of [
663
+ ...defaultPlugins,
664
+ ...config.global.plugins ?? []
665
+ ]) {
666
+ if (!option) continue;
667
+ if (Array.isArray(option)) plugins2.push(...option);
668
+ else plugins2.push(option);
669
+ }
670
+ for (const plugin of plugins2) {
671
+ const out = await plugin.config?.call(this.getPluginContext(), config);
672
+ if (out) config = out;
673
+ }
674
+ return this;
675
+ },
676
+ getConfig() {
677
+ return config;
678
+ },
679
+ creatConfigLoader() {
680
+ return {
681
+ getConfig() {
682
+ return config;
683
+ }
684
+ };
685
+ },
686
+ async initServer(server) {
687
+ for (const plugin of plugins2) {
688
+ await plugin.configureServer?.call(this.getPluginContext(), server);
689
+ }
690
+ },
691
+ async emitAndWrite({
692
+ filterPlugin = () => true
693
+ } = {}) {
694
+ const start2 = performance.now();
695
+ const out = await Promise.all(
696
+ plugins2.map((plugin) => {
697
+ if (!filterPlugin(plugin) || !plugin.emit) return [];
698
+ return plugin.emit.call(this.getPluginContext());
699
+ })
700
+ );
701
+ await Promise.all(
702
+ out.flat().map(async (entry) => {
703
+ const file = import_node_path6.default.join(options.outDir, entry.path);
704
+ await import_promises3.default.mkdir(import_node_path6.default.dirname(file), { recursive: true });
705
+ await import_promises3.default.writeFile(file, entry.content);
706
+ })
707
+ );
708
+ console.log(`[MDX] generated files in ${performance.now() - start2}ms`);
697
709
  }
698
- }
699
- return watcher2;
710
+ };
700
711
  }
701
- var import_chokidar;
702
- var init_watcher = __esm({
703
- "src/next/watcher.ts"() {
712
+ var import_node_path6, import_promises3;
713
+ var init_core = __esm({
714
+ "src/core.ts"() {
704
715
  "use strict";
705
- import_chokidar = require("chokidar");
716
+ import_node_path6 = __toESM(require("path"), 1);
717
+ import_promises3 = __toESM(require("fs/promises"), 1);
706
718
  }
707
719
  });
708
720
 
@@ -764,63 +776,63 @@ function createMDX(createOptions = {}) {
764
776
  };
765
777
  }
766
778
  async function init(dev, options) {
767
- const pluginHandler = createNextPluginHandler(options);
768
- let config;
769
- async function updateConfig() {
770
- config = await pluginHandler.init(
771
- await loadConfig(options.configPath, options.outDir, true)
772
- );
779
+ const core = createNextCore(options);
780
+ async function initOrReload() {
781
+ await core.init({
782
+ config: loadConfig(options.configPath, options.outDir, true)
783
+ });
784
+ await core.emitAndWrite();
773
785
  }
774
- async function emitFiles() {
775
- const start2 = performance.now();
776
- try {
777
- await pluginHandler.emitAndWrite();
778
- } catch (err) {
779
- if (err instanceof ValidationError) {
780
- console.error(err.toStringFormatted());
786
+ async function devServer() {
787
+ const { FSWatcher } = await import("chokidar");
788
+ const watcher = new FSWatcher({
789
+ ignoreInitial: true,
790
+ persistent: true,
791
+ ignored: [options.outDir]
792
+ });
793
+ watcher.add(options.configPath);
794
+ for (const collection of core.getConfig().collections.values()) {
795
+ if (collection.type === "docs") {
796
+ watcher.add(collection.docs.dir);
797
+ watcher.add(collection.meta.dir);
781
798
  } else {
782
- console.error(err);
799
+ watcher.add(collection.dir);
783
800
  }
784
801
  }
785
- console.log(`[MDX] updated map file in ${performance.now() - start2}ms`);
786
- }
787
- async function devServer() {
788
- const { watcher: watcher2 } = await Promise.resolve().then(() => (init_watcher(), watcher_exports));
789
- const instance = watcher2(options.configPath, config, [options.outDir]);
790
- async function onUpdate(event, file) {
791
- const absolutePath = path8.resolve(file);
802
+ watcher.on("ready", () => {
803
+ console.log("[MDX] started dev server");
804
+ });
805
+ watcher.on("all", async (event, file) => {
806
+ const absolutePath = path9.resolve(file);
792
807
  if (event === "change") removeFileCache(absolutePath);
793
- if (absolutePath === path8.resolve(options.configPath)) {
794
- await updateConfig();
808
+ if (absolutePath === path9.resolve(options.configPath)) {
809
+ watcher.removeAllListeners();
810
+ await watcher.close();
811
+ await initOrReload();
795
812
  console.log("[MDX] restarting dev server");
796
- await instance.close();
797
- void devServer();
813
+ await devServer();
798
814
  }
799
- await emitFiles();
800
- }
801
- instance.on("ready", () => {
802
- console.log("[MDX] started dev server");
803
- });
804
- instance.on("all", (event, file) => {
805
- void onUpdate(event, file);
806
815
  });
807
816
  process.on("exit", () => {
817
+ if (watcher.closed) return;
808
818
  console.log("[MDX] closing dev server");
809
- void instance.close();
819
+ void watcher.close();
810
820
  });
821
+ await core.initServer({ watcher });
822
+ }
823
+ await initOrReload();
824
+ if (dev) {
825
+ await devServer();
811
826
  }
812
- await updateConfig();
813
- await emitFiles();
814
- if (dev) void devServer();
815
827
  }
816
828
  async function postInstall(configPath = findConfigFile(), outDir = ".source") {
817
- const pluginHandler = createNextPluginHandler({
829
+ const core = await createNextCore({
818
830
  outDir,
819
831
  configPath
832
+ }).init({
833
+ config: loadConfig(configPath, outDir, true)
820
834
  });
821
- await pluginHandler.init(await loadConfig(configPath, outDir, true));
822
- await pluginHandler.emitAndWrite();
823
- console.log("[MDX] types generated");
835
+ await core.emitAndWrite();
824
836
  }
825
837
  function applyDefaults(options) {
826
838
  return {
@@ -828,11 +840,11 @@ function applyDefaults(options) {
828
840
  configPath: options.configPath ?? findConfigFile()
829
841
  };
830
842
  }
831
- function createNextPluginHandler({
843
+ function createNextCore({
832
844
  outDir,
833
845
  configPath
834
846
  }) {
835
- return createPluginHandler(
847
+ const core = createCore(
836
848
  {
837
849
  environment: "next",
838
850
  outDir,
@@ -840,18 +852,32 @@ function createNextPluginHandler({
840
852
  },
841
853
  [next()]
842
854
  );
855
+ return {
856
+ ...core,
857
+ async emitAndWrite(...args) {
858
+ try {
859
+ await core.emitAndWrite(...args);
860
+ } catch (err) {
861
+ if (err instanceof ValidationError) {
862
+ console.error(err.toStringFormatted());
863
+ } else {
864
+ console.error(err);
865
+ }
866
+ }
867
+ }
868
+ };
843
869
  }
844
- var path8, defaultPageExtensions;
870
+ var path9, defaultPageExtensions;
845
871
  var init_next2 = __esm({
846
872
  "src/next/index.ts"() {
847
873
  "use strict";
848
874
  init_config();
849
- init_plugins();
850
- path8 = __toESM(require("path"), 1);
875
+ path9 = __toESM(require("path"), 1);
851
876
  init_load();
852
877
  init_file_cache();
853
878
  init_validation();
854
879
  init_next();
880
+ init_core();
855
881
  defaultPageExtensions = ["mdx", "md", "jsx", "js", "tsx", "ts"];
856
882
  }
857
883
  });
@@ -982,7 +1008,7 @@ ${e instanceof Error ? e.message : String(e)}`,
982
1008
  { cause: e }
983
1009
  );
984
1010
  }
985
- const ext = path9.extname(file);
1011
+ const ext = path10.extname(file);
986
1012
  data._compiler?.addDependency(file);
987
1013
  if (params.lang || ext !== ".md" && ext !== ".mdx") {
988
1014
  const lang = params.lang ?? ext.slice(1);
@@ -1015,7 +1041,7 @@ ${e instanceof Error ? e.message : String(e)}`,
1015
1041
  } else {
1016
1042
  mdast = await baseProcessor.run(mdast);
1017
1043
  }
1018
- await update(mdast, path9.dirname(file), data);
1044
+ await update(mdast, path10.dirname(file), data);
1019
1045
  return mdast;
1020
1046
  };
1021
1047
  async function update(tree, directory, data) {
@@ -1027,7 +1053,7 @@ ${e instanceof Error ? e.message : String(e)}`,
1027
1053
  if (specifier.length === 0) return "skip";
1028
1054
  const attributes = parseElementAttributes(node);
1029
1055
  const { file: relativePath, section } = parseSpecifier(specifier);
1030
- const file = path9.resolve(
1056
+ const file = path10.resolve(
1031
1057
  "cwd" in attributes ? process.cwd() : directory,
1032
1058
  relativePath
1033
1059
  );
@@ -1044,16 +1070,16 @@ ${e instanceof Error ? e.message : String(e)}`,
1044
1070
  await Promise.all(queue);
1045
1071
  }
1046
1072
  return async (tree, file) => {
1047
- await update(tree, path9.dirname(file.path), file.data);
1073
+ await update(tree, path10.dirname(file.path), file.data);
1048
1074
  };
1049
1075
  }
1050
- var import_unified, import_unist_util_visit2, path9, fs4, import_mdx_plugins, ElementLikeTypes;
1076
+ var import_unified, import_unist_util_visit2, path10, fs4, import_mdx_plugins, ElementLikeTypes;
1051
1077
  var init_remark_include = __esm({
1052
1078
  "src/loaders/mdx/remark-include.ts"() {
1053
1079
  "use strict";
1054
1080
  import_unified = require("unified");
1055
1081
  import_unist_util_visit2 = require("unist-util-visit");
1056
- path9 = __toESM(require("path"), 1);
1082
+ path10 = __toESM(require("path"), 1);
1057
1083
  fs4 = __toESM(require("fs/promises"), 1);
1058
1084
  init_fuma_matter();
1059
1085
  import_mdx_plugins = require("fumadocs-core/mdx-plugins");
@@ -1234,13 +1260,6 @@ var init_build_mdx = __esm({
1234
1260
  });
1235
1261
 
1236
1262
  // src/loaders/mdx/index.ts
1237
- function getConfigHash(config) {
1238
- let hash = hashes.get(config);
1239
- if (hash) return hash;
1240
- hash = Date.now().toString();
1241
- hashes.set(config, hash);
1242
- return hash;
1243
- }
1244
1263
  function createMdxLoader(configLoader) {
1245
1264
  return async ({
1246
1265
  source: value,
@@ -1251,14 +1270,25 @@ function createMdxLoader(configLoader) {
1251
1270
  }) => {
1252
1271
  const matter = fumaMatter(value);
1253
1272
  const parsed = querySchema.parse(query);
1254
- const loaded = await configLoader.getConfig();
1255
- const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
1256
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
1257
- if (cacheDir) {
1258
- const cached = await import_promises4.default.readFile(import_node_path6.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
1273
+ const config = await configLoader.getConfig();
1274
+ let after;
1275
+ if (!isDevelopment && config.global.experimentalBuildCache) {
1276
+ const cacheDir = config.global.experimentalBuildCache;
1277
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
1278
+ const cached = await import_promises4.default.readFile(import_node_path7.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
1259
1279
  if (cached && cached.hash === generateCacheHash(value)) return cached;
1280
+ after = async () => {
1281
+ await import_promises4.default.mkdir(cacheDir, { recursive: true });
1282
+ await import_promises4.default.writeFile(
1283
+ import_node_path7.default.join(cacheDir, cacheKey),
1284
+ JSON.stringify({
1285
+ ...out,
1286
+ hash: generateCacheHash(value)
1287
+ })
1288
+ );
1289
+ };
1260
1290
  }
1261
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
1291
+ const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
1262
1292
  let docCollection;
1263
1293
  switch (collection?.type) {
1264
1294
  case "doc":
@@ -1286,16 +1316,16 @@ function createMdxLoader(configLoader) {
1286
1316
  };
1287
1317
  }
1288
1318
  const data = {};
1289
- if (loaded.global.lastModifiedTime === "git") {
1319
+ if (config.global.lastModifiedTime === "git") {
1290
1320
  data.lastModified = (await getGitTimestamp(filePath))?.getTime();
1291
1321
  }
1292
1322
  const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
1293
1323
  const compiled = await buildMDX(
1294
- `${getConfigHash(loaded)}:${parsed.collection ?? "global"}`,
1324
+ `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
1295
1325
  "\n".repeat(lineOffset) + matter.content,
1296
1326
  {
1297
1327
  development: isDevelopment,
1298
- ...docCollection?.mdxOptions ?? await loaded.getDefaultMDXOptions(),
1328
+ ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
1299
1329
  postprocess: docCollection?.postprocess,
1300
1330
  data,
1301
1331
  filePath,
@@ -1307,19 +1337,17 @@ function createMdxLoader(configLoader) {
1307
1337
  code: String(compiled.value),
1308
1338
  map: compiled.map
1309
1339
  };
1310
- if (cacheDir) {
1311
- await import_promises4.default.mkdir(cacheDir, { recursive: true });
1312
- await import_promises4.default.writeFile(
1313
- import_node_path6.default.join(cacheDir, cacheKey),
1314
- JSON.stringify({
1315
- ...out,
1316
- hash: generateCacheHash(value)
1317
- })
1318
- );
1319
- }
1340
+ await after?.();
1320
1341
  return out;
1321
1342
  };
1322
1343
  }
1344
+ function getConfigHash(config) {
1345
+ let hash = hashes.get(config);
1346
+ if (hash) return hash;
1347
+ hash = Date.now().toString();
1348
+ hashes.set(config, hash);
1349
+ return hash;
1350
+ }
1323
1351
  function generateCacheHash(input) {
1324
1352
  return (0, import_node_crypto2.createHash)("md5").update(input).digest("hex");
1325
1353
  }
@@ -1330,7 +1358,7 @@ function countLines(s) {
1330
1358
  }
1331
1359
  return num;
1332
1360
  }
1333
- var import_zod, import_promises4, import_node_path6, import_node_crypto2, querySchema, cacheEntry, hashes;
1361
+ var import_zod, import_promises4, import_node_path7, import_node_crypto2, querySchema, cacheEntry, hashes;
1334
1362
  var init_mdx = __esm({
1335
1363
  "src/loaders/mdx/index.ts"() {
1336
1364
  "use strict";
@@ -1340,7 +1368,7 @@ var init_mdx = __esm({
1340
1368
  init_build_mdx();
1341
1369
  import_zod = require("zod");
1342
1370
  import_promises4 = __toESM(require("fs/promises"), 1);
1343
- import_node_path6 = __toESM(require("path"), 1);
1371
+ import_node_path7 = __toESM(require("path"), 1);
1344
1372
  import_node_crypto2 = require("crypto");
1345
1373
  querySchema = import_zod.z.object({
1346
1374
  only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
@@ -1375,7 +1403,7 @@ function toVite(loader) {
1375
1403
  };
1376
1404
  };
1377
1405
  }
1378
- var import_node_url2, import_promises5, import_node_querystring, import_node_path7;
1406
+ var import_node_url2, import_promises5, import_node_querystring, import_node_path8;
1379
1407
  var init_adapter = __esm({
1380
1408
  "src/loaders/adapter.ts"() {
1381
1409
  "use strict";
@@ -1383,7 +1411,7 @@ var init_adapter = __esm({
1383
1411
  import_promises5 = __toESM(require("fs/promises"), 1);
1384
1412
  import_node_querystring = require("querystring");
1385
1413
  init_validation();
1386
- import_node_path7 = __toESM(require("path"), 1);
1414
+ import_node_path8 = __toESM(require("path"), 1);
1387
1415
  }
1388
1416
  });
1389
1417
 
@@ -1394,7 +1422,7 @@ function generateGlobImport(patterns, options) {
1394
1422
  cwd: options.base
1395
1423
  });
1396
1424
  for (const item of result) {
1397
- const fullPath = import_node_path8.default.join(options.base, item);
1425
+ const fullPath = import_node_path9.default.join(options.base, item);
1398
1426
  const url = (0, import_node_url3.pathToFileURL)(fullPath);
1399
1427
  for (const [k, v] of Object.entries(options.query ?? {})) {
1400
1428
  url.searchParams.set(k, v);
@@ -1408,12 +1436,12 @@ function generateGlobImport(patterns, options) {
1408
1436
  code += "}";
1409
1437
  return code;
1410
1438
  }
1411
- var import_tinyglobby2, import_node_path8, import_node_url3;
1439
+ var import_tinyglobby2, import_node_path9, import_node_url3;
1412
1440
  var init_glob_import = __esm({
1413
1441
  "src/utils/glob-import.ts"() {
1414
1442
  "use strict";
1415
1443
  import_tinyglobby2 = require("tinyglobby");
1416
- import_node_path8 = __toESM(require("path"), 1);
1444
+ import_node_path9 = __toESM(require("path"), 1);
1417
1445
  import_node_url3 = require("url");
1418
1446
  }
1419
1447
  });
@@ -1426,7 +1454,6 @@ function vite(options) {
1426
1454
  config = v;
1427
1455
  },
1428
1456
  emit() {
1429
- console.log("[Fumadocs MDX] Generating index files");
1430
1457
  return [
1431
1458
  {
1432
1459
  path: "index.ts",
@@ -1498,7 +1525,7 @@ ${obj}
1498
1525
  return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
1499
1526
  {
1500
1527
  ...options2,
1501
- base: import_node_path9.default.relative(outDir, options2.base)
1528
+ base: import_node_path10.default.relative(outDir, options2.base)
1502
1529
  },
1503
1530
  null,
1504
1531
  2
@@ -1538,14 +1565,14 @@ function getGlobBase(collection) {
1538
1565
  }
1539
1566
  return enforceRelative(dir);
1540
1567
  }
1541
- var import_node_path9;
1568
+ var import_node_path10;
1542
1569
  var init_vite = __esm({
1543
1570
  "src/plugins/vite.ts"() {
1544
1571
  "use strict";
1545
1572
  init_import_formatter();
1546
1573
  init_collections();
1547
1574
  init_glob_import();
1548
- import_node_path9 = __toESM(require("path"), 1);
1575
+ import_node_path10 = __toESM(require("path"), 1);
1549
1576
  }
1550
1577
  });
1551
1578
 
@@ -1557,14 +1584,14 @@ __export(vite_exports, {
1557
1584
  });
1558
1585
  async function mdx(config, pluginOptions = {}) {
1559
1586
  const options = applyDefaults2(pluginOptions);
1560
- const { updateViteConfig } = options;
1561
- const pluginHandler = createVitePluginHandler(options);
1562
- const loaded = await pluginHandler.init(buildConfig(config));
1563
- const mdxLoader = toVite(createMdxLoader(resolvedConfig(loaded)));
1564
- async function transformMeta(path15, query, value) {
1565
- const isJson = path15.endsWith(".json");
1587
+ const core = await createViteCore(options).init({
1588
+ config: buildConfig(config)
1589
+ });
1590
+ const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
1591
+ async function transformMeta(path16, query, value) {
1592
+ const isJson = path16.endsWith(".json");
1566
1593
  const parsed = (0, import_node_querystring2.parse)(query);
1567
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
1594
+ const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
1568
1595
  if (!collection) return null;
1569
1596
  let schema;
1570
1597
  switch (collection.type) {
@@ -1585,8 +1612,8 @@ async function mdx(config, pluginOptions = {}) {
1585
1612
  const out = await validate(
1586
1613
  schema,
1587
1614
  data,
1588
- { path: path15, source: value },
1589
- `invalid data in ${path15}`
1615
+ { path: path16, source: value },
1616
+ `invalid data in ${path16}`
1590
1617
  );
1591
1618
  return {
1592
1619
  code: isJson ? JSON.stringify(out) : `export default ${JSON.stringify(out)}`,
@@ -1598,7 +1625,7 @@ async function mdx(config, pluginOptions = {}) {
1598
1625
  // needed, otherwise other plugins will be executed before our `transform`.
1599
1626
  enforce: "pre",
1600
1627
  config(config2) {
1601
- if (!updateViteConfig) return config2;
1628
+ if (!options.updateViteConfig) return config2;
1602
1629
  return (0, import_vite.mergeConfig)(config2, {
1603
1630
  optimizeDeps: {
1604
1631
  exclude: FumadocsDeps
@@ -1610,11 +1637,16 @@ async function mdx(config, pluginOptions = {}) {
1610
1637
  });
1611
1638
  },
1612
1639
  async buildStart() {
1613
- await pluginHandler.emitAndWrite();
1640
+ await core.emitAndWrite();
1641
+ },
1642
+ async configureServer(server) {
1643
+ await core.initServer({
1644
+ watcher: server.watcher
1645
+ });
1614
1646
  },
1615
1647
  async transform(value, id) {
1616
1648
  const [file, query = ""] = id.split("?");
1617
- const ext = path14.extname(file);
1649
+ const ext = path15.extname(file);
1618
1650
  try {
1619
1651
  if ([".yaml", ".json"].includes(ext))
1620
1652
  return await transformMeta(file, query, value);
@@ -1632,17 +1664,17 @@ async function mdx(config, pluginOptions = {}) {
1632
1664
  async function postInstall2(configPath = findConfigFile(), pluginOptions = {}) {
1633
1665
  const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load(), load_exports));
1634
1666
  const options = applyDefaults2(pluginOptions);
1635
- const pluginHandler = createVitePluginHandler(options);
1636
- await pluginHandler.init(await loadConfig2(configPath, options.outDir, true));
1637
- await pluginHandler.emitAndWrite();
1638
- console.log("[MDX] types generated");
1667
+ const core = await createViteCore(options).init({
1668
+ config: loadConfig2(configPath, options.outDir, true)
1669
+ });
1670
+ await core.emitAndWrite();
1639
1671
  }
1640
- function createVitePluginHandler({
1672
+ function createViteCore({
1641
1673
  configPath,
1642
1674
  outDir,
1643
1675
  generateIndexFile
1644
1676
  }) {
1645
- return createPluginHandler(
1677
+ return createCore(
1646
1678
  {
1647
1679
  environment: "vite",
1648
1680
  configPath,
@@ -1661,7 +1693,7 @@ function applyDefaults2(options) {
1661
1693
  outDir: options.outDir ?? ".source"
1662
1694
  };
1663
1695
  }
1664
- var import_vite, import_node_querystring2, path14, import_js_yaml3, FumadocsDeps;
1696
+ var import_vite, import_node_querystring2, path15, import_js_yaml3, FumadocsDeps;
1665
1697
  var init_vite2 = __esm({
1666
1698
  "src/vite/index.ts"() {
1667
1699
  "use strict";
@@ -1669,13 +1701,13 @@ var init_vite2 = __esm({
1669
1701
  init_build();
1670
1702
  import_node_querystring2 = require("querystring");
1671
1703
  init_validation();
1672
- path14 = __toESM(require("path"), 1);
1704
+ path15 = __toESM(require("path"), 1);
1673
1705
  import_js_yaml3 = require("js-yaml");
1674
1706
  init_mdx();
1675
1707
  init_config();
1676
1708
  init_adapter();
1677
1709
  init_vite();
1678
- init_plugins();
1710
+ init_core();
1679
1711
  FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
1680
1712
  }
1681
1713
  });