@rspress/core 2.0.0-beta.14 → 2.0.0-beta.16
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/index.d.ts +18 -4
- package/dist/index.js +263 -249
- package/dist/loader.d.ts +18 -4
- package/dist/processor.d.ts +18 -4
- package/dist/processor.js +24 -24
- package/dist/runtime/App.js +5 -5
- package/dist/runtime/initPageData.js +12 -12
- package/package.json +14 -16
package/dist/index.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
1
|
import __rslib_shim_module__ from 'module';
|
|
2
2
|
const require = /*#__PURE__*/ __rslib_shim_module__.createRequire(import.meta.url);
|
|
3
3
|
import { pluginContainerSyntax } from "@rspress/plugin-container-syntax";
|
|
4
|
-
import
|
|
5
|
-
import
|
|
4
|
+
import node_fs_promises, { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
5
|
+
import node_path, { extname as external_node_path_extname, join } from "node:path";
|
|
6
6
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
7
7
|
import { PLUGIN_REACT_NAME, pluginReact } from "@rsbuild/plugin-react";
|
|
8
8
|
import { APPEARANCE_KEY, MDX_OR_MD_REGEXP, RSPRESS_TEMP_DIR, SEARCH_INDEX_NAME, addLeadingSlash, addTrailingSlash, isDebugMode, isExternalUrl, isSCM, normalizeHref, normalizeSlash, parseUrl, removeLeadingSlash, removeTrailingSlash, slash, withBase, withoutBase } from "@rspress/shared";
|
|
9
9
|
import { logger } from "@rspress/shared/logger";
|
|
10
|
-
import
|
|
10
|
+
import picocolors from "picocolors";
|
|
11
11
|
import { DEFAULT_PAGE_EXTENSIONS } from "@rspress/shared/constants";
|
|
12
12
|
import { glob } from "tinyglobby";
|
|
13
|
-
import
|
|
13
|
+
import node_os from "node:os";
|
|
14
14
|
import { RspackVirtualModulePlugin } from "rspack-plugin-virtual-module";
|
|
15
15
|
import { getIconUrlPath, getNodeAttribute, loadFrontMatter, mergeDocConfig } from "@rspress/shared/node-utils";
|
|
16
16
|
import { groupBy } from "lodash-es";
|
|
17
|
-
import { createHash } from "node:crypto";
|
|
18
17
|
import { compile } from "@rspress/mdx-rs";
|
|
19
18
|
import { htmlToText } from "html-to-text";
|
|
20
|
-
import
|
|
19
|
+
import node_fs, { createReadStream, existsSync } from "node:fs";
|
|
21
20
|
import { createProcessor } from "@mdx-js/mdx";
|
|
22
|
-
import
|
|
21
|
+
import enhanced_resolve from "enhanced-resolve";
|
|
22
|
+
import { createHash } from "node:crypto";
|
|
23
23
|
import { createHead, transformHtmlTemplate } from "@unhead/react/server";
|
|
24
24
|
import { mergeRsbuildConfig as core_mergeRsbuildConfig } from "@rsbuild/core";
|
|
25
25
|
import { visit } from "unist-util-visit";
|
|
@@ -179,7 +179,7 @@ const pluginVirtualModule = (pluginOptions = {})=>({
|
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
181
|
});
|
|
182
|
-
var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.
|
|
182
|
+
var package_namespaceObject = JSON.parse('{"i8":"2.0.0-beta.16"}');
|
|
183
183
|
const RSPRESS_VERSION = package_namespaceObject.i8;
|
|
184
184
|
const isProduction = ()=>'production' === process.env.NODE_ENV;
|
|
185
185
|
const importStatementRegex = /import\s+(.*?)\s+from\s+(['"])(.*?)(?:"|');?/gm;
|
|
@@ -191,29 +191,31 @@ const inlineThemeScript = `{
|
|
|
191
191
|
document.documentElement.classList.toggle('rp-dark', isDark)
|
|
192
192
|
document.documentElement.style.colorScheme = isDark ? 'dark' : 'light'
|
|
193
193
|
}`.replace(/\n/g, ';').replace(/\s{2,}/g, '');
|
|
194
|
-
const dirname =
|
|
195
|
-
const PACKAGE_ROOT =
|
|
196
|
-
const TEMPLATE_PATH =
|
|
197
|
-
const CSR_CLIENT_ENTRY =
|
|
198
|
-
const SSR_CLIENT_ENTRY =
|
|
199
|
-
const SSR_SERVER_ENTRY =
|
|
194
|
+
const dirname = node_path.dirname(fileURLToPath(new URL(import.meta.url)));
|
|
195
|
+
const PACKAGE_ROOT = node_path.join(dirname, '..');
|
|
196
|
+
const TEMPLATE_PATH = node_path.join(PACKAGE_ROOT, 'index.html');
|
|
197
|
+
const CSR_CLIENT_ENTRY = node_path.join(PACKAGE_ROOT, 'dist', 'runtime', 'csrClientEntry.js');
|
|
198
|
+
const SSR_CLIENT_ENTRY = node_path.join(PACKAGE_ROOT, 'dist', 'runtime', 'ssrClientEntry.js');
|
|
199
|
+
const SSR_SERVER_ENTRY = node_path.join(PACKAGE_ROOT, 'dist', 'runtime', 'ssrServerEntry.js');
|
|
200
200
|
const OUTPUT_DIR = 'doc_build';
|
|
201
201
|
const APP_HTML_MARKER = '<!--<?- DOC_CONTENT ?>-->';
|
|
202
202
|
const HEAD_MARKER = '<!--<?- HEAD ?>-->';
|
|
203
203
|
const META_GENERATOR = '<!--<?- GENERATOR ?>-->';
|
|
204
204
|
const DEFAULT_TITLE = 'Rspress';
|
|
205
205
|
const PUBLIC_DIR = 'public';
|
|
206
|
-
const constants_TEMP_DIR =
|
|
206
|
+
const constants_TEMP_DIR = node_path.join(process.cwd(), 'node_modules', RSPRESS_TEMP_DIR);
|
|
207
|
+
const NODE_SSG_BUNDLE_FOLDER = '__ssg__';
|
|
208
|
+
const NODE_SSG_BUNDLE_NAME = 'rspress-ssg-entry.cjs';
|
|
207
209
|
async function pathExists(path) {
|
|
208
210
|
try {
|
|
209
|
-
await
|
|
211
|
+
await node_fs_promises.access(path);
|
|
210
212
|
return true;
|
|
211
213
|
} catch {
|
|
212
214
|
return false;
|
|
213
215
|
}
|
|
214
216
|
}
|
|
215
217
|
async function readJson(path) {
|
|
216
|
-
const raw = await
|
|
218
|
+
const raw = await node_fs_promises.readFile(path, 'utf8');
|
|
217
219
|
return JSON.parse(raw);
|
|
218
220
|
}
|
|
219
221
|
const THEME_DEFAULT_EXPORT_PATTERN = /export\s+default\s+\{/;
|
|
@@ -235,13 +237,13 @@ async function hintThemeBreakingChange(customThemeDir) {
|
|
|
235
237
|
break;
|
|
236
238
|
}
|
|
237
239
|
}
|
|
238
|
-
if (useDefaultExportFilePath) logger.warn(`[Rspress v2] Breaking Change: The "theme/index.tsx" is now using named export instead of default export, please update ${
|
|
240
|
+
if (useDefaultExportFilePath) logger.warn(`[Rspress v2] Breaking Change: The "theme/index.tsx" is now using named export instead of default export, please update ${picocolors.greenBright(useDefaultExportFilePath)} (https://github.com/web-infra-dev/rspress/discussions/1891#discussioncomment-12422737).\n`, picocolors.redBright(`
|
|
239
241
|
- import Theme from '@rspress/theme-default';
|
|
240
242
|
- export default {
|
|
241
243
|
- ...Theme,
|
|
242
244
|
- Layout,
|
|
243
245
|
- };
|
|
244
|
-
- export * from 'rspress/theme';`) +
|
|
246
|
+
- export * from 'rspress/theme';`) + picocolors.greenBright(`
|
|
245
247
|
+ import { Layout } from '@rspress/theme-default';
|
|
246
248
|
|
|
247
249
|
+ export { Layout };
|
|
@@ -259,17 +261,26 @@ function hintSSGFalse() {
|
|
|
259
261
|
function hintReactVersion() {
|
|
260
262
|
logger.info('[Rspress v2] Rspress support React 18 and 19, please confirm that both react and react-dom are installed in package.json with the same version. ');
|
|
261
263
|
}
|
|
262
|
-
const isWindows = 'win32' ===
|
|
264
|
+
const isWindows = 'win32' === node_os.platform();
|
|
263
265
|
function normalizePath_slash(p) {
|
|
264
266
|
return p.replace(/\\/g, '/');
|
|
265
267
|
}
|
|
266
268
|
function normalizePath(id) {
|
|
267
|
-
return
|
|
269
|
+
return node_path.posix.normalize(isWindows ? normalizePath_slash(id) : id);
|
|
268
270
|
}
|
|
269
271
|
function getPageKey(route) {
|
|
270
272
|
const cleanRoute = removeLeadingSlash(route);
|
|
271
273
|
return cleanRoute.replace(/\//g, '_').replace(/\.[^.]+$/, '') || 'index';
|
|
272
274
|
}
|
|
275
|
+
class RoutePage {
|
|
276
|
+
routeMeta;
|
|
277
|
+
static create(routeMeta) {
|
|
278
|
+
return new RoutePage(routeMeta);
|
|
279
|
+
}
|
|
280
|
+
constructor(routeMeta){
|
|
281
|
+
this.routeMeta = routeMeta;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
273
284
|
const getRoutePathParts = (routePath, lang, version, langs, versions)=>{
|
|
274
285
|
const hasTrailSlash = routePath.endsWith('/');
|
|
275
286
|
let versionPart = '';
|
|
@@ -324,6 +335,13 @@ class RouteService {
|
|
|
324
335
|
#base = '';
|
|
325
336
|
#tempDir = '';
|
|
326
337
|
#pluginDriver;
|
|
338
|
+
static async create(options) {
|
|
339
|
+
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
340
|
+
const routeService = new RouteService(scanDir, config, runtimeTempDir, pluginDriver);
|
|
341
|
+
await routeService.#init();
|
|
342
|
+
await pluginDriver.routeServiceGenerated(routeService);
|
|
343
|
+
return routeService;
|
|
344
|
+
}
|
|
327
345
|
constructor(scanDir, userConfig, tempDir, pluginDriver){
|
|
328
346
|
const routeOptions = userConfig?.route || {};
|
|
329
347
|
this.#scanDir = scanDir;
|
|
@@ -343,7 +361,7 @@ class RouteService {
|
|
|
343
361
|
get extensions() {
|
|
344
362
|
return this.#extensions;
|
|
345
363
|
}
|
|
346
|
-
async init() {
|
|
364
|
+
async #init() {
|
|
347
365
|
const extensions = this.#extensions.map((i)=>i.slice(1));
|
|
348
366
|
const files = (await glob([
|
|
349
367
|
`**/*.{${extensions.join(',')}}`,
|
|
@@ -361,10 +379,10 @@ class RouteService {
|
|
|
361
379
|
]
|
|
362
380
|
})).sort();
|
|
363
381
|
files.forEach((filePath)=>{
|
|
364
|
-
const fileRelativePath = normalizePath(
|
|
382
|
+
const fileRelativePath = normalizePath(node_path.relative(this.#scanDir, filePath));
|
|
365
383
|
const { routePath, lang, version } = this.normalizeRoutePath(fileRelativePath);
|
|
366
|
-
const absolutePath =
|
|
367
|
-
const
|
|
384
|
+
const absolutePath = node_path.join(this.#scanDir, fileRelativePath);
|
|
385
|
+
const routeMeta = {
|
|
368
386
|
routePath,
|
|
369
387
|
absolutePath: normalizePath(absolutePath),
|
|
370
388
|
relativePath: fileRelativePath,
|
|
@@ -372,47 +390,45 @@ class RouteService {
|
|
|
372
390
|
lang,
|
|
373
391
|
version
|
|
374
392
|
};
|
|
375
|
-
this.addRoute(
|
|
393
|
+
this.addRoute(routeMeta);
|
|
376
394
|
});
|
|
377
395
|
const externalPages = await this.#pluginDriver.addPages();
|
|
378
396
|
await Promise.all(externalPages.map(async (route, index)=>{
|
|
379
397
|
const { routePath, content, filepath } = route;
|
|
380
398
|
if (filepath) {
|
|
381
|
-
const
|
|
382
|
-
this.addRoute(
|
|
399
|
+
const routeMeta = this.#generateRouteMeta(routePath, filepath);
|
|
400
|
+
this.addRoute(routeMeta);
|
|
383
401
|
return;
|
|
384
402
|
}
|
|
385
403
|
if (content) {
|
|
386
404
|
const filepath = await this.#writeTempFile(index, content);
|
|
387
|
-
const
|
|
388
|
-
this.addRoute(
|
|
405
|
+
const routeMeta = this.#generateRouteMeta(routePath, filepath);
|
|
406
|
+
this.addRoute(routeMeta);
|
|
389
407
|
}
|
|
390
408
|
}));
|
|
391
409
|
await this.#pluginDriver.routeGenerated(this.getRoutes());
|
|
392
410
|
}
|
|
393
|
-
addRoute(
|
|
394
|
-
const { routePath
|
|
395
|
-
if (this.routeData.has(routePath)) {
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
if (-1 !== currRouteExtIndex && (currRouteExtIndex < preRouteExtIndex || -1 === preRouteExtIndex)) this.routeData.set(routePath, routeInfo);
|
|
399
|
-
} else this.routeData.set(routePath, routeInfo);
|
|
411
|
+
async addRoute(routeMeta) {
|
|
412
|
+
const { routePath } = routeMeta;
|
|
413
|
+
if (this.routeData.has(routePath)) throw new Error(`routePath ${routePath} has already been added`);
|
|
414
|
+
const routePage = RoutePage.create(routeMeta);
|
|
415
|
+
this.routeData.set(routePath, routePage);
|
|
400
416
|
}
|
|
401
417
|
removeRoute(filePath) {
|
|
402
|
-
const fileRelativePath =
|
|
418
|
+
const fileRelativePath = node_path.relative(this.#scanDir, filePath);
|
|
403
419
|
const { routePath } = this.normalizeRoutePath(fileRelativePath);
|
|
404
420
|
this.routeData.delete(routePath);
|
|
405
421
|
}
|
|
406
422
|
getRoutes() {
|
|
423
|
+
return Array.from(this.routeData.values()).map((i)=>i.routeMeta);
|
|
424
|
+
}
|
|
425
|
+
getRoutePages() {
|
|
407
426
|
return Array.from(this.routeData.values());
|
|
408
427
|
}
|
|
409
428
|
isExistRoute(routePath) {
|
|
410
429
|
const { routePath: normalizedRoute } = this.normalizeRoutePath(routePath);
|
|
411
430
|
return Boolean(this.routeData.get(normalizedRoute));
|
|
412
431
|
}
|
|
413
|
-
isEmpty() {
|
|
414
|
-
return 0 === this.routeData.size;
|
|
415
|
-
}
|
|
416
432
|
generateRoutesCode() {
|
|
417
433
|
return this.generateRoutesCodeByRouteMeta(this.getRoutes());
|
|
418
434
|
}
|
|
@@ -440,48 +456,30 @@ ${routeMeta.map((route, index)=>{
|
|
|
440
456
|
return normalizeRoutePath(routePath, this.#base, this.#defaultLang, this.#defaultVersion, this.#langs, this.#versions, this.#extensions);
|
|
441
457
|
}
|
|
442
458
|
async #writeTempFile(index, content) {
|
|
443
|
-
const tempFilePath =
|
|
444
|
-
await
|
|
459
|
+
const tempFilePath = node_path.join(this.#tempDir, `temp-${index}.mdx`);
|
|
460
|
+
await node_fs_promises.writeFile(tempFilePath, content);
|
|
445
461
|
return tempFilePath;
|
|
446
462
|
}
|
|
447
|
-
#
|
|
463
|
+
#generateRouteMeta(routePath, filepath) {
|
|
448
464
|
const { routePath: normalizedPath, lang, version } = this.normalizeRoutePath(routePath);
|
|
449
465
|
return {
|
|
450
466
|
routePath: normalizedPath,
|
|
451
467
|
absolutePath: normalizePath(filepath),
|
|
452
|
-
relativePath: normalizePath(
|
|
468
|
+
relativePath: normalizePath(node_path.relative(this.#scanDir, filepath)),
|
|
453
469
|
pageName: getPageKey(routePath),
|
|
454
470
|
lang,
|
|
455
471
|
version
|
|
456
472
|
};
|
|
457
473
|
}
|
|
474
|
+
getRoutePageByRoutePath(routePath) {
|
|
475
|
+
return this.routeData.get(routePath);
|
|
476
|
+
}
|
|
458
477
|
}
|
|
459
|
-
let init_routeService = null;
|
|
460
|
-
async function initRouteService(options) {
|
|
461
|
-
const { scanDir, config, runtimeTempDir, pluginDriver } = options;
|
|
462
|
-
init_routeService = new RouteService(scanDir, config, runtimeTempDir, pluginDriver);
|
|
463
|
-
await init_routeService.init();
|
|
464
|
-
await pluginDriver.routeServiceGenerated(init_routeService);
|
|
465
|
-
return init_routeService;
|
|
466
|
-
}
|
|
467
|
-
function createHash_createHash(str) {
|
|
468
|
-
return createHash('sha256').update(str).digest('hex').slice(0, 8);
|
|
469
|
-
}
|
|
470
|
-
var types_RuntimeModuleID = /*#__PURE__*/ function(RuntimeModuleID) {
|
|
471
|
-
RuntimeModuleID["GlobalStyles"] = "virtual-global-styles";
|
|
472
|
-
RuntimeModuleID["GlobalComponents"] = "virtual-global-components";
|
|
473
|
-
RuntimeModuleID["Routes"] = "virtual-routes";
|
|
474
|
-
RuntimeModuleID["SiteData"] = "virtual-site-data";
|
|
475
|
-
RuntimeModuleID["SearchIndexHash"] = "virtual-search-index-hash";
|
|
476
|
-
RuntimeModuleID["I18nText"] = "virtual-i18n-text";
|
|
477
|
-
RuntimeModuleID["SearchHooks"] = "virtual-search-hooks";
|
|
478
|
-
return RuntimeModuleID;
|
|
479
|
-
}({});
|
|
480
478
|
let flattenMdxContent_resolver;
|
|
481
479
|
let startFlatten = false;
|
|
482
480
|
const processor = createProcessor();
|
|
483
|
-
const { CachedInputFileSystem, ResolverFactory } =
|
|
484
|
-
const fileSystem =
|
|
481
|
+
const { CachedInputFileSystem, ResolverFactory } = enhanced_resolve;
|
|
482
|
+
const fileSystem = node_fs;
|
|
485
483
|
async function resolveDepPath(importPath, importer, alias) {
|
|
486
484
|
if (!flattenMdxContent_resolver) flattenMdxContent_resolver = ResolverFactory.createResolver({
|
|
487
485
|
fileSystem: new CachedInputFileSystem(fileSystem, 0),
|
|
@@ -538,12 +536,12 @@ async function flattenMdxContent(content, basePath, alias) {
|
|
|
538
536
|
const importPath = importNode.source.value;
|
|
539
537
|
let absoluteImportPath;
|
|
540
538
|
try {
|
|
541
|
-
absoluteImportPath = await resolveDepPath(importPath,
|
|
539
|
+
absoluteImportPath = await resolveDepPath(importPath, node_path.dirname(basePath), alias);
|
|
542
540
|
} catch (_e) {
|
|
543
541
|
continue;
|
|
544
542
|
}
|
|
545
543
|
if (MDX_OR_MD_REGEXP.test(absoluteImportPath)) {
|
|
546
|
-
const importedContent =
|
|
544
|
+
const importedContent = node_fs.readFileSync(absoluteImportPath, 'utf-8');
|
|
547
545
|
const { flattenContent: replacedValue, deps: subDeps } = await flattenMdxContent(importedContent, absoluteImportPath, alias);
|
|
548
546
|
result = result.replace(new RegExp(`import\\s+${id}\\s+from\\s+['"](${importPath})['"];?`), '').replace(new RegExp(`<${id}\\s*/>`, 'g'), ()=>replacedValue);
|
|
549
547
|
deps.push(...subDeps, absoluteImportPath);
|
|
@@ -564,105 +562,119 @@ function applyReplaceRulesToNestedObject(obj, replaceRules) {
|
|
|
564
562
|
else if ('object' == typeof obj[key] && null !== obj[key]) obj[key] = applyReplaceRulesToNestedObject(obj[key], replaceRules);
|
|
565
563
|
return obj;
|
|
566
564
|
}
|
|
567
|
-
async function
|
|
568
|
-
const
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
565
|
+
async function getPageIndexInfoByRoute(route, options) {
|
|
566
|
+
const { alias, domain, replaceRules, root, searchCodeBlocks } = options;
|
|
567
|
+
const defaultIndexInfo = {
|
|
568
|
+
title: '',
|
|
569
|
+
content: '',
|
|
570
|
+
_html: '',
|
|
571
|
+
_flattenContent: '',
|
|
572
|
+
routePath: route.routePath,
|
|
573
|
+
lang: route.lang,
|
|
574
|
+
toc: [],
|
|
575
|
+
domain,
|
|
576
|
+
frontmatter: {},
|
|
577
|
+
version: route.version,
|
|
578
|
+
_filepath: route.absolutePath,
|
|
579
|
+
_relativePath: node_path.relative(root, route.absolutePath).split(node_path.sep).join('/')
|
|
580
|
+
};
|
|
581
|
+
if (!MDX_OR_MD_REGEXP.test(route.absolutePath)) return defaultIndexInfo;
|
|
582
|
+
let content = await node_fs_promises.readFile(route.absolutePath, 'utf8');
|
|
583
|
+
const { frontmatter, content: contentWithoutFrontMatter } = loadFrontMatter(content, route.absolutePath, root);
|
|
584
|
+
applyReplaceRulesToNestedObject(frontmatter, replaceRules);
|
|
585
|
+
const { flattenContent } = await flattenMdxContent(applyReplaceRules(contentWithoutFrontMatter, replaceRules), route.absolutePath, alias);
|
|
586
|
+
content = flattenContent.replace(importStatementRegex, '');
|
|
587
|
+
const { html: rawHtml, title, toc: rawToc } = await compile({
|
|
588
|
+
value: content,
|
|
589
|
+
filepath: route.absolutePath,
|
|
590
|
+
development: 'production' !== process.env.NODE_ENV,
|
|
591
|
+
root
|
|
592
|
+
});
|
|
593
|
+
function encodeHtml(html) {
|
|
594
|
+
return html.replace(/<code>([\s\S]*?)<\/\s?code>/gm, function(_match, innerContent) {
|
|
595
|
+
return `<code>${innerContent.replace(/&/g, '&').replace(/"/g, '"').replace(/'/g, ''').replace(/</g, '<').replace(/>/g, '>')}</code>`;
|
|
595
596
|
});
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
597
|
+
}
|
|
598
|
+
const html = encodeHtml(String(rawHtml));
|
|
599
|
+
content = htmlToText(html, {
|
|
600
|
+
wordwrap: 80,
|
|
601
|
+
selectors: [
|
|
602
|
+
{
|
|
603
|
+
selector: 'a',
|
|
604
|
+
options: {
|
|
605
|
+
ignoreHref: true
|
|
606
|
+
}
|
|
607
|
+
},
|
|
608
|
+
{
|
|
609
|
+
selector: 'img',
|
|
610
|
+
format: 'skip'
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
selector: 'pre > code',
|
|
614
|
+
format: searchCodeBlocks ? 'block' : 'skip'
|
|
615
|
+
},
|
|
616
|
+
...[
|
|
617
|
+
'h1',
|
|
618
|
+
'h2',
|
|
619
|
+
'h3',
|
|
620
|
+
'h4',
|
|
621
|
+
'h5',
|
|
622
|
+
'h6'
|
|
623
|
+
].map((tag)=>({
|
|
624
|
+
selector: tag,
|
|
607
625
|
options: {
|
|
608
|
-
|
|
626
|
+
uppercase: false
|
|
609
627
|
}
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
'h6'
|
|
626
|
-
].map((tag)=>({
|
|
627
|
-
selector: tag,
|
|
628
|
-
options: {
|
|
629
|
-
uppercase: false
|
|
630
|
-
}
|
|
631
|
-
}))
|
|
632
|
-
],
|
|
633
|
-
tables: true,
|
|
634
|
-
longWordSplit: {
|
|
635
|
-
forceWrapOnLimit: true
|
|
636
|
-
}
|
|
637
|
-
});
|
|
638
|
-
if (content.startsWith(title)) content = content.slice(title.length);
|
|
639
|
-
const toc = rawToc.map((item)=>{
|
|
640
|
-
const match = item.id.match(/-(\d+)$/);
|
|
641
|
-
let position = -1;
|
|
642
|
-
if (match) for(let i = 0; i < Number(match[1]); i++){
|
|
643
|
-
position = content.indexOf(`\n${item.text}#\n\n`, position + 1);
|
|
644
|
-
if (-1 === position) break;
|
|
645
|
-
}
|
|
646
|
-
return {
|
|
647
|
-
...item,
|
|
648
|
-
charIndex: content.indexOf(`\n${item.text}#\n\n`, position + 1)
|
|
649
|
-
};
|
|
650
|
-
});
|
|
628
|
+
}))
|
|
629
|
+
],
|
|
630
|
+
tables: true,
|
|
631
|
+
longWordSplit: {
|
|
632
|
+
forceWrapOnLimit: true
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
if (content.startsWith(title)) content = content.slice(title.length);
|
|
636
|
+
const toc = rawToc.map((item)=>{
|
|
637
|
+
const match = item.id.match(/-(\d+)$/);
|
|
638
|
+
let position = -1;
|
|
639
|
+
if (match) for(let i = 0; i < Number(match[1]); i++){
|
|
640
|
+
position = content.indexOf(`\n${item.text}#\n\n`, position + 1);
|
|
641
|
+
if (-1 === position) break;
|
|
642
|
+
}
|
|
651
643
|
return {
|
|
652
|
-
...
|
|
653
|
-
|
|
654
|
-
toc,
|
|
655
|
-
content,
|
|
656
|
-
_flattenContent: flattenContent,
|
|
657
|
-
_html: html,
|
|
658
|
-
frontmatter: {
|
|
659
|
-
...frontmatter,
|
|
660
|
-
__content: void 0
|
|
661
|
-
}
|
|
644
|
+
...item,
|
|
645
|
+
charIndex: content.indexOf(`\n${item.text}#\n\n`, position + 1)
|
|
662
646
|
};
|
|
663
|
-
})
|
|
664
|
-
return
|
|
647
|
+
});
|
|
648
|
+
return {
|
|
649
|
+
...defaultIndexInfo,
|
|
650
|
+
title: frontmatter.title || title,
|
|
651
|
+
toc,
|
|
652
|
+
content,
|
|
653
|
+
_html: html,
|
|
654
|
+
_flattenContent: flattenContent,
|
|
655
|
+
frontmatter: {
|
|
656
|
+
...frontmatter,
|
|
657
|
+
__content: void 0
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
async function extractPageData(routeService, options) {
|
|
662
|
+
const pageData = await Promise.all(routeService.getRoutes().map((routeMeta)=>getPageIndexInfoByRoute(routeMeta, options)));
|
|
663
|
+
return pageData;
|
|
664
|
+
}
|
|
665
|
+
function createHash_createHash(str) {
|
|
666
|
+
return createHash('sha256').update(str).digest('hex').slice(0, 8);
|
|
665
667
|
}
|
|
668
|
+
var types_RuntimeModuleID = /*#__PURE__*/ function(RuntimeModuleID) {
|
|
669
|
+
RuntimeModuleID["GlobalStyles"] = "virtual-global-styles";
|
|
670
|
+
RuntimeModuleID["GlobalComponents"] = "virtual-global-components";
|
|
671
|
+
RuntimeModuleID["Routes"] = "virtual-routes";
|
|
672
|
+
RuntimeModuleID["SiteData"] = "virtual-site-data";
|
|
673
|
+
RuntimeModuleID["SearchIndexHash"] = "virtual-search-index-hash";
|
|
674
|
+
RuntimeModuleID["I18nText"] = "virtual-i18n-text";
|
|
675
|
+
RuntimeModuleID["SearchHooks"] = "virtual-search-hooks";
|
|
676
|
+
return RuntimeModuleID;
|
|
677
|
+
}({});
|
|
666
678
|
const DEFAULT_I18N_SOURCE = join(process.cwd(), 'i18n.json');
|
|
667
679
|
function getI18nData(docConfig) {
|
|
668
680
|
const { i18nSourcePath = DEFAULT_I18N_SOURCE } = docConfig;
|
|
@@ -727,7 +739,7 @@ function normalizeThemeConfig(docConfig, pages = []) {
|
|
|
727
739
|
return {
|
|
728
740
|
text: applyReplaceRules(page?.title || '', replaceRules),
|
|
729
741
|
link: normalizedItem,
|
|
730
|
-
_fileKey: page?._relativePath.replace(
|
|
742
|
+
_fileKey: page?._relativePath.replace(node_path.extname(page._relativePath), '')
|
|
731
743
|
};
|
|
732
744
|
}
|
|
733
745
|
return {
|
|
@@ -805,7 +817,13 @@ async function siteDataVMPlugin(context) {
|
|
|
805
817
|
const searchConfig = userConfig?.search || {};
|
|
806
818
|
const domain = searchConfig?.mode === 'remote' ? searchConfig.domain ?? '' : '';
|
|
807
819
|
const searchCodeBlocks = 'codeBlocks' in searchConfig ? Boolean(searchConfig.codeBlocks) : true;
|
|
808
|
-
const pages = await extractPageData(
|
|
820
|
+
const pages = await extractPageData(routeService, {
|
|
821
|
+
replaceRules,
|
|
822
|
+
alias,
|
|
823
|
+
domain,
|
|
824
|
+
root: userDocRoot,
|
|
825
|
+
searchCodeBlocks
|
|
826
|
+
});
|
|
809
827
|
await pluginDriver.modifySearchIndexData(pages);
|
|
810
828
|
const versioned = userConfig.search && 'remote' !== userConfig.search.mode && userConfig.search.versioned;
|
|
811
829
|
const groupedPages = groupBy(pages, (page)=>{
|
|
@@ -823,10 +841,10 @@ async function siteDataVMPlugin(context) {
|
|
|
823
841
|
const [version, lang] = group.split('###');
|
|
824
842
|
const indexVersion = version ? `.${version.replace('.', '_')}` : '';
|
|
825
843
|
const indexLang = lang ? `.${lang}` : '';
|
|
826
|
-
await
|
|
844
|
+
await node_fs_promises.mkdir(constants_TEMP_DIR, {
|
|
827
845
|
recursive: true
|
|
828
846
|
});
|
|
829
|
-
await
|
|
847
|
+
await node_fs_promises.writeFile(node_path.join(constants_TEMP_DIR, `${SEARCH_INDEX_NAME}${indexVersion}${indexLang}.${indexHash}.json`), stringifiedIndex);
|
|
830
848
|
}));
|
|
831
849
|
await Promise.all(pages.map(async (pageData)=>pluginDriver.extendPageData(pageData)));
|
|
832
850
|
const siteData = {
|
|
@@ -849,7 +867,7 @@ async function siteDataVMPlugin(context) {
|
|
|
849
867
|
mode: 'local'
|
|
850
868
|
},
|
|
851
869
|
pages: pages.map((page)=>{
|
|
852
|
-
const { content,
|
|
870
|
+
const { content, domain, _filepath, _html, _flattenContent, ...rest } = page;
|
|
853
871
|
return isProduction() ? rest : {
|
|
854
872
|
...rest,
|
|
855
873
|
_filepath
|
|
@@ -870,7 +888,6 @@ const runtimeModuleFactory = [
|
|
|
870
888
|
siteDataVMPlugin
|
|
871
889
|
];
|
|
872
890
|
function rsbuildPluginDocVM(factoryContext) {
|
|
873
|
-
const { pluginDriver } = factoryContext;
|
|
874
891
|
return {
|
|
875
892
|
name: 'rsbuild-plugin-doc-vm',
|
|
876
893
|
setup (api) {
|
|
@@ -884,16 +901,20 @@ function rsbuildPluginDocVM(factoryContext) {
|
|
|
884
901
|
});
|
|
885
902
|
Object.assign(runtimeModule, moduleResult);
|
|
886
903
|
}
|
|
887
|
-
const modulesByPlugin = await pluginDriver.addRuntimeModules();
|
|
888
|
-
Object.keys(modulesByPlugin).forEach((key)=>{
|
|
889
|
-
if (runtimeModule[key]) throw new Error(`The runtime module ${key} is duplicated, please check your plugin`);
|
|
890
|
-
runtimeModule[key] = modulesByPlugin[key];
|
|
891
|
-
});
|
|
892
904
|
bundlerChain.plugin('rspress-runtime-module').use(new RspackVirtualModulePlugin(runtimeModule));
|
|
893
905
|
});
|
|
894
906
|
}
|
|
895
907
|
};
|
|
896
908
|
}
|
|
909
|
+
async function getVirtualModulesFromPlugins(pluginDriver) {
|
|
910
|
+
const runtimeModule = {};
|
|
911
|
+
const modulesByPlugin = await pluginDriver.addRuntimeModules();
|
|
912
|
+
Object.keys(modulesByPlugin).forEach((key)=>{
|
|
913
|
+
if (runtimeModule[key]) throw new Error(`The runtime module ${key} is duplicated, please check your plugin`);
|
|
914
|
+
runtimeModule[key] = ()=>modulesByPlugin[key];
|
|
915
|
+
});
|
|
916
|
+
return runtimeModule;
|
|
917
|
+
}
|
|
897
918
|
const globalStylesVMPlugin = (context)=>({
|
|
898
919
|
[types_RuntimeModuleID.GlobalStyles]: ()=>{
|
|
899
920
|
const { config, pluginDriver } = context;
|
|
@@ -946,10 +967,10 @@ const searchHookVMPlugin = (context)=>({
|
|
|
946
967
|
async function writeSearchIndex(config) {
|
|
947
968
|
if (config?.search === false) return;
|
|
948
969
|
const cwd = process.cwd();
|
|
949
|
-
const searchIndexFiles = await
|
|
970
|
+
const searchIndexFiles = await node_fs_promises.readdir(constants_TEMP_DIR);
|
|
950
971
|
const outDir = config?.outDir ?? join(cwd, OUTPUT_DIR);
|
|
951
972
|
const targetDir = join(outDir, 'static');
|
|
952
|
-
await
|
|
973
|
+
await node_fs_promises.mkdir(targetDir, {
|
|
953
974
|
recursive: true
|
|
954
975
|
});
|
|
955
976
|
let searchIndexData = '[]';
|
|
@@ -958,9 +979,9 @@ async function writeSearchIndex(config) {
|
|
|
958
979
|
if (!searchIndexFile.includes(SEARCH_INDEX_NAME) || !searchIndexFile.endsWith('.json')) continue;
|
|
959
980
|
const source = join(constants_TEMP_DIR, searchIndexFile);
|
|
960
981
|
const target = join(targetDir, searchIndexFile);
|
|
961
|
-
const searchIndex = await
|
|
982
|
+
const searchIndex = await node_fs_promises.readFile(join(constants_TEMP_DIR, searchIndexFile), 'utf-8');
|
|
962
983
|
searchIndexData = `${searchIndexData.slice(0, -1)}${scanning ? ',' : ''}${searchIndex.slice(1)}`;
|
|
963
|
-
await
|
|
984
|
+
await node_fs_promises.rename(source, target);
|
|
964
985
|
scanning = true;
|
|
965
986
|
}
|
|
966
987
|
if (isProduction() && isSCM() && config?.search?.mode === 'remote') {
|
|
@@ -973,9 +994,9 @@ async function writeSearchIndex(config) {
|
|
|
973
994
|
'Content-Type': 'application/json'
|
|
974
995
|
}
|
|
975
996
|
});
|
|
976
|
-
logger.info(
|
|
997
|
+
logger.info(picocolors.green(`[doc-tools] Search index uploaded to ${apiUrl}, indexName: ${indexName}`));
|
|
977
998
|
} catch (e) {
|
|
978
|
-
logger.info(
|
|
999
|
+
logger.info(picocolors.red(`[doc-tools] Upload search index \`${indexName}\` failed:\n ${e}`));
|
|
979
1000
|
}
|
|
980
1001
|
}
|
|
981
1002
|
}
|
|
@@ -1083,15 +1104,6 @@ async function pMap(iterable, mapper, { concurrency = Number.POSITIVE_INFINITY,
|
|
|
1083
1104
|
});
|
|
1084
1105
|
}
|
|
1085
1106
|
const pMapSkip = Symbol('skip');
|
|
1086
|
-
async function renderFrontmatterHead(route) {
|
|
1087
|
-
if (!isRouteMeta(route)) return '';
|
|
1088
|
-
const content = await promises_default.readFile(route.absolutePath, {
|
|
1089
|
-
encoding: 'utf-8'
|
|
1090
|
-
});
|
|
1091
|
-
const { frontmatter: { head } } = loadFrontMatter(content, route.absolutePath, '', true);
|
|
1092
|
-
if (!head || 0 === head.length) return '';
|
|
1093
|
-
return head.map(([tag, attrs])=>`<${tag} ${renderAttrs(attrs)}>`).join('');
|
|
1094
|
-
}
|
|
1095
1107
|
async function renderConfigHead(config, route) {
|
|
1096
1108
|
if (!isRouteMeta(route)) return '';
|
|
1097
1109
|
if (!config.head || 0 === config.head.length) return '';
|
|
@@ -1125,7 +1137,7 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1125
1137
|
render = await ssrExports.render;
|
|
1126
1138
|
} catch (e) {
|
|
1127
1139
|
if (e instanceof Error) {
|
|
1128
|
-
logger.error(`Failed to load SSG bundle: ${
|
|
1140
|
+
logger.error(`Failed to load SSG bundle: ${picocolors.yellow(ssrBundlePath)}: ${e.message}`);
|
|
1129
1141
|
logger.debug(e);
|
|
1130
1142
|
hintSSGFailed();
|
|
1131
1143
|
}
|
|
@@ -1154,13 +1166,12 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1154
1166
|
({ appHtml } = await render(routePath, head));
|
|
1155
1167
|
} catch (e) {
|
|
1156
1168
|
if (e instanceof Error) {
|
|
1157
|
-
logger.error(`Page "${
|
|
1169
|
+
logger.error(`Page "${picocolors.yellow(routePath)}" SSG rendering failed.\n ${picocolors.gray(e.toString())}`);
|
|
1158
1170
|
throw e;
|
|
1159
1171
|
}
|
|
1160
1172
|
}
|
|
1161
1173
|
const replacedHtmlTemplate = htmlTemplate.replace(/<title>(.*?)<\/title>/gi, '').replace(APP_HTML_MARKER, ()=>appHtml).replace(META_GENERATOR, ()=>`<meta name="generator" content="Rspress v${RSPRESS_VERSION}">`).replace(HEAD_MARKER, [
|
|
1162
|
-
await renderConfigHead(config, route)
|
|
1163
|
-
await renderFrontmatterHead(route)
|
|
1174
|
+
await renderConfigHead(config, route)
|
|
1164
1175
|
].join(''));
|
|
1165
1176
|
const html = await transformHtmlTemplate(head, replacedHtmlTemplate);
|
|
1166
1177
|
const normalizeHtmlFilePath = (path)=>{
|
|
@@ -1174,7 +1185,7 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1174
1185
|
concurrency: process.env.RSPRESS_SSG_CONCURRENCY ? Number.parseInt(process.env.RSPRESS_SSG_CONCURRENCY, 10) : 32
|
|
1175
1186
|
});
|
|
1176
1187
|
const totalTime = Date.now() - startTime;
|
|
1177
|
-
logger.success(`Pages rendered in ${
|
|
1188
|
+
logger.success(`Pages rendered in ${picocolors.yellow(totalTime)} ms.`);
|
|
1178
1189
|
} catch (e) {
|
|
1179
1190
|
if (e instanceof Error) {
|
|
1180
1191
|
logger.error(`Pages render error: ${e.message}`);
|
|
@@ -1184,7 +1195,6 @@ async function renderPages(routeService, config, pluginDriver, ssrBundlePath, ht
|
|
|
1184
1195
|
throw e;
|
|
1185
1196
|
}
|
|
1186
1197
|
}
|
|
1187
|
-
const NODE_SSR_BUNDLE_NAME = 'rspress-ssg-entry.cjs';
|
|
1188
1198
|
const rsbuildPluginSSG = ({ routeService, config, pluginDriver })=>({
|
|
1189
1199
|
name: 'rspress-inner-rsbuild-plugin-ssg',
|
|
1190
1200
|
async setup (api) {
|
|
@@ -1196,7 +1206,8 @@ const rsbuildPluginSSG = ({ routeService, config, pluginDriver })=>({
|
|
|
1196
1206
|
targets: [
|
|
1197
1207
|
'web'
|
|
1198
1208
|
]
|
|
1199
|
-
}, ({ assets, compilation })=>{
|
|
1209
|
+
}, ({ assets, compilation, environment })=>{
|
|
1210
|
+
if ('web' !== environment.name) return;
|
|
1200
1211
|
for (const [assetName, assetSource] of Object.entries(assets))if ('index.html' === assetName) {
|
|
1201
1212
|
htmlTemplate = assetSource.source().toString();
|
|
1202
1213
|
compilation.deleteAsset(assetName);
|
|
@@ -1217,33 +1228,38 @@ const rsbuildPluginSSG = ({ routeService, config, pluginDriver })=>({
|
|
|
1217
1228
|
'node'
|
|
1218
1229
|
]
|
|
1219
1230
|
}, async ({ assets, compilation, environment, compiler })=>{
|
|
1231
|
+
if ('node' !== environment.name) return;
|
|
1220
1232
|
const distPath = environment.distPath;
|
|
1221
|
-
|
|
1233
|
+
const ssgFolderPath = join(distPath, NODE_SSG_BUNDLE_FOLDER);
|
|
1234
|
+
const mainCjsAbsolutePath = join(ssgFolderPath, NODE_SSG_BUNDLE_NAME);
|
|
1235
|
+
await mkdir(ssgFolderPath, {
|
|
1222
1236
|
recursive: true
|
|
1223
1237
|
});
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
}
|
|
1238
|
+
await Promise.all(Object.entries(assets).map(async ([assetName, assetSource])=>{
|
|
1239
|
+
if (assetName.startsWith(`${NODE_SSG_BUNDLE_FOLDER}/`)) {
|
|
1240
|
+
const fileAbsolutePath = join(distPath, assetName);
|
|
1241
|
+
await writeFile(fileAbsolutePath, assetSource.source().toString());
|
|
1242
|
+
compilation.deleteAsset(assetName);
|
|
1243
|
+
}
|
|
1244
|
+
}));
|
|
1231
1245
|
const emitAsset = (assetName, content)=>{
|
|
1232
1246
|
compilation.emitAsset(assetName, new compiler.webpack.sources.RawSource(content));
|
|
1233
1247
|
};
|
|
1234
1248
|
await indexHtmlEmittedInWeb;
|
|
1235
1249
|
await renderPages(routeService, config, pluginDriver, mainCjsAbsolutePath, htmlTemplate, emitAsset);
|
|
1236
|
-
if (!isDebugMode()) await rm(
|
|
1250
|
+
if (!isDebugMode()) await rm(ssgFolderPath, {
|
|
1251
|
+
recursive: true
|
|
1252
|
+
});
|
|
1237
1253
|
});
|
|
1238
1254
|
});
|
|
1239
1255
|
}
|
|
1240
1256
|
});
|
|
1241
|
-
const { CachedInputFileSystem: detectReactVersion_CachedInputFileSystem, ResolverFactory: detectReactVersion_ResolverFactory } =
|
|
1257
|
+
const { CachedInputFileSystem: detectReactVersion_CachedInputFileSystem, ResolverFactory: detectReactVersion_ResolverFactory } = enhanced_resolve;
|
|
1242
1258
|
async function detectPackageMajorVersion(name) {
|
|
1243
1259
|
const cwd = process.cwd();
|
|
1244
|
-
const pkgPath =
|
|
1260
|
+
const pkgPath = node_path.join(cwd, 'node_modules', name);
|
|
1245
1261
|
if (await pathExists(pkgPath)) {
|
|
1246
|
-
const pkgJson = await readJson(
|
|
1262
|
+
const pkgJson = await readJson(node_path.join(pkgPath, 'package.json'));
|
|
1247
1263
|
const version = Number(pkgJson.version?.split('.')[0]);
|
|
1248
1264
|
return version;
|
|
1249
1265
|
}
|
|
@@ -1255,7 +1271,7 @@ async function detectReactVersion() {
|
|
|
1255
1271
|
async function resolveReactRouterDomAlias() {
|
|
1256
1272
|
const alias = {};
|
|
1257
1273
|
const resolver = detectReactVersion_ResolverFactory.createResolver({
|
|
1258
|
-
fileSystem: new detectReactVersion_CachedInputFileSystem(
|
|
1274
|
+
fileSystem: new detectReactVersion_CachedInputFileSystem(node_fs, 0),
|
|
1259
1275
|
mainFields: [
|
|
1260
1276
|
'browser',
|
|
1261
1277
|
'module',
|
|
@@ -1295,7 +1311,7 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
1295
1311
|
];
|
|
1296
1312
|
const alias = {};
|
|
1297
1313
|
const resolver = detectReactVersion_ResolverFactory.createResolver({
|
|
1298
|
-
fileSystem: new detectReactVersion_CachedInputFileSystem(
|
|
1314
|
+
fileSystem: new detectReactVersion_CachedInputFileSystem(node_fs, 0),
|
|
1299
1315
|
extensions: [
|
|
1300
1316
|
'.js'
|
|
1301
1317
|
],
|
|
@@ -1319,7 +1335,7 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
1319
1335
|
});
|
|
1320
1336
|
} catch (e) {
|
|
1321
1337
|
if (e instanceof Error) {
|
|
1322
|
-
logger.warn(`${lib} not found: \n ${
|
|
1338
|
+
logger.warn(`${lib} not found: \n ${picocolors.gray(e.toString())}`);
|
|
1323
1339
|
hintReactVersion();
|
|
1324
1340
|
}
|
|
1325
1341
|
}
|
|
@@ -1327,15 +1343,15 @@ async function resolveReactAlias(reactVersion, isSSR) {
|
|
|
1327
1343
|
return alias;
|
|
1328
1344
|
}
|
|
1329
1345
|
const detectCustomIcon = async (customThemeDir)=>{
|
|
1330
|
-
const assetsDir =
|
|
1346
|
+
const assetsDir = node_path.join(customThemeDir, 'assets');
|
|
1331
1347
|
const alias = {};
|
|
1332
1348
|
if (!existsSync(assetsDir)) return alias;
|
|
1333
1349
|
const files = await glob('*.svg', {
|
|
1334
1350
|
cwd: assetsDir
|
|
1335
1351
|
});
|
|
1336
1352
|
files.forEach((file)=>{
|
|
1337
|
-
const name =
|
|
1338
|
-
alias[`@theme-assets/${name}`] =
|
|
1353
|
+
const name = node_path.basename(file, '.svg');
|
|
1354
|
+
alias[`@theme-assets/${name}`] = node_path.join(assetsDir, file);
|
|
1339
1355
|
});
|
|
1340
1356
|
return alias;
|
|
1341
1357
|
};
|
|
@@ -1373,7 +1389,7 @@ function isPluginIncluded(config, pluginName) {
|
|
|
1373
1389
|
}
|
|
1374
1390
|
async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeService, pluginDriver) {
|
|
1375
1391
|
const cwd = process.cwd();
|
|
1376
|
-
const CUSTOM_THEME_DIR = config?.themeDir ??
|
|
1392
|
+
const CUSTOM_THEME_DIR = config?.themeDir ?? node_path.join(process.cwd(), 'theme');
|
|
1377
1393
|
const outDir = config?.outDir ?? OUTPUT_DIR;
|
|
1378
1394
|
const DEFAULT_THEME = require.resolve('@rspress/theme-default');
|
|
1379
1395
|
const base = config?.base ?? '';
|
|
@@ -1383,7 +1399,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1383
1399
|
if (!icon) return;
|
|
1384
1400
|
icon = icon.toString();
|
|
1385
1401
|
if (icon.startsWith('file://')) icon = fileURLToPath(icon);
|
|
1386
|
-
else if (
|
|
1402
|
+
else if (node_path.isAbsolute(icon)) return node_path.join(userDocRoot, PUBLIC_DIR, icon);
|
|
1387
1403
|
return icon;
|
|
1388
1404
|
};
|
|
1389
1405
|
await hintThemeBreakingChange(CUSTOM_THEME_DIR);
|
|
@@ -1412,7 +1428,8 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1412
1428
|
...globalUIComponentsVMPlugin(context),
|
|
1413
1429
|
...globalStylesVMPlugin(context),
|
|
1414
1430
|
...searchHookVMPlugin(context),
|
|
1415
|
-
...routeListVMPlugin(context)
|
|
1431
|
+
...routeListVMPlugin(context),
|
|
1432
|
+
...await getVirtualModulesFromPlugins(pluginDriver)
|
|
1416
1433
|
}
|
|
1417
1434
|
}),
|
|
1418
1435
|
...enableSSG ? [
|
|
@@ -1427,7 +1444,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1427
1444
|
port: !isProduction() && process.env.PORT ? Number(process.env.PORT) : void 0,
|
|
1428
1445
|
printUrls: ({ urls })=>urls.map((url)=>`${url}/${removeLeadingSlash(base)}`),
|
|
1429
1446
|
publicDir: {
|
|
1430
|
-
name:
|
|
1447
|
+
name: node_path.join(userDocRoot, PUBLIC_DIR)
|
|
1431
1448
|
}
|
|
1432
1449
|
},
|
|
1433
1450
|
dev: {
|
|
@@ -1468,7 +1485,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1468
1485
|
CUSTOM_THEME_DIR,
|
|
1469
1486
|
DEFAULT_THEME
|
|
1470
1487
|
],
|
|
1471
|
-
'@theme-assets':
|
|
1488
|
+
'@theme-assets': node_path.join(DEFAULT_THEME, '../assets'),
|
|
1472
1489
|
'@rspress/core': PACKAGE_ROOT,
|
|
1473
1490
|
'react-lazy-with-preload': require.resolve('react-lazy-with-preload')
|
|
1474
1491
|
}
|
|
@@ -1476,7 +1493,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1476
1493
|
source: {
|
|
1477
1494
|
include: [
|
|
1478
1495
|
PACKAGE_ROOT,
|
|
1479
|
-
|
|
1496
|
+
node_path.join(cwd, 'node_modules', RSPRESS_TEMP_DIR)
|
|
1480
1497
|
],
|
|
1481
1498
|
define: {
|
|
1482
1499
|
'process.env.TEST': JSON.stringify(process.env.TEST),
|
|
@@ -1526,7 +1543,11 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1526
1543
|
chain.module.rule('css-virtual-module').test(/\.rspress[\\/]runtime[\\/]virtual-global-styles/).merge({
|
|
1527
1544
|
sideEffects: true
|
|
1528
1545
|
});
|
|
1529
|
-
if (isServer)
|
|
1546
|
+
if (isServer) {
|
|
1547
|
+
chain.output.filename(`${NODE_SSG_BUNDLE_FOLDER}/${NODE_SSG_BUNDLE_NAME}`);
|
|
1548
|
+
chain.output.chunkFilename(`${NODE_SSG_BUNDLE_FOLDER}/[name].cjs`);
|
|
1549
|
+
chain.target('async-node');
|
|
1550
|
+
}
|
|
1530
1551
|
}
|
|
1531
1552
|
},
|
|
1532
1553
|
environments: {
|
|
@@ -1542,7 +1563,7 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1542
1563
|
index: enableSSG && isProduction() ? SSR_CLIENT_ENTRY : CSR_CLIENT_ENTRY
|
|
1543
1564
|
},
|
|
1544
1565
|
preEntry: [
|
|
1545
|
-
|
|
1566
|
+
node_path.join(DEFAULT_THEME, '../styles/index.js'),
|
|
1546
1567
|
'virtual-global-styles'
|
|
1547
1568
|
],
|
|
1548
1569
|
define: {
|
|
@@ -1564,13 +1585,6 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1564
1585
|
...reactRouterDomAlias
|
|
1565
1586
|
}
|
|
1566
1587
|
},
|
|
1567
|
-
tools: {
|
|
1568
|
-
rspack: {
|
|
1569
|
-
output: {
|
|
1570
|
-
asyncChunks: false
|
|
1571
|
-
}
|
|
1572
|
-
}
|
|
1573
|
-
},
|
|
1574
1588
|
source: {
|
|
1575
1589
|
entry: {
|
|
1576
1590
|
index: SSR_SERVER_ENTRY
|
|
@@ -1596,14 +1610,14 @@ async function createInternalBuildConfig(userDocRoot, config, enableSSG, routeSe
|
|
|
1596
1610
|
}
|
|
1597
1611
|
async function initRsbuild(rootDir, config, pluginDriver, enableSSG, extraRsbuildConfig) {
|
|
1598
1612
|
const cwd = process.cwd();
|
|
1599
|
-
const userDocRoot =
|
|
1613
|
+
const userDocRoot = node_path.resolve(rootDir || config?.root || cwd);
|
|
1600
1614
|
const builderPlugins = config?.builderPlugins ?? [];
|
|
1601
|
-
const runtimeTempDir =
|
|
1602
|
-
const runtimeAbsTempDir =
|
|
1603
|
-
await
|
|
1615
|
+
const runtimeTempDir = node_path.join(RSPRESS_TEMP_DIR, 'runtime');
|
|
1616
|
+
const runtimeAbsTempDir = node_path.join(cwd, 'node_modules', runtimeTempDir);
|
|
1617
|
+
await node_fs_promises.mkdir(runtimeAbsTempDir, {
|
|
1604
1618
|
recursive: true
|
|
1605
1619
|
});
|
|
1606
|
-
const routeService = await
|
|
1620
|
+
const routeService = await RouteService.create({
|
|
1607
1621
|
config,
|
|
1608
1622
|
runtimeTempDir: runtimeAbsTempDir,
|
|
1609
1623
|
scanDir: userDocRoot,
|
|
@@ -1619,34 +1633,34 @@ async function initRsbuild(rootDir, config, pluginDriver, enableSSG, extraRsbuil
|
|
|
1619
1633
|
return rsbuild;
|
|
1620
1634
|
}
|
|
1621
1635
|
function checkLanguageParity_normalizePath(filePath) {
|
|
1622
|
-
return filePath.split(
|
|
1636
|
+
return filePath.split(node_path.sep).join('/');
|
|
1623
1637
|
}
|
|
1624
1638
|
async function getAllMarkdownFilesFrom(dirPath) {
|
|
1625
|
-
const files = await
|
|
1639
|
+
const files = await node_fs_promises.readdir(dirPath, {
|
|
1626
1640
|
withFileTypes: true
|
|
1627
1641
|
});
|
|
1628
1642
|
const allFiles = [];
|
|
1629
1643
|
for (const file of files){
|
|
1630
|
-
const fullPath =
|
|
1644
|
+
const fullPath = node_path.join(dirPath, file.name);
|
|
1631
1645
|
if (file.isDirectory()) {
|
|
1632
1646
|
const nestedFiles = await getAllMarkdownFilesFrom(fullPath);
|
|
1633
1647
|
allFiles.push(...nestedFiles);
|
|
1634
1648
|
} else if ([
|
|
1635
1649
|
'.md',
|
|
1636
1650
|
'.mdx'
|
|
1637
|
-
].includes(
|
|
1651
|
+
].includes(node_path.extname(file.name))) allFiles.push(fullPath);
|
|
1638
1652
|
}
|
|
1639
1653
|
return allFiles;
|
|
1640
1654
|
}
|
|
1641
1655
|
async function collectModuleFiles(contentPath, lang, includedDir, excludedDirs, fileLangMap) {
|
|
1642
|
-
const langDirPath =
|
|
1643
|
-
const langModuleDir =
|
|
1656
|
+
const langDirPath = node_path.join(contentPath, lang);
|
|
1657
|
+
const langModuleDir = node_path.join(langDirPath, includedDir);
|
|
1644
1658
|
try {
|
|
1645
|
-
const moduleDirStats = await
|
|
1659
|
+
const moduleDirStats = await node_fs_promises.stat(langModuleDir);
|
|
1646
1660
|
if (!moduleDirStats.isDirectory()) return fileLangMap;
|
|
1647
1661
|
const files = await getAllMarkdownFilesFrom(langModuleDir);
|
|
1648
1662
|
for (const file of files){
|
|
1649
|
-
const relativePath = checkLanguageParity_normalizePath(
|
|
1663
|
+
const relativePath = checkLanguageParity_normalizePath(node_path.relative(langDirPath, file));
|
|
1650
1664
|
if (excludedDirs.some((excludedDir)=>{
|
|
1651
1665
|
const isFilePath = /\.(md|mdx)$/.test(excludedDir);
|
|
1652
1666
|
return relativePath.includes(excludedDir + (isFilePath ? '' : '/'));
|
|
@@ -1664,7 +1678,7 @@ async function collectModuleFiles(contentPath, lang, includedDir, excludedDirs,
|
|
|
1664
1678
|
async function checkLanguageParity(config) {
|
|
1665
1679
|
if (!config?.languageParity || false === config.languageParity.enabled) return;
|
|
1666
1680
|
logger.info('Checking language parity...');
|
|
1667
|
-
const contentPath =
|
|
1681
|
+
const contentPath = node_path.resolve(config.root);
|
|
1668
1682
|
const includedDirs = config.languageParity.include?.length ? config.languageParity.include : [
|
|
1669
1683
|
''
|
|
1670
1684
|
];
|
|
@@ -1680,7 +1694,7 @@ async function checkLanguageParity(config) {
|
|
|
1680
1694
|
for (const [baseName, langs] of Object.entries(curFileLangMap)){
|
|
1681
1695
|
const missingLangs = supportedLanguages.filter((lang)=>!langs.has(lang));
|
|
1682
1696
|
missingLangs.forEach((lang)=>{
|
|
1683
|
-
const missingPath =
|
|
1697
|
+
const missingPath = node_path.join(lang, baseName);
|
|
1684
1698
|
missingLanguagesFile.push(missingPath);
|
|
1685
1699
|
});
|
|
1686
1700
|
}
|
|
@@ -1723,7 +1737,7 @@ async function bundle(docDirectory, config, pluginDriver, enableSSG) {
|
|
|
1723
1737
|
}
|
|
1724
1738
|
}
|
|
1725
1739
|
function emptyDir(path) {
|
|
1726
|
-
return
|
|
1740
|
+
return node_fs_promises.rm(path, {
|
|
1727
1741
|
force: true,
|
|
1728
1742
|
recursive: true
|
|
1729
1743
|
});
|
|
@@ -1793,9 +1807,9 @@ function normalizeLink(nodeUrl, routeService, relativePath, cleanUrls) {
|
|
|
1793
1807
|
if (nodeUrl.startsWith('#')) return `#${nodeUrl.slice(1)}`;
|
|
1794
1808
|
let { url, hash } = parseUrl(nodeUrl);
|
|
1795
1809
|
if (isExternalUrl(url)) return url + (hash ? `#${hash}` : '');
|
|
1796
|
-
const extname =
|
|
1810
|
+
const extname = node_path.extname(url);
|
|
1797
1811
|
if ((routeService?.extensions ?? DEFAULT_PAGE_EXTENSIONS).includes(extname)) url = url.replace(new RegExp(`\\${extname}$`), '');
|
|
1798
|
-
if (url.startsWith('.')) url =
|
|
1812
|
+
if (url.startsWith('.')) url = node_path.posix.join(slash(node_path.dirname(relativePath)), url);
|
|
1799
1813
|
else if (routeService) {
|
|
1800
1814
|
const [pathVersion, pathLang] = routeService.getRoutePathParts(slash(relativePath));
|
|
1801
1815
|
const [urlVersion, urlLang, urlPath] = routeService.getRoutePathParts(url);
|
|
@@ -1819,12 +1833,12 @@ const remarkPluginNormalizeLink = ({ root, cleanUrls, routeService })=>(tree, fi
|
|
|
1819
1833
|
const images = [];
|
|
1820
1834
|
visit(tree, 'link', (node)=>{
|
|
1821
1835
|
const { url: nodeUrl } = node;
|
|
1822
|
-
const relativePath =
|
|
1836
|
+
const relativePath = node_path.relative(root, file.path);
|
|
1823
1837
|
node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
|
|
1824
1838
|
});
|
|
1825
1839
|
visit(tree, 'definition', (node)=>{
|
|
1826
1840
|
const { url: nodeUrl } = node;
|
|
1827
|
-
const relativePath =
|
|
1841
|
+
const relativePath = node_path.relative(root, file.path);
|
|
1828
1842
|
node.url = normalizeLink(nodeUrl, routeService, relativePath, cleanUrls);
|
|
1829
1843
|
});
|
|
1830
1844
|
const getMdxSrcAttribute = (tempVar)=>({
|