fumadocs-mdx 13.0.3 → 13.0.4

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 (65) hide show
  1. package/dist/bin.cjs +352 -272
  2. package/dist/{build-mdx-TtQzmq6W.d.cts → build-mdx-BjXOmv0b.d.cts} +1 -1
  3. package/dist/{build-mdx--WI4tf2-.d.ts → build-mdx-CY5UldCO.d.ts} +1 -1
  4. package/dist/bun/index.cjs +177 -57
  5. package/dist/bun/index.d.cts +2 -2
  6. package/dist/bun/index.d.ts +2 -2
  7. package/dist/bun/index.js +26 -34
  8. package/dist/chunk-2HXTGJBI.js +101 -0
  9. package/dist/chunk-4JSFLXXT.js +8 -0
  10. package/dist/{chunk-JVZFH6ND.js → chunk-QXHN25N3.js} +2 -2
  11. package/dist/{chunk-EELYB2XC.js → chunk-TZ5EQBFW.js} +9 -103
  12. package/dist/chunk-VUEZTR2H.js +26 -0
  13. package/dist/{chunk-XQ5O7IPO.js → chunk-XYGORKQA.js} +5 -3
  14. package/dist/chunk-YAIPHUCZ.js +56 -0
  15. package/dist/chunk-ZNVPB2IR.js +170 -0
  16. package/dist/config/index.d.cts +2 -2
  17. package/dist/config/index.d.ts +2 -2
  18. package/dist/{core-DhfmVKRA.d.cts → core-DB7TdlyC.d.cts} +21 -7
  19. package/dist/{core-DhfmVKRA.d.ts → core-DB7TdlyC.d.ts} +21 -7
  20. package/dist/index-D7s7kCc2.d.cts +7 -0
  21. package/dist/index-D7s7kCc2.d.ts +7 -0
  22. package/dist/index.d.cts +4 -4
  23. package/dist/index.d.ts +4 -4
  24. package/dist/load-from-file-AVYOFOI7.js +7 -0
  25. package/dist/next/index.cjs +145 -139
  26. package/dist/next/index.js +56 -36
  27. package/dist/node/loader.cjs +242 -128
  28. package/dist/node/loader.d.cts +2 -2
  29. package/dist/node/loader.d.ts +2 -2
  30. package/dist/node/loader.js +29 -16
  31. package/dist/plugins/json-schema.cjs +22 -70
  32. package/dist/plugins/json-schema.d.cts +2 -2
  33. package/dist/plugins/json-schema.d.ts +2 -2
  34. package/dist/plugins/json-schema.js +19 -14
  35. package/dist/runtime/next/async.cjs +56 -11
  36. package/dist/runtime/next/async.d.cts +4 -6
  37. package/dist/runtime/next/async.d.ts +4 -6
  38. package/dist/runtime/next/async.js +3 -6
  39. package/dist/runtime/next/index.d.cts +5 -5
  40. package/dist/runtime/next/index.d.ts +5 -5
  41. package/dist/runtime/vite/browser.d.cts +3 -3
  42. package/dist/runtime/vite/browser.d.ts +3 -3
  43. package/dist/runtime/vite/server.d.cts +3 -3
  44. package/dist/runtime/vite/server.d.ts +3 -3
  45. package/dist/{types-zrV4v6mp.d.cts → types-Bnh9n7mj.d.cts} +2 -2
  46. package/dist/{types-CMva20Zp.d.ts → types-ey1AZqrg.d.ts} +2 -2
  47. package/dist/vite/index.cjs +238 -159
  48. package/dist/vite/index.js +113 -118
  49. package/dist/webpack/{index.cjs → mdx.cjs} +126 -104
  50. package/dist/webpack/mdx.d.cts +6 -0
  51. package/dist/webpack/mdx.d.ts +6 -0
  52. package/dist/webpack/{index.js → mdx.js} +12 -14
  53. package/dist/webpack/meta.cjs +528 -0
  54. package/dist/webpack/meta.d.cts +6 -0
  55. package/dist/webpack/meta.d.ts +6 -0
  56. package/dist/webpack/meta.js +42 -0
  57. package/loader-mdx.cjs +1 -1
  58. package/loader-meta.cjs +7 -0
  59. package/package.json +6 -3
  60. package/dist/chunk-U4MQ44TS.js +0 -53
  61. package/dist/chunk-XZY2AWJI.js +0 -81
  62. package/dist/chunk-YVCR6FUH.js +0 -82
  63. package/dist/load-MNG3CLET.js +0 -7
  64. package/dist/webpack/index.d.cts +0 -15
  65. package/dist/webpack/index.d.ts +0 -15
@@ -118,35 +118,78 @@ var init_preset = __esm({
118
118
  });
119
119
 
120
120
  // src/config/build.ts
121
+ function buildCollection(name, config) {
122
+ if (config.type === "docs") {
123
+ return {
124
+ ...config,
125
+ name,
126
+ meta: buildPrimitiveCollection(name, config.meta),
127
+ docs: buildPrimitiveCollection(name, config.docs)
128
+ };
129
+ }
130
+ return buildPrimitiveCollection(name, config);
131
+ }
132
+ function buildPrimitiveCollection(name, { files, ...config }) {
133
+ const supportedFormats = SupportedFormats[config.type];
134
+ const patterns = files ?? [`**/*.{${supportedFormats.join(",")}}`];
135
+ let matchers;
136
+ return {
137
+ ...config,
138
+ name,
139
+ patterns,
140
+ isFileSupported(filePath) {
141
+ return supportedFormats.some((format) => filePath.endsWith(`.${format}`));
142
+ },
143
+ hasFile(filePath) {
144
+ matchers ??= (Array.isArray(config.dir) ? config.dir : [config.dir]).map(
145
+ (dir) => (0, import_picomatch.default)(patterns, {
146
+ cwd: dir
147
+ })
148
+ );
149
+ return this.isFileSupported(filePath) && matchers.some((matcher) => matcher(filePath));
150
+ }
151
+ };
152
+ }
121
153
  function buildConfig(config) {
122
154
  const collections = /* @__PURE__ */ new Map();
123
- let globalConfig = {};
155
+ const loaded = {};
124
156
  for (const [k, v] of Object.entries(config)) {
125
157
  if (!v) {
126
158
  continue;
127
159
  }
128
160
  if (typeof v === "object" && "type" in v) {
129
161
  if (v.type === "docs") {
130
- collections.set(k, v);
162
+ collections.set(k, buildCollection(k, v));
131
163
  continue;
132
164
  }
133
165
  if (v.type === "doc" || v.type === "meta") {
134
- collections.set(k, v);
166
+ collections.set(
167
+ k,
168
+ buildCollection(k, v)
169
+ );
135
170
  continue;
136
171
  }
137
172
  }
138
173
  if (k === "default" && v) {
139
- globalConfig = v;
174
+ Object.assign(loaded, v);
140
175
  continue;
141
176
  }
142
177
  throw new Error(
143
178
  `Unknown export "${k}", you can only export collections from source configuration file.`
144
179
  );
145
180
  }
181
+ if (loaded.collections) {
182
+ for (const [k, v] of Object.entries(loaded.collections)) {
183
+ collections.set(k, buildCollection(k, v));
184
+ }
185
+ }
146
186
  const mdxOptionsCache = /* @__PURE__ */ new Map();
147
187
  return {
148
- global: globalConfig,
149
- collections,
188
+ global: loaded,
189
+ collectionList: Array.from(collections.values()),
190
+ getCollection(name) {
191
+ return collections.get(name);
192
+ },
150
193
  async getDefaultMDXOptions(mode = "default") {
151
194
  const cached = mdxOptionsCache.get(mode);
152
195
  if (cached) return cached;
@@ -166,15 +209,21 @@ function buildConfig(config) {
166
209
  }
167
210
  };
168
211
  }
212
+ var import_picomatch, SupportedFormats;
169
213
  var init_build = __esm({
170
214
  "src/config/build.ts"() {
171
215
  "use strict";
216
+ import_picomatch = __toESM(require("picomatch"), 1);
217
+ SupportedFormats = {
218
+ doc: ["mdx", "md"],
219
+ meta: ["json", "yaml"]
220
+ };
172
221
  }
173
222
  });
174
223
 
175
- // src/loaders/config/load.ts
176
- var load_exports = {};
177
- __export(load_exports, {
224
+ // src/config/load-from-file.ts
225
+ var load_from_file_exports = {};
226
+ __export(load_from_file_exports, {
178
227
  loadConfig: () => loadConfig
179
228
  });
180
229
  async function compileConfig(configPath, outDir) {
@@ -199,18 +248,18 @@ async function compileConfig(configPath, outDir) {
199
248
  }
200
249
  async function loadConfig(configPath, outDir, build = false) {
201
250
  if (build) await compileConfig(configPath, outDir);
202
- const url = (0, import_node_url3.pathToFileURL)(path11.resolve(outDir, "source.config.mjs"));
251
+ const url = (0, import_node_url3.pathToFileURL)(path9.resolve(outDir, "source.config.mjs"));
203
252
  url.searchParams.set("hash", Date.now().toString());
204
253
  const config = import(url.href).then(
205
254
  (loaded) => buildConfig(loaded)
206
255
  );
207
256
  return await config;
208
257
  }
209
- var path11, import_node_url3;
210
- var init_load = __esm({
211
- "src/loaders/config/load.ts"() {
258
+ var path9, import_node_url3;
259
+ var init_load_from_file = __esm({
260
+ "src/config/load-from-file.ts"() {
212
261
  "use strict";
213
- path11 = __toESM(require("path"), 1);
262
+ path9 = __toESM(require("path"), 1);
214
263
  import_node_url3 = require("url");
215
264
  init_build();
216
265
  }
@@ -225,7 +274,6 @@ __export(vite_exports, {
225
274
  module.exports = __toCommonJS(vite_exports);
226
275
  var import_vite = require("vite");
227
276
  init_build();
228
- var import_node_querystring2 = require("querystring");
229
277
 
230
278
  // src/utils/validation.ts
231
279
  var import_picocolors = __toESM(require("picocolors"), 1);
@@ -265,10 +313,6 @@ async function validate(schema, data, context, errorMessage) {
265
313
  return data;
266
314
  }
267
315
 
268
- // src/vite/index.ts
269
- var path12 = __toESM(require("path"), 1);
270
- var import_js_yaml2 = require("js-yaml");
271
-
272
316
  // src/utils/fuma-matter.ts
273
317
  var import_js_yaml = require("js-yaml");
274
318
  var regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
@@ -703,7 +747,7 @@ function createMdxLoader(configLoader) {
703
747
  );
704
748
  };
705
749
  }
706
- const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
750
+ const collection = parsed.collection ? config.getCollection(parsed.collection) : void 0;
707
751
  let docCollection;
708
752
  switch (collection?.type) {
709
753
  case "doc":
@@ -775,18 +819,11 @@ function countLines(s) {
775
819
  return num;
776
820
  }
777
821
 
778
- // src/loaders/config/index.ts
779
- var import_node_path3 = __toESM(require("path"), 1);
780
- var import_promises2 = __toESM(require("fs/promises"), 1);
781
- function findConfigFile() {
782
- return import_node_path3.default.resolve("source.config.ts");
783
- }
784
-
785
822
  // src/loaders/adapter.ts
786
823
  var import_node_url = require("url");
787
- var import_promises3 = __toESM(require("fs/promises"), 1);
824
+ var import_promises2 = __toESM(require("fs/promises"), 1);
788
825
  var import_node_querystring = require("querystring");
789
- var import_node_path4 = __toESM(require("path"), 1);
826
+ var import_node_path3 = __toESM(require("path"), 1);
790
827
  function toVite(loader) {
791
828
  return async function(file, query, value) {
792
829
  const result = await loader({
@@ -800,6 +837,7 @@ function toVite(loader) {
800
837
  }
801
838
  }
802
839
  });
840
+ if (result === null) return null;
803
841
  return {
804
842
  code: result.code,
805
843
  map: result.map
@@ -808,9 +846,9 @@ function toVite(loader) {
808
846
  }
809
847
 
810
848
  // src/utils/import-formatter.ts
811
- var import_node_path5 = __toESM(require("path"), 1);
849
+ var import_node_path4 = __toESM(require("path"), 1);
812
850
  function toImportPath(file, config) {
813
- const ext = import_node_path5.default.extname(file);
851
+ const ext = import_node_path4.default.extname(file);
814
852
  let filename;
815
853
  if (ext === ".ts" && config.jsExtension) {
816
854
  filename = file.substring(0, file.length - ext.length) + ".js";
@@ -821,43 +859,30 @@ function toImportPath(file, config) {
821
859
  }
822
860
  let importPath;
823
861
  if ("relativeTo" in config) {
824
- importPath = import_node_path5.default.relative(config.relativeTo, filename);
825
- if (!import_node_path5.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
862
+ importPath = import_node_path4.default.relative(config.relativeTo, filename);
863
+ if (!import_node_path4.default.isAbsolute(importPath) && !importPath.startsWith(".")) {
826
864
  importPath = `./${importPath}`;
827
865
  }
828
866
  } else {
829
- importPath = import_node_path5.default.resolve(filename);
867
+ importPath = import_node_path4.default.resolve(filename);
830
868
  }
831
- return importPath.replaceAll(import_node_path5.default.sep, "/");
869
+ return importPath.replaceAll(import_node_path4.default.sep, "/");
832
870
  }
833
871
  function ident(code, tab = 1) {
834
872
  return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
835
873
  }
836
874
 
837
- // src/utils/collections.ts
838
- var import_picomatch = __toESM(require("picomatch"), 1);
839
- var import_tinyglobby = require("tinyglobby");
840
- var import_node_path6 = __toESM(require("path"), 1);
841
- var SupportedFormats = {
842
- doc: ["mdx", "md"],
843
- meta: ["json", "yaml"]
844
- };
845
- function getGlobPatterns(collection) {
846
- if (collection.files) return collection.files;
847
- return [`**/*.{${SupportedFormats[collection.type].join(",")}}`];
848
- }
849
-
850
875
  // src/utils/glob-import.ts
851
- var import_tinyglobby2 = require("tinyglobby");
852
- var import_node_path7 = __toESM(require("path"), 1);
876
+ var import_tinyglobby = require("tinyglobby");
877
+ var import_node_path5 = __toESM(require("path"), 1);
853
878
  var import_node_url2 = require("url");
854
879
  function generateGlobImport(patterns, options) {
855
880
  let code = "{";
856
- const result = (0, import_tinyglobby2.globSync)(patterns, {
881
+ const result = (0, import_tinyglobby.globSync)(patterns, {
857
882
  cwd: options.base
858
883
  });
859
884
  for (const item of result) {
860
- const fullPath = import_node_path7.default.join(options.base, item);
885
+ const fullPath = import_node_path5.default.join(options.base, item);
861
886
  const url = (0, import_node_url2.pathToFileURL)(fullPath);
862
887
  for (const [k, v] of Object.entries(options.query ?? {})) {
863
888
  url.searchParams.set(k, v);
@@ -873,21 +898,40 @@ function generateGlobImport(patterns, options) {
873
898
  }
874
899
 
875
900
  // src/plugins/vite.ts
876
- var import_node_path8 = __toESM(require("path"), 1);
901
+ var import_node_path6 = __toESM(require("path"), 1);
877
902
  function vite({
878
903
  index
879
904
  }) {
880
905
  let config;
906
+ let indexOptions;
907
+ if (index === false) indexOptions = false;
908
+ else indexOptions = applyDefaults(index === true ? {} : index);
881
909
  return {
910
+ name: "vite",
882
911
  config(v) {
883
912
  config = v;
884
913
  },
914
+ configureServer(server) {
915
+ if (!server.watcher || indexOptions === false || indexOptions.runtime === false)
916
+ return;
917
+ server.watcher.on("all", (event, file) => {
918
+ if (event === "change") return;
919
+ const isUpdated = config.collectionList.some((collection) => {
920
+ if (collection.type === "docs")
921
+ return collection.docs.hasFile(file) || collection.meta.hasFile(file);
922
+ return collection.hasFile(file);
923
+ });
924
+ if (isUpdated) {
925
+ this.core.emitAndWrite({
926
+ filterPlugin: (plugin) => plugin.name === "vite"
927
+ });
928
+ }
929
+ });
930
+ },
885
931
  emit() {
886
932
  const out = [];
887
- if (index === false) return out;
888
- const indexOptions = typeof index === "object" ? index : {};
889
- const { browser = false } = indexOptions;
890
- if (browser) {
933
+ if (indexOptions === false) return out;
934
+ if (indexOptions.browser) {
891
935
  out.push({
892
936
  path: "browser.ts",
893
937
  content: indexFile(this, config, indexOptions, "browser")
@@ -899,15 +943,21 @@ function vite({
899
943
  this,
900
944
  config,
901
945
  indexOptions,
902
- browser ? "server" : "all"
946
+ indexOptions.browser ? "server" : "all"
903
947
  )
904
948
  });
905
949
  return out;
906
950
  }
907
951
  };
908
952
  }
909
- function indexFile({ configPath, outDir }, config, options, environment) {
910
- const { addJsExtension = false, runtime } = options;
953
+ function applyDefaults(options) {
954
+ return {
955
+ addJsExtension: options.addJsExtension ?? false,
956
+ browser: options.browser ?? false,
957
+ runtime: options.runtime ?? false
958
+ };
959
+ }
960
+ function indexFile({ configPath, outDir }, config, { addJsExtension, runtime }, environment) {
911
961
  const runtimePath = {
912
962
  all: "fumadocs-mdx/runtime/vite",
913
963
  server: "fumadocs-mdx/runtime/vite.server",
@@ -923,74 +973,68 @@ function indexFile({ configPath, outDir }, config, options, environment) {
923
973
  "",
924
974
  `export const create = fromConfig<typeof Config>();`
925
975
  ];
926
- function docs(name, collection) {
927
- const obj = [
928
- ident(`doc: ${doc(name, collection.docs)}`),
929
- ident(`meta: ${meta(name, collection.meta)}`)
930
- ].join(",\n");
931
- return `{
976
+ function generateCollectionGlob(collection) {
977
+ if (collection.type === "docs") {
978
+ const obj = [
979
+ ident(`doc: ${generateCollectionGlob(collection.docs)}`),
980
+ ident(`meta: ${generateCollectionGlob(collection.meta)}`)
981
+ ].join(",\n");
982
+ return `{
932
983
  ${obj}
933
984
  }`;
934
- }
935
- function doc(name, collection) {
936
- const patterns = getGlobPatterns(collection);
985
+ }
937
986
  const dir = getCollectionDir(collection);
938
- const docGlob = generateGlob(patterns, {
939
- query: {
940
- collection: name
941
- },
942
- base: dir
943
- });
944
- if (collection.async) {
945
- const headBlob = generateGlob(patterns, {
987
+ if (collection.type === "doc") {
988
+ const docGlob = generateGlob(collection.patterns, {
946
989
  query: {
947
- only: "frontmatter",
948
- collection: name
990
+ collection: collection.name
949
991
  },
950
- import: "frontmatter",
951
992
  base: dir
952
993
  });
953
- return `create.docLazy("${name}", "${dir}", ${headBlob}, ${docGlob})`;
994
+ if (collection.async) {
995
+ const headBlob = generateGlob(collection.patterns, {
996
+ query: {
997
+ only: "frontmatter",
998
+ collection: collection.name
999
+ },
1000
+ import: "frontmatter",
1001
+ base: dir
1002
+ });
1003
+ return `create.docLazy("${collection.name}", "${dir}", ${headBlob}, ${docGlob})`;
1004
+ }
1005
+ return `create.doc("${collection.name}", "${dir}", ${docGlob})`;
954
1006
  }
955
- return `create.doc("${name}", "${dir}", ${docGlob})`;
956
- }
957
- function meta(name, collection) {
958
- const patterns = getGlobPatterns(collection);
959
- const dir = getCollectionDir(collection);
960
- return `create.meta("${name}", "${dir}", ${generateGlob(patterns, {
961
- import: "default",
962
- base: dir,
963
- query: {
964
- collection: name
1007
+ return `create.meta("${collection.name}", "${dir}", ${generateGlob(
1008
+ collection.patterns,
1009
+ {
1010
+ import: "default",
1011
+ base: dir,
1012
+ query: {
1013
+ collection: collection.name
1014
+ }
965
1015
  }
966
- })})`;
1016
+ )})`;
967
1017
  }
968
- function generateGlob(patterns, options2) {
1018
+ function generateGlob(patterns, options) {
969
1019
  patterns = patterns.map(normalizeGlobPath);
970
1020
  if (runtime === "node" || runtime === "bun") {
971
- return generateGlobImport(patterns, options2);
1021
+ return generateGlobImport(patterns, options);
972
1022
  } else {
973
1023
  return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
974
1024
  {
975
- ...options2,
976
- base: normalizeGlobPath(import_node_path8.default.relative(outDir, options2.base))
1025
+ ...options,
1026
+ base: normalizeGlobPath(import_node_path6.default.relative(outDir, options.base))
977
1027
  },
978
1028
  null,
979
1029
  2
980
1030
  )})`;
981
1031
  }
982
1032
  }
983
- for (const [name, collection] of config.collections.entries()) {
984
- let body;
985
- if (collection.type === "docs") {
986
- body = docs(name, collection);
987
- } else if (collection.type === "meta") {
988
- body = meta(name, collection);
989
- } else {
990
- body = doc(name, collection);
991
- }
1033
+ for (const collection of config.collectionList) {
992
1034
  lines.push("");
993
- lines.push(`export const ${name} = ${body};`);
1035
+ lines.push(
1036
+ `export const ${collection.name} = ${generateCollectionGlob(collection)};`
1037
+ );
994
1038
  }
995
1039
  return lines.join("\n");
996
1040
  }
@@ -1000,8 +1044,7 @@ function normalizeGlobPath(file) {
1000
1044
  if (file.startsWith("/")) return `.${file}`;
1001
1045
  return `./${file}`;
1002
1046
  }
1003
- function getCollectionDir(collection) {
1004
- const dir = collection.dir;
1047
+ function getCollectionDir({ dir }) {
1005
1048
  if (Array.isArray(dir)) {
1006
1049
  if (dir.length !== 1)
1007
1050
  throw new Error(
@@ -1011,17 +1054,20 @@ function getCollectionDir(collection) {
1011
1054
  }
1012
1055
  return dir;
1013
1056
  }
1014
- function slash(path13) {
1015
- const isExtendedLengthPath = path13.startsWith("\\\\?\\");
1057
+ function slash(path10) {
1058
+ const isExtendedLengthPath = path10.startsWith("\\\\?\\");
1016
1059
  if (isExtendedLengthPath) {
1017
- return path13;
1060
+ return path10;
1018
1061
  }
1019
- return path13.replaceAll("\\", "/");
1062
+ return path10.replaceAll("\\", "/");
1020
1063
  }
1021
1064
 
1022
1065
  // src/core.ts
1023
- var import_node_path9 = __toESM(require("path"), 1);
1024
- var import_promises4 = __toESM(require("fs/promises"), 1);
1066
+ var import_node_path7 = __toESM(require("path"), 1);
1067
+ var import_promises3 = __toESM(require("fs/promises"), 1);
1068
+ function findConfigFile() {
1069
+ return import_node_path7.default.resolve("source.config.ts");
1070
+ }
1025
1071
  function createCore(options, defaultPlugins = []) {
1026
1072
  let config;
1027
1073
  let plugins2;
@@ -1058,13 +1104,6 @@ function createCore(options, defaultPlugins = []) {
1058
1104
  getConfig() {
1059
1105
  return config;
1060
1106
  },
1061
- creatConfigLoader() {
1062
- return {
1063
- getConfig() {
1064
- return config;
1065
- }
1066
- };
1067
- },
1068
1107
  async initServer(server) {
1069
1108
  for (const plugin of plugins2) {
1070
1109
  await plugin.configureServer?.call(this.getPluginContext(), server);
@@ -1082,9 +1121,9 @@ function createCore(options, defaultPlugins = []) {
1082
1121
  );
1083
1122
  await Promise.all(
1084
1123
  out.flat().map(async (entry) => {
1085
- const file = import_node_path9.default.join(options.outDir, entry.path);
1086
- await import_promises4.default.mkdir(import_node_path9.default.dirname(file), { recursive: true });
1087
- await import_promises4.default.writeFile(file, entry.content);
1124
+ const file = import_node_path7.default.join(options.outDir, entry.path);
1125
+ await import_promises3.default.mkdir(import_node_path7.default.dirname(file), { recursive: true });
1126
+ await import_promises3.default.writeFile(file, entry.content);
1088
1127
  })
1089
1128
  );
1090
1129
  console.log(`[MDX] generated files in ${performance.now() - start}ms`);
@@ -1092,21 +1131,37 @@ function createCore(options, defaultPlugins = []) {
1092
1131
  };
1093
1132
  }
1094
1133
 
1095
- // src/vite/index.ts
1096
- var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
1097
- async function mdx(config, pluginOptions = {}) {
1098
- const options = applyDefaults(pluginOptions);
1099
- const core = await createViteCore(options).init({
1100
- config: buildConfig(config)
1101
- });
1102
- const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
1103
- async function transformMeta(path13, query, value) {
1104
- const isJson = path13.endsWith(".json");
1105
- const parsed = (0, import_node_querystring2.parse)(query);
1106
- const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
1134
+ // src/loaders/config.ts
1135
+ var import_promises4 = __toESM(require("fs/promises"), 1);
1136
+ function createIntegratedConfigLoader(core) {
1137
+ return {
1138
+ getConfig() {
1139
+ return core.getConfig();
1140
+ }
1141
+ };
1142
+ }
1143
+
1144
+ // src/loaders/meta.ts
1145
+ var import_js_yaml2 = require("js-yaml");
1146
+ var import_zod2 = require("zod");
1147
+ var querySchema2 = import_zod2.z.object({
1148
+ collection: import_zod2.z.string().optional()
1149
+ }).loose();
1150
+ function createMetaLoader(configLoader, resolve3 = {}) {
1151
+ const { json: resolveJson = "js", yaml: resolveYaml = "js" } = resolve3;
1152
+ return async ({ filePath, query, source }) => {
1153
+ const isJson = filePath.endsWith(".json");
1154
+ const parsed = querySchema2.parse(query);
1155
+ const collection = parsed.collection ? (await configLoader.getConfig()).getCollection(parsed.collection) : void 0;
1107
1156
  if (!collection) return null;
1157
+ let data;
1158
+ try {
1159
+ data = isJson ? JSON.parse(source) : (0, import_js_yaml2.load)(source);
1160
+ } catch (e) {
1161
+ throw new Error(`invalid data in ${filePath}`, { cause: e });
1162
+ }
1108
1163
  let schema;
1109
- switch (collection.type) {
1164
+ switch (collection?.type) {
1110
1165
  case "meta":
1111
1166
  schema = collection.schema;
1112
1167
  break;
@@ -1114,24 +1169,46 @@ async function mdx(config, pluginOptions = {}) {
1114
1169
  schema = collection.meta.schema;
1115
1170
  break;
1116
1171
  }
1117
- if (!schema) return null;
1118
- let data;
1119
- try {
1120
- data = isJson ? JSON.parse(value) : (0, import_js_yaml2.load)(value);
1121
- } catch {
1122
- return null;
1172
+ if (schema) {
1173
+ data = await validate(
1174
+ schema,
1175
+ data,
1176
+ { path: filePath, source },
1177
+ `invalid data in ${filePath}`
1178
+ );
1179
+ }
1180
+ let code;
1181
+ if (isJson) {
1182
+ code = resolveJson === "json" ? JSON.stringify(data) : `export default ${JSON.stringify(data)}`;
1183
+ } else {
1184
+ code = resolveYaml === "yaml" ? (0, import_js_yaml2.dump)(data) : `export default ${JSON.stringify(data)}`;
1123
1185
  }
1124
- const out = await validate(
1125
- schema,
1126
- data,
1127
- { path: path13, source: value },
1128
- `invalid data in ${path13}`
1129
- );
1130
1186
  return {
1131
- code: isJson ? JSON.stringify(out) : `export default ${JSON.stringify(out)}`,
1187
+ code,
1132
1188
  map: null
1133
1189
  };
1134
- }
1190
+ };
1191
+ }
1192
+
1193
+ // src/loaders/index.ts
1194
+ var metaLoaderGlob = /\.(json|yaml)(\?.+?)?$/;
1195
+ var mdxLoaderGlob = /\.mdx?(\?.+?)?$/;
1196
+
1197
+ // src/vite/index.ts
1198
+ var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
1199
+ async function mdx(config, pluginOptions = {}) {
1200
+ const options = applyDefaults2(pluginOptions);
1201
+ const core = await createViteCore(options).init({
1202
+ config: buildConfig(config)
1203
+ });
1204
+ const configLoader = createIntegratedConfigLoader(core);
1205
+ const mdxLoader = toVite(createMdxLoader(configLoader));
1206
+ const metaLoader = toVite(
1207
+ createMetaLoader(configLoader, {
1208
+ // vite has built-in plugin for JSON files
1209
+ json: "json"
1210
+ })
1211
+ );
1135
1212
  return {
1136
1213
  name: "fumadocs-mdx",
1137
1214
  // needed, otherwise other plugins will be executed before our `transform`.
@@ -1157,13 +1234,15 @@ async function mdx(config, pluginOptions = {}) {
1157
1234
  });
1158
1235
  },
1159
1236
  async transform(value, id) {
1160
- const [file, query = ""] = id.split("?");
1161
- const ext = path12.extname(file);
1162
1237
  try {
1163
- if ([".yaml", ".json"].includes(ext))
1164
- return await transformMeta(file, query, value);
1165
- if ([".md", ".mdx"].includes(ext))
1238
+ if (metaLoaderGlob.test(id)) {
1239
+ const [file, query = ""] = id.split("?", 2);
1240
+ return await metaLoader.call(this, file, query, value);
1241
+ }
1242
+ if (mdxLoaderGlob.test(id)) {
1243
+ const [file, query = ""] = id.split("?", 2);
1166
1244
  return await mdxLoader.call(this, file, query, value);
1245
+ }
1167
1246
  } catch (e) {
1168
1247
  if (e instanceof ValidationError) {
1169
1248
  throw new Error(e.toStringFormatted());
@@ -1174,8 +1253,8 @@ async function mdx(config, pluginOptions = {}) {
1174
1253
  };
1175
1254
  }
1176
1255
  async function postInstall(configPath = findConfigFile(), pluginOptions = {}) {
1177
- const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load(), load_exports));
1178
- const options = applyDefaults(pluginOptions);
1256
+ const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load_from_file(), load_from_file_exports));
1257
+ const options = applyDefaults2(pluginOptions);
1179
1258
  const core = await createViteCore(options).init({
1180
1259
  config: loadConfig2(configPath, options.outDir, true)
1181
1260
  });
@@ -1199,7 +1278,7 @@ function createViteCore({
1199
1278
  ]
1200
1279
  );
1201
1280
  }
1202
- function applyDefaults(options) {
1281
+ function applyDefaults2(options) {
1203
1282
  return {
1204
1283
  updateViteConfig: options.updateViteConfig ?? true,
1205
1284
  generateIndexFile: options.generateIndexFile ?? true,