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
@@ -3,7 +3,7 @@ import { StructuredData } from 'fumadocs-core/mdx-plugins';
3
3
  import { TOCItemType } from 'fumadocs-core/toc';
4
4
  import { FC } from 'react';
5
5
  import { MDXProps } from 'mdx/types';
6
- import { E as ExtractedReference } from './remark-postprocess-K233ZVBK.js';
6
+ import { E as ExtractedReference } from './core-B6j6Fxse.js';
7
7
 
8
8
  type Processor = ReturnType<typeof createProcessor>;
9
9
  interface CompilerOptions {
@@ -3,7 +3,7 @@ import { StructuredData } from 'fumadocs-core/mdx-plugins';
3
3
  import { TOCItemType } from 'fumadocs-core/toc';
4
4
  import { FC } from 'react';
5
5
  import { MDXProps } from 'mdx/types';
6
- import { E as ExtractedReference } from './remark-postprocess-K233ZVBK.cjs';
6
+ import { E as ExtractedReference } from './core-B6j6Fxse.cjs';
7
7
 
8
8
  type Processor = ReturnType<typeof createProcessor>;
9
9
  interface CompilerOptions {
@@ -623,14 +623,6 @@ var cacheEntry = import_zod.z.object({
623
623
  map: import_zod.z.any().optional(),
624
624
  hash: import_zod.z.string().optional()
625
625
  });
626
- var hashes = /* @__PURE__ */ new WeakMap();
627
- function getConfigHash(config) {
628
- let hash = hashes.get(config);
629
- if (hash) return hash;
630
- hash = Date.now().toString();
631
- hashes.set(config, hash);
632
- return hash;
633
- }
634
626
  function createMdxLoader(configLoader) {
635
627
  return async ({
636
628
  source: value,
@@ -641,14 +633,25 @@ function createMdxLoader(configLoader) {
641
633
  }) => {
642
634
  const matter = fumaMatter(value);
643
635
  const parsed = querySchema.parse(query);
644
- const loaded = await configLoader.getConfig();
645
- const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
646
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
647
- if (cacheDir) {
636
+ const config = await configLoader.getConfig();
637
+ let after;
638
+ if (!isDevelopment && config.global.experimentalBuildCache) {
639
+ const cacheDir = config.global.experimentalBuildCache;
640
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
648
641
  const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
649
642
  if (cached && cached.hash === generateCacheHash(value)) return cached;
643
+ after = async () => {
644
+ await import_promises.default.mkdir(cacheDir, { recursive: true });
645
+ await import_promises.default.writeFile(
646
+ import_node_path2.default.join(cacheDir, cacheKey),
647
+ JSON.stringify({
648
+ ...out,
649
+ hash: generateCacheHash(value)
650
+ })
651
+ );
652
+ };
650
653
  }
651
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
654
+ const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
652
655
  let docCollection;
653
656
  switch (collection?.type) {
654
657
  case "doc":
@@ -676,16 +679,16 @@ function createMdxLoader(configLoader) {
676
679
  };
677
680
  }
678
681
  const data = {};
679
- if (loaded.global.lastModifiedTime === "git") {
682
+ if (config.global.lastModifiedTime === "git") {
680
683
  data.lastModified = (await getGitTimestamp(filePath))?.getTime();
681
684
  }
682
685
  const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
683
686
  const compiled = await buildMDX(
684
- `${getConfigHash(loaded)}:${parsed.collection ?? "global"}`,
687
+ `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
685
688
  "\n".repeat(lineOffset) + matter.content,
686
689
  {
687
690
  development: isDevelopment,
688
- ...docCollection?.mdxOptions ?? await loaded.getDefaultMDXOptions(),
691
+ ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
689
692
  postprocess: docCollection?.postprocess,
690
693
  data,
691
694
  filePath,
@@ -697,19 +700,18 @@ function createMdxLoader(configLoader) {
697
700
  code: String(compiled.value),
698
701
  map: compiled.map
699
702
  };
700
- if (cacheDir) {
701
- await import_promises.default.mkdir(cacheDir, { recursive: true });
702
- await import_promises.default.writeFile(
703
- import_node_path2.default.join(cacheDir, cacheKey),
704
- JSON.stringify({
705
- ...out,
706
- hash: generateCacheHash(value)
707
- })
708
- );
709
- }
703
+ await after?.();
710
704
  return out;
711
705
  };
712
706
  }
707
+ var hashes = /* @__PURE__ */ new WeakMap();
708
+ function getConfigHash(config) {
709
+ let hash = hashes.get(config);
710
+ if (hash) return hash;
711
+ hash = Date.now().toString();
712
+ hashes.set(config, hash);
713
+ return hash;
714
+ }
713
715
  function generateCacheHash(input) {
714
716
  return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
715
717
  }
@@ -727,24 +729,102 @@ var import_promises2 = __toESM(require("fs/promises"), 1);
727
729
  function findConfigFile() {
728
730
  return import_node_path3.default.resolve("source.config.ts");
729
731
  }
730
- function resolvedConfig(loaded) {
732
+
733
+ // src/bun/index.ts
734
+ init_build();
735
+ var import_node_querystring = require("querystring");
736
+ var import_node_url = require("url");
737
+
738
+ // src/core.ts
739
+ var import_node_path4 = __toESM(require("path"), 1);
740
+ var import_promises3 = __toESM(require("fs/promises"), 1);
741
+ function createCore(options, defaultPlugins = []) {
742
+ let config;
743
+ let plugins2;
731
744
  return {
745
+ _options: options,
746
+ getPluginContext() {
747
+ return {
748
+ core: this,
749
+ ...options
750
+ };
751
+ },
752
+ /**
753
+ * Convenient cache store, reset when config changes
754
+ */
755
+ cache: /* @__PURE__ */ new Map(),
756
+ async init({ config: newConfig }) {
757
+ config = await newConfig;
758
+ this.cache.clear();
759
+ plugins2 = [];
760
+ for await (const option of [
761
+ ...defaultPlugins,
762
+ ...config.global.plugins ?? []
763
+ ]) {
764
+ if (!option) continue;
765
+ if (Array.isArray(option)) plugins2.push(...option);
766
+ else plugins2.push(option);
767
+ }
768
+ for (const plugin of plugins2) {
769
+ const out = await plugin.config?.call(this.getPluginContext(), config);
770
+ if (out) config = out;
771
+ }
772
+ return this;
773
+ },
732
774
  getConfig() {
733
- return loaded;
775
+ return config;
776
+ },
777
+ creatConfigLoader() {
778
+ return {
779
+ getConfig() {
780
+ return config;
781
+ }
782
+ };
783
+ },
784
+ async initServer(server) {
785
+ for (const plugin of plugins2) {
786
+ await plugin.configureServer?.call(this.getPluginContext(), server);
787
+ }
788
+ },
789
+ async emitAndWrite({
790
+ filterPlugin = () => true
791
+ } = {}) {
792
+ const start = performance.now();
793
+ const out = await Promise.all(
794
+ plugins2.map((plugin) => {
795
+ if (!filterPlugin(plugin) || !plugin.emit) return [];
796
+ return plugin.emit.call(this.getPluginContext());
797
+ })
798
+ );
799
+ await Promise.all(
800
+ out.flat().map(async (entry) => {
801
+ const file = import_node_path4.default.join(options.outDir, entry.path);
802
+ await import_promises3.default.mkdir(import_node_path4.default.dirname(file), { recursive: true });
803
+ await import_promises3.default.writeFile(file, entry.content);
804
+ })
805
+ );
806
+ console.log(`[MDX] generated files in ${performance.now() - start}ms`);
734
807
  }
735
808
  };
736
809
  }
737
810
 
738
811
  // src/bun/index.ts
739
- init_build();
740
- var import_node_querystring = require("querystring");
741
- var import_node_url = require("url");
742
812
  function createMdxPlugin(options = {}) {
743
- const { configPath = findConfigFile() } = options;
813
+ const {
814
+ environment = "bun",
815
+ outDir = ".source",
816
+ configPath = findConfigFile()
817
+ } = options;
744
818
  async function getMdxLoader() {
745
819
  const importPath = (0, import_node_url.pathToFileURL)(configPath).href;
746
- const out = buildConfig(await import(importPath));
747
- return createMdxLoader(resolvedConfig(out));
820
+ const core = await createCore({
821
+ environment,
822
+ outDir,
823
+ configPath
824
+ }).init({
825
+ config: buildConfig(await import(importPath))
826
+ });
827
+ return createMdxLoader(core.creatConfigLoader());
748
828
  }
749
829
  return {
750
830
  name: "bun-plugin-fumadocs-mdx",
@@ -1,8 +1,13 @@
1
1
  import { BunPlugin } from 'bun';
2
+ import { C as CoreOptions } from '../core-B6j6Fxse.cjs';
3
+ import '@standard-schema/spec';
4
+ import 'fumadocs-core/mdx-plugins';
5
+ import '@mdx-js/mdx';
6
+ import 'unified';
7
+ import 'zod';
8
+ import 'chokidar';
2
9
 
3
- interface MdxPluginOptions {
4
- configPath?: string;
5
- }
10
+ type MdxPluginOptions = Partial<CoreOptions>;
6
11
  declare function createMdxPlugin(options?: MdxPluginOptions): BunPlugin;
7
12
 
8
13
  export { type MdxPluginOptions, createMdxPlugin };
@@ -1,8 +1,13 @@
1
1
  import { BunPlugin } from 'bun';
2
+ import { C as CoreOptions } from '../core-B6j6Fxse.js';
3
+ import '@standard-schema/spec';
4
+ import 'fumadocs-core/mdx-plugins';
5
+ import '@mdx-js/mdx';
6
+ import 'unified';
7
+ import 'zod';
8
+ import 'chokidar';
2
9
 
3
- interface MdxPluginOptions {
4
- configPath?: string;
5
- }
10
+ type MdxPluginOptions = Partial<CoreOptions>;
6
11
  declare function createMdxPlugin(options?: MdxPluginOptions): BunPlugin;
7
12
 
8
13
  export { type MdxPluginOptions, createMdxPlugin };
package/dist/bun/index.js CHANGED
@@ -1,26 +1,36 @@
1
+ import {
2
+ buildConfig
3
+ } from "../chunk-U4MQ44TS.js";
1
4
  import {
2
5
  createMdxLoader
3
- } from "../chunk-CEA6MYJU.js";
6
+ } from "../chunk-XQ5O7IPO.js";
4
7
  import "../chunk-3J3WL7WN.js";
5
8
  import "../chunk-K5ZLPEIQ.js";
6
9
  import {
7
- buildConfig
8
- } from "../chunk-U4MQ44TS.js";
9
- import {
10
- findConfigFile,
11
- resolvedConfig
12
- } from "../chunk-HI62EXSB.js";
10
+ createCore,
11
+ findConfigFile
12
+ } from "../chunk-EELYB2XC.js";
13
13
  import "../chunk-VWJKRQZR.js";
14
14
 
15
15
  // src/bun/index.ts
16
16
  import { parse } from "querystring";
17
17
  import { pathToFileURL } from "url";
18
18
  function createMdxPlugin(options = {}) {
19
- const { configPath = findConfigFile() } = options;
19
+ const {
20
+ environment = "bun",
21
+ outDir = ".source",
22
+ configPath = findConfigFile()
23
+ } = options;
20
24
  async function getMdxLoader() {
21
25
  const importPath = pathToFileURL(configPath).href;
22
- const out = buildConfig(await import(importPath));
23
- return createMdxLoader(resolvedConfig(out));
26
+ const core = await createCore({
27
+ environment,
28
+ outDir,
29
+ configPath
30
+ }).init({
31
+ config: buildConfig(await import(importPath))
32
+ });
33
+ return createMdxLoader(core.creatConfigLoader());
24
34
  }
25
35
  return {
26
36
  name: "bun-plugin-fumadocs-mdx",
@@ -38,28 +38,8 @@ function ident(code, tab = 1) {
38
38
  return code.split("\n").map((v) => " ".repeat(tab) + v).join("\n");
39
39
  }
40
40
 
41
- // src/utils/collections.ts
42
- function getSupportedFormats(collection) {
43
- return {
44
- doc: ["mdx", "md"],
45
- meta: ["json", "yaml"]
46
- }[collection.type];
47
- }
48
- function getGlobPatterns(collection) {
49
- if (collection.files) return collection.files;
50
- return [`**/*.{${getSupportedFormats(collection).join(",")}}`];
51
- }
52
- function isFileSupported(filePath, collection) {
53
- for (const format of getSupportedFormats(collection)) {
54
- if (filePath.endsWith(`.${format}`)) return true;
55
- }
56
- return false;
57
- }
58
-
59
41
  export {
60
42
  getImportCode,
61
43
  toImportPath,
62
- ident,
63
- getGlobPatterns,
64
- isFileSupported
44
+ ident
65
45
  };
@@ -0,0 +1,207 @@
1
+ // src/loaders/config/index.ts
2
+ import path from "path";
3
+ import fs from "fs/promises";
4
+ function findConfigFile() {
5
+ return path.resolve("source.config.ts");
6
+ }
7
+ function staticConfig({
8
+ core,
9
+ buildConfig
10
+ }) {
11
+ let cached;
12
+ async function newConfig() {
13
+ const { loadConfig } = await import("./load-MNG3CLET.js");
14
+ await core.init({
15
+ config: loadConfig(
16
+ core._options.configPath,
17
+ core._options.outDir,
18
+ buildConfig
19
+ )
20
+ });
21
+ return core.getConfig();
22
+ }
23
+ return {
24
+ async getConfig() {
25
+ return cached ??= newConfig();
26
+ }
27
+ };
28
+ }
29
+ function dynamicConfig({
30
+ core,
31
+ buildConfig
32
+ }) {
33
+ let loaded;
34
+ async function getConfigHash() {
35
+ const stats = await fs.stat(core._options.configPath).catch(() => void 0);
36
+ if (stats) {
37
+ return stats.mtime.getTime().toString();
38
+ }
39
+ throw new Error("Cannot find config file");
40
+ }
41
+ async function newConfig() {
42
+ const { loadConfig } = await import("./load-MNG3CLET.js");
43
+ await core.init({
44
+ config: loadConfig(
45
+ core._options.configPath,
46
+ core._options.outDir,
47
+ buildConfig
48
+ )
49
+ });
50
+ return core.getConfig();
51
+ }
52
+ return {
53
+ async getConfig() {
54
+ const hash = await getConfigHash();
55
+ if (loaded && loaded.hash === hash) return loaded.config;
56
+ loaded = {
57
+ hash,
58
+ config: newConfig()
59
+ };
60
+ return loaded.config;
61
+ }
62
+ };
63
+ }
64
+
65
+ // src/utils/validation.ts
66
+ import picocolors from "picocolors";
67
+ var ValidationError = class extends Error {
68
+ constructor(message, issues) {
69
+ super(
70
+ `${message}:
71
+ ${issues.map((issue) => ` ${issue.path}: ${issue.message}`).join("\n")}`
72
+ );
73
+ this.title = message;
74
+ this.issues = issues;
75
+ }
76
+ toStringFormatted() {
77
+ return [
78
+ picocolors.bold(`[MDX] ${this.title}:`),
79
+ ...this.issues.map(
80
+ (issue) => picocolors.redBright(
81
+ `- ${picocolors.bold(issue.path?.join(".") ?? "*")}: ${issue.message}`
82
+ )
83
+ )
84
+ ].join("\n");
85
+ }
86
+ };
87
+ async function validate(schema, data, context, errorMessage) {
88
+ if (typeof schema === "function" && !("~standard" in schema)) {
89
+ schema = schema(context);
90
+ }
91
+ if ("~standard" in schema) {
92
+ const result = await schema["~standard"].validate(
93
+ data
94
+ );
95
+ if (result.issues) {
96
+ throw new ValidationError(errorMessage, result.issues);
97
+ }
98
+ return result.value;
99
+ }
100
+ return data;
101
+ }
102
+
103
+ // src/core.ts
104
+ import path2 from "path";
105
+ import fs2 from "fs/promises";
106
+ function createCore(options, defaultPlugins = []) {
107
+ let config;
108
+ let plugins;
109
+ return {
110
+ _options: options,
111
+ getPluginContext() {
112
+ return {
113
+ core: this,
114
+ ...options
115
+ };
116
+ },
117
+ /**
118
+ * Convenient cache store, reset when config changes
119
+ */
120
+ cache: /* @__PURE__ */ new Map(),
121
+ async init({ config: newConfig }) {
122
+ config = await newConfig;
123
+ this.cache.clear();
124
+ plugins = [];
125
+ for await (const option of [
126
+ ...defaultPlugins,
127
+ ...config.global.plugins ?? []
128
+ ]) {
129
+ if (!option) continue;
130
+ if (Array.isArray(option)) plugins.push(...option);
131
+ else plugins.push(option);
132
+ }
133
+ for (const plugin of plugins) {
134
+ const out = await plugin.config?.call(this.getPluginContext(), config);
135
+ if (out) config = out;
136
+ }
137
+ return this;
138
+ },
139
+ getConfig() {
140
+ return config;
141
+ },
142
+ creatConfigLoader() {
143
+ return {
144
+ getConfig() {
145
+ return config;
146
+ }
147
+ };
148
+ },
149
+ async initServer(server) {
150
+ for (const plugin of plugins) {
151
+ await plugin.configureServer?.call(this.getPluginContext(), server);
152
+ }
153
+ },
154
+ async emitAndWrite({
155
+ filterPlugin = () => true
156
+ } = {}) {
157
+ const start = performance.now();
158
+ const out = await Promise.all(
159
+ plugins.map((plugin) => {
160
+ if (!filterPlugin(plugin) || !plugin.emit) return [];
161
+ return plugin.emit.call(this.getPluginContext());
162
+ })
163
+ );
164
+ await Promise.all(
165
+ out.flat().map(async (entry) => {
166
+ const file = path2.join(options.outDir, entry.path);
167
+ await fs2.mkdir(path2.dirname(file), { recursive: true });
168
+ await fs2.writeFile(file, entry.content);
169
+ })
170
+ );
171
+ console.log(`[MDX] generated files in ${performance.now() - start}ms`);
172
+ }
173
+ };
174
+ }
175
+
176
+ // src/utils/git-timestamp.ts
177
+ import path3 from "path";
178
+ import { x } from "tinyexec";
179
+ var cache = /* @__PURE__ */ new Map();
180
+ async function getGitTimestamp(file) {
181
+ const cached = cache.get(file);
182
+ if (cached) return cached;
183
+ try {
184
+ const out = await x(
185
+ "git",
186
+ ["log", "-1", '--pretty="%ai"', path3.relative(process.cwd(), file)],
187
+ {
188
+ throwOnError: true
189
+ }
190
+ );
191
+ const time = new Date(out.stdout);
192
+ cache.set(file, time);
193
+ return time;
194
+ } catch {
195
+ return;
196
+ }
197
+ }
198
+
199
+ export {
200
+ findConfigFile,
201
+ staticConfig,
202
+ dynamicConfig,
203
+ ValidationError,
204
+ validate,
205
+ getGitTimestamp,
206
+ createCore
207
+ };
@@ -4,7 +4,7 @@ import {
4
4
  import {
5
5
  getGitTimestamp,
6
6
  validate
7
- } from "./chunk-HI62EXSB.js";
7
+ } from "./chunk-EELYB2XC.js";
8
8
  import {
9
9
  fumaMatter
10
10
  } from "./chunk-VWJKRQZR.js";
@@ -23,14 +23,6 @@ var cacheEntry = z.object({
23
23
  map: z.any().optional(),
24
24
  hash: z.string().optional()
25
25
  });
26
- var hashes = /* @__PURE__ */ new WeakMap();
27
- function getConfigHash(config) {
28
- let hash = hashes.get(config);
29
- if (hash) return hash;
30
- hash = Date.now().toString();
31
- hashes.set(config, hash);
32
- return hash;
33
- }
34
26
  function createMdxLoader(configLoader) {
35
27
  return async ({
36
28
  source: value,
@@ -41,14 +33,25 @@ function createMdxLoader(configLoader) {
41
33
  }) => {
42
34
  const matter = fumaMatter(value);
43
35
  const parsed = querySchema.parse(query);
44
- const loaded = await configLoader.getConfig();
45
- const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
46
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
47
- if (cacheDir) {
36
+ const config = await configLoader.getConfig();
37
+ let after;
38
+ if (!isDevelopment && config.global.experimentalBuildCache) {
39
+ const cacheDir = config.global.experimentalBuildCache;
40
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
48
41
  const cached = await fs.readFile(path.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
49
42
  if (cached && cached.hash === generateCacheHash(value)) return cached;
43
+ after = async () => {
44
+ await fs.mkdir(cacheDir, { recursive: true });
45
+ await fs.writeFile(
46
+ path.join(cacheDir, cacheKey),
47
+ JSON.stringify({
48
+ ...out,
49
+ hash: generateCacheHash(value)
50
+ })
51
+ );
52
+ };
50
53
  }
51
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
54
+ const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
52
55
  let docCollection;
53
56
  switch (collection?.type) {
54
57
  case "doc":
@@ -76,16 +79,16 @@ function createMdxLoader(configLoader) {
76
79
  };
77
80
  }
78
81
  const data = {};
79
- if (loaded.global.lastModifiedTime === "git") {
82
+ if (config.global.lastModifiedTime === "git") {
80
83
  data.lastModified = (await getGitTimestamp(filePath))?.getTime();
81
84
  }
82
85
  const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
83
86
  const compiled = await buildMDX(
84
- `${getConfigHash(loaded)}:${parsed.collection ?? "global"}`,
87
+ `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
85
88
  "\n".repeat(lineOffset) + matter.content,
86
89
  {
87
90
  development: isDevelopment,
88
- ...docCollection?.mdxOptions ?? await loaded.getDefaultMDXOptions(),
91
+ ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
89
92
  postprocess: docCollection?.postprocess,
90
93
  data,
91
94
  filePath,
@@ -97,19 +100,18 @@ function createMdxLoader(configLoader) {
97
100
  code: String(compiled.value),
98
101
  map: compiled.map
99
102
  };
100
- if (cacheDir) {
101
- await fs.mkdir(cacheDir, { recursive: true });
102
- await fs.writeFile(
103
- path.join(cacheDir, cacheKey),
104
- JSON.stringify({
105
- ...out,
106
- hash: generateCacheHash(value)
107
- })
108
- );
109
- }
103
+ await after?.();
110
104
  return out;
111
105
  };
112
106
  }
107
+ var hashes = /* @__PURE__ */ new WeakMap();
108
+ function getConfigHash(config) {
109
+ let hash = hashes.get(config);
110
+ if (hash) return hash;
111
+ hash = Date.now().toString();
112
+ hashes.set(config, hash);
113
+ return hash;
114
+ }
113
115
  function generateCacheHash(input) {
114
116
  return createHash("md5").update(input).digest("hex");
115
117
  }