fumadocs-mdx 13.0.0 → 13.0.2

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 +294 -257
  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 +145 -108
  39. package/dist/vite/index.js +53 -45
  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
@@ -199,18 +199,18 @@ async function compileConfig(configPath, outDir) {
199
199
  }
200
200
  async function loadConfig(configPath, outDir, build = false) {
201
201
  if (build) await compileConfig(configPath, outDir);
202
- const url = (0, import_node_url3.pathToFileURL)(path10.resolve(outDir, "source.config.mjs"));
202
+ const url = (0, import_node_url3.pathToFileURL)(path11.resolve(outDir, "source.config.mjs"));
203
203
  url.searchParams.set("hash", Date.now().toString());
204
204
  const config = import(url.href).then(
205
205
  (loaded) => buildConfig(loaded)
206
206
  );
207
207
  return await config;
208
208
  }
209
- var path10, import_node_url3;
209
+ var path11, import_node_url3;
210
210
  var init_load = __esm({
211
211
  "src/loaders/config/load.ts"() {
212
212
  "use strict";
213
- path10 = __toESM(require("path"), 1);
213
+ path11 = __toESM(require("path"), 1);
214
214
  import_node_url3 = require("url");
215
215
  init_build();
216
216
  }
@@ -266,7 +266,7 @@ async function validate(schema, data, context, errorMessage) {
266
266
  }
267
267
 
268
268
  // src/vite/index.ts
269
- var path11 = __toESM(require("path"), 1);
269
+ var path12 = __toESM(require("path"), 1);
270
270
  var import_js_yaml2 = require("js-yaml");
271
271
 
272
272
  // src/utils/fuma-matter.ts
@@ -675,14 +675,6 @@ var cacheEntry = import_zod.z.object({
675
675
  map: import_zod.z.any().optional(),
676
676
  hash: import_zod.z.string().optional()
677
677
  });
678
- var hashes = /* @__PURE__ */ new WeakMap();
679
- function getConfigHash(config) {
680
- let hash = hashes.get(config);
681
- if (hash) return hash;
682
- hash = Date.now().toString();
683
- hashes.set(config, hash);
684
- return hash;
685
- }
686
678
  function createMdxLoader(configLoader) {
687
679
  return async ({
688
680
  source: value,
@@ -693,14 +685,25 @@ function createMdxLoader(configLoader) {
693
685
  }) => {
694
686
  const matter = fumaMatter(value);
695
687
  const parsed = querySchema.parse(query);
696
- const loaded = await configLoader.getConfig();
697
- const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
698
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
699
- if (cacheDir) {
688
+ const config = await configLoader.getConfig();
689
+ let after;
690
+ if (!isDevelopment && config.global.experimentalBuildCache) {
691
+ const cacheDir = config.global.experimentalBuildCache;
692
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
700
693
  const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
701
694
  if (cached && cached.hash === generateCacheHash(value)) return cached;
695
+ after = async () => {
696
+ await import_promises.default.mkdir(cacheDir, { recursive: true });
697
+ await import_promises.default.writeFile(
698
+ import_node_path2.default.join(cacheDir, cacheKey),
699
+ JSON.stringify({
700
+ ...out,
701
+ hash: generateCacheHash(value)
702
+ })
703
+ );
704
+ };
702
705
  }
703
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
706
+ const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
704
707
  let docCollection;
705
708
  switch (collection?.type) {
706
709
  case "doc":
@@ -728,16 +731,16 @@ function createMdxLoader(configLoader) {
728
731
  };
729
732
  }
730
733
  const data = {};
731
- if (loaded.global.lastModifiedTime === "git") {
734
+ if (config.global.lastModifiedTime === "git") {
732
735
  data.lastModified = (await getGitTimestamp(filePath))?.getTime();
733
736
  }
734
737
  const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
735
738
  const compiled = await buildMDX(
736
- `${getConfigHash(loaded)}:${parsed.collection ?? "global"}`,
739
+ `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
737
740
  "\n".repeat(lineOffset) + matter.content,
738
741
  {
739
742
  development: isDevelopment,
740
- ...docCollection?.mdxOptions ?? await loaded.getDefaultMDXOptions(),
743
+ ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
741
744
  postprocess: docCollection?.postprocess,
742
745
  data,
743
746
  filePath,
@@ -749,19 +752,18 @@ function createMdxLoader(configLoader) {
749
752
  code: String(compiled.value),
750
753
  map: compiled.map
751
754
  };
752
- if (cacheDir) {
753
- await import_promises.default.mkdir(cacheDir, { recursive: true });
754
- await import_promises.default.writeFile(
755
- import_node_path2.default.join(cacheDir, cacheKey),
756
- JSON.stringify({
757
- ...out,
758
- hash: generateCacheHash(value)
759
- })
760
- );
761
- }
755
+ await after?.();
762
756
  return out;
763
757
  };
764
758
  }
759
+ var hashes = /* @__PURE__ */ new WeakMap();
760
+ function getConfigHash(config) {
761
+ let hash = hashes.get(config);
762
+ if (hash) return hash;
763
+ hash = Date.now().toString();
764
+ hashes.set(config, hash);
765
+ return hash;
766
+ }
765
767
  function generateCacheHash(input) {
766
768
  return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
767
769
  }
@@ -779,13 +781,6 @@ var import_promises2 = __toESM(require("fs/promises"), 1);
779
781
  function findConfigFile() {
780
782
  return import_node_path3.default.resolve("source.config.ts");
781
783
  }
782
- function resolvedConfig(loaded) {
783
- return {
784
- getConfig() {
785
- return loaded;
786
- }
787
- };
788
- }
789
784
 
790
785
  // src/loaders/adapter.ts
791
786
  var import_node_url = require("url");
@@ -840,28 +835,29 @@ function ident(code, tab = 1) {
840
835
  }
841
836
 
842
837
  // src/utils/collections.ts
843
- function getSupportedFormats(collection) {
844
- return {
845
- doc: ["mdx", "md"],
846
- meta: ["json", "yaml"]
847
- }[collection.type];
848
- }
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
+ };
849
845
  function getGlobPatterns(collection) {
850
846
  if (collection.files) return collection.files;
851
- return [`**/*.{${getSupportedFormats(collection).join(",")}}`];
847
+ return [`**/*.{${SupportedFormats[collection.type].join(",")}}`];
852
848
  }
853
849
 
854
850
  // src/utils/glob-import.ts
855
- var import_tinyglobby = require("tinyglobby");
856
- var import_node_path6 = __toESM(require("path"), 1);
851
+ var import_tinyglobby2 = require("tinyglobby");
852
+ var import_node_path7 = __toESM(require("path"), 1);
857
853
  var import_node_url2 = require("url");
858
854
  function generateGlobImport(patterns, options) {
859
855
  let code = "{";
860
- const result = (0, import_tinyglobby.globSync)(patterns, {
856
+ const result = (0, import_tinyglobby2.globSync)(patterns, {
861
857
  cwd: options.base
862
858
  });
863
859
  for (const item of result) {
864
- const fullPath = import_node_path6.default.join(options.base, item);
860
+ const fullPath = import_node_path7.default.join(options.base, item);
865
861
  const url = (0, import_node_url2.pathToFileURL)(fullPath);
866
862
  for (const [k, v] of Object.entries(options.query ?? {})) {
867
863
  url.searchParams.set(k, v);
@@ -877,7 +873,7 @@ function generateGlobImport(patterns, options) {
877
873
  }
878
874
 
879
875
  // src/plugins/vite.ts
880
- var import_node_path7 = __toESM(require("path"), 1);
876
+ var import_node_path8 = __toESM(require("path"), 1);
881
877
  function vite(options) {
882
878
  let config;
883
879
  return {
@@ -885,7 +881,6 @@ function vite(options) {
885
881
  config = v;
886
882
  },
887
883
  emit() {
888
- console.log("[Fumadocs MDX] Generating index files");
889
884
  return [
890
885
  {
891
886
  path: "index.ts",
@@ -918,12 +913,12 @@ ${obj}
918
913
  }
919
914
  function doc(name, collection) {
920
915
  const patterns = getGlobPatterns(collection);
921
- const base = getGlobBase(collection);
916
+ const dir = getCollectionDir(collection);
922
917
  const docGlob = generateGlob(patterns, {
923
918
  query: {
924
919
  collection: name
925
920
  },
926
- base
921
+ base: dir
927
922
  });
928
923
  if (collection.async) {
929
924
  const headBlob = generateGlob(patterns, {
@@ -932,32 +927,32 @@ ${obj}
932
927
  collection: name
933
928
  },
934
929
  import: "frontmatter",
935
- base
930
+ base: dir
936
931
  });
937
- return `create.docLazy("${name}", "${base}", ${headBlob}, ${docGlob})`;
932
+ return `create.docLazy("${name}", "${dir}", ${headBlob}, ${docGlob})`;
938
933
  }
939
- return `create.doc("${name}", "${base}", ${docGlob})`;
934
+ return `create.doc("${name}", "${dir}", ${docGlob})`;
940
935
  }
941
936
  function meta(name, collection) {
942
937
  const patterns = getGlobPatterns(collection);
943
- const base = getGlobBase(collection);
944
- return `create.meta("${name}", "${base}", ${generateGlob(patterns, {
938
+ const dir = getCollectionDir(collection);
939
+ return `create.meta("${name}", "${dir}", ${generateGlob(patterns, {
945
940
  import: "default",
946
- base,
941
+ base: dir,
947
942
  query: {
948
943
  collection: name
949
944
  }
950
945
  })})`;
951
946
  }
952
947
  function generateGlob(patterns, options2) {
953
- patterns = mapGlobPatterns(patterns);
948
+ patterns = patterns.map(normalizeGlobPath);
954
949
  if (runtime === "node" || runtime === "bun") {
955
950
  return generateGlobImport(patterns, options2);
956
951
  } else {
957
952
  return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
958
953
  {
959
954
  ...options2,
960
- base: import_node_path7.default.relative(outDir, options2.base)
955
+ base: normalizeGlobPath(import_node_path8.default.relative(outDir, options2.base))
961
956
  },
962
957
  null,
963
958
  2
@@ -978,63 +973,100 @@ ${obj}
978
973
  }
979
974
  return lines.join("\n");
980
975
  }
981
- function mapGlobPatterns(patterns) {
982
- return patterns.map(enforceRelative);
983
- }
984
- function enforceRelative(file) {
976
+ function normalizeGlobPath(file) {
977
+ file = slash(file);
985
978
  if (file.startsWith("./")) return file;
986
979
  if (file.startsWith("/")) return `.${file}`;
987
980
  return `./${file}`;
988
981
  }
989
- function getGlobBase(collection) {
990
- let dir = collection.dir;
982
+ function getCollectionDir(collection) {
983
+ const dir = collection.dir;
991
984
  if (Array.isArray(dir)) {
992
985
  if (dir.length !== 1)
993
986
  throw new Error(
994
987
  `[Fumadocs MDX] Vite Plugin doesn't support multiple \`dir\` for a collection at the moment.`
995
988
  );
996
- dir = dir[0];
989
+ return dir[0];
990
+ }
991
+ return dir;
992
+ }
993
+ function slash(path13) {
994
+ const isExtendedLengthPath = path13.startsWith("\\\\?\\");
995
+ if (isExtendedLengthPath) {
996
+ return path13;
997
997
  }
998
- return enforceRelative(dir);
998
+ return path13.replaceAll("\\", "/");
999
999
  }
1000
1000
 
1001
- // src/plugins/index.ts
1002
- var import_node_path8 = __toESM(require("path"), 1);
1001
+ // src/core.ts
1002
+ var import_node_path9 = __toESM(require("path"), 1);
1003
1003
  var import_promises4 = __toESM(require("fs/promises"), 1);
1004
- function createPluginHandler(context, defaultPlugins = []) {
1005
- const plugins2 = [];
1006
- async function write(entry) {
1007
- const file = import_node_path8.default.join(context.outDir, entry.path);
1008
- await import_promises4.default.mkdir(import_node_path8.default.dirname(file), { recursive: true });
1009
- await import_promises4.default.writeFile(file, entry.content);
1010
- }
1004
+ function createCore(options, defaultPlugins = []) {
1005
+ let config;
1006
+ let plugins2;
1011
1007
  return {
1012
- async init(config) {
1013
- if (config.global.plugins) {
1014
- defaultPlugins.push(...config.global.plugins);
1015
- }
1016
- for await (const option of defaultPlugins) {
1008
+ _options: options,
1009
+ getPluginContext() {
1010
+ return {
1011
+ core: this,
1012
+ ...options
1013
+ };
1014
+ },
1015
+ /**
1016
+ * Convenient cache store, reset when config changes
1017
+ */
1018
+ cache: /* @__PURE__ */ new Map(),
1019
+ async init({ config: newConfig }) {
1020
+ config = await newConfig;
1021
+ this.cache.clear();
1022
+ plugins2 = [];
1023
+ for await (const option of [
1024
+ ...defaultPlugins,
1025
+ ...config.global.plugins ?? []
1026
+ ]) {
1017
1027
  if (!option) continue;
1018
1028
  if (Array.isArray(option)) plugins2.push(...option);
1019
1029
  else plugins2.push(option);
1020
1030
  }
1021
1031
  for (const plugin of plugins2) {
1022
- const out = await plugin.config?.call(context, config);
1032
+ const out = await plugin.config?.call(this.getPluginContext(), config);
1023
1033
  if (out) config = out;
1024
1034
  }
1035
+ return this;
1036
+ },
1037
+ getConfig() {
1025
1038
  return config;
1026
1039
  },
1027
- async emit() {
1040
+ creatConfigLoader() {
1041
+ return {
1042
+ getConfig() {
1043
+ return config;
1044
+ }
1045
+ };
1046
+ },
1047
+ async initServer(server) {
1048
+ for (const plugin of plugins2) {
1049
+ await plugin.configureServer?.call(this.getPluginContext(), server);
1050
+ }
1051
+ },
1052
+ async emitAndWrite({
1053
+ filterPlugin = () => true
1054
+ } = {}) {
1055
+ const start = performance.now();
1028
1056
  const out = await Promise.all(
1029
1057
  plugins2.map((plugin) => {
1030
- return plugin.emit?.call(context) ?? [];
1058
+ if (!filterPlugin(plugin) || !plugin.emit) return [];
1059
+ return plugin.emit.call(this.getPluginContext());
1031
1060
  })
1032
1061
  );
1033
- return out.flat();
1034
- },
1035
- async emitAndWrite() {
1036
- const entries = await this.emit();
1037
- await Promise.all(entries.map(write));
1062
+ await Promise.all(
1063
+ out.flat().map(async (entry) => {
1064
+ const file = import_node_path9.default.join(options.outDir, entry.path);
1065
+ await import_promises4.default.mkdir(import_node_path9.default.dirname(file), { recursive: true });
1066
+ await import_promises4.default.writeFile(file, entry.content);
1067
+ })
1068
+ );
1069
+ console.log(`[MDX] generated files in ${performance.now() - start}ms`);
1038
1070
  }
1039
1071
  };
1040
1072
  }
@@ -1043,14 +1075,14 @@ function createPluginHandler(context, defaultPlugins = []) {
1043
1075
  var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
1044
1076
  async function mdx(config, pluginOptions = {}) {
1045
1077
  const options = applyDefaults(pluginOptions);
1046
- const { updateViteConfig } = options;
1047
- const pluginHandler = createVitePluginHandler(options);
1048
- const loaded = await pluginHandler.init(buildConfig(config));
1049
- const mdxLoader = toVite(createMdxLoader(resolvedConfig(loaded)));
1050
- async function transformMeta(path12, query, value) {
1051
- const isJson = path12.endsWith(".json");
1078
+ const core = await createViteCore(options).init({
1079
+ config: buildConfig(config)
1080
+ });
1081
+ const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
1082
+ async function transformMeta(path13, query, value) {
1083
+ const isJson = path13.endsWith(".json");
1052
1084
  const parsed = (0, import_node_querystring2.parse)(query);
1053
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
1085
+ const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
1054
1086
  if (!collection) return null;
1055
1087
  let schema;
1056
1088
  switch (collection.type) {
@@ -1071,8 +1103,8 @@ async function mdx(config, pluginOptions = {}) {
1071
1103
  const out = await validate(
1072
1104
  schema,
1073
1105
  data,
1074
- { path: path12, source: value },
1075
- `invalid data in ${path12}`
1106
+ { path: path13, source: value },
1107
+ `invalid data in ${path13}`
1076
1108
  );
1077
1109
  return {
1078
1110
  code: isJson ? JSON.stringify(out) : `export default ${JSON.stringify(out)}`,
@@ -1084,7 +1116,7 @@ async function mdx(config, pluginOptions = {}) {
1084
1116
  // needed, otherwise other plugins will be executed before our `transform`.
1085
1117
  enforce: "pre",
1086
1118
  config(config2) {
1087
- if (!updateViteConfig) return config2;
1119
+ if (!options.updateViteConfig) return config2;
1088
1120
  return (0, import_vite.mergeConfig)(config2, {
1089
1121
  optimizeDeps: {
1090
1122
  exclude: FumadocsDeps
@@ -1096,11 +1128,16 @@ async function mdx(config, pluginOptions = {}) {
1096
1128
  });
1097
1129
  },
1098
1130
  async buildStart() {
1099
- await pluginHandler.emitAndWrite();
1131
+ await core.emitAndWrite();
1132
+ },
1133
+ async configureServer(server) {
1134
+ await core.initServer({
1135
+ watcher: server.watcher
1136
+ });
1100
1137
  },
1101
1138
  async transform(value, id) {
1102
1139
  const [file, query = ""] = id.split("?");
1103
- const ext = path11.extname(file);
1140
+ const ext = path12.extname(file);
1104
1141
  try {
1105
1142
  if ([".yaml", ".json"].includes(ext))
1106
1143
  return await transformMeta(file, query, value);
@@ -1118,17 +1155,17 @@ async function mdx(config, pluginOptions = {}) {
1118
1155
  async function postInstall(configPath = findConfigFile(), pluginOptions = {}) {
1119
1156
  const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load(), load_exports));
1120
1157
  const options = applyDefaults(pluginOptions);
1121
- const pluginHandler = createVitePluginHandler(options);
1122
- await pluginHandler.init(await loadConfig2(configPath, options.outDir, true));
1123
- await pluginHandler.emitAndWrite();
1124
- console.log("[MDX] types generated");
1158
+ const core = await createViteCore(options).init({
1159
+ config: loadConfig2(configPath, options.outDir, true)
1160
+ });
1161
+ await core.emitAndWrite();
1125
1162
  }
1126
- function createVitePluginHandler({
1163
+ function createViteCore({
1127
1164
  configPath,
1128
1165
  outDir,
1129
1166
  generateIndexFile
1130
1167
  }) {
1131
- return createPluginHandler(
1168
+ return createCore(
1132
1169
  {
1133
1170
  environment: "vite",
1134
1171
  configPath,
@@ -1,29 +1,28 @@
1
1
  import {
2
- toVite
3
- } from "../chunk-XV5Z4BFL.js";
4
- import {
5
- getGlobPatterns,
6
2
  ident,
7
3
  toImportPath
8
- } from "../chunk-6Y5JDZHD.js";
4
+ } from "../chunk-CXA4JO4Z.js";
5
+ import {
6
+ buildConfig
7
+ } from "../chunk-U4MQ44TS.js";
8
+ import {
9
+ toVite
10
+ } from "../chunk-YVCR6FUH.js";
9
11
  import {
10
12
  createMdxLoader
11
- } from "../chunk-CEA6MYJU.js";
13
+ } from "../chunk-XQ5O7IPO.js";
12
14
  import "../chunk-3J3WL7WN.js";
13
15
  import "../chunk-K5ZLPEIQ.js";
14
- import {
15
- buildConfig
16
- } from "../chunk-U4MQ44TS.js";
17
16
  import {
18
17
  ValidationError,
18
+ createCore,
19
19
  findConfigFile,
20
- resolvedConfig,
21
20
  validate
22
- } from "../chunk-HI62EXSB.js";
21
+ } from "../chunk-EELYB2XC.js";
23
22
  import "../chunk-VWJKRQZR.js";
24
23
  import {
25
- createPluginHandler
26
- } from "../chunk-4MAYA5QX.js";
24
+ getGlobPatterns
25
+ } from "../chunk-XZY2AWJI.js";
27
26
 
28
27
  // src/vite/index.ts
29
28
  import {
@@ -67,7 +66,6 @@ function vite(options) {
67
66
  config = v;
68
67
  },
69
68
  emit() {
70
- console.log("[Fumadocs MDX] Generating index files");
71
69
  return [
72
70
  {
73
71
  path: "index.ts",
@@ -100,12 +98,12 @@ ${obj}
100
98
  }
101
99
  function doc(name, collection) {
102
100
  const patterns = getGlobPatterns(collection);
103
- const base = getGlobBase(collection);
101
+ const dir = getCollectionDir(collection);
104
102
  const docGlob = generateGlob(patterns, {
105
103
  query: {
106
104
  collection: name
107
105
  },
108
- base
106
+ base: dir
109
107
  });
110
108
  if (collection.async) {
111
109
  const headBlob = generateGlob(patterns, {
@@ -114,32 +112,32 @@ ${obj}
114
112
  collection: name
115
113
  },
116
114
  import: "frontmatter",
117
- base
115
+ base: dir
118
116
  });
119
- return `create.docLazy("${name}", "${base}", ${headBlob}, ${docGlob})`;
117
+ return `create.docLazy("${name}", "${dir}", ${headBlob}, ${docGlob})`;
120
118
  }
121
- return `create.doc("${name}", "${base}", ${docGlob})`;
119
+ return `create.doc("${name}", "${dir}", ${docGlob})`;
122
120
  }
123
121
  function meta(name, collection) {
124
122
  const patterns = getGlobPatterns(collection);
125
- const base = getGlobBase(collection);
126
- return `create.meta("${name}", "${base}", ${generateGlob(patterns, {
123
+ const dir = getCollectionDir(collection);
124
+ return `create.meta("${name}", "${dir}", ${generateGlob(patterns, {
127
125
  import: "default",
128
- base,
126
+ base: dir,
129
127
  query: {
130
128
  collection: name
131
129
  }
132
130
  })})`;
133
131
  }
134
132
  function generateGlob(patterns, options2) {
135
- patterns = mapGlobPatterns(patterns);
133
+ patterns = patterns.map(normalizeGlobPath);
136
134
  if (runtime === "node" || runtime === "bun") {
137
135
  return generateGlobImport(patterns, options2);
138
136
  } else {
139
137
  return `import.meta.glob(${JSON.stringify(patterns)}, ${JSON.stringify(
140
138
  {
141
139
  ...options2,
142
- base: path2.relative(outDir, options2.base)
140
+ base: normalizeGlobPath(path2.relative(outDir, options2.base))
143
141
  },
144
142
  null,
145
143
  2
@@ -160,38 +158,43 @@ ${obj}
160
158
  }
161
159
  return lines.join("\n");
162
160
  }
163
- function mapGlobPatterns(patterns) {
164
- return patterns.map(enforceRelative);
165
- }
166
- function enforceRelative(file) {
161
+ function normalizeGlobPath(file) {
162
+ file = slash(file);
167
163
  if (file.startsWith("./")) return file;
168
164
  if (file.startsWith("/")) return `.${file}`;
169
165
  return `./${file}`;
170
166
  }
171
- function getGlobBase(collection) {
172
- let dir = collection.dir;
167
+ function getCollectionDir(collection) {
168
+ const dir = collection.dir;
173
169
  if (Array.isArray(dir)) {
174
170
  if (dir.length !== 1)
175
171
  throw new Error(
176
172
  `[Fumadocs MDX] Vite Plugin doesn't support multiple \`dir\` for a collection at the moment.`
177
173
  );
178
- dir = dir[0];
174
+ return dir[0];
175
+ }
176
+ return dir;
177
+ }
178
+ function slash(path4) {
179
+ const isExtendedLengthPath = path4.startsWith("\\\\?\\");
180
+ if (isExtendedLengthPath) {
181
+ return path4;
179
182
  }
180
- return enforceRelative(dir);
183
+ return path4.replaceAll("\\", "/");
181
184
  }
182
185
 
183
186
  // src/vite/index.ts
184
187
  var FumadocsDeps = ["fumadocs-core", "fumadocs-ui", "fumadocs-openapi"];
185
188
  async function mdx(config, pluginOptions = {}) {
186
189
  const options = applyDefaults(pluginOptions);
187
- const { updateViteConfig } = options;
188
- const pluginHandler = createVitePluginHandler(options);
189
- const loaded = await pluginHandler.init(buildConfig(config));
190
- const mdxLoader = toVite(createMdxLoader(resolvedConfig(loaded)));
190
+ const core = await createViteCore(options).init({
191
+ config: buildConfig(config)
192
+ });
193
+ const mdxLoader = toVite(createMdxLoader(core.creatConfigLoader()));
191
194
  async function transformMeta(path4, query, value) {
192
195
  const isJson = path4.endsWith(".json");
193
196
  const parsed = parse(query);
194
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
197
+ const collection = parsed.collection ? core.getConfig().collections.get(parsed.collection) : void 0;
195
198
  if (!collection) return null;
196
199
  let schema;
197
200
  switch (collection.type) {
@@ -225,7 +228,7 @@ async function mdx(config, pluginOptions = {}) {
225
228
  // needed, otherwise other plugins will be executed before our `transform`.
226
229
  enforce: "pre",
227
230
  config(config2) {
228
- if (!updateViteConfig) return config2;
231
+ if (!options.updateViteConfig) return config2;
229
232
  return mergeConfig(config2, {
230
233
  optimizeDeps: {
231
234
  exclude: FumadocsDeps
@@ -237,7 +240,12 @@ async function mdx(config, pluginOptions = {}) {
237
240
  });
238
241
  },
239
242
  async buildStart() {
240
- await pluginHandler.emitAndWrite();
243
+ await core.emitAndWrite();
244
+ },
245
+ async configureServer(server) {
246
+ await core.initServer({
247
+ watcher: server.watcher
248
+ });
241
249
  },
242
250
  async transform(value, id) {
243
251
  const [file, query = ""] = id.split("?");
@@ -259,17 +267,17 @@ async function mdx(config, pluginOptions = {}) {
259
267
  async function postInstall(configPath = findConfigFile(), pluginOptions = {}) {
260
268
  const { loadConfig } = await import("../load-MNG3CLET.js");
261
269
  const options = applyDefaults(pluginOptions);
262
- const pluginHandler = createVitePluginHandler(options);
263
- await pluginHandler.init(await loadConfig(configPath, options.outDir, true));
264
- await pluginHandler.emitAndWrite();
265
- console.log("[MDX] types generated");
270
+ const core = await createViteCore(options).init({
271
+ config: loadConfig(configPath, options.outDir, true)
272
+ });
273
+ await core.emitAndWrite();
266
274
  }
267
- function createVitePluginHandler({
275
+ function createViteCore({
268
276
  configPath,
269
277
  outDir,
270
278
  generateIndexFile
271
279
  }) {
272
- return createPluginHandler(
280
+ return createCore(
273
281
  {
274
282
  environment: "vite",
275
283
  configPath,