fumadocs-mdx 13.0.8 → 14.0.0
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.
- package/dist/bin.cjs +924 -901
- package/dist/bin.js +3 -3
- package/dist/build-mdx-6UAK5FF5.js +8 -0
- package/dist/bun/index.cjs +578 -471
- package/dist/bun/index.d.cts +3 -2
- package/dist/bun/index.d.ts +3 -2
- package/dist/bun/index.js +12 -12
- package/dist/chunk-4JSFLXXT.js +8 -0
- package/dist/chunk-5UMZCWKV.js +17 -0
- package/dist/chunk-5YXP7JLN.js +138 -0
- package/dist/{chunk-2E2JCOSO.js → chunk-6NISOLQ6.js} +16 -44
- package/dist/chunk-7L2KNF6B.js +180 -0
- package/dist/chunk-E5DJTSIM.js +86 -0
- package/dist/{chunk-K5ZLPEIQ.js → chunk-FBLMK4RS.js} +9 -6
- package/dist/{chunk-QXHN25N3.js → chunk-OXSRIWQW.js} +7 -8
- package/dist/chunk-PKI7ZDA5.js +29 -0
- package/dist/{chunk-3J3WL7WN.js → chunk-SLY7WXTX.js} +71 -58
- package/dist/{chunk-5FTSWCB4.js → chunk-SRSRFOVI.js} +8 -10
- package/dist/chunk-TYJDYTKH.js +85 -0
- package/dist/chunk-XHJCLBZ4.js +406 -0
- package/dist/{chunk-2HXTGJBI.js → chunk-ZY6UZ7NH.js} +22 -19
- package/dist/config/index.cjs +79 -71
- package/dist/config/index.d.cts +2 -1
- package/dist/config/index.d.ts +2 -1
- package/dist/config/index.js +5 -5
- package/dist/index-BlVBvy-z.d.ts +8 -0
- package/dist/{core-DB7TdlyC.d.cts → index-D7JdSMpp.d.cts} +99 -61
- package/dist/{core-DB7TdlyC.d.ts → index-D7JdSMpp.d.ts} +99 -61
- package/dist/index-P2NNUkHn.d.cts +8 -0
- package/dist/index.d.cts +3 -74
- package/dist/index.d.ts +3 -74
- package/dist/load-from-file-I3ALLIVB.js +8 -0
- package/dist/next/index.cjs +698 -476
- package/dist/next/index.d.cts +11 -1
- package/dist/next/index.d.ts +11 -1
- package/dist/next/index.js +78 -281
- package/dist/node/loader.cjs +704 -602
- package/dist/node/loader.js +10 -11
- package/dist/plugins/index-file.cjs +471 -0
- package/dist/plugins/index-file.d.cts +29 -0
- package/dist/plugins/index-file.d.ts +29 -0
- package/dist/plugins/index-file.js +8 -0
- package/dist/plugins/json-schema.d.cts +3 -2
- package/dist/plugins/json-schema.d.ts +3 -2
- package/dist/plugins/last-modified.cjs +75 -0
- package/dist/plugins/last-modified.d.cts +27 -0
- package/dist/plugins/last-modified.d.ts +27 -0
- package/dist/plugins/last-modified.js +44 -0
- package/dist/runtime/{vite/browser.cjs → browser.cjs} +40 -53
- package/dist/runtime/browser.d.cts +50 -0
- package/dist/runtime/browser.d.ts +50 -0
- package/dist/runtime/browser.js +68 -0
- package/dist/runtime/dynamic.cjs +985 -0
- package/dist/runtime/dynamic.d.cts +27 -0
- package/dist/runtime/dynamic.d.ts +27 -0
- package/dist/runtime/dynamic.js +78 -0
- package/dist/runtime/server.cjs +173 -0
- package/dist/runtime/server.d.cts +161 -0
- package/dist/runtime/server.d.ts +161 -0
- package/dist/runtime/server.js +8 -0
- package/dist/vite/index.cjs +934 -638
- package/dist/vite/index.d.cts +12 -22
- package/dist/vite/index.d.ts +12 -22
- package/dist/vite/index.js +30 -221
- package/dist/webpack/mdx.cjs +613 -515
- package/dist/webpack/mdx.d.cts +9 -1
- package/dist/webpack/mdx.d.ts +9 -1
- package/dist/webpack/mdx.js +12 -17
- package/dist/webpack/meta.cjs +327 -233
- package/dist/webpack/meta.d.cts +9 -1
- package/dist/webpack/meta.d.ts +9 -1
- package/dist/webpack/meta.js +13 -15
- package/package.json +15 -32
- package/dist/build-mdx-BjXOmv0b.d.cts +0 -53
- package/dist/build-mdx-CY5UldCO.d.ts +0 -53
- package/dist/chunk-2AQRQXSO.js +0 -119
- package/dist/chunk-CXA4JO4Z.js +0 -45
- package/dist/chunk-DMJ6I4C3.js +0 -76
- package/dist/chunk-FSZMKRVH.js +0 -80
- package/dist/chunk-II3H5ZVZ.js +0 -77
- package/dist/chunk-KILFIBVW.js +0 -75
- package/dist/chunk-NVRDCY6Z.js +0 -30
- package/dist/chunk-VUEZTR2H.js +0 -26
- package/dist/index-D7s7kCc2.d.cts +0 -7
- package/dist/index-D7s7kCc2.d.ts +0 -7
- package/dist/load-from-file-AVYOFOI7.js +0 -7
- package/dist/preset-ZMP6U62C.js +0 -6
- package/dist/runtime/next/async.cjs +0 -760
- package/dist/runtime/next/async.d.cts +0 -19
- package/dist/runtime/next/async.d.ts +0 -19
- package/dist/runtime/next/async.js +0 -86
- package/dist/runtime/next/index.cjs +0 -136
- package/dist/runtime/next/index.d.cts +0 -33
- package/dist/runtime/next/index.d.ts +0 -33
- package/dist/runtime/next/index.js +0 -11
- package/dist/runtime/vite/browser.d.cts +0 -59
- package/dist/runtime/vite/browser.d.ts +0 -59
- package/dist/runtime/vite/browser.js +0 -11
- package/dist/runtime/vite/server.cjs +0 -243
- package/dist/runtime/vite/server.d.cts +0 -30
- package/dist/runtime/vite/server.d.ts +0 -30
- package/dist/runtime/vite/server.js +0 -111
- package/dist/types-Bnh9n7mj.d.cts +0 -45
- package/dist/types-ey1AZqrg.d.ts +0 -45
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { j as Plugin } from '../index-D7JdSMpp.cjs';
|
|
2
|
+
import '@mdx-js/mdx';
|
|
3
|
+
import '@standard-schema/spec';
|
|
4
|
+
import 'unified';
|
|
5
|
+
import 'fumadocs-core/mdx-plugins';
|
|
6
|
+
import 'zod';
|
|
7
|
+
import 'chokidar';
|
|
8
|
+
import 'vfile';
|
|
9
|
+
|
|
10
|
+
interface LastModifiedPluginOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Version control to obtain the last modified time.
|
|
13
|
+
*
|
|
14
|
+
* - `git`: Requires `git` to be installed.
|
|
15
|
+
*
|
|
16
|
+
* If you are using Vercel, please set `VERCEL_DEEP_CLONE` environment variable to `true`.
|
|
17
|
+
*
|
|
18
|
+
* @defaultValue 'git'
|
|
19
|
+
*/
|
|
20
|
+
versionControl?: 'git';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Injects `lastModified` property to page exports.
|
|
24
|
+
*/
|
|
25
|
+
declare function lastModified(options?: LastModifiedPluginOptions): Plugin;
|
|
26
|
+
|
|
27
|
+
export { type LastModifiedPluginOptions, lastModified as default };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { j as Plugin } from '../index-D7JdSMpp.js';
|
|
2
|
+
import '@mdx-js/mdx';
|
|
3
|
+
import '@standard-schema/spec';
|
|
4
|
+
import 'unified';
|
|
5
|
+
import 'fumadocs-core/mdx-plugins';
|
|
6
|
+
import 'zod';
|
|
7
|
+
import 'chokidar';
|
|
8
|
+
import 'vfile';
|
|
9
|
+
|
|
10
|
+
interface LastModifiedPluginOptions {
|
|
11
|
+
/**
|
|
12
|
+
* Version control to obtain the last modified time.
|
|
13
|
+
*
|
|
14
|
+
* - `git`: Requires `git` to be installed.
|
|
15
|
+
*
|
|
16
|
+
* If you are using Vercel, please set `VERCEL_DEEP_CLONE` environment variable to `true`.
|
|
17
|
+
*
|
|
18
|
+
* @defaultValue 'git'
|
|
19
|
+
*/
|
|
20
|
+
versionControl?: 'git';
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Injects `lastModified` property to page exports.
|
|
24
|
+
*/
|
|
25
|
+
declare function lastModified(options?: LastModifiedPluginOptions): Plugin;
|
|
26
|
+
|
|
27
|
+
export { type LastModifiedPluginOptions, lastModified as default };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/plugins/last-modified.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { x } from "tinyexec";
|
|
4
|
+
var cache = /* @__PURE__ */ new Map();
|
|
5
|
+
function lastModified(options = {}) {
|
|
6
|
+
const { versionControl = "git" } = options;
|
|
7
|
+
return {
|
|
8
|
+
name: "last-modified",
|
|
9
|
+
doc: {
|
|
10
|
+
async vfile(file) {
|
|
11
|
+
if (versionControl === "git") {
|
|
12
|
+
const timestamp = await getGitTimestamp(this.filePath).then(
|
|
13
|
+
(v) => v?.getTime()
|
|
14
|
+
);
|
|
15
|
+
if (timestamp === void 0) return;
|
|
16
|
+
file.data["mdx-export"] ??= [];
|
|
17
|
+
file.data["mdx-export"].push({
|
|
18
|
+
name: "lastModified",
|
|
19
|
+
value: timestamp
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
async function getGitTimestamp(file) {
|
|
27
|
+
const cached = cache.get(file);
|
|
28
|
+
if (cached) return cached;
|
|
29
|
+
const timePromise = (async () => {
|
|
30
|
+
const out = await x(
|
|
31
|
+
"git",
|
|
32
|
+
["log", "-1", '--pretty="%ai"', path.relative(process.cwd(), file)],
|
|
33
|
+
{
|
|
34
|
+
throwOnError: true
|
|
35
|
+
}
|
|
36
|
+
);
|
|
37
|
+
return new Date(out.stdout);
|
|
38
|
+
})();
|
|
39
|
+
cache.set(file, timePromise);
|
|
40
|
+
return timePromise.catch(() => void 0);
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
lastModified as default
|
|
44
|
+
};
|
|
@@ -17,77 +17,69 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
};
|
|
18
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
19
|
|
|
20
|
-
// src/runtime/
|
|
20
|
+
// src/runtime/browser.ts
|
|
21
21
|
var browser_exports = {};
|
|
22
22
|
__export(browser_exports, {
|
|
23
23
|
createClientLoader: () => createClientLoader,
|
|
24
|
-
fromConfig: () =>
|
|
25
|
-
fromConfigBase: () => fromConfigBase,
|
|
26
|
-
toClientRenderer: () => toClientRenderer
|
|
24
|
+
fromConfig: () => fromConfig
|
|
27
25
|
});
|
|
28
26
|
module.exports = __toCommonJS(browser_exports);
|
|
29
|
-
|
|
30
|
-
// src/runtime/vite/base.ts
|
|
31
27
|
var import_react = require("react");
|
|
32
|
-
function
|
|
33
|
-
function normalize(entries, base) {
|
|
34
|
-
const out = {};
|
|
35
|
-
for (const k in entries) {
|
|
36
|
-
const mappedK = k.startsWith("./") ? k.slice(2) : k;
|
|
37
|
-
if (base) Object.assign(entries[k], { base });
|
|
38
|
-
out[mappedK] = entries[k];
|
|
39
|
-
}
|
|
40
|
-
return out;
|
|
41
|
-
}
|
|
28
|
+
function fromConfig() {
|
|
42
29
|
return {
|
|
43
|
-
doc(
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return {
|
|
51
|
-
base,
|
|
52
|
-
head: normalize(head),
|
|
53
|
-
body: normalize(body)
|
|
30
|
+
doc(_name, glob) {
|
|
31
|
+
const raw = glob;
|
|
32
|
+
const out = {
|
|
33
|
+
raw,
|
|
34
|
+
createClientLoader({ id = _name, ...options }) {
|
|
35
|
+
return createClientLoader(raw, { id, ...options });
|
|
36
|
+
}
|
|
54
37
|
};
|
|
38
|
+
return out;
|
|
55
39
|
}
|
|
56
40
|
};
|
|
57
41
|
}
|
|
58
42
|
var loaderStore = /* @__PURE__ */ new Map();
|
|
59
|
-
function createClientLoader(
|
|
43
|
+
function createClientLoader(globEntries, options) {
|
|
60
44
|
const { id = "", component } = options;
|
|
61
|
-
|
|
45
|
+
const renderers = {};
|
|
46
|
+
const loaders = /* @__PURE__ */ new Map();
|
|
62
47
|
const store = loaderStore.get(id) ?? {
|
|
63
48
|
preloaded: /* @__PURE__ */ new Map()
|
|
64
49
|
};
|
|
65
50
|
loaderStore.set(id, store);
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
51
|
+
for (const k in globEntries) {
|
|
52
|
+
loaders.set(k.startsWith("./") ? k.slice(2) : k, globEntries[k]);
|
|
53
|
+
}
|
|
54
|
+
function getLoader(path) {
|
|
55
|
+
const loader = loaders.get(path);
|
|
56
|
+
if (!loader)
|
|
57
|
+
throw new Error(
|
|
58
|
+
`[createClientLoader] ${path} does not exist in available entries`
|
|
59
|
+
);
|
|
60
|
+
return loader;
|
|
61
|
+
}
|
|
62
|
+
function getRenderer(path) {
|
|
63
|
+
if (path in renderers) return renderers[path];
|
|
64
|
+
const OnDemand = (0, import_react.lazy)(async () => {
|
|
65
|
+
const loaded = await getLoader(path)();
|
|
66
|
+
return { default: (props) => component(loaded, props) };
|
|
67
|
+
});
|
|
68
|
+
renderers[path] = (props) => {
|
|
69
|
+
const cached = store.preloaded.get(path);
|
|
70
|
+
if (!cached) return (0, import_react.createElement)(OnDemand, props);
|
|
71
|
+
return component(cached, props);
|
|
72
|
+
};
|
|
73
|
+
return renderers[path];
|
|
81
74
|
}
|
|
82
75
|
return {
|
|
83
76
|
async preload(path) {
|
|
84
|
-
const loaded = await
|
|
77
|
+
const loaded = await getLoader(path)();
|
|
85
78
|
store.preloaded.set(path, loaded);
|
|
86
79
|
return loaded;
|
|
87
80
|
},
|
|
88
|
-
getRenderer,
|
|
89
81
|
getComponent(path) {
|
|
90
|
-
return getRenderer()
|
|
82
|
+
return getRenderer(path);
|
|
91
83
|
},
|
|
92
84
|
useContent(path, props) {
|
|
93
85
|
const Comp = this.getComponent(path);
|
|
@@ -95,13 +87,8 @@ function createClientLoader(files, options) {
|
|
|
95
87
|
}
|
|
96
88
|
};
|
|
97
89
|
}
|
|
98
|
-
function toClientRenderer(files, component) {
|
|
99
|
-
return createClientLoader(files, { component }).getRenderer();
|
|
100
|
-
}
|
|
101
90
|
// Annotate the CommonJS export names for ESM import in node:
|
|
102
91
|
0 && (module.exports = {
|
|
103
92
|
createClientLoader,
|
|
104
|
-
fromConfig
|
|
105
|
-
fromConfigBase,
|
|
106
|
-
toClientRenderer
|
|
93
|
+
fromConfig
|
|
107
94
|
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ReactNode, FC } from 'react';
|
|
2
|
+
import { CompiledMDXFile } from './server.cjs';
|
|
3
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
+
import { D as DocCollection, b as DocsCollection } from '../index-D7JdSMpp.cjs';
|
|
5
|
+
import 'fumadocs-core/source';
|
|
6
|
+
import 'fumadocs-core/mdx-plugins';
|
|
7
|
+
import 'fumadocs-core/toc';
|
|
8
|
+
import 'mdx/types';
|
|
9
|
+
import 'mdast';
|
|
10
|
+
import '@mdx-js/mdx';
|
|
11
|
+
import 'unified';
|
|
12
|
+
import 'zod';
|
|
13
|
+
import 'chokidar';
|
|
14
|
+
import 'vfile';
|
|
15
|
+
|
|
16
|
+
interface ClientLoaderOptions<Frontmatter, Props> {
|
|
17
|
+
/**
|
|
18
|
+
* Loader ID (usually your collection name)
|
|
19
|
+
*
|
|
20
|
+
* The code splitting strategy of frameworks like Tanstack Start may duplicate `createClientLoader()` into different chunks.
|
|
21
|
+
*
|
|
22
|
+
* We use loader ID to share cache between multiple instances of client loader.
|
|
23
|
+
*
|
|
24
|
+
* @defaultValue ''
|
|
25
|
+
*/
|
|
26
|
+
id?: string;
|
|
27
|
+
component: (loaded: CompiledMDXFile<Frontmatter>, props: Props) => ReactNode;
|
|
28
|
+
}
|
|
29
|
+
interface ClientLoader<Frontmatter, Props> {
|
|
30
|
+
preload: (path: string) => Promise<CompiledMDXFile<Frontmatter>>;
|
|
31
|
+
/**
|
|
32
|
+
* Get a component that renders content with `React.lazy`.
|
|
33
|
+
*/
|
|
34
|
+
getComponent: (path: string) => FC<Props>;
|
|
35
|
+
/**
|
|
36
|
+
* Get react nodes that renders content with `React.lazy`.
|
|
37
|
+
*/
|
|
38
|
+
useContent: (path: string, props: Props) => ReactNode;
|
|
39
|
+
}
|
|
40
|
+
type BrowserCreate<Config> = ReturnType<typeof fromConfig<Config>>;
|
|
41
|
+
interface DocCollectionEntry<Frontmatter> {
|
|
42
|
+
raw: Record<string, () => Promise<CompiledMDXFile<Frontmatter>>>;
|
|
43
|
+
createClientLoader: <Props extends object>(options: ClientLoaderOptions<Frontmatter, Props>) => ClientLoader<Frontmatter, Props>;
|
|
44
|
+
}
|
|
45
|
+
declare function fromConfig<Config>(): {
|
|
46
|
+
doc<Name extends keyof Config>(_name: Name, glob: Record<string, () => Promise<unknown>>): Config[Name] extends DocCollection<infer Schema> | DocsCollection<infer Schema> ? DocCollectionEntry<StandardSchemaV1.InferOutput<Schema>> : never;
|
|
47
|
+
};
|
|
48
|
+
declare function createClientLoader<Frontmatter, Props extends object = object>(globEntries: Record<string, () => Promise<CompiledMDXFile<Frontmatter>>>, options: ClientLoaderOptions<Frontmatter, Props>): ClientLoader<Frontmatter, Props>;
|
|
49
|
+
|
|
50
|
+
export { type BrowserCreate, type ClientLoader, type ClientLoaderOptions, type DocCollectionEntry, createClientLoader, fromConfig };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ReactNode, FC } from 'react';
|
|
2
|
+
import { CompiledMDXFile } from './server.js';
|
|
3
|
+
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
4
|
+
import { D as DocCollection, b as DocsCollection } from '../index-D7JdSMpp.js';
|
|
5
|
+
import 'fumadocs-core/source';
|
|
6
|
+
import 'fumadocs-core/mdx-plugins';
|
|
7
|
+
import 'fumadocs-core/toc';
|
|
8
|
+
import 'mdx/types';
|
|
9
|
+
import 'mdast';
|
|
10
|
+
import '@mdx-js/mdx';
|
|
11
|
+
import 'unified';
|
|
12
|
+
import 'zod';
|
|
13
|
+
import 'chokidar';
|
|
14
|
+
import 'vfile';
|
|
15
|
+
|
|
16
|
+
interface ClientLoaderOptions<Frontmatter, Props> {
|
|
17
|
+
/**
|
|
18
|
+
* Loader ID (usually your collection name)
|
|
19
|
+
*
|
|
20
|
+
* The code splitting strategy of frameworks like Tanstack Start may duplicate `createClientLoader()` into different chunks.
|
|
21
|
+
*
|
|
22
|
+
* We use loader ID to share cache between multiple instances of client loader.
|
|
23
|
+
*
|
|
24
|
+
* @defaultValue ''
|
|
25
|
+
*/
|
|
26
|
+
id?: string;
|
|
27
|
+
component: (loaded: CompiledMDXFile<Frontmatter>, props: Props) => ReactNode;
|
|
28
|
+
}
|
|
29
|
+
interface ClientLoader<Frontmatter, Props> {
|
|
30
|
+
preload: (path: string) => Promise<CompiledMDXFile<Frontmatter>>;
|
|
31
|
+
/**
|
|
32
|
+
* Get a component that renders content with `React.lazy`.
|
|
33
|
+
*/
|
|
34
|
+
getComponent: (path: string) => FC<Props>;
|
|
35
|
+
/**
|
|
36
|
+
* Get react nodes that renders content with `React.lazy`.
|
|
37
|
+
*/
|
|
38
|
+
useContent: (path: string, props: Props) => ReactNode;
|
|
39
|
+
}
|
|
40
|
+
type BrowserCreate<Config> = ReturnType<typeof fromConfig<Config>>;
|
|
41
|
+
interface DocCollectionEntry<Frontmatter> {
|
|
42
|
+
raw: Record<string, () => Promise<CompiledMDXFile<Frontmatter>>>;
|
|
43
|
+
createClientLoader: <Props extends object>(options: ClientLoaderOptions<Frontmatter, Props>) => ClientLoader<Frontmatter, Props>;
|
|
44
|
+
}
|
|
45
|
+
declare function fromConfig<Config>(): {
|
|
46
|
+
doc<Name extends keyof Config>(_name: Name, glob: Record<string, () => Promise<unknown>>): Config[Name] extends DocCollection<infer Schema> | DocsCollection<infer Schema> ? DocCollectionEntry<StandardSchemaV1.InferOutput<Schema>> : never;
|
|
47
|
+
};
|
|
48
|
+
declare function createClientLoader<Frontmatter, Props extends object = object>(globEntries: Record<string, () => Promise<CompiledMDXFile<Frontmatter>>>, options: ClientLoaderOptions<Frontmatter, Props>): ClientLoader<Frontmatter, Props>;
|
|
49
|
+
|
|
50
|
+
export { type BrowserCreate, type ClientLoader, type ClientLoaderOptions, type DocCollectionEntry, createClientLoader, fromConfig };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// src/runtime/browser.ts
|
|
2
|
+
import { lazy, createElement } from "react";
|
|
3
|
+
function fromConfig() {
|
|
4
|
+
return {
|
|
5
|
+
doc(_name, glob) {
|
|
6
|
+
const raw = glob;
|
|
7
|
+
const out = {
|
|
8
|
+
raw,
|
|
9
|
+
createClientLoader({ id = _name, ...options }) {
|
|
10
|
+
return createClientLoader(raw, { id, ...options });
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
var loaderStore = /* @__PURE__ */ new Map();
|
|
18
|
+
function createClientLoader(globEntries, options) {
|
|
19
|
+
const { id = "", component } = options;
|
|
20
|
+
const renderers = {};
|
|
21
|
+
const loaders = /* @__PURE__ */ new Map();
|
|
22
|
+
const store = loaderStore.get(id) ?? {
|
|
23
|
+
preloaded: /* @__PURE__ */ new Map()
|
|
24
|
+
};
|
|
25
|
+
loaderStore.set(id, store);
|
|
26
|
+
for (const k in globEntries) {
|
|
27
|
+
loaders.set(k.startsWith("./") ? k.slice(2) : k, globEntries[k]);
|
|
28
|
+
}
|
|
29
|
+
function getLoader(path) {
|
|
30
|
+
const loader = loaders.get(path);
|
|
31
|
+
if (!loader)
|
|
32
|
+
throw new Error(
|
|
33
|
+
`[createClientLoader] ${path} does not exist in available entries`
|
|
34
|
+
);
|
|
35
|
+
return loader;
|
|
36
|
+
}
|
|
37
|
+
function getRenderer(path) {
|
|
38
|
+
if (path in renderers) return renderers[path];
|
|
39
|
+
const OnDemand = lazy(async () => {
|
|
40
|
+
const loaded = await getLoader(path)();
|
|
41
|
+
return { default: (props) => component(loaded, props) };
|
|
42
|
+
});
|
|
43
|
+
renderers[path] = (props) => {
|
|
44
|
+
const cached = store.preloaded.get(path);
|
|
45
|
+
if (!cached) return createElement(OnDemand, props);
|
|
46
|
+
return component(cached, props);
|
|
47
|
+
};
|
|
48
|
+
return renderers[path];
|
|
49
|
+
}
|
|
50
|
+
return {
|
|
51
|
+
async preload(path) {
|
|
52
|
+
const loaded = await getLoader(path)();
|
|
53
|
+
store.preloaded.set(path, loaded);
|
|
54
|
+
return loaded;
|
|
55
|
+
},
|
|
56
|
+
getComponent(path) {
|
|
57
|
+
return getRenderer(path);
|
|
58
|
+
},
|
|
59
|
+
useContent(path, props) {
|
|
60
|
+
const Comp = this.getComponent(path);
|
|
61
|
+
return createElement(Comp, props);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
createClientLoader,
|
|
67
|
+
fromConfig
|
|
68
|
+
};
|