@module-federation/rspress-plugin 2.5.1 → 2.7.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.
- package/dist/index.js +33 -33
- package/package.json +12 -11
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
|
|
6
|
-
import
|
|
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
|
|
13
|
-
import
|
|
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(
|
|
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(
|
|
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
|
|
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 =
|
|
119
|
-
await
|
|
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
|
|
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
|
|
128
|
+
if (!existingContent.trim()) await promises.writeFile(mdPath, markdown);
|
|
129
129
|
}));
|
|
130
130
|
}
|
|
131
131
|
async function patchLlmsFullTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
|
|
132
|
-
const llmsFullPath =
|
|
133
|
-
await
|
|
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
|
|
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
|
|
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
|
|
153
|
+
await promises.writeFile(llmsFullPath, patched);
|
|
154
154
|
}
|
|
155
155
|
async function patchLlmsTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
|
|
156
|
-
const llmsPath =
|
|
156
|
+
const llmsPath = node_path.join(options.outputDir, outputPrefix, 'llms.txt');
|
|
157
157
|
let original = '';
|
|
158
158
|
try {
|
|
159
|
-
original = await
|
|
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
|
|
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 =
|
|
197
|
-
if (!
|
|
198
|
-
const files =
|
|
199
|
-
const searchIndexFiles = files.filter((file)=>file.startsWith(SEARCH_INDEX_NAME) && file.endsWith('.json') &&
|
|
200
|
-
if (searchIndexFiles) return searchIndexFiles.map((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(
|
|
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(
|
|
241
|
-
const html = await
|
|
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(`${
|
|
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
|
|
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.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Module Federation plugin for Rspress",
|
|
6
6
|
"keywords": [
|
|
@@ -30,24 +30,25 @@
|
|
|
30
30
|
"module": "./dist/index.js",
|
|
31
31
|
"types": "./dist/plugin.d.ts",
|
|
32
32
|
"devDependencies": {
|
|
33
|
-
"@rslib/core": "^0.
|
|
34
|
-
"@rspress/core": "2.0.
|
|
33
|
+
"@rslib/core": "^0.23.2",
|
|
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": "6.0.3"
|
|
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.
|
|
43
|
-
"@rspress/shared": "2.0.
|
|
44
|
-
"@module-federation/sdk": "2.
|
|
45
|
-
"@module-federation/enhanced": "2.
|
|
46
|
-
"@module-federation/rsbuild-plugin": "2.
|
|
47
|
-
"@module-federation/error-codes": "2.
|
|
43
|
+
"lodash-es": "4.18.1",
|
|
44
|
+
"@rspress/shared": "2.0.14",
|
|
45
|
+
"@module-federation/sdk": "2.7.0",
|
|
46
|
+
"@module-federation/enhanced": "2.7.0",
|
|
47
|
+
"@module-federation/rsbuild-plugin": "2.7.0",
|
|
48
|
+
"@module-federation/error-codes": "2.7.0"
|
|
48
49
|
},
|
|
49
50
|
"peerDependencies": {
|
|
50
|
-
"@rspress/core": "
|
|
51
|
+
"@rspress/core": "2.0.14"
|
|
51
52
|
},
|
|
52
53
|
"scripts": {
|
|
53
54
|
"build": "rslib build",
|