@rspress/core 0.0.0-next-20230815052558
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/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +1576 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.d.ts +58 -0
- package/dist/loader.js +4132 -0
- package/dist/loader.js.map +1 -0
- package/dist/theme/bundle.css +2633 -0
- package/dist/theme/bundle.d.ts +145 -0
- package/dist/theme/bundle.js +6472 -0
- package/dist/theme/bundle.js.map +1 -0
- package/dist/theme/index.ts +6 -0
- package/index.html +16 -0
- package/loader.cjs +8 -0
- package/package.json +170 -0
- package/runtime.ts +1 -0
- package/src/runtime/App.tsx +107 -0
- package/src/runtime/Content.tsx +21 -0
- package/src/runtime/NoSSR.tsx +16 -0
- package/src/runtime/clientEntry.tsx +49 -0
- package/src/runtime/hooks.ts +44 -0
- package/src/runtime/index.ts +16 -0
- package/src/runtime/ssrEntry.tsx +31 -0
- package/src/runtime/utils.ts +36 -0
- package/src/theme-default/assets/arrow-down.svg +1 -0
- package/src/theme-default/assets/arrow-right.svg +1 -0
- package/src/theme-default/assets/copy.svg +1 -0
- package/src/theme-default/assets/down.svg +1 -0
- package/src/theme-default/assets/edit.svg +1 -0
- package/src/theme-default/assets/github.svg +1 -0
- package/src/theme-default/assets/loading.svg +1 -0
- package/src/theme-default/assets/menu.svg +1 -0
- package/src/theme-default/assets/moon.svg +3 -0
- package/src/theme-default/assets/right.svg +1 -0
- package/src/theme-default/assets/small-menu.svg +1 -0
- package/src/theme-default/assets/success.svg +1 -0
- package/src/theme-default/assets/sun.svg +11 -0
- package/src/theme-default/assets/translator.svg +1 -0
- package/src/theme-default/components/Aside/index.tsx +82 -0
- package/src/theme-default/components/BackDrop/index.module.scss +14 -0
- package/src/theme-default/components/BackDrop/index.tsx +13 -0
- package/src/theme-default/components/Button/index.module.scss +68 -0
- package/src/theme-default/components/Button/index.tsx +40 -0
- package/src/theme-default/components/DocFooter/index.module.scss +60 -0
- package/src/theme-default/components/DocFooter/index.tsx +63 -0
- package/src/theme-default/components/EditLink/index.module.scss +11 -0
- package/src/theme-default/components/EditLink/index.tsx +23 -0
- package/src/theme-default/components/HomeFeatures/index.module.scss +50 -0
- package/src/theme-default/components/HomeFeatures/index.tsx +65 -0
- package/src/theme-default/components/HomeFooter/index.tsx +15 -0
- package/src/theme-default/components/HomeHero/index.module.scss +30 -0
- package/src/theme-default/components/HomeHero/index.tsx +68 -0
- package/src/theme-default/components/Link/index.module.scss +10 -0
- package/src/theme-default/components/Link/index.tsx +71 -0
- package/src/theme-default/components/LocalSideBar/index.module.scss +39 -0
- package/src/theme-default/components/LocalSideBar/index.tsx +49 -0
- package/src/theme-default/components/Nav/NavMenuGroup.tsx +149 -0
- package/src/theme-default/components/Nav/NavMenuSingleItem.tsx +32 -0
- package/src/theme-default/components/Nav/index.module.scss +102 -0
- package/src/theme-default/components/Nav/index.tsx +213 -0
- package/src/theme-default/components/NavHambmger/index.module.scss +81 -0
- package/src/theme-default/components/NavHambmger/index.tsx +41 -0
- package/src/theme-default/components/NavScreen/index.module.scss +46 -0
- package/src/theme-default/components/NavScreen/index.tsx +138 -0
- package/src/theme-default/components/NavScreenMenuGroup/NavScreenMenuGroup.tsx +84 -0
- package/src/theme-default/components/NavScreenMenuGroup/index.module.scss +41 -0
- package/src/theme-default/components/Overview/index.module.scss +155 -0
- package/src/theme-default/components/Overview/index.tsx +129 -0
- package/src/theme-default/components/Search/NoSearchResult.tsx +13 -0
- package/src/theme-default/components/Search/SuggestItem.tsx +135 -0
- package/src/theme-default/components/Search/assets/close.svg +1 -0
- package/src/theme-default/components/Search/assets/file.svg +1 -0
- package/src/theme-default/components/Search/assets/header.svg +1 -0
- package/src/theme-default/components/Search/assets/jump.svg +1 -0
- package/src/theme-default/components/Search/assets/loading.svg +1 -0
- package/src/theme-default/components/Search/assets/search.svg +1 -0
- package/src/theme-default/components/Search/assets/title.svg +1 -0
- package/src/theme-default/components/Search/empty.svg +4 -0
- package/src/theme-default/components/Search/index.module.scss +234 -0
- package/src/theme-default/components/Search/index.tsx +347 -0
- package/src/theme-default/components/Search/logic/Provider.ts +35 -0
- package/src/theme-default/components/Search/logic/providers/LocalProvider.ts +103 -0
- package/src/theme-default/components/Search/logic/providers/RemoteProvider.ts +40 -0
- package/src/theme-default/components/Search/logic/search.ts +341 -0
- package/src/theme-default/components/Search/logic/util.test.ts +21 -0
- package/src/theme-default/components/Search/logic/util.ts +127 -0
- package/src/theme-default/components/Sidebar/index.module.scss +97 -0
- package/src/theme-default/components/Sidebar/index.tsx +379 -0
- package/src/theme-default/components/SocialLinks/HiddenLinks.tsx +29 -0
- package/src/theme-default/components/SocialLinks/LinkContent.tsx +90 -0
- package/src/theme-default/components/SocialLinks/ShownLinks.tsx +32 -0
- package/src/theme-default/components/SocialLinks/index.module.scss +18 -0
- package/src/theme-default/components/SocialLinks/index.tsx +36 -0
- package/src/theme-default/components/SocialLinks/presetIcons.tsx +165 -0
- package/src/theme-default/components/SwitchAppearance/index.module.scss +15 -0
- package/src/theme-default/components/SwitchAppearance/index.tsx +35 -0
- package/src/theme-default/components/Tabs/index.module.scss +26 -0
- package/src/theme-default/components/Tabs/index.tsx +124 -0
- package/src/theme-default/components/Tag/index.tsx +18 -0
- package/src/theme-default/index.ts +23 -0
- package/src/theme-default/layout/DocLayout/docComponents/code.tsx +135 -0
- package/src/theme-default/layout/DocLayout/docComponents/hr.tsx +10 -0
- package/src/theme-default/layout/DocLayout/docComponents/index.module.scss +65 -0
- package/src/theme-default/layout/DocLayout/docComponents/index.tsx +33 -0
- package/src/theme-default/layout/DocLayout/docComponents/link.tsx +23 -0
- package/src/theme-default/layout/DocLayout/docComponents/list.tsx +13 -0
- package/src/theme-default/layout/DocLayout/docComponents/paragraph.tsx +19 -0
- package/src/theme-default/layout/DocLayout/docComponents/pre.tsx +37 -0
- package/src/theme-default/layout/DocLayout/docComponents/prisim-theme.ts +73 -0
- package/src/theme-default/layout/DocLayout/docComponents/table.tsx +37 -0
- package/src/theme-default/layout/DocLayout/docComponents/title.tsx +40 -0
- package/src/theme-default/layout/DocLayout/index.module.scss +87 -0
- package/src/theme-default/layout/DocLayout/index.tsx +125 -0
- package/src/theme-default/layout/HomeLayout/index.css +18 -0
- package/src/theme-default/layout/HomeLayout/index.tsx +38 -0
- package/src/theme-default/layout/Layout/index.tsx +152 -0
- package/src/theme-default/layout/NotFountLayout/index.tsx +40 -0
- package/src/theme-default/logic/TabDataContext.ts +14 -0
- package/src/theme-default/logic/index.ts +6 -0
- package/src/theme-default/logic/sideEffects.ts +164 -0
- package/src/theme-default/logic/useAppearance.ts +58 -0
- package/src/theme-default/logic/useEditLink.ts +30 -0
- package/src/theme-default/logic/useLocaleSiteData.ts +26 -0
- package/src/theme-default/logic/useNav.ts +36 -0
- package/src/theme-default/logic/usePrevNextPage.ts +40 -0
- package/src/theme-default/logic/useSidebarData.ts +73 -0
- package/src/theme-default/logic/utils.ts +38 -0
- package/src/theme-default/styles/base.css +102 -0
- package/src/theme-default/styles/doc/code.css +172 -0
- package/src/theme-default/styles/doc/container.css +104 -0
- package/src/theme-default/styles/doc/index.css +6 -0
- package/src/theme-default/styles/index.ts +9 -0
- package/src/theme-default/styles/scrollbar.css +28 -0
- package/src/theme-default/styles/vars.css +78 -0
- package/theme.ts +2 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,1576 @@
|
|
|
1
|
+
// src/node/dev.ts
|
|
2
|
+
import { removeLeadingSlash as removeLeadingSlash2 } from "@rspress/shared";
|
|
3
|
+
import fs9 from "@modern-js/utils/fs-extra";
|
|
4
|
+
|
|
5
|
+
// src/node/createBuilder.ts
|
|
6
|
+
import path9 from "path";
|
|
7
|
+
import { createRequire as createRequire3 } from "module";
|
|
8
|
+
import { isDebugMode, removeTrailingSlash } from "@rspress/shared";
|
|
9
|
+
import sirv from "sirv";
|
|
10
|
+
import fs8 from "@modern-js/utils/fs-extra";
|
|
11
|
+
|
|
12
|
+
// tailwind.config.ts
|
|
13
|
+
var tailwindConfig = {
|
|
14
|
+
content: [],
|
|
15
|
+
darkMode: "class",
|
|
16
|
+
theme: {
|
|
17
|
+
backgroundColor: (ctx) => ({
|
|
18
|
+
...ctx.theme("colors"),
|
|
19
|
+
white: "var(--modern-c-bg)",
|
|
20
|
+
soft: "var(--modern-c-bg-soft)",
|
|
21
|
+
mute: "var(--modern-c-bg-mute)"
|
|
22
|
+
}),
|
|
23
|
+
extend: {
|
|
24
|
+
borderRadius: {
|
|
25
|
+
"4xl": "2rem"
|
|
26
|
+
},
|
|
27
|
+
breakpoints: {
|
|
28
|
+
xs: "640px",
|
|
29
|
+
sm: "768px",
|
|
30
|
+
md: "960px",
|
|
31
|
+
lg: "1280px"
|
|
32
|
+
},
|
|
33
|
+
maxWidth: {
|
|
34
|
+
"60": "15rem"
|
|
35
|
+
},
|
|
36
|
+
maxHeight: {
|
|
37
|
+
"60": "15rem"
|
|
38
|
+
},
|
|
39
|
+
colors: {
|
|
40
|
+
brand: {
|
|
41
|
+
DEFAULT: "var(--modern-c-brand)",
|
|
42
|
+
light: "var(--modern-c-brand-light)",
|
|
43
|
+
dark: "var(--modern-c-brand-dark)",
|
|
44
|
+
lighter: "var(--modern-c-brand-lighter)",
|
|
45
|
+
darker: "var(--modern-c-brand-darker)"
|
|
46
|
+
},
|
|
47
|
+
text: {
|
|
48
|
+
1: "var(--modern-c-text-1)",
|
|
49
|
+
2: "var(--modern-c-text-2)",
|
|
50
|
+
3: "var(--modern-c-text-3)",
|
|
51
|
+
4: "var(--modern-c-text-4)"
|
|
52
|
+
},
|
|
53
|
+
divider: {
|
|
54
|
+
DEFAULT: "var(--modern-c-divider)",
|
|
55
|
+
light: "var(--modern-c-divider-light)",
|
|
56
|
+
dark: "var(--modern-c-divider-dark)"
|
|
57
|
+
},
|
|
58
|
+
gray: {
|
|
59
|
+
light: {
|
|
60
|
+
1: "var(--modern-c-gray-light-1)",
|
|
61
|
+
2: "var(--modern-c-gray-light-2)",
|
|
62
|
+
3: "var(--modern-c-gray-light-3)",
|
|
63
|
+
4: "var(--modern-c-gray-light-4)",
|
|
64
|
+
5: "var(--modern-c-gray-light-5)"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// src/node/constants.ts
|
|
73
|
+
import path from "path";
|
|
74
|
+
import { fileURLToPath } from "url";
|
|
75
|
+
var isProduction = () => process.env.NODE_ENV === "production";
|
|
76
|
+
var importStatementRegex = /import\s+(.*?)\s+from\s+['"](.*?)['"];?/gm;
|
|
77
|
+
var dirname = path.dirname(fileURLToPath(new URL(import.meta.url)));
|
|
78
|
+
var PACKAGE_ROOT = path.join(dirname, "..");
|
|
79
|
+
var CLIENT_ENTRY = path.join(PACKAGE_ROOT, "src", "runtime", "clientEntry.tsx");
|
|
80
|
+
var SSR_ENTRY = path.join(PACKAGE_ROOT, "src", "runtime", "ssrEntry.tsx");
|
|
81
|
+
var OUTPUT_DIR = "doc_build";
|
|
82
|
+
var APP_HTML_MARKER = "<!--<?- DOC_CONTENT ?>-->";
|
|
83
|
+
var HEAD_MARKER = "<!--<?- HEAD ?>-->";
|
|
84
|
+
var HTML_START_TAG = "<html";
|
|
85
|
+
var BODY_START_TAG = "<body";
|
|
86
|
+
var PUBLIC_DIR = "public";
|
|
87
|
+
var TEMP_DIR = path.join(process.cwd(), "node_modules", ".modern-doc");
|
|
88
|
+
|
|
89
|
+
// src/node/runtimeModule/routeData.ts
|
|
90
|
+
import { join } from "path";
|
|
91
|
+
import { addLeadingSlash } from "@rspress/shared";
|
|
92
|
+
|
|
93
|
+
// src/node/runtimeModule/RuntimeModulePlugin.ts
|
|
94
|
+
import path2 from "path";
|
|
95
|
+
import fs from "fs-extra";
|
|
96
|
+
function _check_private_redeclaration(obj, privateCollection) {
|
|
97
|
+
if (privateCollection.has(obj)) {
|
|
98
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function _class_private_method_get(receiver, privateSet, fn) {
|
|
102
|
+
if (!privateSet.has(receiver)) {
|
|
103
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
104
|
+
}
|
|
105
|
+
return fn;
|
|
106
|
+
}
|
|
107
|
+
function _class_private_method_init(obj, privateSet) {
|
|
108
|
+
_check_private_redeclaration(obj, privateSet);
|
|
109
|
+
privateSet.add(obj);
|
|
110
|
+
}
|
|
111
|
+
function _define_property(obj, key, value) {
|
|
112
|
+
if (key in obj) {
|
|
113
|
+
Object.defineProperty(obj, key, {
|
|
114
|
+
value,
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
writable: true
|
|
118
|
+
});
|
|
119
|
+
} else {
|
|
120
|
+
obj[key] = value;
|
|
121
|
+
}
|
|
122
|
+
return obj;
|
|
123
|
+
}
|
|
124
|
+
var _normalizePath = /* @__PURE__ */ new WeakSet();
|
|
125
|
+
var RuntimeModulesPlugin = class {
|
|
126
|
+
apply() {
|
|
127
|
+
Object.entries(this.staticModules).forEach(([path10, content]) => {
|
|
128
|
+
fs.writeFileSync(_class_private_method_get(this, _normalizePath, normalizePath).call(this, path10), content);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
writeModule(path1, content) {
|
|
132
|
+
const normalizedPath = _class_private_method_get(this, _normalizePath, normalizePath).call(this, path1);
|
|
133
|
+
this.staticModules[normalizedPath] = content;
|
|
134
|
+
fs.writeFileSync(normalizedPath, content);
|
|
135
|
+
}
|
|
136
|
+
constructor(staticModules) {
|
|
137
|
+
_class_private_method_init(this, _normalizePath);
|
|
138
|
+
_define_property(this, "staticModules", void 0);
|
|
139
|
+
this.staticModules = staticModules;
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
function normalizePath(p) {
|
|
143
|
+
if (path2.isAbsolute(p)) {
|
|
144
|
+
return p;
|
|
145
|
+
} else {
|
|
146
|
+
return path2.join(process.cwd(), p);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// src/node/runtimeModule/routeData.ts
|
|
151
|
+
async function routeVMPlugin(context) {
|
|
152
|
+
const { runtimeTempDir, routeService: routeService2 } = context;
|
|
153
|
+
const routeModulePathForClient = join(runtimeTempDir, `${RuntimeModuleID.RouteForClient}.js`);
|
|
154
|
+
const routeModulePathForSSR = join(runtimeTempDir, `${RuntimeModuleID.RouteForSSR}.js`);
|
|
155
|
+
const plugin = new RuntimeModulesPlugin({
|
|
156
|
+
[routeModulePathForClient]: routeService2.generateRoutesCode(false),
|
|
157
|
+
[routeModulePathForSSR]: routeService2.generateRoutesCode(true)
|
|
158
|
+
});
|
|
159
|
+
return plugin;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/node/runtimeModule/siteData.ts
|
|
163
|
+
import path6, { join as join3 } from "path";
|
|
164
|
+
import { withBase, MDX_REGEXP as MDX_REGEXP2, SEARCH_INDEX_NAME, addLeadingSlash as addLeadingSlash2, isExternalUrl, withoutBase } from "@rspress/shared";
|
|
165
|
+
import yamlFrontMatter from "yaml-front-matter";
|
|
166
|
+
import { htmlToText } from "html-to-text";
|
|
167
|
+
import fs4 from "@modern-js/utils/fs-extra";
|
|
168
|
+
import { compile } from "@modern-js/mdx-rs-binding";
|
|
169
|
+
|
|
170
|
+
// src/node/utils/applyReplaceRules.ts
|
|
171
|
+
function applyReplaceRules(code = "", replaceRules = []) {
|
|
172
|
+
let result = code;
|
|
173
|
+
for (const rule of replaceRules) {
|
|
174
|
+
result = result.replace(rule.search, rule.replace);
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// src/node/utils/normalizePath.ts
|
|
180
|
+
import os from "os";
|
|
181
|
+
import path3 from "path";
|
|
182
|
+
var isWindows = os.platform() === "win32";
|
|
183
|
+
function slash(p) {
|
|
184
|
+
return p.replace(/\\/g, "/");
|
|
185
|
+
}
|
|
186
|
+
function normalizePath2(id) {
|
|
187
|
+
return path3.posix.normalize(isWindows ? slash(id) : id);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/node/utils/getPageKey.ts
|
|
191
|
+
import { removeLeadingSlash } from "@rspress/shared";
|
|
192
|
+
function getPageKey(route) {
|
|
193
|
+
const cleanRoute = removeLeadingSlash(route);
|
|
194
|
+
return cleanRoute.replace(/\//g, "_").replace(/\.[^.]+$/, "") || "index";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/node/utils/createHash.ts
|
|
198
|
+
import { createHash as createHashFunc } from "crypto";
|
|
199
|
+
function createHash(str) {
|
|
200
|
+
return createHashFunc("sha256").update(str).digest("hex").slice(0, 8);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// src/node/utils/logger.ts
|
|
204
|
+
import { createRequire } from "module";
|
|
205
|
+
var require2 = createRequire(import.meta.url);
|
|
206
|
+
var { logger } = require2("@modern-js/utils/logger");
|
|
207
|
+
|
|
208
|
+
// src/node/utils/detectReactVersion.ts
|
|
209
|
+
import path5 from "path";
|
|
210
|
+
import fs3 from "@modern-js/utils/fs-extra";
|
|
211
|
+
|
|
212
|
+
// src/node/utils/flattenMdxContent.ts
|
|
213
|
+
import path4 from "path";
|
|
214
|
+
import enhancedResolve from "enhanced-resolve";
|
|
215
|
+
import fs2 from "@modern-js/utils/fs-extra";
|
|
216
|
+
import { createProcessor } from "@mdx-js/mdx";
|
|
217
|
+
import { MDX_REGEXP } from "@rspress/shared";
|
|
218
|
+
var { CachedInputFileSystem, ResolverFactory } = enhancedResolve;
|
|
219
|
+
var processor = createProcessor();
|
|
220
|
+
var resolver;
|
|
221
|
+
var startFlatten = false;
|
|
222
|
+
async function resolveDepPath(importPath, importer, alias) {
|
|
223
|
+
if (!resolver) {
|
|
224
|
+
resolver = ResolverFactory.createResolver({
|
|
225
|
+
fileSystem: new CachedInputFileSystem(fs2),
|
|
226
|
+
extensions: [
|
|
227
|
+
".mdx",
|
|
228
|
+
".md",
|
|
229
|
+
".js"
|
|
230
|
+
],
|
|
231
|
+
alias
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const resolveResult = await new Promise((resolve, reject) => {
|
|
235
|
+
resolver.resolve({
|
|
236
|
+
importer
|
|
237
|
+
}, importer, importPath, {}, (err, filePath) => {
|
|
238
|
+
if (err) {
|
|
239
|
+
return reject(err);
|
|
240
|
+
}
|
|
241
|
+
if (!filePath) {
|
|
242
|
+
return reject(new Error(`Empty result when resolving ${importPath} from ${importer}`));
|
|
243
|
+
}
|
|
244
|
+
return resolve(filePath);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
return resolveResult;
|
|
248
|
+
}
|
|
249
|
+
async function flattenMdxContent(content, basePath, alias) {
|
|
250
|
+
let result = content;
|
|
251
|
+
if (!startFlatten) {
|
|
252
|
+
resolver = ResolverFactory.createResolver({
|
|
253
|
+
fileSystem: new CachedInputFileSystem(fs2),
|
|
254
|
+
extensions: [
|
|
255
|
+
".mdx",
|
|
256
|
+
".md",
|
|
257
|
+
".js"
|
|
258
|
+
],
|
|
259
|
+
alias
|
|
260
|
+
});
|
|
261
|
+
startFlatten = true;
|
|
262
|
+
}
|
|
263
|
+
let ast;
|
|
264
|
+
try {
|
|
265
|
+
ast = processor.parse(content);
|
|
266
|
+
} catch (e) {
|
|
267
|
+
return content;
|
|
268
|
+
}
|
|
269
|
+
const importNodes = ast.children.filter((node) => node.type === "mdxjsEsm").map((node) => {
|
|
270
|
+
result = result.replace(node.value, "");
|
|
271
|
+
return node.data?.estree?.body || [];
|
|
272
|
+
}).flat().filter((node) => node.type === "ImportDeclaration");
|
|
273
|
+
for (const importNode of importNodes) {
|
|
274
|
+
const id = importNode.specifiers[0].local.name;
|
|
275
|
+
const importPath = importNode.source.value;
|
|
276
|
+
let absoluteImportPath;
|
|
277
|
+
try {
|
|
278
|
+
absoluteImportPath = await resolveDepPath(importPath, path4.dirname(basePath), alias);
|
|
279
|
+
} catch (e) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (MDX_REGEXP.test(absoluteImportPath)) {
|
|
283
|
+
const importedContent = fs2.readFileSync(absoluteImportPath, "utf-8");
|
|
284
|
+
const replacedValue = await flattenMdxContent(importedContent, absoluteImportPath, alias);
|
|
285
|
+
result = result.replace(new RegExp(`<${id}\\s*/>`), () => replacedValue);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return result;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// src/node/utils/detectReactVersion.ts
|
|
292
|
+
var DEFAULT_REACT_VERSION = 18;
|
|
293
|
+
async function detectReactVersion() {
|
|
294
|
+
const cwd = process.cwd();
|
|
295
|
+
const reactPath = path5.join(cwd, "node_modules", "react");
|
|
296
|
+
if (await fs3.pathExists(reactPath)) {
|
|
297
|
+
const reactPkg = await fs3.readJson(path5.join(reactPath, "package.json"));
|
|
298
|
+
const version = Number(reactPkg.version.split(".")[0]);
|
|
299
|
+
return version;
|
|
300
|
+
} else {
|
|
301
|
+
return DEFAULT_REACT_VERSION;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
async function resolveReactAlias(reactVersion) {
|
|
305
|
+
const basedir = reactVersion === DEFAULT_REACT_VERSION ? PACKAGE_ROOT : process.cwd();
|
|
306
|
+
const libPaths = [
|
|
307
|
+
"react",
|
|
308
|
+
"react/jsx-runtime",
|
|
309
|
+
"react/jsx-dev-runtime",
|
|
310
|
+
"react-dom",
|
|
311
|
+
"react-dom/server"
|
|
312
|
+
];
|
|
313
|
+
if (reactVersion === DEFAULT_REACT_VERSION) {
|
|
314
|
+
libPaths.push("react-dom/client");
|
|
315
|
+
}
|
|
316
|
+
const alias = {};
|
|
317
|
+
await Promise.all(libPaths.map(async (lib) => {
|
|
318
|
+
try {
|
|
319
|
+
alias[lib] = await resolveDepPath(lib, basedir, {});
|
|
320
|
+
} catch (e) {
|
|
321
|
+
console.log(`warning: ${lib} not found`);
|
|
322
|
+
}
|
|
323
|
+
}));
|
|
324
|
+
return alias;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// src/node/runtimeModule/i18n.ts
|
|
328
|
+
import { join as join2 } from "path";
|
|
329
|
+
import { createRequire as createRequire2 } from "module";
|
|
330
|
+
var require3 = createRequire2(import.meta.url);
|
|
331
|
+
var DEFAULT_I18N_SOURCE = join2(process.cwd(), "i18n.json");
|
|
332
|
+
function getI18nData(docConfig) {
|
|
333
|
+
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
334
|
+
try {
|
|
335
|
+
const i18nSource = require3(i18nSourcePath);
|
|
336
|
+
return i18nSource;
|
|
337
|
+
} catch (e) {
|
|
338
|
+
return {};
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
function i18nVMPlugin(context) {
|
|
342
|
+
const { config, runtimeTempDir } = context;
|
|
343
|
+
const i18nData = getI18nData(config);
|
|
344
|
+
const modulePath = join2(runtimeTempDir, `${RuntimeModuleID.I18nText}.js`);
|
|
345
|
+
return new RuntimeModulesPlugin({
|
|
346
|
+
[modulePath]: `export default ${JSON.stringify(i18nData)}`
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// src/node/runtimeModule/siteData.ts
|
|
351
|
+
var { loadFront } = yamlFrontMatter;
|
|
352
|
+
var pages;
|
|
353
|
+
function deletePriviteKey(obj) {
|
|
354
|
+
if (typeof obj !== "object" || obj === null) {
|
|
355
|
+
return obj;
|
|
356
|
+
}
|
|
357
|
+
const newObj = {
|
|
358
|
+
...obj
|
|
359
|
+
};
|
|
360
|
+
Object.keys(newObj).forEach((key) => {
|
|
361
|
+
if (key.startsWith("_")) {
|
|
362
|
+
delete newObj[key];
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
return newObj;
|
|
366
|
+
}
|
|
367
|
+
function normalizeThemeConfig(docConfig, pages2 = []) {
|
|
368
|
+
const { locales: siteLocales, base = "", lang, replaceRules = [] } = docConfig;
|
|
369
|
+
docConfig.themeConfig = docConfig.themeConfig || {};
|
|
370
|
+
const { themeConfig } = docConfig;
|
|
371
|
+
const locales = siteLocales ?? (themeConfig?.locales || []);
|
|
372
|
+
const i18nTextData = getI18nData(docConfig);
|
|
373
|
+
const normalizeLangPrefix = (link, currentLang) => {
|
|
374
|
+
if (!currentLang || !link || withoutBase(link, base).startsWith(`/${currentLang}`) || isExternalUrl(link)) {
|
|
375
|
+
return link;
|
|
376
|
+
}
|
|
377
|
+
return lang === currentLang ? link : `/${currentLang}${addLeadingSlash2(link)}`;
|
|
378
|
+
};
|
|
379
|
+
const getI18nText = (key, currentLang) => {
|
|
380
|
+
const text = i18nTextData[key]?.[currentLang];
|
|
381
|
+
return text || key;
|
|
382
|
+
};
|
|
383
|
+
const normalizeSidebar = (sidebar, currentLang) => {
|
|
384
|
+
const normalizedSidebar = {};
|
|
385
|
+
if (!sidebar) {
|
|
386
|
+
return {};
|
|
387
|
+
}
|
|
388
|
+
const normalizeSidebarItem = (item) => {
|
|
389
|
+
if (typeof item === "object" && "items" in item) {
|
|
390
|
+
return {
|
|
391
|
+
text: applyReplaceRules(getI18nText(item.text, currentLang), replaceRules),
|
|
392
|
+
link: normalizeLangPrefix(item.link, currentLang),
|
|
393
|
+
collapsed: item.collapsed ?? false,
|
|
394
|
+
collapsible: item.collapsible ?? true,
|
|
395
|
+
tag: item.tag,
|
|
396
|
+
items: item.items.map((subItem) => {
|
|
397
|
+
return normalizeSidebarItem(subItem);
|
|
398
|
+
})
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
if (typeof item === "string") {
|
|
402
|
+
const normalizedItem = normalizeLangPrefix(item, currentLang);
|
|
403
|
+
const page = pages2.find((page2) => page2.routePath === withBase(normalizedItem, base));
|
|
404
|
+
return {
|
|
405
|
+
text: applyReplaceRules(page?.title || "", replaceRules),
|
|
406
|
+
link: normalizedItem
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
return {
|
|
410
|
+
text: applyReplaceRules(getI18nText(item.text, currentLang), replaceRules),
|
|
411
|
+
link: normalizeLangPrefix(item.link, currentLang),
|
|
412
|
+
tag: item.tag
|
|
413
|
+
};
|
|
414
|
+
};
|
|
415
|
+
Object.keys(sidebar).forEach((key) => {
|
|
416
|
+
const value = sidebar[key];
|
|
417
|
+
const normalizedKey = normalizeLangPrefix(key, currentLang);
|
|
418
|
+
normalizedSidebar[normalizedKey] = value.map(normalizeSidebarItem);
|
|
419
|
+
});
|
|
420
|
+
return normalizedSidebar;
|
|
421
|
+
};
|
|
422
|
+
const normalizeNav = (nav, currentLang) => {
|
|
423
|
+
if (!nav) {
|
|
424
|
+
return [];
|
|
425
|
+
}
|
|
426
|
+
return nav?.map((navItem) => {
|
|
427
|
+
const text = applyReplaceRules(getI18nText(navItem.text, currentLang), replaceRules);
|
|
428
|
+
if ("link" in navItem) {
|
|
429
|
+
return {
|
|
430
|
+
...navItem,
|
|
431
|
+
text,
|
|
432
|
+
link: normalizeLangPrefix(navItem.link, currentLang)
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
if ("items" in navItem) {
|
|
436
|
+
return {
|
|
437
|
+
...navItem,
|
|
438
|
+
text,
|
|
439
|
+
items: navItem.items.map((item) => {
|
|
440
|
+
return {
|
|
441
|
+
...item,
|
|
442
|
+
text: applyReplaceRules(getI18nText(item.text, currentLang), replaceRules),
|
|
443
|
+
link: normalizeLangPrefix(item.link, currentLang)
|
|
444
|
+
};
|
|
445
|
+
})
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
return navItem;
|
|
449
|
+
});
|
|
450
|
+
};
|
|
451
|
+
if (locales.length) {
|
|
452
|
+
themeConfig.locales = locales.map(({ lang: currentLang, label }) => {
|
|
453
|
+
const localeInThemeConfig = themeConfig.locales?.find((locale) => locale.lang === currentLang);
|
|
454
|
+
return {
|
|
455
|
+
lang: currentLang,
|
|
456
|
+
label,
|
|
457
|
+
...localeInThemeConfig || {},
|
|
458
|
+
sidebar: normalizeSidebar(localeInThemeConfig?.sidebar ?? themeConfig.sidebar, currentLang),
|
|
459
|
+
nav: normalizeNav(localeInThemeConfig?.nav ?? themeConfig.nav, currentLang)
|
|
460
|
+
};
|
|
461
|
+
});
|
|
462
|
+
} else {
|
|
463
|
+
themeConfig.sidebar = normalizeSidebar(themeConfig?.sidebar);
|
|
464
|
+
themeConfig.nav = normalizeNav(themeConfig?.nav);
|
|
465
|
+
}
|
|
466
|
+
return themeConfig;
|
|
467
|
+
}
|
|
468
|
+
async function extractPageData(replaceRules, alias, domain, root, routeService2) {
|
|
469
|
+
return Promise.all(routeService2.getRoutes().filter((route) => MDX_REGEXP2.test(route.absolutePath)).map(async (route, index) => {
|
|
470
|
+
let content = await fs4.readFile(route.absolutePath, "utf8");
|
|
471
|
+
const frontmatter = {
|
|
472
|
+
...loadFront(content)
|
|
473
|
+
};
|
|
474
|
+
Object.keys(frontmatter).forEach((key) => {
|
|
475
|
+
if (typeof frontmatter[key] === "string") {
|
|
476
|
+
frontmatter[key] = applyReplaceRules(frontmatter[key], replaceRules);
|
|
477
|
+
}
|
|
478
|
+
});
|
|
479
|
+
const flattenContent = await flattenMdxContent(frontmatter.__content, route.absolutePath, alias);
|
|
480
|
+
content = flattenContent.replace(importStatementRegex, "");
|
|
481
|
+
const { html, title, toc: rawToc } = await compile({
|
|
482
|
+
value: content,
|
|
483
|
+
filepath: route.absolutePath,
|
|
484
|
+
development: process.env.NODE_ENV !== "production",
|
|
485
|
+
root
|
|
486
|
+
});
|
|
487
|
+
if (!title?.length && !frontmatter && !frontmatter.title?.length) {
|
|
488
|
+
return null;
|
|
489
|
+
}
|
|
490
|
+
content = htmlToText(String(html), {
|
|
491
|
+
wordwrap: 80,
|
|
492
|
+
selectors: [
|
|
493
|
+
{
|
|
494
|
+
selector: "a",
|
|
495
|
+
options: {
|
|
496
|
+
ignoreHref: true
|
|
497
|
+
}
|
|
498
|
+
},
|
|
499
|
+
{
|
|
500
|
+
selector: "img",
|
|
501
|
+
format: "skip"
|
|
502
|
+
},
|
|
503
|
+
...[
|
|
504
|
+
"h1",
|
|
505
|
+
"h2",
|
|
506
|
+
"h3",
|
|
507
|
+
"h4",
|
|
508
|
+
"h5",
|
|
509
|
+
"h6"
|
|
510
|
+
].map((tag) => ({
|
|
511
|
+
selector: tag,
|
|
512
|
+
options: {
|
|
513
|
+
uppercase: false
|
|
514
|
+
}
|
|
515
|
+
}))
|
|
516
|
+
],
|
|
517
|
+
tables: true,
|
|
518
|
+
longWordSplit: {
|
|
519
|
+
forceWrapOnLimit: true
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
if (content.startsWith(title)) {
|
|
523
|
+
content = content.slice(title.length);
|
|
524
|
+
}
|
|
525
|
+
const toc = rawToc.map((item) => {
|
|
526
|
+
const match = item.id.match(/-(\d+)$/);
|
|
527
|
+
let position = -1;
|
|
528
|
+
if (match) {
|
|
529
|
+
for (let i = 0; i < Number(match[1]); i++) {
|
|
530
|
+
position = content.indexOf(`
|
|
531
|
+
${item.text}#
|
|
532
|
+
|
|
533
|
+
`, position + 1);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return {
|
|
537
|
+
...item,
|
|
538
|
+
charIndex: content.indexOf(`
|
|
539
|
+
${item.text}#
|
|
540
|
+
|
|
541
|
+
`, position + 1)
|
|
542
|
+
};
|
|
543
|
+
});
|
|
544
|
+
return {
|
|
545
|
+
id: index,
|
|
546
|
+
title: frontmatter.title || title,
|
|
547
|
+
routePath: route.routePath,
|
|
548
|
+
lang: route.lang,
|
|
549
|
+
toc,
|
|
550
|
+
domain,
|
|
551
|
+
// Stripped frontmatter content
|
|
552
|
+
content,
|
|
553
|
+
frontmatter: {
|
|
554
|
+
...frontmatter,
|
|
555
|
+
__content: void 0
|
|
556
|
+
},
|
|
557
|
+
_filepath: route.absolutePath,
|
|
558
|
+
_relativePath: path6.relative(root, route.absolutePath)
|
|
559
|
+
};
|
|
560
|
+
}));
|
|
561
|
+
}
|
|
562
|
+
async function siteDataVMPlugin(context) {
|
|
563
|
+
const { runtimeTempDir, config, isSSR, alias, userRoot, routeService: routeService2, pluginDriver } = context;
|
|
564
|
+
const entryPath = join3(runtimeTempDir, `${RuntimeModuleID.SiteData}.mjs`);
|
|
565
|
+
const searchIndexHashPath = join3(runtimeTempDir, `${RuntimeModuleID.SearchIndexHash}.js`);
|
|
566
|
+
const userConfig = config;
|
|
567
|
+
const replaceRules = userConfig?.replaceRules || [];
|
|
568
|
+
if (!pages) {
|
|
569
|
+
if (!isSSR) {
|
|
570
|
+
logger.info("Extracting site data...");
|
|
571
|
+
}
|
|
572
|
+
const domain = userConfig?.search && userConfig?.search?.mode === "remote" ? userConfig?.search.domain ?? "" : "";
|
|
573
|
+
pages = (await extractPageData(replaceRules, alias, domain, userRoot, routeService2)).filter(Boolean);
|
|
574
|
+
await pluginDriver.modifySearchIndexData(pages);
|
|
575
|
+
}
|
|
576
|
+
const pagesByLang = pages.reduce((acc, page) => {
|
|
577
|
+
if (!acc[page.lang]) {
|
|
578
|
+
acc[page.lang] = [];
|
|
579
|
+
}
|
|
580
|
+
if (page.frontmatter?.pageType === "home") {
|
|
581
|
+
return acc;
|
|
582
|
+
}
|
|
583
|
+
acc[page.lang].push(page);
|
|
584
|
+
return acc;
|
|
585
|
+
}, {});
|
|
586
|
+
const indexHashByLang = {};
|
|
587
|
+
await Promise.all(Object.keys(pagesByLang).map(async (lang) => {
|
|
588
|
+
const stringfiedIndex = JSON.stringify(pagesByLang[lang].map(deletePriviteKey));
|
|
589
|
+
const indexHash = createHash(stringfiedIndex);
|
|
590
|
+
indexHashByLang[lang] = indexHash;
|
|
591
|
+
await fs4.ensureDir(TEMP_DIR);
|
|
592
|
+
await fs4.writeFile(path6.join(TEMP_DIR, `${SEARCH_INDEX_NAME}.${lang}.${indexHash}.json`), stringfiedIndex);
|
|
593
|
+
}));
|
|
594
|
+
await Promise.all(pages.map(async (pageData) => pluginDriver.extendPageData(pageData)));
|
|
595
|
+
const siteData = {
|
|
596
|
+
title: userConfig?.title || "",
|
|
597
|
+
description: userConfig?.description || "",
|
|
598
|
+
icon: userConfig?.icon || "",
|
|
599
|
+
themeConfig: normalizeThemeConfig(userConfig, pages),
|
|
600
|
+
base: userConfig?.base || "/",
|
|
601
|
+
lang: userConfig?.lang || "",
|
|
602
|
+
logo: userConfig?.logo || "",
|
|
603
|
+
search: userConfig?.search ?? {
|
|
604
|
+
mode: "local"
|
|
605
|
+
},
|
|
606
|
+
pages: pages.map((page) => {
|
|
607
|
+
const { content, id, domain, _filepath, ...rest } = page;
|
|
608
|
+
return rest;
|
|
609
|
+
}),
|
|
610
|
+
markdown: {
|
|
611
|
+
showLineNumbers: userConfig?.markdown?.showLineNumbers ?? false
|
|
612
|
+
}
|
|
613
|
+
};
|
|
614
|
+
const plugin = new RuntimeModulesPlugin({
|
|
615
|
+
[entryPath]: `export default ${JSON.stringify(siteData)}`,
|
|
616
|
+
[searchIndexHashPath]: `export default ${JSON.stringify(indexHashByLang)}`
|
|
617
|
+
});
|
|
618
|
+
return plugin;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// src/node/runtimeModule/globalUIComponents.ts
|
|
622
|
+
import { join as join4 } from "path";
|
|
623
|
+
async function globalUIComponentsVMPlugin(context) {
|
|
624
|
+
const { runtimeTempDir, config, pluginDriver } = context;
|
|
625
|
+
let index = 0;
|
|
626
|
+
const modulePath = join4(runtimeTempDir, `${RuntimeModuleID.GlobalComponents}.js`);
|
|
627
|
+
const globalUIComponentsByPlugins = pluginDriver.globalUIComponents();
|
|
628
|
+
const moduleContent = [
|
|
629
|
+
...config?.globalUIComponents || [],
|
|
630
|
+
...globalUIComponentsByPlugins
|
|
631
|
+
].map((source) => `import Comp_${index++} from ${JSON.stringify(source)};`).concat(`export default [${Array.from({
|
|
632
|
+
length: index
|
|
633
|
+
}, (_, i) => `Comp_${i}`).join(", ")}];`).join("");
|
|
634
|
+
return new RuntimeModulesPlugin({
|
|
635
|
+
[modulePath]: moduleContent
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// src/node/runtimeModule/globalStyles.ts
|
|
640
|
+
import { join as join5 } from "path";
|
|
641
|
+
async function globalStylesVMPlugin(context) {
|
|
642
|
+
const { runtimeTempDir, config, pluginDriver } = context;
|
|
643
|
+
const modulePath = join5(runtimeTempDir, `${RuntimeModuleID.GlobalStyles}.js`);
|
|
644
|
+
const globalStylesByPlugins = pluginDriver.globalStyles();
|
|
645
|
+
const moduleContent = [
|
|
646
|
+
config?.globalStyles || "",
|
|
647
|
+
...globalStylesByPlugins
|
|
648
|
+
].filter((source) => source.length > 0).map((source) => `import ${JSON.stringify(source)};`).join("");
|
|
649
|
+
return new RuntimeModulesPlugin({
|
|
650
|
+
[modulePath]: moduleContent
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// src/node/runtimeModule/searchHooks.ts
|
|
655
|
+
import { join as join6 } from "path";
|
|
656
|
+
import fs5 from "@modern-js/utils/fs-extra";
|
|
657
|
+
async function searchHookVMPlugin(context) {
|
|
658
|
+
const { config } = context;
|
|
659
|
+
let content = "export const onSearch = () => {}";
|
|
660
|
+
if (typeof config.search === "object" && typeof config.search.searchHooks === "string") {
|
|
661
|
+
content = await fs5.readFile(config.search.searchHooks, "utf-8");
|
|
662
|
+
}
|
|
663
|
+
const modulePath = join6(context.runtimeTempDir, `${RuntimeModuleID.SearchHooks}.ts`);
|
|
664
|
+
return new RuntimeModulesPlugin({
|
|
665
|
+
[modulePath]: content
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
// src/node/runtimeModule/index.ts
|
|
670
|
+
var runtimeModuleFactory = [
|
|
671
|
+
/**
|
|
672
|
+
* Generate route data for client and server runtime
|
|
673
|
+
*/
|
|
674
|
+
routeVMPlugin,
|
|
675
|
+
/**
|
|
676
|
+
* Generate search index and site data for client runtime
|
|
677
|
+
*/
|
|
678
|
+
siteDataVMPlugin,
|
|
679
|
+
/**
|
|
680
|
+
* Generate global components from config and plugins
|
|
681
|
+
*/
|
|
682
|
+
globalUIComponentsVMPlugin,
|
|
683
|
+
/**
|
|
684
|
+
* Generate global styles from config and plugins
|
|
685
|
+
*/
|
|
686
|
+
globalStylesVMPlugin,
|
|
687
|
+
/**
|
|
688
|
+
* Generate i18n text for client runtime
|
|
689
|
+
*/
|
|
690
|
+
i18nVMPlugin,
|
|
691
|
+
/**
|
|
692
|
+
* Generate search hook module
|
|
693
|
+
*/
|
|
694
|
+
searchHookVMPlugin
|
|
695
|
+
];
|
|
696
|
+
function builderDocVMPlugin(factoryContext) {
|
|
697
|
+
return {
|
|
698
|
+
name: "vmBuilderPlugin",
|
|
699
|
+
setup(api) {
|
|
700
|
+
api.modifyBundlerChain(async (bundlerChain) => {
|
|
701
|
+
const alias = bundlerChain.resolve.alias.entries();
|
|
702
|
+
let index = 0;
|
|
703
|
+
for (const factory of runtimeModuleFactory) {
|
|
704
|
+
bundlerChain.plugin(`runtime-module-${index++}`).use(await factory({
|
|
705
|
+
...factoryContext,
|
|
706
|
+
alias
|
|
707
|
+
}));
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
};
|
|
712
|
+
}
|
|
713
|
+
var RuntimeModuleID;
|
|
714
|
+
(function(RuntimeModuleID2) {
|
|
715
|
+
RuntimeModuleID2["GlobalStyles"] = "virtual-global-styles";
|
|
716
|
+
RuntimeModuleID2["GlobalComponents"] = "virtual-global-components";
|
|
717
|
+
RuntimeModuleID2["RouteForClient"] = "virtual-routes";
|
|
718
|
+
RuntimeModuleID2["RouteForSSR"] = "virtual-routes-ssr";
|
|
719
|
+
RuntimeModuleID2["SiteData"] = "virtual-site-data";
|
|
720
|
+
RuntimeModuleID2["SearchIndexHash"] = "virtual-search-index-hash";
|
|
721
|
+
RuntimeModuleID2["I18nText"] = "virtual-i18n-text";
|
|
722
|
+
RuntimeModuleID2["SearchHooks"] = "virtual-search-hooks";
|
|
723
|
+
})(RuntimeModuleID || (RuntimeModuleID = {}));
|
|
724
|
+
var runtimeModuleIDs = [
|
|
725
|
+
RuntimeModuleID.GlobalStyles,
|
|
726
|
+
RuntimeModuleID.GlobalComponents,
|
|
727
|
+
RuntimeModuleID.RouteForClient,
|
|
728
|
+
RuntimeModuleID.RouteForSSR,
|
|
729
|
+
RuntimeModuleID.SiteData,
|
|
730
|
+
RuntimeModuleID.SearchIndexHash,
|
|
731
|
+
RuntimeModuleID.I18nText,
|
|
732
|
+
RuntimeModuleID.SearchHooks
|
|
733
|
+
];
|
|
734
|
+
|
|
735
|
+
// src/node/searchIndex.ts
|
|
736
|
+
import path7, { join as join7 } from "path";
|
|
737
|
+
import fs6 from "@modern-js/utils/fs-extra";
|
|
738
|
+
import chalk from "@modern-js/utils/chalk";
|
|
739
|
+
import fetch from "node-fetch";
|
|
740
|
+
import { isSCM, SEARCH_INDEX_NAME as SEARCH_INDEX_NAME2 } from "@rspress/shared";
|
|
741
|
+
async function writeSearchIndex(config) {
|
|
742
|
+
if (config?.search === false) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const cwd = process.cwd();
|
|
746
|
+
const searchIndexFiles = await fs6.readdir(TEMP_DIR);
|
|
747
|
+
const outDir = config?.outDir ?? join7(cwd, OUTPUT_DIR);
|
|
748
|
+
let searchIndexData = "[]";
|
|
749
|
+
let scaning = false;
|
|
750
|
+
for (const searchIndexFile of searchIndexFiles) {
|
|
751
|
+
if (!searchIndexFile.includes(SEARCH_INDEX_NAME2) || !searchIndexFile.endsWith(".json")) {
|
|
752
|
+
continue;
|
|
753
|
+
}
|
|
754
|
+
const source = join7(TEMP_DIR, searchIndexFile);
|
|
755
|
+
const target = join7(outDir, "static", searchIndexFile);
|
|
756
|
+
const searchIndex = await fs6.readFile(join7(TEMP_DIR, searchIndexFile), "utf-8");
|
|
757
|
+
searchIndexData = `${searchIndexData.slice(0, -1)}${scaning ? "," : ""}${searchIndex.slice(1)}`;
|
|
758
|
+
await fs6.move(source, target, {
|
|
759
|
+
overwrite: true
|
|
760
|
+
});
|
|
761
|
+
scaning = true;
|
|
762
|
+
}
|
|
763
|
+
if (isProduction() && isSCM() && config?.search?.mode === "remote") {
|
|
764
|
+
const { apiUrl, indexName } = config.search;
|
|
765
|
+
try {
|
|
766
|
+
await fetch(`${apiUrl}?index=${indexName}`, {
|
|
767
|
+
method: "PUT",
|
|
768
|
+
body: searchIndexData,
|
|
769
|
+
headers: {
|
|
770
|
+
"Content-Type": "application/json"
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
logger.info(chalk.green(`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`));
|
|
774
|
+
} catch (e) {
|
|
775
|
+
logger.info(chalk.red(`[doc-tools] Upload search index \`${indexName}\` failed:
|
|
776
|
+
${e}`));
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
function serveSearchIndexMiddleware(config) {
|
|
781
|
+
return (req, res, next) => {
|
|
782
|
+
const searchIndexRequestMatch = `/${SEARCH_INDEX_NAME2}.`;
|
|
783
|
+
if (req.url?.includes(searchIndexRequestMatch)) {
|
|
784
|
+
res.setHeader("Content-Type", "application/json");
|
|
785
|
+
const searchIndexFile = req.url?.split("/").pop();
|
|
786
|
+
const searchIndex = fs6.readFileSync(path7.join(process.cwd(), config?.outDir || OUTPUT_DIR, "static", searchIndexFile), "utf-8");
|
|
787
|
+
res.end(searchIndex);
|
|
788
|
+
} else {
|
|
789
|
+
next?.();
|
|
790
|
+
}
|
|
791
|
+
};
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
// src/node/route/RouteService.ts
|
|
795
|
+
import path8 from "path";
|
|
796
|
+
import fs7 from "@modern-js/utils/fs-extra";
|
|
797
|
+
import { withBase as withBase2 } from "@rspress/shared";
|
|
798
|
+
function _check_private_redeclaration2(obj, privateCollection) {
|
|
799
|
+
if (privateCollection.has(obj)) {
|
|
800
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
function _class_apply_descriptor_get(receiver, descriptor) {
|
|
804
|
+
if (descriptor.get) {
|
|
805
|
+
return descriptor.get.call(receiver);
|
|
806
|
+
}
|
|
807
|
+
return descriptor.value;
|
|
808
|
+
}
|
|
809
|
+
function _class_apply_descriptor_set(receiver, descriptor, value) {
|
|
810
|
+
if (descriptor.set) {
|
|
811
|
+
descriptor.set.call(receiver, value);
|
|
812
|
+
} else {
|
|
813
|
+
if (!descriptor.writable) {
|
|
814
|
+
throw new TypeError("attempted to set read only private field");
|
|
815
|
+
}
|
|
816
|
+
descriptor.value = value;
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
function _class_extract_field_descriptor(receiver, privateMap, action) {
|
|
820
|
+
if (!privateMap.has(receiver)) {
|
|
821
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
822
|
+
}
|
|
823
|
+
return privateMap.get(receiver);
|
|
824
|
+
}
|
|
825
|
+
function _class_private_field_get(receiver, privateMap) {
|
|
826
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "get");
|
|
827
|
+
return _class_apply_descriptor_get(receiver, descriptor);
|
|
828
|
+
}
|
|
829
|
+
function _class_private_field_init(obj, privateMap, value) {
|
|
830
|
+
_check_private_redeclaration2(obj, privateMap);
|
|
831
|
+
privateMap.set(obj, value);
|
|
832
|
+
}
|
|
833
|
+
function _class_private_field_set(receiver, privateMap, value) {
|
|
834
|
+
var descriptor = _class_extract_field_descriptor(receiver, privateMap, "set");
|
|
835
|
+
_class_apply_descriptor_set(receiver, descriptor, value);
|
|
836
|
+
return value;
|
|
837
|
+
}
|
|
838
|
+
function _class_private_method_get2(receiver, privateSet, fn) {
|
|
839
|
+
if (!privateSet.has(receiver)) {
|
|
840
|
+
throw new TypeError("attempted to get private field on non-instance");
|
|
841
|
+
}
|
|
842
|
+
return fn;
|
|
843
|
+
}
|
|
844
|
+
function _class_private_method_init2(obj, privateSet) {
|
|
845
|
+
_check_private_redeclaration2(obj, privateSet);
|
|
846
|
+
privateSet.add(obj);
|
|
847
|
+
}
|
|
848
|
+
function _define_property2(obj, key, value) {
|
|
849
|
+
if (key in obj) {
|
|
850
|
+
Object.defineProperty(obj, key, {
|
|
851
|
+
value,
|
|
852
|
+
enumerable: true,
|
|
853
|
+
configurable: true,
|
|
854
|
+
writable: true
|
|
855
|
+
});
|
|
856
|
+
} else {
|
|
857
|
+
obj[key] = value;
|
|
858
|
+
}
|
|
859
|
+
return obj;
|
|
860
|
+
}
|
|
861
|
+
var DEFAULT_PAGE_EXTENSIONS = [
|
|
862
|
+
"js",
|
|
863
|
+
"jsx",
|
|
864
|
+
"ts",
|
|
865
|
+
"tsx",
|
|
866
|
+
"md",
|
|
867
|
+
"mdx"
|
|
868
|
+
];
|
|
869
|
+
var addLeadingSlash3 = (str) => {
|
|
870
|
+
return str.startsWith("/") ? str : `/${str}`;
|
|
871
|
+
};
|
|
872
|
+
var normalizeRoutePath = (routePath, lang, base) => {
|
|
873
|
+
let normalizedRoutePath = routePath.replace(/^\//, "").replace(new RegExp(`^${lang}`), "").replace(/\.[^.]+$/, "");
|
|
874
|
+
normalizedRoutePath = addLeadingSlash3(normalizedRoutePath).replace(/\/index$/, "/");
|
|
875
|
+
return withBase2(normalizedRoutePath, base);
|
|
876
|
+
};
|
|
877
|
+
var _scanDir = /* @__PURE__ */ new WeakMap();
|
|
878
|
+
var _defaultLang = /* @__PURE__ */ new WeakMap();
|
|
879
|
+
var _langs = /* @__PURE__ */ new WeakMap();
|
|
880
|
+
var _extensions = /* @__PURE__ */ new WeakMap();
|
|
881
|
+
var _include = /* @__PURE__ */ new WeakMap();
|
|
882
|
+
var _exclude = /* @__PURE__ */ new WeakMap();
|
|
883
|
+
var _base = /* @__PURE__ */ new WeakMap();
|
|
884
|
+
var _tempDir = /* @__PURE__ */ new WeakMap();
|
|
885
|
+
var _pluginDriver = /* @__PURE__ */ new WeakMap();
|
|
886
|
+
var _writeTempFile = /* @__PURE__ */ new WeakSet();
|
|
887
|
+
var _getLang = /* @__PURE__ */ new WeakSet();
|
|
888
|
+
var _generateRouteInfo = /* @__PURE__ */ new WeakSet();
|
|
889
|
+
var RouteService = class {
|
|
890
|
+
async init() {
|
|
891
|
+
const { default: globby } = await import("@modern-js/utils/globby");
|
|
892
|
+
const files = globby.sync([
|
|
893
|
+
`**/*.{${_class_private_field_get(this, _extensions).join(",")}}`,
|
|
894
|
+
..._class_private_field_get(this, _include)
|
|
895
|
+
], {
|
|
896
|
+
cwd: _class_private_field_get(this, _scanDir),
|
|
897
|
+
absolute: true,
|
|
898
|
+
ignore: [
|
|
899
|
+
..._class_private_field_get(this, _exclude),
|
|
900
|
+
"**/node_modules/**",
|
|
901
|
+
"**/.eslintrc.js",
|
|
902
|
+
"**/.turbo/**"
|
|
903
|
+
]
|
|
904
|
+
}).sort();
|
|
905
|
+
files.forEach((filePath) => {
|
|
906
|
+
const fileRelativePath = normalizePath2(path8.relative(_class_private_field_get(this, _scanDir), filePath));
|
|
907
|
+
const lang = _class_private_method_get2(this, _getLang, getLang).call(this, fileRelativePath);
|
|
908
|
+
const routePath = normalizeRoutePath(fileRelativePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base));
|
|
909
|
+
const absolutePath = path8.join(_class_private_field_get(this, _scanDir), fileRelativePath);
|
|
910
|
+
const routeInfo = {
|
|
911
|
+
routePath,
|
|
912
|
+
absolutePath: normalizePath2(absolutePath),
|
|
913
|
+
relativePath: fileRelativePath,
|
|
914
|
+
pageName: getPageKey(fileRelativePath),
|
|
915
|
+
lang
|
|
916
|
+
};
|
|
917
|
+
this.addRoute(routeInfo);
|
|
918
|
+
});
|
|
919
|
+
const externalPages = await _class_private_field_get(this, _pluginDriver).addPages();
|
|
920
|
+
await Promise.all(externalPages.map(async (route, index) => {
|
|
921
|
+
const { routePath, content, filepath } = route;
|
|
922
|
+
if (filepath) {
|
|
923
|
+
const routeInfo = _class_private_method_get2(this, _generateRouteInfo, generateRouteInfo).call(this, routePath, filepath);
|
|
924
|
+
this.addRoute(routeInfo);
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
if (content) {
|
|
928
|
+
const filepath2 = await _class_private_method_get2(this, _writeTempFile, writeTempFile).call(this, index, content);
|
|
929
|
+
const routeInfo = _class_private_method_get2(this, _generateRouteInfo, generateRouteInfo).call(this, routePath, filepath2);
|
|
930
|
+
this.addRoute(routeInfo);
|
|
931
|
+
}
|
|
932
|
+
}));
|
|
933
|
+
await _class_private_field_get(this, _pluginDriver).routeGenerated(this.getRoutes());
|
|
934
|
+
}
|
|
935
|
+
addRoute(routeInfo) {
|
|
936
|
+
const { routePath, absolutePath } = routeInfo;
|
|
937
|
+
if (this.routeData.has(routePath)) {
|
|
938
|
+
const preRouteExtIndex = _class_private_field_get(this, _extensions).indexOf(path8.extname(this.routeData.get(routePath).absolutePath).slice(1));
|
|
939
|
+
const currRouteExtIndex = _class_private_field_get(this, _extensions).indexOf(path8.extname(absolutePath).slice(1));
|
|
940
|
+
if (currRouteExtIndex !== -1 && (currRouteExtIndex < preRouteExtIndex || preRouteExtIndex === -1)) {
|
|
941
|
+
this.routeData.set(routePath, routeInfo);
|
|
942
|
+
}
|
|
943
|
+
} else {
|
|
944
|
+
this.routeData.set(routePath, routeInfo);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
removeRoute(filePath) {
|
|
948
|
+
const fileRelativePath = path8.relative(_class_private_field_get(this, _scanDir), filePath);
|
|
949
|
+
const routePath = normalizeRoutePath(fileRelativePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base));
|
|
950
|
+
this.routeData.delete(routePath);
|
|
951
|
+
}
|
|
952
|
+
getRoutes() {
|
|
953
|
+
return Array.from(this.routeData.values());
|
|
954
|
+
}
|
|
955
|
+
isExistRoute(routePath) {
|
|
956
|
+
const normalizedRoute = normalizeRoutePath(routePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base));
|
|
957
|
+
return this.routeData.get(normalizedRoute);
|
|
958
|
+
}
|
|
959
|
+
isEmpty() {
|
|
960
|
+
return this.routeData.size === 0;
|
|
961
|
+
}
|
|
962
|
+
generateRoutesCode(isStaticImport) {
|
|
963
|
+
return `
|
|
964
|
+
import React from 'react';
|
|
965
|
+
import { lazyWithPreload } from "react-lazy-with-preload";
|
|
966
|
+
${this.getRoutes().map((route, index) => {
|
|
967
|
+
return isStaticImport ? `import * as Route${index} from '${route.absolutePath}';` : `const Route${index} = lazyWithPreload(() => import(/* webpackChunkName: "${route.pageName}" */'${route.absolutePath}'))`;
|
|
968
|
+
}).join("\n")}
|
|
969
|
+
export const routes = [
|
|
970
|
+
${this.getRoutes().map((route, index) => {
|
|
971
|
+
const preload = isStaticImport ? `() => Route${index}` : `async () => {
|
|
972
|
+
await Route${index}.preload();
|
|
973
|
+
return import("${route.absolutePath}");
|
|
974
|
+
}`;
|
|
975
|
+
const component = isStaticImport ? `Route${index}.default` : `Route${index}`;
|
|
976
|
+
return `{ path: '${route.routePath}', element: React.createElement(${component}), filePath: '${route.relativePath}', preload: ${preload}, lang: '${route.lang}' }`;
|
|
977
|
+
}).join(",\n")}
|
|
978
|
+
];
|
|
979
|
+
`;
|
|
980
|
+
}
|
|
981
|
+
constructor(scanDir, userConfig, tempDir, pluginDriver) {
|
|
982
|
+
_class_private_method_init2(this, _writeTempFile);
|
|
983
|
+
_class_private_method_init2(this, _getLang);
|
|
984
|
+
_class_private_method_init2(this, _generateRouteInfo);
|
|
985
|
+
_define_property2(this, "routeData", /* @__PURE__ */ new Map());
|
|
986
|
+
_class_private_field_init(this, _scanDir, {
|
|
987
|
+
writable: true,
|
|
988
|
+
value: void 0
|
|
989
|
+
});
|
|
990
|
+
_class_private_field_init(this, _defaultLang, {
|
|
991
|
+
writable: true,
|
|
992
|
+
value: void 0
|
|
993
|
+
});
|
|
994
|
+
_class_private_field_init(this, _langs, {
|
|
995
|
+
writable: true,
|
|
996
|
+
value: void 0
|
|
997
|
+
});
|
|
998
|
+
_class_private_field_init(this, _extensions, {
|
|
999
|
+
writable: true,
|
|
1000
|
+
value: []
|
|
1001
|
+
});
|
|
1002
|
+
_class_private_field_init(this, _include, {
|
|
1003
|
+
writable: true,
|
|
1004
|
+
value: []
|
|
1005
|
+
});
|
|
1006
|
+
_class_private_field_init(this, _exclude, {
|
|
1007
|
+
writable: true,
|
|
1008
|
+
value: []
|
|
1009
|
+
});
|
|
1010
|
+
_class_private_field_init(this, _base, {
|
|
1011
|
+
writable: true,
|
|
1012
|
+
value: ""
|
|
1013
|
+
});
|
|
1014
|
+
_class_private_field_init(this, _tempDir, {
|
|
1015
|
+
writable: true,
|
|
1016
|
+
value: ""
|
|
1017
|
+
});
|
|
1018
|
+
_class_private_field_init(this, _pluginDriver, {
|
|
1019
|
+
writable: true,
|
|
1020
|
+
value: void 0
|
|
1021
|
+
});
|
|
1022
|
+
const routeOptions = userConfig?.route || {};
|
|
1023
|
+
_class_private_field_set(this, _scanDir, scanDir);
|
|
1024
|
+
_class_private_field_set(this, _extensions, routeOptions.extensions || DEFAULT_PAGE_EXTENSIONS);
|
|
1025
|
+
_class_private_field_set(this, _include, routeOptions.include || []);
|
|
1026
|
+
_class_private_field_set(this, _exclude, routeOptions.exclude || []);
|
|
1027
|
+
_class_private_field_set(this, _defaultLang, userConfig?.lang || "");
|
|
1028
|
+
_class_private_field_set(this, _langs, userConfig?.themeConfig?.locales?.map((locale) => locale.lang) || []);
|
|
1029
|
+
_class_private_field_set(this, _base, userConfig?.base || "");
|
|
1030
|
+
_class_private_field_set(this, _tempDir, tempDir);
|
|
1031
|
+
_class_private_field_set(this, _pluginDriver, pluginDriver);
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
async function writeTempFile(index, content) {
|
|
1035
|
+
const tempFilePath = path8.join(_class_private_field_get(this, _tempDir), `temp-${index}.mdx`);
|
|
1036
|
+
await fs7.writeFile(tempFilePath, content);
|
|
1037
|
+
return tempFilePath;
|
|
1038
|
+
}
|
|
1039
|
+
function getLang(filepath) {
|
|
1040
|
+
return _class_private_field_get(this, _langs).find((lang) => filepath.startsWith(lang)) || _class_private_field_get(this, _defaultLang);
|
|
1041
|
+
}
|
|
1042
|
+
function generateRouteInfo(routePath, filepath) {
|
|
1043
|
+
return {
|
|
1044
|
+
routePath: normalizeRoutePath(routePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base)),
|
|
1045
|
+
absolutePath: normalizePath2(filepath),
|
|
1046
|
+
relativePath: normalizePath2(path8.relative(_class_private_field_get(this, _scanDir), filepath)),
|
|
1047
|
+
pageName: getPageKey(routePath),
|
|
1048
|
+
lang: _class_private_method_get2(this, _getLang, getLang).call(this, filepath)
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// src/node/route/init.ts
|
|
1053
|
+
var routeService = null;
|
|
1054
|
+
async function initRouteService(options) {
|
|
1055
|
+
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
1056
|
+
routeService = new RouteService(scanDir, config, runtimeTempDir, pluginDriver);
|
|
1057
|
+
await routeService.init();
|
|
1058
|
+
return routeService;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
// src/node/createBuilder.ts
|
|
1062
|
+
var require4 = createRequire3(import.meta.url);
|
|
1063
|
+
async function createInternalBuildConfig(userRoot, config, isSSR, runtimeTempDir, routeService2) {
|
|
1064
|
+
const cwd = process.cwd();
|
|
1065
|
+
const { default: fs1 } = await import("@modern-js/utils/fs-extra");
|
|
1066
|
+
const CUSTOM_THEME_DIR = config?.themeDir ?? path9.join(process.cwd(), "theme");
|
|
1067
|
+
const outDir = config?.outDir ?? OUTPUT_DIR;
|
|
1068
|
+
const DEFAULT_THEME_DIR = isDebugMode() ? path9.join(PACKAGE_ROOT, "src", "theme-default") : path9.join(PACKAGE_ROOT, "dist", "theme");
|
|
1069
|
+
const themeDir = await fs1.pathExists(CUSTOM_THEME_DIR) ? CUSTOM_THEME_DIR : DEFAULT_THEME_DIR;
|
|
1070
|
+
const checkDeadLinks = (config?.markdown?.checkDeadLinks && !isSSR) ?? false;
|
|
1071
|
+
const base = config?.base ?? "";
|
|
1072
|
+
const publicDir = path9.join(userRoot, "public");
|
|
1073
|
+
const isPublicDirExist = await fs1.pathExists(publicDir);
|
|
1074
|
+
const assetPrefix = isProduction() ? removeTrailingSlash(config?.builderConfig?.output?.assetPrefix ?? base) : "";
|
|
1075
|
+
const enableMdxRs = config?.markdown?.experimentalMdxRs ?? false;
|
|
1076
|
+
const reactVersion = await detectReactVersion();
|
|
1077
|
+
const browserslist = {
|
|
1078
|
+
web: isProduction() ? [
|
|
1079
|
+
"chrome >= 61",
|
|
1080
|
+
"edge >= 16",
|
|
1081
|
+
"firefox >= 60",
|
|
1082
|
+
"safari >= 11",
|
|
1083
|
+
"ios_saf >= 11"
|
|
1084
|
+
] : [
|
|
1085
|
+
"last 1 chrome version",
|
|
1086
|
+
"last 1 firefox version",
|
|
1087
|
+
"last 1 safari version"
|
|
1088
|
+
],
|
|
1089
|
+
node: [
|
|
1090
|
+
"node >= 14"
|
|
1091
|
+
]
|
|
1092
|
+
};
|
|
1093
|
+
return {
|
|
1094
|
+
html: {
|
|
1095
|
+
favicon: config?.icon,
|
|
1096
|
+
template: path9.join(PACKAGE_ROOT, "index.html")
|
|
1097
|
+
},
|
|
1098
|
+
output: {
|
|
1099
|
+
distPath: {
|
|
1100
|
+
// `root` must be a relative path in Builder
|
|
1101
|
+
root: path9.isAbsolute(outDir) ? path9.relative(cwd, outDir) : outDir
|
|
1102
|
+
},
|
|
1103
|
+
// TODO: switch to 'usage' if Rspack supports it
|
|
1104
|
+
polyfill: "entry",
|
|
1105
|
+
svgDefaultExport: "component",
|
|
1106
|
+
disableTsChecker: true,
|
|
1107
|
+
// Disable production source map, it is useless for doc site
|
|
1108
|
+
disableSourceMap: isProduction(),
|
|
1109
|
+
overrideBrowserslist: browserslist,
|
|
1110
|
+
assetPrefix
|
|
1111
|
+
},
|
|
1112
|
+
source: {
|
|
1113
|
+
alias: {
|
|
1114
|
+
"@mdx-js/react": require4.resolve("@mdx-js/react"),
|
|
1115
|
+
"@": path9.join(PACKAGE_ROOT, "src"),
|
|
1116
|
+
"@/runtime": path9.join(PACKAGE_ROOT, "src", "runtime", "index.ts"),
|
|
1117
|
+
"@theme": themeDir,
|
|
1118
|
+
"@modern-js/doc-core": PACKAGE_ROOT,
|
|
1119
|
+
"react-lazy-with-preload": require4.resolve("react-lazy-with-preload"),
|
|
1120
|
+
...runtimeModuleIDs.reduce((acc, cur) => {
|
|
1121
|
+
acc[cur] = path9.join(runtimeTempDir, cur);
|
|
1122
|
+
return acc;
|
|
1123
|
+
}, {}),
|
|
1124
|
+
...await resolveReactAlias(reactVersion)
|
|
1125
|
+
},
|
|
1126
|
+
include: [
|
|
1127
|
+
PACKAGE_ROOT
|
|
1128
|
+
],
|
|
1129
|
+
define: {
|
|
1130
|
+
__ASSET_PREFIX__: JSON.stringify(assetPrefix),
|
|
1131
|
+
"process.env.__SSR__": JSON.stringify(isSSR),
|
|
1132
|
+
"process.env.__IS_REACT_18__": JSON.stringify(reactVersion === 18)
|
|
1133
|
+
}
|
|
1134
|
+
},
|
|
1135
|
+
tools: {
|
|
1136
|
+
devServer: {
|
|
1137
|
+
// Serve static files
|
|
1138
|
+
after: [
|
|
1139
|
+
...isPublicDirExist ? [
|
|
1140
|
+
sirv(publicDir)
|
|
1141
|
+
] : [],
|
|
1142
|
+
serveSearchIndexMiddleware(config)
|
|
1143
|
+
],
|
|
1144
|
+
historyApiFallback: true
|
|
1145
|
+
},
|
|
1146
|
+
postcss(config2) {
|
|
1147
|
+
if (isDebugMode()) {
|
|
1148
|
+
config2.postcssOptions.plugins.push(require4("tailwindcss")({
|
|
1149
|
+
config: {
|
|
1150
|
+
...tailwindConfig,
|
|
1151
|
+
content: [
|
|
1152
|
+
path9.join(PACKAGE_ROOT, "src", "theme-default", "**/*")
|
|
1153
|
+
]
|
|
1154
|
+
}
|
|
1155
|
+
}));
|
|
1156
|
+
}
|
|
1157
|
+
},
|
|
1158
|
+
bundlerChain(chain) {
|
|
1159
|
+
chain.module.rule("MDX").type("jsx").test(/\.mdx?$/).oneOf("MDXCompile").use("mdx-loader").loader(require4.resolve("../loader.cjs")).options({
|
|
1160
|
+
config,
|
|
1161
|
+
docDirectory: userRoot,
|
|
1162
|
+
checkDeadLinks,
|
|
1163
|
+
enableMdxRs,
|
|
1164
|
+
routeService: routeService2
|
|
1165
|
+
}).end().use("string-replace-loader").loader(require4.resolve("string-replace-loader")).options({
|
|
1166
|
+
multiple: config?.replaceRules || []
|
|
1167
|
+
});
|
|
1168
|
+
chain.resolve.extensions.prepend(".md").prepend(".mdx").prepend(".mjs");
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
};
|
|
1172
|
+
}
|
|
1173
|
+
async function createModernBuilder(rootDir, config, pluginDriver, isSSR = false, extraBuilderConfig) {
|
|
1174
|
+
const cwd = process.cwd();
|
|
1175
|
+
const userRoot = path9.resolve(rootDir || config?.root || cwd);
|
|
1176
|
+
const builderPlugins = config?.builderPlugins ?? [];
|
|
1177
|
+
const runtimeTempDir = path9.join(TEMP_DIR, "runtime");
|
|
1178
|
+
await fs8.ensureDir(runtimeTempDir);
|
|
1179
|
+
const routeService2 = await initRouteService({
|
|
1180
|
+
config,
|
|
1181
|
+
runtimeTempDir,
|
|
1182
|
+
scanDir: userRoot,
|
|
1183
|
+
pluginDriver
|
|
1184
|
+
});
|
|
1185
|
+
const { default: { createBuilder, mergeBuilderConfig } } = await import("@modern-js/builder");
|
|
1186
|
+
const { default: { builderRspackProvider } } = await import("@modern-js/builder-rspack-provider");
|
|
1187
|
+
const internalBuilderConfig = await createInternalBuildConfig(userRoot, config, isSSR, runtimeTempDir, routeService2);
|
|
1188
|
+
const builderProvider = builderRspackProvider({
|
|
1189
|
+
builderConfig: mergeBuilderConfig(internalBuilderConfig, ...config?.plugins?.map((plugin) => plugin.builderConfig ?? {}) || [], config?.builderConfig || {}, extraBuilderConfig || {})
|
|
1190
|
+
});
|
|
1191
|
+
const builder = await createBuilder(builderProvider, {
|
|
1192
|
+
target: isSSR ? "node" : "web",
|
|
1193
|
+
entry: {
|
|
1194
|
+
main: isSSR ? SSR_ENTRY : CLIENT_ENTRY
|
|
1195
|
+
}
|
|
1196
|
+
});
|
|
1197
|
+
builder.addPlugins([
|
|
1198
|
+
builderDocVMPlugin({
|
|
1199
|
+
userRoot,
|
|
1200
|
+
config,
|
|
1201
|
+
isSSR,
|
|
1202
|
+
runtimeTempDir,
|
|
1203
|
+
routeService: routeService2,
|
|
1204
|
+
pluginDriver
|
|
1205
|
+
}),
|
|
1206
|
+
...builderPlugins
|
|
1207
|
+
]);
|
|
1208
|
+
return builder;
|
|
1209
|
+
}
|
|
1210
|
+
|
|
1211
|
+
// src/node/PluginDriver.ts
|
|
1212
|
+
function _check_private_redeclaration3(obj, privateCollection) {
|
|
1213
|
+
if (privateCollection.has(obj)) {
|
|
1214
|
+
throw new TypeError("Cannot initialize the same private elements twice on an object");
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
function _class_apply_descriptor_get2(receiver, descriptor) {
|
|
1218
|
+
if (descriptor.get) {
|
|
1219
|
+
return descriptor.get.call(receiver);
|
|
1220
|
+
}
|
|
1221
|
+
return descriptor.value;
|
|
1222
|
+
}
|
|
1223
|
+
function _class_apply_descriptor_set2(receiver, descriptor, value) {
|
|
1224
|
+
if (descriptor.set) {
|
|
1225
|
+
descriptor.set.call(receiver, value);
|
|
1226
|
+
} else {
|
|
1227
|
+
if (!descriptor.writable) {
|
|
1228
|
+
throw new TypeError("attempted to set read only private field");
|
|
1229
|
+
}
|
|
1230
|
+
descriptor.value = value;
|
|
1231
|
+
}
|
|
1232
|
+
}
|
|
1233
|
+
function _class_extract_field_descriptor2(receiver, privateMap, action) {
|
|
1234
|
+
if (!privateMap.has(receiver)) {
|
|
1235
|
+
throw new TypeError("attempted to " + action + " private field on non-instance");
|
|
1236
|
+
}
|
|
1237
|
+
return privateMap.get(receiver);
|
|
1238
|
+
}
|
|
1239
|
+
function _class_private_field_get2(receiver, privateMap) {
|
|
1240
|
+
var descriptor = _class_extract_field_descriptor2(receiver, privateMap, "get");
|
|
1241
|
+
return _class_apply_descriptor_get2(receiver, descriptor);
|
|
1242
|
+
}
|
|
1243
|
+
function _class_private_field_init2(obj, privateMap, value) {
|
|
1244
|
+
_check_private_redeclaration3(obj, privateMap);
|
|
1245
|
+
privateMap.set(obj, value);
|
|
1246
|
+
}
|
|
1247
|
+
function _class_private_field_set2(receiver, privateMap, value) {
|
|
1248
|
+
var descriptor = _class_extract_field_descriptor2(receiver, privateMap, "set");
|
|
1249
|
+
_class_apply_descriptor_set2(receiver, descriptor, value);
|
|
1250
|
+
return value;
|
|
1251
|
+
}
|
|
1252
|
+
var _config = /* @__PURE__ */ new WeakMap();
|
|
1253
|
+
var _plugins = /* @__PURE__ */ new WeakMap();
|
|
1254
|
+
var _isProd = /* @__PURE__ */ new WeakMap();
|
|
1255
|
+
var PluginDriver = class {
|
|
1256
|
+
// The init function is used to initialize the doc plugins and will execute before the build process.
|
|
1257
|
+
async init() {
|
|
1258
|
+
this.clearPlugins();
|
|
1259
|
+
const config = _class_private_field_get2(this, _config);
|
|
1260
|
+
const themeConfig = config?.themeConfig || {};
|
|
1261
|
+
const enableLastUpdated = themeConfig?.lastUpdated || themeConfig?.locales?.some((locale) => locale.lastUpdated);
|
|
1262
|
+
const mediumZoomConfig = config?.mediumZoom ?? true;
|
|
1263
|
+
const haveNavSidebarConfig = themeConfig.nav || themeConfig.sidebar || themeConfig.locales?.[0].nav || themeConfig.locales?.[0].sidebar;
|
|
1264
|
+
if (enableLastUpdated) {
|
|
1265
|
+
const { pluginLastUpdated } = await import("@rspress/plugin-last-updated");
|
|
1266
|
+
this.addPlugin(pluginLastUpdated());
|
|
1267
|
+
}
|
|
1268
|
+
if (mediumZoomConfig) {
|
|
1269
|
+
const { pluginMediumZoom } = await import("@rspress/plugin-medium-zoom");
|
|
1270
|
+
this.addPlugin(pluginMediumZoom());
|
|
1271
|
+
}
|
|
1272
|
+
if (!haveNavSidebarConfig) {
|
|
1273
|
+
const { pluginAutoNavSidebar } = await import("@rspress/plugin-auto-sidebar");
|
|
1274
|
+
this.addPlugin(pluginAutoNavSidebar());
|
|
1275
|
+
}
|
|
1276
|
+
(config.plugins || []).forEach((plugin) => {
|
|
1277
|
+
this.addPlugin(plugin);
|
|
1278
|
+
});
|
|
1279
|
+
}
|
|
1280
|
+
addPlugin(plugin) {
|
|
1281
|
+
const exsitedIndex = _class_private_field_get2(this, _plugins).findIndex((item) => item.name === plugin.name);
|
|
1282
|
+
if (exsitedIndex !== -1) {
|
|
1283
|
+
throw new Error(`The plugin "${plugin.name}" has been registered`);
|
|
1284
|
+
} else {
|
|
1285
|
+
_class_private_field_get2(this, _plugins).push(plugin);
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
clearPlugins() {
|
|
1289
|
+
_class_private_field_set2(this, _plugins, []);
|
|
1290
|
+
}
|
|
1291
|
+
async modifyConfig() {
|
|
1292
|
+
let config = _class_private_field_get2(this, _config);
|
|
1293
|
+
for (const plugin of _class_private_field_get2(this, _plugins)) {
|
|
1294
|
+
if (typeof plugin.config === "function") {
|
|
1295
|
+
config = await plugin.config(config || {});
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
_class_private_field_set2(this, _config, config);
|
|
1299
|
+
return _class_private_field_get2(this, _config);
|
|
1300
|
+
}
|
|
1301
|
+
async beforeBuild() {
|
|
1302
|
+
return this._runParallelAsyncHook("beforeBuild", _class_private_field_get2(this, _config) || {}, _class_private_field_get2(this, _isProd));
|
|
1303
|
+
}
|
|
1304
|
+
async afterBuild() {
|
|
1305
|
+
return this._runParallelAsyncHook("afterBuild", _class_private_field_get2(this, _config) || {}, _class_private_field_get2(this, _isProd));
|
|
1306
|
+
}
|
|
1307
|
+
async modifySearchIndexData(pages2) {
|
|
1308
|
+
return this._runParallelAsyncHook("modifySearchIndexData", pages2);
|
|
1309
|
+
}
|
|
1310
|
+
async extendPageData(pageData) {
|
|
1311
|
+
return this._runParallelAsyncHook("extendPageData", pageData);
|
|
1312
|
+
}
|
|
1313
|
+
async addPages() {
|
|
1314
|
+
const result = await this._runParallelAsyncHook("addPages", _class_private_field_get2(this, _config) || {}, _class_private_field_get2(this, _isProd));
|
|
1315
|
+
return result.flat();
|
|
1316
|
+
}
|
|
1317
|
+
async routeGenerated(routes) {
|
|
1318
|
+
return this._runParallelAsyncHook("routeGenerated", routes);
|
|
1319
|
+
}
|
|
1320
|
+
async addSSGRoutes() {
|
|
1321
|
+
const result = await this._runParallelAsyncHook("addSSGRoutes", _class_private_field_get2(this, _config) || {}, _class_private_field_get2(this, _isProd));
|
|
1322
|
+
return result.flat();
|
|
1323
|
+
}
|
|
1324
|
+
globalUIComponents() {
|
|
1325
|
+
const result = _class_private_field_get2(this, _plugins).map((plugin) => {
|
|
1326
|
+
return plugin.globalUIComponents || [];
|
|
1327
|
+
});
|
|
1328
|
+
return result.flat();
|
|
1329
|
+
}
|
|
1330
|
+
globalStyles() {
|
|
1331
|
+
return _class_private_field_get2(this, _plugins).filter((plugin) => typeof plugin.globalStyles === "string").map((plugin) => {
|
|
1332
|
+
return plugin.globalStyles;
|
|
1333
|
+
});
|
|
1334
|
+
}
|
|
1335
|
+
_runParallelAsyncHook(hookName, ...args) {
|
|
1336
|
+
return Promise.all(_class_private_field_get2(this, _plugins).filter((plugin) => typeof plugin[hookName] === "function").map((plugin) => {
|
|
1337
|
+
return plugin[hookName](...args);
|
|
1338
|
+
}));
|
|
1339
|
+
}
|
|
1340
|
+
_runSerialAysncHook(hookName, ...args) {
|
|
1341
|
+
return _class_private_field_get2(this, _plugins).reduce(async (prev, plugin) => {
|
|
1342
|
+
if (typeof plugin[hookName] === "function") {
|
|
1343
|
+
await prev;
|
|
1344
|
+
return plugin[hookName](...args);
|
|
1345
|
+
}
|
|
1346
|
+
return prev;
|
|
1347
|
+
}, Promise.resolve());
|
|
1348
|
+
}
|
|
1349
|
+
constructor(config, isProd) {
|
|
1350
|
+
_class_private_field_init2(this, _config, {
|
|
1351
|
+
writable: true,
|
|
1352
|
+
value: void 0
|
|
1353
|
+
});
|
|
1354
|
+
_class_private_field_init2(this, _plugins, {
|
|
1355
|
+
writable: true,
|
|
1356
|
+
value: void 0
|
|
1357
|
+
});
|
|
1358
|
+
_class_private_field_init2(this, _isProd, {
|
|
1359
|
+
writable: true,
|
|
1360
|
+
value: void 0
|
|
1361
|
+
});
|
|
1362
|
+
_class_private_field_set2(this, _config, config);
|
|
1363
|
+
_class_private_field_set2(this, _isProd, isProd);
|
|
1364
|
+
}
|
|
1365
|
+
};
|
|
1366
|
+
|
|
1367
|
+
// src/node/dev.ts
|
|
1368
|
+
async function dev(options) {
|
|
1369
|
+
const { docDirectory, config } = options;
|
|
1370
|
+
const base = config?.base ?? "";
|
|
1371
|
+
const isProd = false;
|
|
1372
|
+
const pluginDriver = new PluginDriver(config, isProd);
|
|
1373
|
+
await pluginDriver.init();
|
|
1374
|
+
try {
|
|
1375
|
+
const modifiedConfig = await pluginDriver.modifyConfig();
|
|
1376
|
+
await pluginDriver.beforeBuild();
|
|
1377
|
+
await fs9.emptyDir(TEMP_DIR);
|
|
1378
|
+
const builder = await createModernBuilder(docDirectory, modifiedConfig, pluginDriver, false, {});
|
|
1379
|
+
const { server } = await builder.startDevServer({
|
|
1380
|
+
printURLs: (urls) => {
|
|
1381
|
+
return urls.map(({ label, url }) => ({
|
|
1382
|
+
label,
|
|
1383
|
+
url: `${url}/${removeLeadingSlash2(base)}`
|
|
1384
|
+
}));
|
|
1385
|
+
}
|
|
1386
|
+
});
|
|
1387
|
+
await pluginDriver.afterBuild();
|
|
1388
|
+
return server;
|
|
1389
|
+
} finally {
|
|
1390
|
+
await writeSearchIndex(config);
|
|
1391
|
+
}
|
|
1392
|
+
}
|
|
1393
|
+
|
|
1394
|
+
// src/node/build.ts
|
|
1395
|
+
import { dirname as dirname2, isAbsolute, join as join8 } from "path";
|
|
1396
|
+
import { pathToFileURL } from "url";
|
|
1397
|
+
import chalk2 from "@modern-js/utils/chalk";
|
|
1398
|
+
import fs10 from "@modern-js/utils/fs-extra";
|
|
1399
|
+
import { APPEARANCE_KEY, normalizeSlash, withBase as withBase3 } from "@rspress/shared";
|
|
1400
|
+
var CHECK_DARK_LIGHT_SCRIPT = `
|
|
1401
|
+
<script id="check-dark-light">
|
|
1402
|
+
;(() => {
|
|
1403
|
+
const saved = localStorage.getItem('${APPEARANCE_KEY}')
|
|
1404
|
+
const prefereDark = window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
1405
|
+
if (!saved || saved === 'auto' ? prefereDark : saved === 'dark') {
|
|
1406
|
+
document.documentElement.classList.add('dark')
|
|
1407
|
+
}
|
|
1408
|
+
})()
|
|
1409
|
+
</script>
|
|
1410
|
+
`;
|
|
1411
|
+
async function bundle(appDirectory, docDirectory, config, pluginDriver) {
|
|
1412
|
+
try {
|
|
1413
|
+
const outputDir = config?.outDir ?? OUTPUT_DIR;
|
|
1414
|
+
const [clientBuilder, ssrBuilder] = await Promise.all([
|
|
1415
|
+
createModernBuilder(docDirectory, config, pluginDriver, false),
|
|
1416
|
+
createModernBuilder(docDirectory, config, pluginDriver, true, {
|
|
1417
|
+
output: {
|
|
1418
|
+
distPath: {
|
|
1419
|
+
root: `${outputDir}/ssr`
|
|
1420
|
+
}
|
|
1421
|
+
}
|
|
1422
|
+
})
|
|
1423
|
+
]);
|
|
1424
|
+
await Promise.all([
|
|
1425
|
+
clientBuilder.build(),
|
|
1426
|
+
ssrBuilder.build()
|
|
1427
|
+
]);
|
|
1428
|
+
const logoPaths = [];
|
|
1429
|
+
const { logo } = config;
|
|
1430
|
+
if (typeof logo === "string") {
|
|
1431
|
+
logoPaths.push(logo);
|
|
1432
|
+
} else if (typeof logo === "object") {
|
|
1433
|
+
logoPaths.push(logo.light);
|
|
1434
|
+
logoPaths.push(logo.dark);
|
|
1435
|
+
}
|
|
1436
|
+
logoPaths.filter((p) => p.startsWith("/")).forEach((p) => {
|
|
1437
|
+
const normalize = (rawPath) => isAbsolute(rawPath) ? rawPath : join8(appDirectory, rawPath);
|
|
1438
|
+
const logoPath = join8(normalize(docDirectory || config.root), PUBLIC_DIR, p);
|
|
1439
|
+
const outputLogoPath = join8(normalize(outputDir), p);
|
|
1440
|
+
fs10.copyFileSync(logoPath, outputLogoPath);
|
|
1441
|
+
});
|
|
1442
|
+
} finally {
|
|
1443
|
+
await writeSearchIndex(config);
|
|
1444
|
+
}
|
|
1445
|
+
}
|
|
1446
|
+
async function renderPages(appDirectory, config, pluginDriver) {
|
|
1447
|
+
logger.info("Rendering pages...");
|
|
1448
|
+
const startTime = Date.now();
|
|
1449
|
+
const outputPath = config?.outDir ?? join8(appDirectory, OUTPUT_DIR);
|
|
1450
|
+
const ssrBundlePath = join8(outputPath, "ssr", "bundles", "main.js");
|
|
1451
|
+
try {
|
|
1452
|
+
const { default: fs11 } = await import("@modern-js/utils/fs-extra");
|
|
1453
|
+
let render = null;
|
|
1454
|
+
try {
|
|
1455
|
+
const { default: ssrExports } = await import(pathToFileURL(ssrBundlePath).toString());
|
|
1456
|
+
({ render } = ssrExports);
|
|
1457
|
+
} catch (e) {
|
|
1458
|
+
logger.warn(`Failed to load SSR bundle: ${ssrBundlePath}, fallback to CSR.`);
|
|
1459
|
+
}
|
|
1460
|
+
const routes = routeService.getRoutes();
|
|
1461
|
+
const base = config?.base ?? "";
|
|
1462
|
+
const htmlTemplatePath = join8(outputPath, "html", "main", "index.html");
|
|
1463
|
+
const htmlTemplate = await fs11.readFile(htmlTemplatePath, "utf-8");
|
|
1464
|
+
const additionalRoutes = (await pluginDriver.addSSGRoutes()).map((route) => ({
|
|
1465
|
+
routePath: withBase3(route.path, base)
|
|
1466
|
+
}));
|
|
1467
|
+
await Promise.all([
|
|
1468
|
+
...routes,
|
|
1469
|
+
...additionalRoutes
|
|
1470
|
+
].filter((route) => {
|
|
1471
|
+
return !route.routePath.includes(":");
|
|
1472
|
+
}).map(async (route) => {
|
|
1473
|
+
const helmetContext = {
|
|
1474
|
+
context: {}
|
|
1475
|
+
};
|
|
1476
|
+
const { routePath } = route;
|
|
1477
|
+
let appHtml = "";
|
|
1478
|
+
if (render) {
|
|
1479
|
+
try {
|
|
1480
|
+
({ appHtml } = await render(routePath, helmetContext.context));
|
|
1481
|
+
} catch (e) {
|
|
1482
|
+
logger.warn(`page "${routePath}" render error: ${e.message}, fallback to CSR.`);
|
|
1483
|
+
}
|
|
1484
|
+
}
|
|
1485
|
+
const { helmet } = helmetContext.context;
|
|
1486
|
+
let html = htmlTemplate.replace(APP_HTML_MARKER, () => appHtml).replace(HEAD_MARKER, (config?.head || []).concat([
|
|
1487
|
+
helmet?.title?.toString(),
|
|
1488
|
+
helmet?.meta?.toString(),
|
|
1489
|
+
helmet?.link?.toString(),
|
|
1490
|
+
helmet?.style?.toString(),
|
|
1491
|
+
helmet?.script?.toString(),
|
|
1492
|
+
CHECK_DARK_LIGHT_SCRIPT
|
|
1493
|
+
]).join(""));
|
|
1494
|
+
if (helmet?.htmlAttributes) {
|
|
1495
|
+
html = html.replace(HTML_START_TAG, `${HTML_START_TAG} ${helmet?.htmlAttributes?.toString()}`);
|
|
1496
|
+
}
|
|
1497
|
+
if (helmet?.bodyAttributes) {
|
|
1498
|
+
html = html.replace(BODY_START_TAG, `${BODY_START_TAG} ${helmet?.bodyAttributes?.toString()}`);
|
|
1499
|
+
}
|
|
1500
|
+
const normalizeHtmlFilePath = (path10) => {
|
|
1501
|
+
const normalizedBase = normalizeSlash(config?.base || "/");
|
|
1502
|
+
if (path10.endsWith("/")) {
|
|
1503
|
+
return `${path10}index.html`.replace(normalizedBase, "");
|
|
1504
|
+
}
|
|
1505
|
+
return `${path10}.html`.replace(normalizedBase, "");
|
|
1506
|
+
};
|
|
1507
|
+
const fileName = normalizeHtmlFilePath(routePath);
|
|
1508
|
+
await fs11.ensureDir(join8(outputPath, dirname2(fileName)));
|
|
1509
|
+
await fs11.writeFile(join8(outputPath, fileName), html);
|
|
1510
|
+
}));
|
|
1511
|
+
await fs11.remove(join8(outputPath, "ssr"));
|
|
1512
|
+
await fs11.remove(join8(outputPath, "html"));
|
|
1513
|
+
const totalTime = Date.now() - startTime;
|
|
1514
|
+
logger.success(`Pages rendered in ${chalk2.yellow(totalTime)} ms.`);
|
|
1515
|
+
} catch (e) {
|
|
1516
|
+
logger.error(`Pages render error: ${e.stack}`);
|
|
1517
|
+
throw e;
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
async function build(options) {
|
|
1521
|
+
const { docDirectory, appDirectory, config } = options;
|
|
1522
|
+
const pluginDriver = new PluginDriver(config, true);
|
|
1523
|
+
await pluginDriver.init();
|
|
1524
|
+
const modifiedConfig = await pluginDriver.modifyConfig();
|
|
1525
|
+
await pluginDriver.beforeBuild();
|
|
1526
|
+
await fs10.emptyDir(TEMP_DIR);
|
|
1527
|
+
await bundle(appDirectory, docDirectory, modifiedConfig, pluginDriver);
|
|
1528
|
+
await renderPages(appDirectory, modifiedConfig, pluginDriver);
|
|
1529
|
+
await pluginDriver.afterBuild();
|
|
1530
|
+
}
|
|
1531
|
+
|
|
1532
|
+
// src/node/serve.ts
|
|
1533
|
+
import sirv2 from "sirv";
|
|
1534
|
+
import compression from "compression";
|
|
1535
|
+
import polka from "polka";
|
|
1536
|
+
async function serve(options) {
|
|
1537
|
+
const { config, port: userPort, host: userHost } = options;
|
|
1538
|
+
const envPort = process.env.PORT;
|
|
1539
|
+
const envHost = process.env.HOST;
|
|
1540
|
+
const port = envPort || userPort || 4173;
|
|
1541
|
+
const host = envHost || userHost || "localhost";
|
|
1542
|
+
const base = config?.base?.replace(/^\//, "").replace(/\/$/, "") || "";
|
|
1543
|
+
const compress = compression();
|
|
1544
|
+
const serve2 = sirv2(config?.outDir || OUTPUT_DIR, {
|
|
1545
|
+
etag: true,
|
|
1546
|
+
maxAge: 31536e3,
|
|
1547
|
+
immutable: true
|
|
1548
|
+
});
|
|
1549
|
+
if (base) {
|
|
1550
|
+
polka().use(base, compress, serve2).listen(port, host, (err) => {
|
|
1551
|
+
if (err) {
|
|
1552
|
+
throw err;
|
|
1553
|
+
}
|
|
1554
|
+
console.log(`Preview server running at http://${host}:${port}/${base}/
|
|
1555
|
+
`);
|
|
1556
|
+
});
|
|
1557
|
+
} else {
|
|
1558
|
+
polka().use(compress, serve2).listen(port, host, (err) => {
|
|
1559
|
+
if (err) {
|
|
1560
|
+
throw err;
|
|
1561
|
+
}
|
|
1562
|
+
console.log(`Preview server running at http://${host}:${port}/
|
|
1563
|
+
`);
|
|
1564
|
+
});
|
|
1565
|
+
}
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
// src/index.ts
|
|
1569
|
+
export * from "@rspress/shared";
|
|
1570
|
+
export {
|
|
1571
|
+
build,
|
|
1572
|
+
dev,
|
|
1573
|
+
serve
|
|
1574
|
+
};
|
|
1575
|
+
|
|
1576
|
+
//# sourceMappingURL=index.js.map
|