@rspress/core 0.0.10 → 0.0.12

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.js CHANGED
@@ -1,14 +1,13 @@
1
1
  // src/node/dev.ts
2
2
  import { removeLeadingSlash as removeLeadingSlash2 } from "@rspress/shared";
3
- import fs9 from "@modern-js/utils/fs-extra";
4
3
  import { logger as logger3 } from "@rspress/shared/logger";
5
4
 
6
5
  // src/node/createBuilder.ts
7
6
  import path8 from "path";
8
7
  import { createRequire as createRequire3 } from "module";
9
- import { isDebugMode, removeTrailingSlash } from "@rspress/shared";
10
- import sirv from "sirv";
11
8
  import fs8 from "@modern-js/utils/fs-extra";
9
+ import { RSPRESS_TEMP_DIR as RSPRESS_TEMP_DIR2, isDebugMode, removeTrailingSlash } from "@rspress/shared";
10
+ import sirv from "sirv";
12
11
 
13
12
  // tailwind.config.ts
14
13
  var tailwindConfig = {
@@ -97,16 +96,17 @@ var BODY_START_TAG = "<body";
97
96
  var PUBLIC_DIR = "public";
98
97
  var TEMP_DIR = path.join(process.cwd(), "node_modules", RSPRESS_TEMP_DIR);
99
98
 
99
+ // src/node/runtimeModule/index.ts
100
+ import { RspackVirtualModulePlugin } from "rspack-plugin-virtual-module";
101
+
100
102
  // src/node/runtimeModule/routeData.ts
101
103
  import { addLeadingSlash } from "@rspress/shared";
102
- import { RspackVirtualModulePlugin } from "rspack-plugin-virtual-module";
103
104
  async function routeVMPlugin(context) {
104
105
  const { routeService: routeService2 } = context;
105
- const plugin = new RspackVirtualModulePlugin({
106
+ return {
106
107
  [RuntimeModuleID.RouteForClient]: routeService2.generateRoutesCode(false),
107
108
  [RuntimeModuleID.RouteForSSR]: routeService2.generateRoutesCode(true)
108
- });
109
- return plugin;
109
+ };
110
110
  }
111
111
 
112
112
  // src/node/runtimeModule/siteData.ts
@@ -116,7 +116,6 @@ 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
118
  import { compile } from "@modern-js/mdx-rs-binding";
119
- import { RspackVirtualModulePlugin as RspackVirtualModulePlugin3 } from "rspack-plugin-virtual-module";
120
119
 
121
120
  // src/node/utils/applyReplaceRules.ts
122
121
  function applyReplaceRules(code = "", replaceRules = []) {
@@ -295,7 +294,6 @@ async function flattenMdxContent(content, basePath, alias) {
295
294
  // src/node/runtimeModule/i18n.ts
296
295
  import { join } from "path";
297
296
  import { createRequire as createRequire2 } from "module";
298
- import { RspackVirtualModulePlugin as RspackVirtualModulePlugin2 } from "rspack-plugin-virtual-module";
299
297
  var require3 = createRequire2(import.meta.url);
300
298
  var DEFAULT_I18N_SOURCE = join(process.cwd(), "i18n.json");
301
299
  function getI18nData(docConfig) {
@@ -310,14 +308,13 @@ function getI18nData(docConfig) {
310
308
  function i18nVMPlugin(context) {
311
309
  const { config } = context;
312
310
  const i18nData = getI18nData(config);
313
- return new RspackVirtualModulePlugin2({
311
+ return {
314
312
  [RuntimeModuleID.I18nText]: `export default ${JSON.stringify(i18nData)}`
315
- });
313
+ };
316
314
  }
317
315
 
318
316
  // src/node/runtimeModule/siteData.ts
319
317
  var { loadFront } = yamlFrontMatter;
320
- var pages;
321
318
  function deletePriviteKey(obj) {
322
319
  if (typeof obj !== "object" || obj === null) {
323
320
  return obj;
@@ -332,7 +329,7 @@ function deletePriviteKey(obj) {
332
329
  });
333
330
  return newObj;
334
331
  }
335
- function normalizeThemeConfig(docConfig, pages2 = []) {
332
+ function normalizeThemeConfig(docConfig, pages = []) {
336
333
  const { locales: siteLocales, base = "", lang, replaceRules = [] } = docConfig;
337
334
  docConfig.themeConfig = docConfig.themeConfig || {};
338
335
  const { themeConfig } = docConfig;
@@ -368,7 +365,7 @@ function normalizeThemeConfig(docConfig, pages2 = []) {
368
365
  }
369
366
  if (typeof item === "string") {
370
367
  const normalizedItem = normalizeLangPrefix(item, currentLang);
371
- const page = pages2.find((page2) => page2.routePath === withBase(normalizedItem, base));
368
+ const page = pages.find((page2) => page2.routePath === withBase(normalizedItem, base));
372
369
  return {
373
370
  text: applyReplaceRules(page?.title || "", replaceRules),
374
371
  link: normalizedItem
@@ -531,11 +528,9 @@ async function siteDataVMPlugin(context) {
531
528
  const { config, alias, userRoot, routeService: routeService2, pluginDriver } = context;
532
529
  const userConfig = config;
533
530
  const replaceRules = userConfig?.replaceRules || [];
534
- if (!pages) {
535
- const domain = userConfig?.search && userConfig?.search?.mode === "remote" ? userConfig?.search.domain ?? "" : "";
536
- pages = (await extractPageData(replaceRules, alias, domain, userRoot, routeService2)).filter(Boolean);
537
- await pluginDriver.modifySearchIndexData(pages);
538
- }
531
+ const domain = userConfig?.search && userConfig?.search?.mode === "remote" ? userConfig?.search.domain ?? "" : "";
532
+ const pages = (await extractPageData(replaceRules, alias, domain, userRoot, routeService2)).filter(Boolean);
533
+ await pluginDriver.modifySearchIndexData(pages);
539
534
  const pagesByLang = pages.reduce((acc, page) => {
540
535
  if (!acc[page.lang]) {
541
536
  acc[page.lang] = [];
@@ -567,40 +562,55 @@ async function siteDataVMPlugin(context) {
567
562
  mode: "local"
568
563
  },
569
564
  pages: pages.map((page) => {
570
- const { content, id, domain, _filepath, ...rest } = page;
565
+ const { content, id, domain: domain2, _filepath, ...rest } = page;
571
566
  return rest;
572
567
  }),
573
568
  markdown: {
574
569
  showLineNumbers: userConfig?.markdown?.showLineNumbers ?? false
575
570
  }
576
571
  };
577
- const plugin = new RspackVirtualModulePlugin3({
572
+ return {
578
573
  [`${RuntimeModuleID.SiteData}.mjs`]: `export default ${JSON.stringify(siteData)}`,
579
574
  [RuntimeModuleID.SearchIndexHash]: `export default ${JSON.stringify(indexHashByLang)}`
580
- });
581
- return plugin;
575
+ };
582
576
  }
583
577
 
584
578
  // src/node/runtimeModule/globalUIComponents.ts
585
- import { RspackVirtualModulePlugin as RspackVirtualModulePlugin4 } from "rspack-plugin-virtual-module";
586
579
  async function globalUIComponentsVMPlugin(context) {
587
580
  const { config, pluginDriver } = context;
588
581
  let index = 0;
589
582
  const globalUIComponentsByPlugins = pluginDriver.globalUIComponents();
590
- const moduleContent = [
583
+ const globalComponents = [
591
584
  ...config?.globalUIComponents || [],
592
585
  ...globalUIComponentsByPlugins
593
- ].map((source) => `import Comp_${index++} from ${JSON.stringify(source)};`).concat(`export default [${Array.from({
586
+ ];
587
+ const moduleContent = globalComponents.map((source) => {
588
+ const name = `Comp_${index}`;
589
+ if (Array.isArray(source)) {
590
+ return `import ${name} from '${source[0]}';
591
+ const Props_${index++} = ${JSON.stringify(source[1])};
592
+ `;
593
+ } else {
594
+ index++;
595
+ return `import ${name} from '${source}';
596
+ `;
597
+ }
598
+ }).concat(`export default [${Array.from({
594
599
  length: index
595
- }, (_, i) => `Comp_${i}`).join(", ")}];`).join("");
596
- return new RspackVirtualModulePlugin4({
600
+ }, (_, i) => {
601
+ if (Array.isArray(globalComponents[i])) {
602
+ return `[${`Comp_${i}`}, Props_${i}]`;
603
+ } else {
604
+ return `Comp_${i}`;
605
+ }
606
+ }).join(", ")}];`).join("");
607
+ return {
597
608
  [RuntimeModuleID.GlobalComponents]: moduleContent
598
- });
609
+ };
599
610
  }
600
611
 
601
612
  // src/node/runtimeModule/globalStyles.ts
602
613
  import fs4 from "@modern-js/utils/fs-extra";
603
- import { RspackVirtualModulePlugin as RspackVirtualModulePlugin5 } from "rspack-plugin-virtual-module";
604
614
  async function globalStylesVMPlugin(context) {
605
615
  const { config, pluginDriver } = context;
606
616
  const globalStylesByPlugins = pluginDriver.globalStyles();
@@ -616,23 +626,22 @@ async function globalStylesVMPlugin(context) {
616
626
  ].filter((source) => source.length > 0).map(async (source) => {
617
627
  return `import ${JSON.stringify(source)};`;
618
628
  }))).join("");
619
- return new RspackVirtualModulePlugin5({
629
+ return {
620
630
  [RuntimeModuleID.GlobalStyles]: moduleContent
621
- });
631
+ };
622
632
  }
623
633
 
624
634
  // src/node/runtimeModule/searchHooks.ts
625
635
  import fs5 from "@modern-js/utils/fs-extra";
626
- import { RspackVirtualModulePlugin as RspackVirtualModulePlugin6 } from "rspack-plugin-virtual-module";
627
636
  async function searchHookVMPlugin(context) {
628
637
  const { config } = context;
629
638
  let content = "export const onSearch = () => {}";
630
639
  if (typeof config.search === "object" && typeof config.search.searchHooks === "string") {
631
640
  content = await fs5.readFile(config.search.searchHooks, "utf-8");
632
641
  }
633
- return new RspackVirtualModulePlugin6({
642
+ return {
634
643
  [`${RuntimeModuleID.SearchHooks}.ts`]: content
635
- });
644
+ };
636
645
  }
637
646
 
638
647
  // src/node/runtimeModule/index.ts
@@ -668,13 +677,15 @@ function builderDocVMPlugin(factoryContext) {
668
677
  setup(api) {
669
678
  api.modifyBundlerChain(async (bundlerChain) => {
670
679
  const alias = bundlerChain.resolve.alias.entries();
671
- let index = 0;
680
+ const runtimeModule = {};
672
681
  for (const factory of runtimeModuleFactory) {
673
- bundlerChain.plugin(`runtime-module-${index++}`).use(await factory({
682
+ const moduleResult = await factory({
674
683
  ...factoryContext,
675
684
  alias
676
- }));
685
+ });
686
+ Object.assign(runtimeModule, moduleResult);
677
687
  }
688
+ bundlerChain.plugin(`rspress-runtime-module`).use(new RspackVirtualModulePlugin(runtimeModule, factoryContext.runtimeTempDir));
678
689
  });
679
690
  }
680
691
  };
@@ -1029,17 +1040,17 @@ async function initRouteService(options) {
1029
1040
 
1030
1041
  // src/node/createBuilder.ts
1031
1042
  var require4 = createRequire3(import.meta.url);
1032
- async function createInternalBuildConfig(userRoot, config, isSSR, runtimeTempDir, routeService2) {
1043
+ async function createInternalBuildConfig(userRoot, config, isSSR, routeService2) {
1033
1044
  const cwd = process.cwd();
1034
- const { default: fs11 } = await import("@modern-js/utils/fs-extra");
1045
+ const { default: fs10 } = await import("@modern-js/utils/fs-extra");
1035
1046
  const CUSTOM_THEME_DIR = config?.themeDir ?? path8.join(process.cwd(), "theme");
1036
1047
  const outDir = config?.outDir ?? OUTPUT_DIR;
1037
1048
  const DEFAULT_THEME_DIR = isDebugMode() ? path8.join(PACKAGE_ROOT, "src", "theme-default") : path8.join(PACKAGE_ROOT, "dist", "theme");
1038
- const themeDir = await fs11.pathExists(CUSTOM_THEME_DIR) ? CUSTOM_THEME_DIR : DEFAULT_THEME_DIR;
1049
+ const themeDir = await fs10.pathExists(CUSTOM_THEME_DIR) ? CUSTOM_THEME_DIR : DEFAULT_THEME_DIR;
1039
1050
  const checkDeadLinks = (config?.markdown?.checkDeadLinks && !isSSR) ?? false;
1040
1051
  const base = config?.base ?? "";
1041
1052
  const publicDir = path8.join(userRoot, "public");
1042
- const isPublicDirExist = await fs11.pathExists(publicDir);
1053
+ const isPublicDirExist = await fs10.pathExists(publicDir);
1043
1054
  const assetPrefix = isProduction() ? removeTrailingSlash(config?.builderConfig?.output?.assetPrefix ?? base) : "";
1044
1055
  const enableMdxRs = config?.markdown?.experimentalMdxRs ?? false;
1045
1056
  const reactVersion = await detectReactVersion();
@@ -1125,6 +1136,13 @@ async function createInternalBuildConfig(userRoot, config, isSSR, runtimeTempDir
1125
1136
  }));
1126
1137
  }
1127
1138
  },
1139
+ rspack: {
1140
+ // This config can be removed after upgrading Rspack v0.4
1141
+ // https://github.com/web-infra-dev/rspack/issues/3096
1142
+ optimization: {
1143
+ chunkIds: "deterministic"
1144
+ }
1145
+ },
1128
1146
  bundlerChain(chain) {
1129
1147
  chain.module.rule("MDX").type("jsx").test(/\.mdx?$/).oneOf("MDXCompile").use("mdx-loader").loader(require4.resolve("../loader.cjs")).options({
1130
1148
  config,
@@ -1144,17 +1162,18 @@ async function createModernBuilder(rootDir, config, pluginDriver, isSSR = false,
1144
1162
  const cwd = process.cwd();
1145
1163
  const userRoot = path8.resolve(rootDir || config?.root || cwd);
1146
1164
  const builderPlugins = config?.builderPlugins ?? [];
1147
- const runtimeTempDir = path8.join(TEMP_DIR, "runtime");
1148
- await fs8.ensureDir(runtimeTempDir);
1165
+ const runtimeTempDir = path8.join(RSPRESS_TEMP_DIR2, "runtime");
1166
+ const runtimeAbsTempDir = path8.join(cwd, "node_modules", runtimeTempDir);
1167
+ await fs8.ensureDir(runtimeAbsTempDir);
1149
1168
  const routeService2 = await initRouteService({
1150
1169
  config,
1151
- runtimeTempDir,
1170
+ runtimeTempDir: runtimeAbsTempDir,
1152
1171
  scanDir: userRoot,
1153
1172
  pluginDriver
1154
1173
  });
1155
1174
  const { default: { createBuilder, mergeBuilderConfig } } = await import("@modern-js/builder");
1156
1175
  const { default: { builderRspackProvider } } = await import("@modern-js/builder-rspack-provider");
1157
- const internalBuilderConfig = await createInternalBuildConfig(userRoot, config, isSSR, runtimeTempDir, routeService2);
1176
+ const internalBuilderConfig = await createInternalBuildConfig(userRoot, config, isSSR, routeService2);
1158
1177
  const builderProvider = builderRspackProvider({
1159
1178
  builderConfig: mergeBuilderConfig(internalBuilderConfig, ...config?.plugins?.map((plugin) => plugin.builderConfig ?? {}) || [], config?.builderConfig || {}, extraBuilderConfig || {})
1160
1179
  });
@@ -1239,7 +1258,7 @@ var PluginDriver = class {
1239
1258
  }
1240
1259
  if (mediumZoomConfig) {
1241
1260
  const { pluginMediumZoom } = await import("@rspress/plugin-medium-zoom");
1242
- this.addPlugin(pluginMediumZoom());
1261
+ this.addPlugin(pluginMediumZoom(typeof mediumZoomConfig === "object" ? mediumZoomConfig : void 0));
1243
1262
  }
1244
1263
  if (!haveNavSidebarConfig) {
1245
1264
  const { pluginAutoNavSidebar } = await import("@rspress/plugin-auto-nav-sidebar");
@@ -1277,8 +1296,8 @@ var PluginDriver = class {
1277
1296
  async afterBuild() {
1278
1297
  return this._runParallelAsyncHook("afterBuild", _class_private_field_get2(this, _config) || {}, _class_private_field_get2(this, _isProd));
1279
1298
  }
1280
- async modifySearchIndexData(pages2) {
1281
- return this._runParallelAsyncHook("modifySearchIndexData", pages2);
1299
+ async modifySearchIndexData(pages) {
1300
+ return this._runParallelAsyncHook("modifySearchIndexData", pages);
1282
1301
  }
1283
1302
  async extendPageData(pageData) {
1284
1303
  return this._runParallelAsyncHook("extendPageData", pageData);
@@ -1347,7 +1366,6 @@ async function dev(options) {
1347
1366
  try {
1348
1367
  const modifiedConfig = await pluginDriver.modifyConfig();
1349
1368
  await pluginDriver.beforeBuild();
1350
- await fs9.emptyDir(TEMP_DIR);
1351
1369
  const builder = await createModernBuilder(docDirectory, modifiedConfig, pluginDriver, false, {});
1352
1370
  const { server } = await builder.startDevServer({
1353
1371
  printURLs: (urls) => {
@@ -1371,7 +1389,7 @@ async function dev(options) {
1371
1389
  import { dirname as dirname2, isAbsolute, join as join3 } from "path";
1372
1390
  import { pathToFileURL } from "url";
1373
1391
  import chalk2 from "@modern-js/utils/chalk";
1374
- import fs10 from "@modern-js/utils/fs-extra";
1392
+ import fs9 from "@modern-js/utils/fs-extra";
1375
1393
  import { APPEARANCE_KEY, normalizeSlash, withBase as withBase3 } from "@rspress/shared";
1376
1394
  var CHECK_DARK_LIGHT_SCRIPT = `
1377
1395
  <script id="check-dark-light">
@@ -1413,7 +1431,7 @@ async function bundle(appDirectory, docDirectory, config, pluginDriver) {
1413
1431
  const normalize = (rawPath) => isAbsolute(rawPath) ? rawPath : join3(appDirectory, rawPath);
1414
1432
  const logoPath = join3(normalize(docDirectory || config.root), PUBLIC_DIR, p);
1415
1433
  const outputLogoPath = join3(normalize(outputDir), p);
1416
- fs10.copyFileSync(logoPath, outputLogoPath);
1434
+ fs9.copyFileSync(logoPath, outputLogoPath);
1417
1435
  });
1418
1436
  } finally {
1419
1437
  await writeSearchIndex(config);
@@ -1425,7 +1443,7 @@ async function renderPages(appDirectory, config, pluginDriver) {
1425
1443
  const outputPath = config?.outDir ?? join3(appDirectory, OUTPUT_DIR);
1426
1444
  const ssrBundlePath = join3(outputPath, "ssr", "bundles", "main.js");
1427
1445
  try {
1428
- const { default: fs11 } = await import("@modern-js/utils/fs-extra");
1446
+ const { default: fs10 } = await import("@modern-js/utils/fs-extra");
1429
1447
  let render = null;
1430
1448
  try {
1431
1449
  const { default: ssrExports } = await import(pathToFileURL(ssrBundlePath).toString());
@@ -1436,7 +1454,7 @@ async function renderPages(appDirectory, config, pluginDriver) {
1436
1454
  const routes = routeService.getRoutes();
1437
1455
  const base = config?.base ?? "";
1438
1456
  const htmlTemplatePath = join3(outputPath, "html", "main", "index.html");
1439
- const htmlTemplate = await fs11.readFile(htmlTemplatePath, "utf-8");
1457
+ const htmlTemplate = await fs10.readFile(htmlTemplatePath, "utf-8");
1440
1458
  const additionalRoutes = (await pluginDriver.addSSGRoutes()).map((route) => ({
1441
1459
  routePath: withBase3(route.path, base)
1442
1460
  }));
@@ -1481,11 +1499,11 @@ async function renderPages(appDirectory, config, pluginDriver) {
1481
1499
  return `${path9}.html`.replace(normalizedBase, "");
1482
1500
  };
1483
1501
  const fileName = normalizeHtmlFilePath(routePath);
1484
- await fs11.ensureDir(join3(outputPath, dirname2(fileName)));
1485
- await fs11.writeFile(join3(outputPath, fileName), html);
1502
+ await fs10.ensureDir(join3(outputPath, dirname2(fileName)));
1503
+ await fs10.writeFile(join3(outputPath, fileName), html);
1486
1504
  }));
1487
- await fs11.remove(join3(outputPath, "ssr"));
1488
- await fs11.remove(join3(outputPath, "html"));
1505
+ await fs10.remove(join3(outputPath, "ssr"));
1506
+ await fs10.remove(join3(outputPath, "html"));
1489
1507
  const totalTime = Date.now() - startTime;
1490
1508
  logger.success(`Pages rendered in ${chalk2.yellow(totalTime)} ms.`);
1491
1509
  } catch (e) {
@@ -1499,7 +1517,7 @@ async function build(options) {
1499
1517
  await pluginDriver.init();
1500
1518
  const modifiedConfig = await pluginDriver.modifyConfig();
1501
1519
  await pluginDriver.beforeBuild();
1502
- await fs10.emptyDir(TEMP_DIR);
1520
+ await fs9.emptyDir(TEMP_DIR);
1503
1521
  await bundle(appDirectory, docDirectory, modifiedConfig, pluginDriver);
1504
1522
  await renderPages(appDirectory, modifiedConfig, pluginDriver);
1505
1523
  await pluginDriver.afterBuild();