@rspress/core 0.0.13 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/dist/index.js +90 -50
  2. package/dist/index.js.map +1 -1
  3. package/dist/loader.d.ts +3 -0
  4. package/dist/loader.js +28 -72
  5. package/dist/loader.js.map +1 -1
  6. package/dist/runtime/App.d.ts +8 -0
  7. package/dist/runtime/App.js +1 -0
  8. package/dist/runtime/Content.d.ts +6 -0
  9. package/dist/runtime/Content.js +7 -2
  10. package/dist/runtime/NoSSR.d.ts +4 -0
  11. package/dist/runtime/clientEntry.d.ts +1 -0
  12. package/dist/runtime/hooks.d.ts +29 -0
  13. package/dist/runtime/hooks.js +23 -1
  14. package/dist/runtime/index.d.ts +6 -0
  15. package/dist/runtime/ssrEntry.d.ts +6 -0
  16. package/dist/runtime/tsconfig.json +9 -0
  17. package/dist/runtime/utils.d.ts +6 -0
  18. package/dist/theme/bundle.css +94 -53
  19. package/dist/theme/bundle.d.ts +12 -1
  20. package/dist/theme/bundle.js +527 -489
  21. package/dist/theme/bundle.js.map +1 -1
  22. package/package.json +14 -14
  23. package/src/runtime/App.tsx +1 -0
  24. package/src/runtime/Content.tsx +11 -2
  25. package/src/runtime/global.d.ts +49 -0
  26. package/src/runtime/hooks.ts +47 -1
  27. package/src/runtime/tsconfig.json +9 -0
  28. package/src/theme-default/components/LocalSideBar/{index.module.scss → index.scss} +6 -5
  29. package/src/theme-default/components/LocalSideBar/index.tsx +4 -4
  30. package/src/theme-default/components/Nav/index.tsx +19 -5
  31. package/src/theme-default/components/Search/SearchPanel.tsx +339 -0
  32. package/src/theme-default/components/Search/index.tsx +6 -326
  33. package/src/theme-default/index.ts +3 -1
  34. package/src/theme-default/layout/DocLayout/docComponents/link.tsx +8 -23
  35. package/src/theme-default/layout/DocLayout/index.module.scss +52 -3
  36. package/src/theme-default/layout/DocLayout/index.tsx +9 -7
  37. package/src/theme-default/logic/index.ts +1 -0
  38. package/src/theme-default/logic/useAppearance.ts +0 -1
  39. package/src/theme-default/logic/usePathUtils.ts +72 -0
package/dist/index.js CHANGED
@@ -115,7 +115,7 @@ import { withBase, MDX_REGEXP as MDX_REGEXP2, SEARCH_INDEX_NAME, addLeadingSlash
115
115
  import yamlFrontMatter from "yaml-front-matter";
116
116
  import { htmlToText } from "html-to-text";
117
117
  import fs3 from "@modern-js/utils/fs-extra";
118
- import { compile } from "@modern-js/mdx-rs-binding";
118
+ import { compile } from "@rspress/mdx-rs";
119
119
 
120
120
  // src/node/utils/applyReplaceRules.ts
121
121
  function applyReplaceRules(code = "", replaceRules = []) {
@@ -519,17 +519,18 @@ ${item.text}#
519
519
  ...frontmatter,
520
520
  __content: void 0
521
521
  },
522
+ version: route.version,
522
523
  _filepath: route.absolutePath,
523
524
  _relativePath: path5.relative(root, route.absolutePath)
524
525
  };
525
526
  }));
526
527
  }
527
528
  async function siteDataVMPlugin(context) {
528
- const { config, alias, userRoot, routeService: routeService2, pluginDriver } = context;
529
+ const { config, alias, userDocRoot, routeService: routeService2, pluginDriver } = context;
529
530
  const userConfig = config;
530
531
  const replaceRules = userConfig?.replaceRules || [];
531
532
  const domain = userConfig?.search && userConfig?.search?.mode === "remote" ? userConfig?.search.domain ?? "" : "";
532
- const pages = (await extractPageData(replaceRules, alias, domain, userRoot, routeService2)).filter(Boolean);
533
+ const pages = (await extractPageData(replaceRules, alias, domain, userDocRoot, routeService2)).filter(Boolean);
533
534
  await pluginDriver.modifySearchIndexData(pages);
534
535
  const pagesByLang = pages.reduce((acc, page) => {
535
536
  if (!acc[page.lang]) {
@@ -557,7 +558,12 @@ async function siteDataVMPlugin(context) {
557
558
  themeConfig: normalizeThemeConfig(userConfig, pages),
558
559
  base: userConfig?.base || "/",
559
560
  lang: userConfig?.lang || "",
561
+ locales: userConfig?.locales || userConfig.themeConfig?.locales || [],
560
562
  logo: userConfig?.logo || "",
563
+ multiVersion: {
564
+ default: userConfig?.multiVersion?.default || "",
565
+ versions: userConfig?.multiVersion?.versions || []
566
+ },
561
567
  search: userConfig?.search ?? {
562
568
  mode: "local"
563
569
  },
@@ -849,14 +855,38 @@ var DEFAULT_PAGE_EXTENSIONS = [
849
855
  var addLeadingSlash3 = (str) => {
850
856
  return str.startsWith("/") ? str : `/${str}`;
851
857
  };
852
- var normalizeRoutePath = (routePath, lang, base) => {
853
- let normalizedRoutePath = routePath.replace(/^\//, "").replace(new RegExp(`^${lang}`), "").replace(/\.[^.]+$/, "");
854
- normalizedRoutePath = addLeadingSlash3(normalizedRoutePath).replace(/\/index$/, "/");
855
- return withBase2(normalizedRoutePath, base);
858
+ var normalizeRoutePath = (routePath, lang, base, version) => {
859
+ let versionPart = "";
860
+ let langPart = "";
861
+ let purePathPart = "";
862
+ const parts = routePath.split("/").filter(Boolean);
863
+ if (version) {
864
+ const versionToMatch = parts.shift();
865
+ if (versionToMatch !== version) {
866
+ versionPart = versionToMatch;
867
+ }
868
+ }
869
+ if (lang) {
870
+ const langToMatch = parts.shift();
871
+ if (langToMatch !== lang) {
872
+ langPart = langToMatch;
873
+ }
874
+ }
875
+ purePathPart = parts.join("/");
876
+ const normalizedRoutePath = addLeadingSlash3([
877
+ versionPart,
878
+ langPart,
879
+ purePathPart
880
+ ].filter(Boolean).join("/")).replace(/\.[^.]+$/, "").replace(/\/index$/, "/");
881
+ return {
882
+ routePath: withBase2(normalizedRoutePath, base),
883
+ lang: langPart || lang,
884
+ version: versionPart || version
885
+ };
856
886
  };
857
887
  var _scanDir = /* @__PURE__ */ new WeakMap();
858
888
  var _defaultLang = /* @__PURE__ */ new WeakMap();
859
- var _langs = /* @__PURE__ */ new WeakMap();
889
+ var _defaultVersion = /* @__PURE__ */ new WeakMap();
860
890
  var _extensions = /* @__PURE__ */ new WeakMap();
861
891
  var _include = /* @__PURE__ */ new WeakMap();
862
892
  var _exclude = /* @__PURE__ */ new WeakMap();
@@ -864,7 +894,6 @@ var _base = /* @__PURE__ */ new WeakMap();
864
894
  var _tempDir = /* @__PURE__ */ new WeakMap();
865
895
  var _pluginDriver = /* @__PURE__ */ new WeakMap();
866
896
  var _writeTempFile = /* @__PURE__ */ new WeakSet();
867
- var _getLang = /* @__PURE__ */ new WeakSet();
868
897
  var _generateRouteInfo = /* @__PURE__ */ new WeakSet();
869
898
  var RouteService = class {
870
899
  async init() {
@@ -884,15 +913,15 @@ var RouteService = class {
884
913
  }).sort();
885
914
  files.forEach((filePath) => {
886
915
  const fileRelativePath = normalizePath(path7.relative(_class_private_field_get(this, _scanDir), filePath));
887
- const lang = _class_private_method_get(this, _getLang, getLang).call(this, fileRelativePath);
888
- const routePath = normalizeRoutePath(fileRelativePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base));
916
+ const { routePath, lang, version } = normalizeRoutePath(fileRelativePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base), _class_private_field_get(this, _defaultVersion));
889
917
  const absolutePath = path7.join(_class_private_field_get(this, _scanDir), fileRelativePath);
890
918
  const routeInfo = {
891
919
  routePath,
892
920
  absolutePath: normalizePath(absolutePath),
893
921
  relativePath: fileRelativePath,
894
922
  pageName: getPageKey(fileRelativePath),
895
- lang
923
+ lang,
924
+ version
896
925
  };
897
926
  this.addRoute(routeInfo);
898
927
  });
@@ -926,41 +955,43 @@ var RouteService = class {
926
955
  }
927
956
  removeRoute(filePath) {
928
957
  const fileRelativePath = path7.relative(_class_private_field_get(this, _scanDir), filePath);
929
- const routePath = normalizeRoutePath(fileRelativePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base));
958
+ const { routePath } = normalizeRoutePath(fileRelativePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base), _class_private_field_get(this, _defaultVersion));
930
959
  this.routeData.delete(routePath);
931
960
  }
932
961
  getRoutes() {
933
962
  return Array.from(this.routeData.values());
934
963
  }
935
964
  isExistRoute(routePath) {
936
- const normalizedRoute = normalizeRoutePath(routePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base));
965
+ const { routePath: normalizedRoute } = normalizeRoutePath(routePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base), _class_private_field_get(this, _defaultVersion));
937
966
  return this.routeData.get(normalizedRoute);
938
967
  }
939
968
  isEmpty() {
940
969
  return this.routeData.size === 0;
941
970
  }
942
- generateRoutesCode(isStaticImport) {
971
+ generateRoutesCode(isStaticImport = false) {
972
+ return this.generateRoutesCodeByRouteMeta(this.getRoutes(), isStaticImport);
973
+ }
974
+ generateRoutesCodeByRouteMeta(routeMeta, isStaticImport) {
943
975
  return `
944
976
  import React from 'react';
945
977
  import { lazyWithPreload } from "react-lazy-with-preload";
946
- ${this.getRoutes().map((route, index) => {
978
+ ${routeMeta.map((route, index) => {
947
979
  return isStaticImport ? `import * as Route${index} from '${route.absolutePath}';` : `const Route${index} = lazyWithPreload(() => import(/* webpackChunkName: "${route.pageName}" */'${route.absolutePath}'))`;
948
980
  }).join("\n")}
949
981
  export const routes = [
950
- ${this.getRoutes().map((route, index) => {
982
+ ${routeMeta.map((route, index) => {
951
983
  const preload = isStaticImport ? `() => Route${index}` : `async () => {
952
984
  await Route${index}.preload();
953
985
  return import("${route.absolutePath}");
954
986
  }`;
955
987
  const component = isStaticImport ? `Route${index}.default` : `Route${index}`;
956
- return `{ path: '${route.routePath}', element: React.createElement(${component}), filePath: '${route.relativePath}', preload: ${preload}, lang: '${route.lang}' }`;
988
+ return `{ path: '${route.routePath}', element: React.createElement(${component}), filePath: '${route.relativePath}', preload: ${preload}, lang: '${route.lang}', version: '${route.version}' }`;
957
989
  }).join(",\n")}
958
990
  ];
959
991
  `;
960
992
  }
961
993
  constructor(scanDir, userConfig, tempDir, pluginDriver) {
962
994
  _class_private_method_init(this, _writeTempFile);
963
- _class_private_method_init(this, _getLang);
964
995
  _class_private_method_init(this, _generateRouteInfo);
965
996
  _define_property(this, "routeData", /* @__PURE__ */ new Map());
966
997
  _class_private_field_init(this, _scanDir, {
@@ -971,9 +1002,9 @@ ${this.getRoutes().map((route, index) => {
971
1002
  writable: true,
972
1003
  value: void 0
973
1004
  });
974
- _class_private_field_init(this, _langs, {
1005
+ _class_private_field_init(this, _defaultVersion, {
975
1006
  writable: true,
976
- value: void 0
1007
+ value: ""
977
1008
  });
978
1009
  _class_private_field_init(this, _extensions, {
979
1010
  writable: true,
@@ -1005,10 +1036,12 @@ ${this.getRoutes().map((route, index) => {
1005
1036
  _class_private_field_set(this, _include, routeOptions.include || []);
1006
1037
  _class_private_field_set(this, _exclude, routeOptions.exclude || []);
1007
1038
  _class_private_field_set(this, _defaultLang, userConfig?.lang || "");
1008
- _class_private_field_set(this, _langs, userConfig?.themeConfig?.locales?.map((locale) => locale.lang) || []);
1009
1039
  _class_private_field_set(this, _base, userConfig?.base || "");
1010
1040
  _class_private_field_set(this, _tempDir, tempDir);
1011
1041
  _class_private_field_set(this, _pluginDriver, pluginDriver);
1042
+ if (userConfig.multiVersion) {
1043
+ _class_private_field_set(this, _defaultVersion, userConfig.multiVersion.default || "");
1044
+ }
1012
1045
  }
1013
1046
  };
1014
1047
  async function writeTempFile(index, content) {
@@ -1016,16 +1049,15 @@ async function writeTempFile(index, content) {
1016
1049
  await fs7.writeFile(tempFilePath, content);
1017
1050
  return tempFilePath;
1018
1051
  }
1019
- function getLang(filepath) {
1020
- return _class_private_field_get(this, _langs).find((lang) => filepath.startsWith(lang)) || _class_private_field_get(this, _defaultLang);
1021
- }
1022
1052
  function generateRouteInfo(routePath, filepath) {
1053
+ const { routePath: normalizedPath, lang, version } = normalizeRoutePath(routePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base), _class_private_field_get(this, _defaultVersion));
1023
1054
  return {
1024
- routePath: normalizeRoutePath(routePath, _class_private_field_get(this, _defaultLang), _class_private_field_get(this, _base)),
1055
+ routePath: normalizedPath,
1025
1056
  absolutePath: normalizePath(filepath),
1026
1057
  relativePath: normalizePath(path7.relative(_class_private_field_get(this, _scanDir), filepath)),
1027
1058
  pageName: getPageKey(routePath),
1028
- lang: _class_private_method_get(this, _getLang, getLang).call(this, filepath)
1059
+ lang,
1060
+ version
1029
1061
  };
1030
1062
  }
1031
1063
 
@@ -1040,7 +1072,7 @@ async function initRouteService(options) {
1040
1072
 
1041
1073
  // src/node/createBuilder.ts
1042
1074
  var require4 = createRequire3(import.meta.url);
1043
- async function createInternalBuildConfig(userRoot, config, isSSR, routeService2) {
1075
+ async function createInternalBuildConfig(userDocRoot, config, isSSR, routeService2, pluginDriver) {
1044
1076
  const cwd = process.cwd();
1045
1077
  const { default: fs10 } = await import("@modern-js/utils/fs-extra");
1046
1078
  const CUSTOM_THEME_DIR = config?.themeDir ?? path8.join(process.cwd(), "theme");
@@ -1049,11 +1081,20 @@ async function createInternalBuildConfig(userRoot, config, isSSR, routeService2)
1049
1081
  const themeDir = await fs10.pathExists(CUSTOM_THEME_DIR) ? CUSTOM_THEME_DIR : DEFAULT_THEME_DIR;
1050
1082
  const checkDeadLinks = (config?.markdown?.checkDeadLinks && !isSSR) ?? false;
1051
1083
  const base = config?.base ?? "";
1052
- const publicDir = path8.join(userRoot, "public");
1084
+ const publicDir = path8.join(userDocRoot, "public");
1053
1085
  const isPublicDirExist = await fs10.pathExists(publicDir);
1054
1086
  const assetPrefix = isProduction() ? removeTrailingSlash(config?.builderConfig?.output?.assetPrefix ?? base) : "";
1055
1087
  const enableMdxRs = config?.markdown?.experimentalMdxRs ?? false;
1056
1088
  const reactVersion = await detectReactVersion();
1089
+ const normalizeIcon = (icon) => {
1090
+ if (!icon) {
1091
+ return void 0;
1092
+ }
1093
+ if (path8.isAbsolute(icon)) {
1094
+ return path8.join(userDocRoot, PUBLIC_DIR, icon);
1095
+ }
1096
+ return icon;
1097
+ };
1057
1098
  const browserslist = {
1058
1099
  web: isProduction() ? [
1059
1100
  "chrome >= 61",
@@ -1076,7 +1117,7 @@ async function createInternalBuildConfig(userRoot, config, isSSR, routeService2)
1076
1117
  progressBar: false
1077
1118
  },
1078
1119
  html: {
1079
- favicon: config?.icon,
1120
+ favicon: normalizeIcon(config?.icon),
1080
1121
  template: path8.join(PACKAGE_ROOT, "index.html")
1081
1122
  },
1082
1123
  output: {
@@ -1113,6 +1154,10 @@ async function createInternalBuildConfig(userRoot, config, isSSR, routeService2)
1113
1154
  "process.env.TEST": JSON.stringify(process.env.TEST)
1114
1155
  }
1115
1156
  },
1157
+ performance: {
1158
+ // No need to print the server bundles size
1159
+ printFileSize: !isSSR
1160
+ },
1116
1161
  tools: {
1117
1162
  devServer: {
1118
1163
  // Serve static files
@@ -1146,10 +1191,11 @@ async function createInternalBuildConfig(userRoot, config, isSSR, routeService2)
1146
1191
  bundlerChain(chain) {
1147
1192
  chain.module.rule("MDX").type("jsx").test(/\.mdx?$/).oneOf("MDXCompile").use("mdx-loader").loader(require4.resolve("../loader.cjs")).options({
1148
1193
  config,
1149
- docDirectory: userRoot,
1194
+ docDirectory: userDocRoot,
1150
1195
  checkDeadLinks,
1151
1196
  enableMdxRs,
1152
- routeService: routeService2
1197
+ routeService: routeService2,
1198
+ pluginDriver
1153
1199
  }).end().use("string-replace-loader").loader(require4.resolve("string-replace-loader")).options({
1154
1200
  multiple: config?.replaceRules || []
1155
1201
  });
@@ -1160,7 +1206,7 @@ async function createInternalBuildConfig(userRoot, config, isSSR, routeService2)
1160
1206
  }
1161
1207
  async function createModernBuilder(rootDir, config, pluginDriver, isSSR = false, extraBuilderConfig) {
1162
1208
  const cwd = process.cwd();
1163
- const userRoot = path8.resolve(rootDir || config?.root || cwd);
1209
+ const userDocRoot = path8.resolve(rootDir || config?.root || cwd);
1164
1210
  const builderPlugins = config?.builderPlugins ?? [];
1165
1211
  const runtimeTempDir = path8.join(RSPRESS_TEMP_DIR2, "runtime");
1166
1212
  const runtimeAbsTempDir = path8.join(cwd, "node_modules", runtimeTempDir);
@@ -1168,12 +1214,12 @@ async function createModernBuilder(rootDir, config, pluginDriver, isSSR = false,
1168
1214
  const routeService2 = await initRouteService({
1169
1215
  config,
1170
1216
  runtimeTempDir: runtimeAbsTempDir,
1171
- scanDir: userRoot,
1217
+ scanDir: userDocRoot,
1172
1218
  pluginDriver
1173
1219
  });
1174
1220
  const { default: { createBuilder, mergeBuilderConfig } } = await import("@modern-js/builder");
1175
1221
  const { default: { builderRspackProvider } } = await import("@modern-js/builder-rspack-provider");
1176
- const internalBuilderConfig = await createInternalBuildConfig(userRoot, config, isSSR, routeService2);
1222
+ const internalBuilderConfig = await createInternalBuildConfig(userDocRoot, config, isSSR, routeService2, pluginDriver);
1177
1223
  const builderProvider = builderRspackProvider({
1178
1224
  builderConfig: mergeBuilderConfig(internalBuilderConfig, ...config?.plugins?.map((plugin) => plugin.builderConfig ?? {}) || [], config?.builderConfig || {}, extraBuilderConfig || {})
1179
1225
  });
@@ -1186,7 +1232,7 @@ async function createModernBuilder(rootDir, config, pluginDriver, isSSR = false,
1186
1232
  });
1187
1233
  builder.addPlugins([
1188
1234
  builderDocVMPlugin({
1189
- userRoot,
1235
+ userDocRoot,
1190
1236
  config,
1191
1237
  isSSR,
1192
1238
  runtimeTempDir,
@@ -1277,6 +1323,9 @@ var PluginDriver = class {
1277
1323
  _class_private_field_get2(this, _plugins).push(plugin);
1278
1324
  }
1279
1325
  }
1326
+ getPlugins() {
1327
+ return _class_private_field_get2(this, _plugins);
1328
+ }
1280
1329
  clearPlugins() {
1281
1330
  _class_private_field_set2(this, _plugins, []);
1282
1331
  }
@@ -1353,6 +1402,7 @@ var PluginDriver = class {
1353
1402
  });
1354
1403
  _class_private_field_set2(this, _config, config);
1355
1404
  _class_private_field_set2(this, _isProd, isProd);
1405
+ _class_private_field_set2(this, _plugins, []);
1356
1406
  }
1357
1407
  };
1358
1408
 
@@ -1386,7 +1436,7 @@ async function dev(options) {
1386
1436
  }
1387
1437
 
1388
1438
  // src/node/build.ts
1389
- import { dirname as dirname2, isAbsolute, join as join3 } from "path";
1439
+ import { dirname as dirname2, join as join3 } from "path";
1390
1440
  import { pathToFileURL } from "url";
1391
1441
  import chalk2 from "@modern-js/utils/chalk";
1392
1442
  import fs9 from "@modern-js/utils/fs-extra";
@@ -1419,20 +1469,10 @@ async function bundle(appDirectory, docDirectory, config, pluginDriver) {
1419
1469
  clientBuilder.build(),
1420
1470
  ssrBuilder.build()
1421
1471
  ]);
1422
- const logoPaths = [];
1423
- const { logo } = config;
1424
- if (typeof logo === "string") {
1425
- logoPaths.push(logo);
1426
- } else if (typeof logo === "object") {
1427
- logoPaths.push(logo.light);
1428
- logoPaths.push(logo.dark);
1472
+ const publicDir = join3(docDirectory, PUBLIC_DIR);
1473
+ if (await fs9.pathExists(publicDir)) {
1474
+ await fs9.copy(publicDir, outputDir);
1429
1475
  }
1430
- logoPaths.filter((p) => p.startsWith("/")).forEach((p) => {
1431
- const normalize = (rawPath) => isAbsolute(rawPath) ? rawPath : join3(appDirectory, rawPath);
1432
- const logoPath = join3(normalize(docDirectory || config.root), PUBLIC_DIR, p);
1433
- const outputLogoPath = join3(normalize(outputDir), p);
1434
- fs9.copyFileSync(logoPath, outputLogoPath);
1435
- });
1436
1476
  } finally {
1437
1477
  await writeSearchIndex(config);
1438
1478
  }