fumadocs-mdx 12.0.3 → 13.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/dist/bin.cjs +1116 -359
  2. package/dist/bin.js +4 -4
  3. package/dist/build-mdx-CCNr86q6.d.ts +53 -0
  4. package/dist/build-mdx-D-r3_eQL.d.cts +53 -0
  5. package/dist/bun/index.cjs +196 -52
  6. package/dist/bun/index.d.cts +8 -3
  7. package/dist/bun/index.d.ts +8 -3
  8. package/dist/bun/index.js +19 -10
  9. package/dist/{chunk-QAUWMR5D.js → chunk-3J3WL7WN.js} +23 -5
  10. package/dist/{chunk-6Y5JDZHD.js → chunk-CXA4JO4Z.js} +1 -21
  11. package/dist/chunk-EELYB2XC.js +207 -0
  12. package/dist/{chunk-46UPKP5R.js → chunk-II3H5ZVZ.js} +5 -5
  13. package/dist/{chunk-LGYVNESJ.js → chunk-JVZFH6ND.js} +6 -22
  14. package/dist/{chunk-LMG6UWCL.js → chunk-K5ZLPEIQ.js} +56 -16
  15. package/dist/{chunk-OMAMTKDE.js → chunk-KILFIBVW.js} +3 -12
  16. package/dist/chunk-NVRDCY6Z.js +30 -0
  17. package/dist/{chunk-RMDXSZYE.js → chunk-XQ5O7IPO.js} +31 -24
  18. package/dist/chunk-XZY2AWJI.js +81 -0
  19. package/dist/{chunk-VXEBLM4X.js → chunk-YVCR6FUH.js} +1 -1
  20. package/dist/config/index.cjs +56 -16
  21. package/dist/config/index.d.cts +2 -1
  22. package/dist/config/index.d.ts +2 -1
  23. package/dist/config/index.js +1 -1
  24. package/dist/{define-DJbJduHy.d.ts → core-B6j6Fxse.d.cts} +89 -2
  25. package/dist/{define-DJbJduHy.d.cts → core-B6j6Fxse.d.ts} +89 -2
  26. package/dist/index.cjs +0 -109
  27. package/dist/index.d.cts +75 -9
  28. package/dist/index.d.ts +75 -9
  29. package/dist/index.js +0 -11
  30. package/dist/{load-UUXLUBHL.js → load-MNG3CLET.js} +1 -3
  31. package/dist/next/index.cjs +298 -234
  32. package/dist/next/index.d.cts +2 -11
  33. package/dist/next/index.d.ts +2 -11
  34. package/dist/next/index.js +177 -141
  35. package/dist/node/loader.cjs +228 -85
  36. package/dist/node/loader.js +19 -9
  37. package/dist/plugins/json-schema.cjs +162 -0
  38. package/dist/plugins/json-schema.d.cts +24 -0
  39. package/dist/plugins/json-schema.d.ts +24 -0
  40. package/dist/plugins/json-schema.js +78 -0
  41. package/dist/runtime/next/async.cjs +108 -70
  42. package/dist/runtime/next/async.d.cts +9 -6
  43. package/dist/runtime/next/async.d.ts +9 -6
  44. package/dist/runtime/next/async.js +8 -18
  45. package/dist/runtime/next/index.cjs +25 -14
  46. package/dist/runtime/next/index.d.cts +11 -8
  47. package/dist/runtime/next/index.d.ts +11 -8
  48. package/dist/runtime/next/index.js +2 -2
  49. package/dist/runtime/vite/browser.cjs +7 -3
  50. package/dist/runtime/vite/browser.d.cts +56 -7
  51. package/dist/runtime/vite/browser.d.ts +56 -7
  52. package/dist/runtime/vite/browser.js +2 -1
  53. package/dist/runtime/vite/server.cjs +40 -34
  54. package/dist/runtime/vite/server.d.cts +13 -10
  55. package/dist/runtime/vite/server.d.ts +13 -10
  56. package/dist/runtime/vite/server.js +8 -23
  57. package/dist/{types-TeHjsmja.d.ts → types-AGzTfBmf.d.ts} +3 -10
  58. package/dist/{types-BRx1QsIJ.d.cts → types-DKGMoay5.d.cts} +3 -10
  59. package/dist/vite/index.cjs +443 -249
  60. package/dist/vite/index.d.cts +23 -10
  61. package/dist/vite/index.d.ts +23 -10
  62. package/dist/vite/index.js +213 -36
  63. package/dist/{loader-mdx.cjs → webpack/index.cjs} +268 -82
  64. package/dist/{loader-mdx.d.ts → webpack/index.d.cts} +1 -0
  65. package/dist/{loader-mdx.d.cts → webpack/index.d.ts} +1 -0
  66. package/dist/webpack/index.js +44 -0
  67. package/loader-mdx.cjs +1 -1
  68. package/package.json +30 -16
  69. package/dist/browser-BupUnhpC.d.ts +0 -98
  70. package/dist/browser-R0x9IPaQ.d.cts +0 -98
  71. package/dist/chunk-ADR6R7HM.js +0 -29
  72. package/dist/chunk-IQAEAI4P.js +0 -66
  73. package/dist/chunk-XMFLD5J6.js +0 -30
  74. package/dist/chunk-ZLCSVXCD.js +0 -10
  75. package/dist/chunk-ZX7TM4AR.js +0 -127
  76. package/dist/loader-mdx.js +0 -25
  77. package/dist/postinstall-SCSXM4IM.js +0 -10
  78. package/dist/shared-CfiiRctw.d.ts +0 -70
  79. package/dist/shared-fFqiuWJC.d.cts +0 -70
  80. package/dist/watcher-HGOH3APP.js +0 -22
@@ -175,7 +175,6 @@ var init_build = __esm({
175
175
  // src/loaders/config/load.ts
176
176
  var load_exports = {};
177
177
  __export(load_exports, {
178
- getConfigHash: () => getConfigHash,
179
178
  loadConfig: () => loadConfig
180
179
  });
181
180
  async function compileConfig(configPath, outDir) {
@@ -198,46 +197,31 @@ async function compileConfig(configPath, outDir) {
198
197
  throw new Error("failed to compile configuration file");
199
198
  }
200
199
  }
201
- async function loadConfig(configPath, outDir, hash, build = false) {
202
- if (cache3 && cache3.hash === hash) {
203
- return await cache3.config;
204
- }
200
+ async function loadConfig(configPath, outDir, build = false) {
205
201
  if (build) await compileConfig(configPath, outDir);
206
202
  const url = (0, import_node_url.pathToFileURL)(path4.resolve(outDir, "source.config.mjs"));
207
- const config = import(`${url.href}?hash=${hash}`).then((loaded) => {
208
- return buildConfig(
209
- // every call to `loadConfig` will cause the previous cache to be ignored
210
- loaded
211
- );
212
- });
213
- if (hash) cache3 = { config, hash };
203
+ url.searchParams.set("hash", Date.now().toString());
204
+ const config = import(url.href).then(
205
+ (loaded) => buildConfig(loaded)
206
+ );
214
207
  return await config;
215
208
  }
216
- async function getConfigHash(configPath) {
217
- const stats = await fs3.stat(configPath).catch(() => void 0);
218
- if (stats) {
219
- return stats.mtime.getTime().toString();
220
- }
221
- throw new Error("Cannot find config file");
222
- }
223
- var fs3, path4, import_node_url, cache3;
209
+ var path4, import_node_url;
224
210
  var init_load = __esm({
225
211
  "src/loaders/config/load.ts"() {
226
212
  "use strict";
227
- fs3 = __toESM(require("fs/promises"), 1);
228
213
  path4 = __toESM(require("path"), 1);
229
214
  import_node_url = require("url");
230
215
  init_build();
231
- cache3 = null;
232
216
  }
233
217
  });
234
218
 
235
- // src/loader-mdx.ts
236
- var loader_mdx_exports = {};
237
- __export(loader_mdx_exports, {
219
+ // src/webpack/index.ts
220
+ var webpack_exports = {};
221
+ __export(webpack_exports, {
238
222
  default: () => loader
239
223
  });
240
- module.exports = __toCommonJS(loader_mdx_exports);
224
+ module.exports = __toCommonJS(webpack_exports);
241
225
 
242
226
  // src/utils/fuma-matter.ts
243
227
  var import_js_yaml = require("js-yaml");
@@ -321,12 +305,56 @@ var import_mdx = require("@mdx-js/mdx");
321
305
 
322
306
  // src/loaders/mdx/remark-include.ts
323
307
  var import_unified = require("unified");
324
- var import_unist_util_visit = require("unist-util-visit");
308
+ var import_unist_util_visit2 = require("unist-util-visit");
325
309
  var path2 = __toESM(require("path"), 1);
326
310
  var fs = __toESM(require("fs/promises"), 1);
327
- var import_remark_parse = __toESM(require("remark-parse"), 1);
328
- var import_remark_mdx = __toESM(require("remark-mdx"), 1);
329
311
  var import_mdx_plugins = require("fumadocs-core/mdx-plugins");
312
+
313
+ // src/loaders/mdx/remark-unravel.ts
314
+ var import_unist_util_visit = require("unist-util-visit");
315
+ function remarkMarkAndUnravel() {
316
+ return (tree) => {
317
+ (0, import_unist_util_visit.visit)(tree, function(node, index, parent) {
318
+ let offset = -1;
319
+ let all = true;
320
+ let oneOrMore = false;
321
+ if (parent && typeof index === "number" && node.type === "paragraph") {
322
+ const children = node.children;
323
+ while (++offset < children.length) {
324
+ const child = children[offset];
325
+ if (child.type === "mdxJsxTextElement" || child.type === "mdxTextExpression") {
326
+ oneOrMore = true;
327
+ } else if (child.type === "text" && child.value.trim().length === 0) {
328
+ } else {
329
+ all = false;
330
+ break;
331
+ }
332
+ }
333
+ if (all && oneOrMore) {
334
+ offset = -1;
335
+ const newChildren = [];
336
+ while (++offset < children.length) {
337
+ const child = children[offset];
338
+ if (child.type === "mdxJsxTextElement") {
339
+ child.type = "mdxJsxFlowElement";
340
+ }
341
+ if (child.type === "mdxTextExpression") {
342
+ child.type = "mdxFlowExpression";
343
+ }
344
+ if (child.type === "text" && /^[\t\r\n ]+$/.test(String(child.value))) {
345
+ } else {
346
+ newChildren.push(child);
347
+ }
348
+ }
349
+ parent.children.splice(index, 1, ...newChildren);
350
+ return index;
351
+ }
352
+ }
353
+ });
354
+ };
355
+ }
356
+
357
+ // src/loaders/mdx/remark-include.ts
330
358
  var ElementLikeTypes = [
331
359
  "mdxJsxFlowElement",
332
360
  "mdxJsxTextElement",
@@ -366,7 +394,7 @@ function parseSpecifier(specifier) {
366
394
  function extractSection(root, section) {
367
395
  let nodes;
368
396
  let capturingHeadingContent = false;
369
- (0, import_unist_util_visit.visit)(root, (node) => {
397
+ (0, import_unist_util_visit2.visit)(root, (node) => {
370
398
  if (node.type === "heading") {
371
399
  if (capturingHeadingContent) {
372
400
  return false;
@@ -398,7 +426,7 @@ function extractSection(root, section) {
398
426
  }
399
427
  function remarkInclude() {
400
428
  const TagName = "include";
401
- async function embedContent(file, heading, params, data) {
429
+ const embedContent = async (file, heading, params, data) => {
402
430
  let content;
403
431
  try {
404
432
  content = (await fs.readFile(file)).toString();
@@ -421,18 +449,17 @@ ${e instanceof Error ? e.message : String(e)}`,
421
449
  data: {}
422
450
  };
423
451
  }
424
- const parser = (data._getProcessor ?? getDefaultProcessor)(
425
- ext === ".mdx" ? "mdx" : "md"
426
- );
452
+ const parser = data._getProcessor ? data._getProcessor(ext === ".mdx" ? "mdx" : "md") : this;
427
453
  const parsed = fumaMatter(content);
428
454
  let mdast = parser.parse({
429
455
  path: file,
430
456
  value: parsed.content,
431
457
  data: { frontmatter: parsed.data }
432
458
  });
459
+ const baseProcessor = (0, import_unified.unified)().use(remarkMarkAndUnravel);
433
460
  if (heading) {
434
461
  const extracted = extractSection(
435
- await (0, import_unified.unified)().use(import_mdx_plugins.remarkHeading).run(mdast),
462
+ await baseProcessor.use(import_mdx_plugins.remarkHeading).run(mdast),
436
463
  heading
437
464
  );
438
465
  if (!extracted)
@@ -440,13 +467,15 @@ ${e instanceof Error ? e.message : String(e)}`,
440
467
  `Cannot find section ${heading} in ${file}, make sure you have encapsulated the section in a <section id="${heading}"> tag, or a :::section directive with remark-directive configured.`
441
468
  );
442
469
  mdast = extracted;
470
+ } else {
471
+ mdast = await baseProcessor.run(mdast);
443
472
  }
444
473
  await update(mdast, path2.dirname(file), data);
445
474
  return mdast;
446
- }
475
+ };
447
476
  async function update(tree, directory, data) {
448
477
  const queue = [];
449
- (0, import_unist_util_visit.visit)(tree, ElementLikeTypes, (_node, _, parent) => {
478
+ (0, import_unist_util_visit2.visit)(tree, ElementLikeTypes, (_node, _, parent) => {
450
479
  const node = _node;
451
480
  if (node.name !== TagName) return;
452
481
  const specifier = flattenNode(node);
@@ -473,24 +502,28 @@ ${e instanceof Error ? e.message : String(e)}`,
473
502
  await update(tree, path2.dirname(file.path), file.data);
474
503
  };
475
504
  }
476
- function getDefaultProcessor(format) {
477
- const mdProcessor = (0, import_unified.unified)().use(import_remark_parse.default);
478
- if (format === "md") return mdProcessor;
479
- return mdProcessor.use(import_remark_mdx.default);
480
- }
481
505
 
482
506
  // src/loaders/mdx/remark-postprocess.ts
483
- var import_unist_util_visit2 = require("unist-util-visit");
507
+ var import_unist_util_visit3 = require("unist-util-visit");
484
508
  var import_mdast_util_to_markdown = require("mdast-util-to-markdown");
485
509
  var import_estree_util_value_to_estree = require("estree-util-value-to-estree");
510
+ var import_unist_util_remove_position = require("unist-util-remove-position");
511
+ var import_remark_mdx = __toESM(require("remark-mdx"), 1);
486
512
  function remarkPostprocess({
513
+ _format,
487
514
  includeProcessedMarkdown = false,
515
+ includeMDAST = false,
488
516
  valueToExport = []
489
- } = {}) {
517
+ }) {
518
+ let _stringifyProcessor;
519
+ const getStringifyProcessor = () => {
520
+ if (_format === "mdx") return this;
521
+ return _stringifyProcessor ??= this().use(import_remark_mdx.default).freeze();
522
+ };
490
523
  return (tree, file) => {
491
524
  let title;
492
525
  const urls = [];
493
- (0, import_unist_util_visit2.visit)(tree, ["heading", "link"], (node) => {
526
+ (0, import_unist_util_visit3.visit)(tree, ["heading", "link"], (node) => {
494
527
  if (node.type === "heading" && node.depth === 1) {
495
528
  title = flattenNode2(node);
496
529
  }
@@ -506,12 +539,19 @@ function remarkPostprocess({
506
539
  }
507
540
  file.data.extractedReferences = urls;
508
541
  if (includeProcessedMarkdown) {
542
+ const processor = getStringifyProcessor();
509
543
  file.data._markdown = (0, import_mdast_util_to_markdown.toMarkdown)(tree, {
510
- ...this.data("settings"),
544
+ ...processor.data("settings"),
511
545
  // from https://github.com/remarkjs/remark/blob/main/packages/remark-stringify/lib/index.js
512
- extensions: this.data("toMarkdownExtensions") || []
546
+ extensions: processor.data("toMarkdownExtensions") || []
513
547
  });
514
548
  }
549
+ if (includeMDAST) {
550
+ const options = includeMDAST === true ? {} : includeMDAST;
551
+ file.data._mdast = JSON.stringify(
552
+ options.removePosition ? (0, import_unist_util_remove_position.removePosition)(structuredClone(tree)) : tree
553
+ );
554
+ }
515
555
  for (const { name, value } of file.data["mdx-export"] ?? []) {
516
556
  tree.children.unshift(getMdastExport(name, value));
517
557
  }
@@ -579,6 +619,7 @@ async function buildMDX(cacheKey, source, options) {
579
619
  [
580
620
  remarkPostprocess,
581
621
  {
622
+ _format: format,
582
623
  ...options.postprocess,
583
624
  valueToExport: [
584
625
  ...options.postprocess?.valueToExport ?? [],
@@ -586,7 +627,8 @@ async function buildMDX(cacheKey, source, options) {
586
627
  "extractedReferences",
587
628
  "frontmatter",
588
629
  "lastModified",
589
- "_markdown"
630
+ "_markdown",
631
+ "_mdast"
590
632
  ]
591
633
  }
592
634
  ]
@@ -618,10 +660,7 @@ var import_node_path2 = __toESM(require("path"), 1);
618
660
  var import_node_crypto = require("crypto");
619
661
  var querySchema = import_zod.z.object({
620
662
  only: import_zod.z.literal(["frontmatter", "all"]).default("all"),
621
- collection: import_zod.z.string().optional(),
622
- hash: import_zod.z.string().describe(
623
- "the hash of config, used for revalidation on Turbopack/Webpack."
624
- ).optional()
663
+ collection: import_zod.z.string().optional()
625
664
  }).loose();
626
665
  var cacheEntry = import_zod.z.object({
627
666
  code: import_zod.z.string(),
@@ -638,14 +677,25 @@ function createMdxLoader(configLoader) {
638
677
  }) => {
639
678
  const matter = fumaMatter(value);
640
679
  const parsed = querySchema.parse(query);
641
- const loaded = await configLoader.getConfig(parsed.hash);
642
- const cacheDir = isDevelopment ? void 0 : loaded.global.experimentalBuildCache;
643
- const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
644
- if (cacheDir) {
680
+ const config = await configLoader.getConfig();
681
+ let after;
682
+ if (!isDevelopment && config.global.experimentalBuildCache) {
683
+ const cacheDir = config.global.experimentalBuildCache;
684
+ const cacheKey = `${parsed.hash}_${parsed.collection ?? "global"}_${generateCacheHash(filePath)}`;
645
685
  const cached = await import_promises.default.readFile(import_node_path2.default.join(cacheDir, cacheKey)).then((content) => cacheEntry.parse(JSON.parse(content.toString()))).catch(() => null);
646
686
  if (cached && cached.hash === generateCacheHash(value)) return cached;
687
+ after = async () => {
688
+ await import_promises.default.mkdir(cacheDir, { recursive: true });
689
+ await import_promises.default.writeFile(
690
+ import_node_path2.default.join(cacheDir, cacheKey),
691
+ JSON.stringify({
692
+ ...out,
693
+ hash: generateCacheHash(value)
694
+ })
695
+ );
696
+ };
647
697
  }
648
- const collection = parsed.collection ? loaded.collections.get(parsed.collection) : void 0;
698
+ const collection = parsed.collection ? config.collections.get(parsed.collection) : void 0;
649
699
  let docCollection;
650
700
  switch (collection?.type) {
651
701
  case "doc":
@@ -673,16 +723,16 @@ function createMdxLoader(configLoader) {
673
723
  };
674
724
  }
675
725
  const data = {};
676
- if (loaded.global.lastModifiedTime === "git") {
726
+ if (config.global.lastModifiedTime === "git") {
677
727
  data.lastModified = (await getGitTimestamp(filePath))?.getTime();
678
728
  }
679
729
  const lineOffset = isDevelopment ? countLines(matter.matter) : 0;
680
730
  const compiled = await buildMDX(
681
- `${parsed.hash ?? ""}:${parsed.collection ?? "global"}`,
731
+ `${getConfigHash(config)}:${parsed.collection ?? "global"}`,
682
732
  "\n".repeat(lineOffset) + matter.content,
683
733
  {
684
734
  development: isDevelopment,
685
- ...docCollection?.mdxOptions ?? await loaded.getDefaultMDXOptions(),
735
+ ...docCollection?.mdxOptions ?? await config.getDefaultMDXOptions(),
686
736
  postprocess: docCollection?.postprocess,
687
737
  data,
688
738
  filePath,
@@ -694,19 +744,18 @@ function createMdxLoader(configLoader) {
694
744
  code: String(compiled.value),
695
745
  map: compiled.map
696
746
  };
697
- if (cacheDir) {
698
- await import_promises.default.mkdir(cacheDir, { recursive: true });
699
- await import_promises.default.writeFile(
700
- import_node_path2.default.join(cacheDir, cacheKey),
701
- JSON.stringify({
702
- ...out,
703
- hash: generateCacheHash(value)
704
- })
705
- );
706
- }
747
+ await after?.();
707
748
  return out;
708
749
  };
709
750
  }
751
+ var hashes = /* @__PURE__ */ new WeakMap();
752
+ function getConfigHash(config) {
753
+ let hash = hashes.get(config);
754
+ if (hash) return hash;
755
+ hash = Date.now().toString();
756
+ hashes.set(config, hash);
757
+ return hash;
758
+ }
710
759
  function generateCacheHash(input) {
711
760
  return (0, import_node_crypto.createHash)("md5").update(input).digest("hex");
712
761
  }
@@ -720,22 +769,68 @@ function countLines(s) {
720
769
 
721
770
  // src/loaders/config/index.ts
722
771
  var import_node_path3 = __toESM(require("path"), 1);
723
- function dynamicConfig(configPath, outDir) {
772
+ var import_promises2 = __toESM(require("fs/promises"), 1);
773
+ function staticConfig({
774
+ core,
775
+ buildConfig: buildConfig2
776
+ }) {
777
+ let cached;
778
+ async function newConfig() {
779
+ const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load(), load_exports));
780
+ await core.init({
781
+ config: loadConfig2(
782
+ core._options.configPath,
783
+ core._options.outDir,
784
+ buildConfig2
785
+ )
786
+ });
787
+ return core.getConfig();
788
+ }
724
789
  return {
725
- async getConfig(hash) {
726
- const { loadConfig: loadConfig2, getConfigHash: getConfigHash2 } = await Promise.resolve().then(() => (init_load(), load_exports));
727
- return loadConfig2(
728
- configPath,
729
- outDir,
730
- hash ?? await getConfigHash2(configPath)
731
- );
790
+ async getConfig() {
791
+ return cached ??= newConfig();
792
+ }
793
+ };
794
+ }
795
+ function dynamicConfig({
796
+ core,
797
+ buildConfig: buildConfig2
798
+ }) {
799
+ let loaded;
800
+ async function getConfigHash2() {
801
+ const stats = await import_promises2.default.stat(core._options.configPath).catch(() => void 0);
802
+ if (stats) {
803
+ return stats.mtime.getTime().toString();
804
+ }
805
+ throw new Error("Cannot find config file");
806
+ }
807
+ async function newConfig() {
808
+ const { loadConfig: loadConfig2 } = await Promise.resolve().then(() => (init_load(), load_exports));
809
+ await core.init({
810
+ config: loadConfig2(
811
+ core._options.configPath,
812
+ core._options.outDir,
813
+ buildConfig2
814
+ )
815
+ });
816
+ return core.getConfig();
817
+ }
818
+ return {
819
+ async getConfig() {
820
+ const hash = await getConfigHash2();
821
+ if (loaded && loaded.hash === hash) return loaded.config;
822
+ loaded = {
823
+ hash,
824
+ config: newConfig()
825
+ };
826
+ return loaded.config;
732
827
  }
733
828
  };
734
829
  }
735
830
 
736
831
  // src/loaders/adapter.ts
737
832
  var import_node_url2 = require("url");
738
- var import_promises2 = __toESM(require("fs/promises"), 1);
833
+ var import_promises3 = __toESM(require("fs/promises"), 1);
739
834
  var import_node_querystring = require("querystring");
740
835
  var import_node_path4 = __toESM(require("path"), 1);
741
836
  function toWebpack(loader2) {
@@ -761,11 +856,102 @@ function toWebpack(loader2) {
761
856
  };
762
857
  }
763
858
 
764
- // src/loader-mdx.ts
859
+ // src/core.ts
860
+ var import_node_path5 = __toESM(require("path"), 1);
861
+ var import_promises4 = __toESM(require("fs/promises"), 1);
862
+ function createCore(options, defaultPlugins = []) {
863
+ let config;
864
+ let plugins2;
865
+ return {
866
+ _options: options,
867
+ getPluginContext() {
868
+ return {
869
+ core: this,
870
+ ...options
871
+ };
872
+ },
873
+ /**
874
+ * Convenient cache store, reset when config changes
875
+ */
876
+ cache: /* @__PURE__ */ new Map(),
877
+ async init({ config: newConfig }) {
878
+ config = await newConfig;
879
+ this.cache.clear();
880
+ plugins2 = [];
881
+ for await (const option of [
882
+ ...defaultPlugins,
883
+ ...config.global.plugins ?? []
884
+ ]) {
885
+ if (!option) continue;
886
+ if (Array.isArray(option)) plugins2.push(...option);
887
+ else plugins2.push(option);
888
+ }
889
+ for (const plugin of plugins2) {
890
+ const out = await plugin.config?.call(this.getPluginContext(), config);
891
+ if (out) config = out;
892
+ }
893
+ return this;
894
+ },
895
+ getConfig() {
896
+ return config;
897
+ },
898
+ creatConfigLoader() {
899
+ return {
900
+ getConfig() {
901
+ return config;
902
+ }
903
+ };
904
+ },
905
+ async initServer(server) {
906
+ for (const plugin of plugins2) {
907
+ await plugin.configureServer?.call(this.getPluginContext(), server);
908
+ }
909
+ },
910
+ async emitAndWrite({
911
+ filterPlugin = () => true
912
+ } = {}) {
913
+ const start = performance.now();
914
+ const out = await Promise.all(
915
+ plugins2.map((plugin) => {
916
+ if (!filterPlugin(plugin) || !plugin.emit) return [];
917
+ return plugin.emit.call(this.getPluginContext());
918
+ })
919
+ );
920
+ await Promise.all(
921
+ out.flat().map(async (entry) => {
922
+ const file = import_node_path5.default.join(options.outDir, entry.path);
923
+ await import_promises4.default.mkdir(import_node_path5.default.dirname(file), { recursive: true });
924
+ await import_promises4.default.writeFile(file, entry.content);
925
+ })
926
+ );
927
+ console.log(`[MDX] generated files in ${performance.now() - start}ms`);
928
+ }
929
+ };
930
+ }
931
+
932
+ // src/webpack/index.ts
765
933
  var instance;
766
934
  async function loader(source, callback) {
935
+ const { isDev, outDir, configPath } = this.getOptions();
767
936
  this.cacheable(true);
768
- const { configPath, outDir } = this.getOptions();
769
- instance ??= toWebpack(createMdxLoader(dynamicConfig(configPath, outDir)));
937
+ this.addDependency(configPath);
938
+ if (!instance) {
939
+ const core = createCore({
940
+ environment: "webpack",
941
+ outDir,
942
+ configPath
943
+ });
944
+ instance = toWebpack(
945
+ createMdxLoader(
946
+ isDev ? dynamicConfig({
947
+ core,
948
+ buildConfig: false
949
+ }) : staticConfig({
950
+ core,
951
+ buildConfig: false
952
+ })
953
+ )
954
+ );
955
+ }
770
956
  await instance.call(this, source, callback);
771
957
  }
@@ -3,6 +3,7 @@ import { LoaderContext } from 'webpack';
3
3
  interface Options {
4
4
  configPath: string;
5
5
  outDir: string;
6
+ isDev: boolean;
6
7
  }
7
8
  /**
8
9
  * Load MDX/markdown files
@@ -3,6 +3,7 @@ import { LoaderContext } from 'webpack';
3
3
  interface Options {
4
4
  configPath: string;
5
5
  outDir: string;
6
+ isDev: boolean;
6
7
  }
7
8
  /**
8
9
  * Load MDX/markdown files
@@ -0,0 +1,44 @@
1
+ import {
2
+ toWebpack
3
+ } from "../chunk-YVCR6FUH.js";
4
+ import {
5
+ createMdxLoader
6
+ } from "../chunk-XQ5O7IPO.js";
7
+ import "../chunk-3J3WL7WN.js";
8
+ import "../chunk-K5ZLPEIQ.js";
9
+ import {
10
+ createCore,
11
+ dynamicConfig,
12
+ staticConfig
13
+ } from "../chunk-EELYB2XC.js";
14
+ import "../chunk-VWJKRQZR.js";
15
+
16
+ // src/webpack/index.ts
17
+ var instance;
18
+ async function loader(source, callback) {
19
+ const { isDev, outDir, configPath } = this.getOptions();
20
+ this.cacheable(true);
21
+ this.addDependency(configPath);
22
+ if (!instance) {
23
+ const core = createCore({
24
+ environment: "webpack",
25
+ outDir,
26
+ configPath
27
+ });
28
+ instance = toWebpack(
29
+ createMdxLoader(
30
+ isDev ? dynamicConfig({
31
+ core,
32
+ buildConfig: false
33
+ }) : staticConfig({
34
+ core,
35
+ buildConfig: false
36
+ })
37
+ )
38
+ );
39
+ }
40
+ await instance.call(this, source, callback);
41
+ }
42
+ export {
43
+ loader as default
44
+ };
package/loader-mdx.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  module.exports = function loader(code) {
2
2
  const callback = this.async();
3
3
 
4
- import('./dist/loader-mdx.js').then((mod) =>
4
+ import('./dist/webpack/index.js').then((mod) =>
5
5
  mod.default.call(this, code, callback),
6
6
  );
7
7
  };