@module-federation/rspress-plugin 2.6.0 → 2.8.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 (2) hide show
  1. package/dist/index.js +33 -33
  2. package/package.json +9 -8
package/dist/index.js CHANGED
@@ -2,18 +2,18 @@ import { pluginModuleFederation } from "@module-federation/rsbuild-plugin";
2
2
  import { BUILD_002, buildDescMap } from "@module-federation/error-codes";
3
3
  import { logAndReport } from "@module-federation/error-codes/node";
4
4
  import { createLogger, createModuleFederationConfig } from "@module-federation/sdk";
5
- import promises_default from "node:fs/promises";
6
- import external_node_path_default from "node:path";
7
- import { load } from "cheerio";
5
+ import promises from "node:fs/promises";
6
+ import node_path from "node:path";
8
7
  import { htmlToText } from "html-to-text";
9
8
  import "node:crypto";
10
9
  import { groupBy } from "lodash-es";
11
10
  import { SEARCH_INDEX_NAME } from "@rspress/shared";
12
- import external_path_default from "path";
13
- import external_fs_default from "fs";
11
+ import path from "path";
12
+ import fs from "fs";
13
+ import * as __rspack_external_cheerio from "cheerio";
14
14
  const logger = createLogger('[ Module Federation Rspress Plugin ]');
15
15
  const src_logger = logger;
16
- const replaceHtmlExt = (filepath)=>filepath.replace(external_node_path_default.extname(filepath), '.html');
16
+ const replaceHtmlExt = (filepath)=>filepath.replace(node_path.extname(filepath), '.html');
17
17
  const routePageToMdFilename = (routePath)=>{
18
18
  const fileName = routePath.endsWith('/') ? `${routePath}index.md` : `${routePath}.md`;
19
19
  return fileName.replace(/^\/+/, '');
@@ -27,12 +27,12 @@ const routePathToMdPath = (routePath, base)=>{
27
27
  const mdPath = normalizedRoutePath.endsWith('/') ? `${normalizedRoutePath}index.md` : `${normalizedRoutePath}.md`;
28
28
  return addBase(mdPath, base);
29
29
  };
30
- const getRouteHtmlPath = (route, { outputDir, defaultLang })=>replaceHtmlExt(external_node_path_default.join(outputDir, route.lang === defaultLang ? route.relativePath.replace(route.lang, '') : route.relativePath));
30
+ const getRouteHtmlPath = (route, { outputDir, defaultLang })=>replaceHtmlExt(node_path.join(outputDir, route.lang === defaultLang ? route.relativePath.replace(route.lang, '') : route.relativePath));
31
31
  function normalizeText(text) {
32
32
  return text.replace(/\s+/g, ' ').replace(/^#+\s*/, '').trim();
33
33
  }
34
34
  function htmlToMarkdown(html) {
35
- const $ = load(html);
35
+ const $ = __rspack_external_cheerio.load(html);
36
36
  $("script, style, noscript, template").remove();
37
37
  $('header, nav, aside, footer').remove();
38
38
  $(".rp-nav,.rp-sidebar,.rp-aside,.rp-footer,.rp-doc-footer,.rp-not-doc").remove();
@@ -71,7 +71,7 @@ function htmlToMarkdown(html) {
71
71
  }).trim();
72
72
  }
73
73
  function extractPageInfoFromHtml(html) {
74
- const $ = load(html);
74
+ const $ = __rspack_external_cheerio.load(html);
75
75
  const $doc = $('main').first().length ? $('main').first() : $('article').first().length ? $('article').first() : $('.rp-doc').first().length ? $('.rp-doc').first() : $('body');
76
76
  $doc.find('a.header-anchor, .header-anchor, .rp-copy, .rp-llms-button').remove();
77
77
  const title = normalizeText($doc.find('h1').first().text()) || normalizeText($('h1').first().text()) || normalizeText($('title').first().text());
@@ -84,7 +84,7 @@ function extractPageInfoFromHtml(html) {
84
84
  async function extractRouteMarkdown(routes, options) {
85
85
  return Promise.all(routes.map(async (route)=>{
86
86
  const htmlPath = getRouteHtmlPath(route, options);
87
- const html = await promises_default.readFile(htmlPath, 'utf-8');
87
+ const html = await promises.readFile(htmlPath, 'utf-8');
88
88
  const { title, description } = extractPageInfoFromHtml(html);
89
89
  return {
90
90
  route,
@@ -115,22 +115,22 @@ function generateLlmsFullTxt(routeMarkdownList, options) {
115
115
  async function writeRouteMarkdownIfEmpty(routeMarkdownList, options) {
116
116
  await Promise.all(routeMarkdownList.map(async ({ route, markdown })=>{
117
117
  const mdFilename = routePageToMdFilename(route.routePath);
118
- const mdPath = external_node_path_default.join(options.outputDir, mdFilename);
119
- await promises_default.mkdir(external_node_path_default.dirname(mdPath), {
118
+ const mdPath = node_path.join(options.outputDir, mdFilename);
119
+ await promises.mkdir(node_path.dirname(mdPath), {
120
120
  recursive: true
121
121
  });
122
122
  let existingContent = '';
123
123
  try {
124
- existingContent = await promises_default.readFile(mdPath, 'utf-8');
124
+ existingContent = await promises.readFile(mdPath, 'utf-8');
125
125
  } catch (error) {
126
126
  if ('ENOENT' !== error.code) throw error;
127
127
  }
128
- if (!existingContent.trim()) await promises_default.writeFile(mdPath, markdown);
128
+ if (!existingContent.trim()) await promises.writeFile(mdPath, markdown);
129
129
  }));
130
130
  }
131
131
  async function patchLlmsFullTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
132
- const llmsFullPath = external_node_path_default.join(options.outputDir, outputPrefix, 'llms-full.txt');
133
- await promises_default.mkdir(external_node_path_default.dirname(llmsFullPath), {
132
+ const llmsFullPath = node_path.join(options.outputDir, outputPrefix, 'llms-full.txt');
133
+ await promises.mkdir(node_path.dirname(llmsFullPath), {
134
134
  recursive: true
135
135
  });
136
136
  const markdownMap = new Map(routeMarkdownList.map(({ route, markdown })=>[
@@ -139,24 +139,24 @@ async function patchLlmsFullTxtIfEmpty(routeMarkdownList, options, outputPrefix)
139
139
  ]));
140
140
  let original = '';
141
141
  try {
142
- original = await promises_default.readFile(llmsFullPath, 'utf-8');
142
+ original = await promises.readFile(llmsFullPath, 'utf-8');
143
143
  } catch (error) {
144
144
  if ('ENOENT' !== error.code) throw error;
145
145
  }
146
- if (!original) return void await promises_default.writeFile(llmsFullPath, generateLlmsFullTxt(routeMarkdownList, options));
146
+ if (!original) return void await promises.writeFile(llmsFullPath, generateLlmsFullTxt(routeMarkdownList, options));
147
147
  const patched = original.replace(/(---\nurl:\s*([^\n]+)\n---\n)([\s\S]*?)(?=\n---\nurl:|$)/g, (section, header, url, body)=>{
148
148
  if (body.trim()) return section;
149
149
  const markdown = markdownMap.get(url.trim());
150
150
  if (!markdown) return section;
151
151
  return `${header}${markdown}\n`;
152
152
  });
153
- await promises_default.writeFile(llmsFullPath, patched);
153
+ await promises.writeFile(llmsFullPath, patched);
154
154
  }
155
155
  async function patchLlmsTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
156
- const llmsPath = external_node_path_default.join(options.outputDir, outputPrefix, 'llms.txt');
156
+ const llmsPath = node_path.join(options.outputDir, outputPrefix, 'llms.txt');
157
157
  let original = '';
158
158
  try {
159
- original = await promises_default.readFile(llmsPath, 'utf-8');
159
+ original = await promises.readFile(llmsPath, 'utf-8');
160
160
  } catch (error) {
161
161
  if ('ENOENT' !== error.code) throw error;
162
162
  return;
@@ -174,7 +174,7 @@ async function patchLlmsTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
174
174
  const suffix = pageInfo.description ? `: ${pageInfo.description}` : '';
175
175
  return `- [${pageInfo.title}](${url})${suffix}`;
176
176
  });
177
- if (patched !== original) await promises_default.writeFile(llmsPath, patched);
177
+ if (patched !== original) await promises.writeFile(llmsPath, patched);
178
178
  }
179
179
  async function rebuildLlmsByHtml(routes, options) {
180
180
  const routeMarkdownList = await extractRouteMarkdown(routes, options);
@@ -193,14 +193,14 @@ async function rebuildLlmsByHtml(routes, options) {
193
193
  }));
194
194
  }
195
195
  function findSearchIndexPaths(outputDir) {
196
- const staticDir = external_path_default.join(outputDir, 'static');
197
- if (!external_fs_default.existsSync(staticDir)) return;
198
- const files = external_fs_default.readdirSync(staticDir);
199
- const searchIndexFiles = files.filter((file)=>file.startsWith(SEARCH_INDEX_NAME) && file.endsWith('.json') && external_fs_default.statSync(external_path_default.join(staticDir, file)).isFile());
200
- if (searchIndexFiles) return searchIndexFiles.map((searchIndexFile)=>external_path_default.join(staticDir, searchIndexFile));
196
+ const staticDir = path.join(outputDir, 'static');
197
+ if (!fs.existsSync(staticDir)) return;
198
+ const files = fs.readdirSync(staticDir);
199
+ const searchIndexFiles = files.filter((file)=>file.startsWith(SEARCH_INDEX_NAME) && file.endsWith('.json') && fs.statSync(path.join(staticDir, file)).isFile());
200
+ if (searchIndexFiles) return searchIndexFiles.map((searchIndexFile)=>path.join(staticDir, searchIndexFile));
201
201
  }
202
202
  function generateTocFromHtml(html) {
203
- const $ = load(html);
203
+ const $ = __rspack_external_cheerio.load(html);
204
204
  const headings = $('h1, h2, h3, h4, h5, h6');
205
205
  const toc = [];
206
206
  let title = '';
@@ -221,7 +221,7 @@ function generateTocFromHtml(html) {
221
221
  title
222
222
  };
223
223
  }
224
- const rebuildSearchIndexByHtml_replaceHtmlExt = (filepath)=>filepath.replace(external_node_path_default.extname(filepath), '.html');
224
+ const rebuildSearchIndexByHtml_replaceHtmlExt = (filepath)=>filepath.replace(node_path.extname(filepath), '.html');
225
225
  async function extractPageDataFromHtml(routes, options) {
226
226
  return Promise.all(routes.map(async (route)=>{
227
227
  const { searchCodeBlocks, outputDir, defaultLang } = options;
@@ -237,8 +237,8 @@ async function extractPageDataFromHtml(routes, options) {
237
237
  _filepath: route.absolutePath,
238
238
  _relativePath: ''
239
239
  };
240
- const htmlPath = rebuildSearchIndexByHtml_replaceHtmlExt(external_node_path_default.join(outputDir, route.lang === defaultLang ? route.relativePath.replace(route.lang, '') : route.relativePath));
241
- const html = await promises_default.readFile(htmlPath, 'utf-8');
240
+ const htmlPath = rebuildSearchIndexByHtml_replaceHtmlExt(node_path.join(outputDir, route.lang === defaultLang ? route.relativePath.replace(route.lang, '') : route.relativePath));
241
+ const html = await promises.readFile(htmlPath, 'utf-8');
242
242
  let { toc: rawToc, title } = generateTocFromHtml(html);
243
243
  let content = html;
244
244
  content = htmlToText(html, {
@@ -326,12 +326,12 @@ async function rebuildSearchIndexByHtml(routes, options) {
326
326
  const [version, lang] = group.split('###');
327
327
  const indexVersion = version ? `.${version.replace('.', '_')}` : '';
328
328
  const indexLang = lang ? `.${lang}` : '';
329
- const searchFilePath = searchFilePaths.find((filePath)=>filePath.replace(`${external_node_path_default.dirname(filePath)}/`, '').startsWith(`${SEARCH_INDEX_NAME}${indexVersion}${indexLang}`));
329
+ const searchFilePath = searchFilePaths.find((filePath)=>filePath.replace(`${node_path.dirname(filePath)}/`, '').startsWith(`${SEARCH_INDEX_NAME}${indexVersion}${indexLang}`));
330
330
  if (!searchFilePath) {
331
331
  src_logger.error(`Cannot find search index file for version ${version} and lang ${lang}!`);
332
332
  process.exit(1);
333
333
  }
334
- await promises_default.writeFile(searchFilePath, stringifiedIndex);
334
+ await promises.writeFile(searchFilePath, stringifiedIndex);
335
335
  }));
336
336
  }
337
337
  const isDev = ()=>'development' === process.env.NODE_ENV;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@module-federation/rspress-plugin",
3
- "version": "2.6.0",
3
+ "version": "2.8.0",
4
4
  "type": "module",
5
5
  "description": "Module Federation plugin for Rspress",
6
6
  "keywords": [
@@ -30,21 +30,22 @@
30
30
  "module": "./dist/index.js",
31
31
  "types": "./dist/plugin.d.ts",
32
32
  "devDependencies": {
33
- "@rslib/core": "^0.9.2",
33
+ "@rslib/core": "^0.23.2",
34
34
  "@rspress/core": "2.0.14",
35
35
  "@types/html-to-text": "^9.0.4",
36
36
  "@types/lodash-es": "^4.17.12",
37
- "@types/react": "^18.3.11"
37
+ "@types/react": "^18.3.11",
38
+ "typescript": "7.0.2"
38
39
  },
39
40
  "dependencies": {
40
41
  "cheerio": "1.0.0-rc.12",
41
42
  "html-to-text": "9.0.5",
42
- "lodash-es": "4.17.21",
43
+ "lodash-es": "4.18.1",
43
44
  "@rspress/shared": "2.0.14",
44
- "@module-federation/enhanced": "2.6.0",
45
- "@module-federation/sdk": "2.6.0",
46
- "@module-federation/rsbuild-plugin": "2.6.0",
47
- "@module-federation/error-codes": "2.6.0"
45
+ "@module-federation/sdk": "2.8.0",
46
+ "@module-federation/enhanced": "2.8.0",
47
+ "@module-federation/error-codes": "2.8.0",
48
+ "@module-federation/rsbuild-plugin": "2.8.0"
48
49
  },
49
50
  "peerDependencies": {
50
51
  "@rspress/core": "2.0.14"