@module-federation/rspress-plugin 0.0.0-chore-bump-node-22-20260710161714
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/LICENSE +21 -0
- package/README.md +33 -0
- package/dist/findSearchIndexPath.d.ts +1 -0
- package/dist/index.js +461 -0
- package/dist/logger.d.ts +2 -0
- package/dist/plugin.d.ts +9 -0
- package/dist/rebuildLlmsByHtml.d.ts +8 -0
- package/dist/rebuildSearchIndexByHtml.d.ts +10 -0
- package/package.json +59 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 ScriptedAlchemy LLC (Zack Jackson) Zhou Shaw (zhouxiao)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# @examples/mf-react-component
|
|
2
|
+
|
|
3
|
+
This example demonstrates how to use Rslib to build a simple Module Federation React component.
|
|
4
|
+
|
|
5
|
+
### Command
|
|
6
|
+
|
|
7
|
+
Build package
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
nx build rslib-module
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Serve package
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
nx serve rslib-module
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Dev package
|
|
20
|
+
|
|
21
|
+
1.
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
nx dev rslib-module
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
2.
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
nx storybook rslib-module
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
visit http://localhost:6006
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function findSearchIndexPaths(outputDir: string): string[] | undefined;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,461 @@
|
|
|
1
|
+
import { pluginModuleFederation } from "@module-federation/rsbuild-plugin";
|
|
2
|
+
import { BUILD_002, buildDescMap } from "@module-federation/error-codes";
|
|
3
|
+
import { logAndReport } from "@module-federation/error-codes/node";
|
|
4
|
+
import { createLogger, createModuleFederationConfig } from "@module-federation/sdk";
|
|
5
|
+
import promises from "node:fs/promises";
|
|
6
|
+
import node_path from "node:path";
|
|
7
|
+
import { htmlToText } from "html-to-text";
|
|
8
|
+
import "node:crypto";
|
|
9
|
+
import { groupBy } from "lodash-es";
|
|
10
|
+
import { SEARCH_INDEX_NAME } from "@rspress/shared";
|
|
11
|
+
import path from "path";
|
|
12
|
+
import fs from "fs";
|
|
13
|
+
import * as __rspack_external_cheerio from "cheerio";
|
|
14
|
+
const logger = createLogger('[ Module Federation Rspress Plugin ]');
|
|
15
|
+
const src_logger = logger;
|
|
16
|
+
const replaceHtmlExt = (filepath)=>filepath.replace(node_path.extname(filepath), '.html');
|
|
17
|
+
const routePageToMdFilename = (routePath)=>{
|
|
18
|
+
const fileName = routePath.endsWith('/') ? `${routePath}index.md` : `${routePath}.md`;
|
|
19
|
+
return fileName.replace(/^\/+/, '');
|
|
20
|
+
};
|
|
21
|
+
const addBase = (url, base)=>{
|
|
22
|
+
if (!base || '/' === base) return url;
|
|
23
|
+
return `${base.replace(/\/$/, '')}/${url.replace(/^\//, '')}`;
|
|
24
|
+
};
|
|
25
|
+
const routePathToMdPath = (routePath, base)=>{
|
|
26
|
+
const normalizedRoutePath = routePath.replace(/\.html$/, '');
|
|
27
|
+
const mdPath = normalizedRoutePath.endsWith('/') ? `${normalizedRoutePath}index.md` : `${normalizedRoutePath}.md`;
|
|
28
|
+
return addBase(mdPath, base);
|
|
29
|
+
};
|
|
30
|
+
const getRouteHtmlPath = (route, { outputDir, defaultLang })=>replaceHtmlExt(node_path.join(outputDir, route.lang === defaultLang ? route.relativePath.replace(route.lang, '') : route.relativePath));
|
|
31
|
+
function normalizeText(text) {
|
|
32
|
+
return text.replace(/\s+/g, ' ').replace(/^#+\s*/, '').trim();
|
|
33
|
+
}
|
|
34
|
+
function htmlToMarkdown(html) {
|
|
35
|
+
const $ = __rspack_external_cheerio.load(html);
|
|
36
|
+
$("script, style, noscript, template").remove();
|
|
37
|
+
$('header, nav, aside, footer').remove();
|
|
38
|
+
$(".rp-nav,.rp-sidebar,.rp-aside,.rp-footer,.rp-doc-footer,.rp-not-doc").remove();
|
|
39
|
+
const docHtml = $('main').first().html() || $('article').first().html() || $('.rp-doc').first().html() || $('body').html() || html;
|
|
40
|
+
return htmlToText(docHtml, {
|
|
41
|
+
wordwrap: 80,
|
|
42
|
+
selectors: [
|
|
43
|
+
{
|
|
44
|
+
selector: 'a',
|
|
45
|
+
options: {
|
|
46
|
+
ignoreHref: false
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
selector: 'img',
|
|
51
|
+
format: 'skip'
|
|
52
|
+
},
|
|
53
|
+
...[
|
|
54
|
+
'h1',
|
|
55
|
+
'h2',
|
|
56
|
+
'h3',
|
|
57
|
+
'h4',
|
|
58
|
+
'h5',
|
|
59
|
+
'h6'
|
|
60
|
+
].map((tag)=>({
|
|
61
|
+
selector: tag,
|
|
62
|
+
options: {
|
|
63
|
+
uppercase: false
|
|
64
|
+
}
|
|
65
|
+
}))
|
|
66
|
+
],
|
|
67
|
+
tables: true,
|
|
68
|
+
longWordSplit: {
|
|
69
|
+
forceWrapOnLimit: true
|
|
70
|
+
}
|
|
71
|
+
}).trim();
|
|
72
|
+
}
|
|
73
|
+
function extractPageInfoFromHtml(html) {
|
|
74
|
+
const $ = __rspack_external_cheerio.load(html);
|
|
75
|
+
const $doc = $('main').first().length ? $('main').first() : $('article').first().length ? $('article').first() : $('.rp-doc').first().length ? $('.rp-doc').first() : $('body');
|
|
76
|
+
$doc.find('a.header-anchor, .header-anchor, .rp-copy, .rp-llms-button').remove();
|
|
77
|
+
const title = normalizeText($doc.find('h1').first().text()) || normalizeText($('h1').first().text()) || normalizeText($('title').first().text());
|
|
78
|
+
const description = $doc.find('p').toArray().map((paragraph)=>normalizeText($(paragraph).text())).find(Boolean);
|
|
79
|
+
return {
|
|
80
|
+
title,
|
|
81
|
+
description
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
async function extractRouteMarkdown(routes, options) {
|
|
85
|
+
return Promise.all(routes.map(async (route)=>{
|
|
86
|
+
const htmlPath = getRouteHtmlPath(route, options);
|
|
87
|
+
const html = await promises.readFile(htmlPath, 'utf-8');
|
|
88
|
+
const { title, description } = extractPageInfoFromHtml(html);
|
|
89
|
+
return {
|
|
90
|
+
route,
|
|
91
|
+
markdown: htmlToMarkdown(html),
|
|
92
|
+
title,
|
|
93
|
+
description
|
|
94
|
+
};
|
|
95
|
+
}));
|
|
96
|
+
}
|
|
97
|
+
function getDefaultVersion(routeMarkdownList) {
|
|
98
|
+
return routeMarkdownList.find(({ route })=>route.version)?.route.version;
|
|
99
|
+
}
|
|
100
|
+
function getLlmsOutputPrefix(route, defaultLang, defaultVersion) {
|
|
101
|
+
const langPrefix = route.lang === defaultLang ? '' : `${route.lang}/`;
|
|
102
|
+
const versionPrefix = defaultVersion && route.version && route.version !== defaultVersion ? `${route.version}/` : '';
|
|
103
|
+
return `${versionPrefix}${langPrefix}`;
|
|
104
|
+
}
|
|
105
|
+
function generateLlmsFullTxt(routeMarkdownList, options) {
|
|
106
|
+
return routeMarkdownList.map(({ route, markdown })=>[
|
|
107
|
+
'---',
|
|
108
|
+
`url: ${routePathToMdPath(route.routePath, options.base)}`,
|
|
109
|
+
'---',
|
|
110
|
+
'',
|
|
111
|
+
markdown,
|
|
112
|
+
''
|
|
113
|
+
].join('\n')).join('\n');
|
|
114
|
+
}
|
|
115
|
+
async function writeRouteMarkdownIfEmpty(routeMarkdownList, options) {
|
|
116
|
+
await Promise.all(routeMarkdownList.map(async ({ route, markdown })=>{
|
|
117
|
+
const mdFilename = routePageToMdFilename(route.routePath);
|
|
118
|
+
const mdPath = node_path.join(options.outputDir, mdFilename);
|
|
119
|
+
await promises.mkdir(node_path.dirname(mdPath), {
|
|
120
|
+
recursive: true
|
|
121
|
+
});
|
|
122
|
+
let existingContent = '';
|
|
123
|
+
try {
|
|
124
|
+
existingContent = await promises.readFile(mdPath, 'utf-8');
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if ('ENOENT' !== error.code) throw error;
|
|
127
|
+
}
|
|
128
|
+
if (!existingContent.trim()) await promises.writeFile(mdPath, markdown);
|
|
129
|
+
}));
|
|
130
|
+
}
|
|
131
|
+
async function patchLlmsFullTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
|
|
132
|
+
const llmsFullPath = node_path.join(options.outputDir, outputPrefix, 'llms-full.txt');
|
|
133
|
+
await promises.mkdir(node_path.dirname(llmsFullPath), {
|
|
134
|
+
recursive: true
|
|
135
|
+
});
|
|
136
|
+
const markdownMap = new Map(routeMarkdownList.map(({ route, markdown })=>[
|
|
137
|
+
routePathToMdPath(route.routePath, options.base),
|
|
138
|
+
markdown
|
|
139
|
+
]));
|
|
140
|
+
let original = '';
|
|
141
|
+
try {
|
|
142
|
+
original = await promises.readFile(llmsFullPath, 'utf-8');
|
|
143
|
+
} catch (error) {
|
|
144
|
+
if ('ENOENT' !== error.code) throw error;
|
|
145
|
+
}
|
|
146
|
+
if (!original) return void await promises.writeFile(llmsFullPath, generateLlmsFullTxt(routeMarkdownList, options));
|
|
147
|
+
const patched = original.replace(/(---\nurl:\s*([^\n]+)\n---\n)([\s\S]*?)(?=\n---\nurl:|$)/g, (section, header, url, body)=>{
|
|
148
|
+
if (body.trim()) return section;
|
|
149
|
+
const markdown = markdownMap.get(url.trim());
|
|
150
|
+
if (!markdown) return section;
|
|
151
|
+
return `${header}${markdown}\n`;
|
|
152
|
+
});
|
|
153
|
+
await promises.writeFile(llmsFullPath, patched);
|
|
154
|
+
}
|
|
155
|
+
async function patchLlmsTxtIfEmpty(routeMarkdownList, options, outputPrefix) {
|
|
156
|
+
const llmsPath = node_path.join(options.outputDir, outputPrefix, 'llms.txt');
|
|
157
|
+
let original = '';
|
|
158
|
+
try {
|
|
159
|
+
original = await promises.readFile(llmsPath, 'utf-8');
|
|
160
|
+
} catch (error) {
|
|
161
|
+
if ('ENOENT' !== error.code) throw error;
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const pageInfoMap = new Map(routeMarkdownList.map(({ route, title, description })=>[
|
|
165
|
+
routePathToMdPath(route.routePath, options.base),
|
|
166
|
+
{
|
|
167
|
+
title,
|
|
168
|
+
description
|
|
169
|
+
}
|
|
170
|
+
]));
|
|
171
|
+
const patched = original.replace(/^- \[\]\(([^)]+)\)(?::\s*)?$/gm, (line, url)=>{
|
|
172
|
+
const pageInfo = pageInfoMap.get(url.trim());
|
|
173
|
+
if (!pageInfo?.title) return line;
|
|
174
|
+
const suffix = pageInfo.description ? `: ${pageInfo.description}` : '';
|
|
175
|
+
return `- [${pageInfo.title}](${url})${suffix}`;
|
|
176
|
+
});
|
|
177
|
+
if (patched !== original) await promises.writeFile(llmsPath, patched);
|
|
178
|
+
}
|
|
179
|
+
async function rebuildLlmsByHtml(routes, options) {
|
|
180
|
+
const routeMarkdownList = await extractRouteMarkdown(routes, options);
|
|
181
|
+
const defaultVersion = options.defaultVersion || getDefaultVersion(routeMarkdownList);
|
|
182
|
+
const groupedRouteMarkdown = new Map();
|
|
183
|
+
await writeRouteMarkdownIfEmpty(routeMarkdownList, options);
|
|
184
|
+
for (const routeMarkdown of routeMarkdownList){
|
|
185
|
+
const outputPrefix = getLlmsOutputPrefix(routeMarkdown.route, options.defaultLang, defaultVersion);
|
|
186
|
+
const group = groupedRouteMarkdown.get(outputPrefix) || [];
|
|
187
|
+
group.push(routeMarkdown);
|
|
188
|
+
groupedRouteMarkdown.set(outputPrefix, group);
|
|
189
|
+
}
|
|
190
|
+
await Promise.all(Array.from(groupedRouteMarkdown.entries()).map(async ([outputPrefix, group])=>{
|
|
191
|
+
await patchLlmsTxtIfEmpty(group, options, outputPrefix);
|
|
192
|
+
await patchLlmsFullTxtIfEmpty(group, options, outputPrefix);
|
|
193
|
+
}));
|
|
194
|
+
}
|
|
195
|
+
function findSearchIndexPaths(outputDir) {
|
|
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
|
+
}
|
|
202
|
+
function generateTocFromHtml(html) {
|
|
203
|
+
const $ = __rspack_external_cheerio.load(html);
|
|
204
|
+
const headings = $('h1, h2, h3, h4, h5, h6');
|
|
205
|
+
const toc = [];
|
|
206
|
+
let title = '';
|
|
207
|
+
headings.each((index, heading)=>{
|
|
208
|
+
const $heading = $(heading);
|
|
209
|
+
const text = $heading.text();
|
|
210
|
+
const id = $heading.attr('id');
|
|
211
|
+
const depth = parseInt(heading.tagName.replace('h', ''), 10);
|
|
212
|
+
if (id) toc.push({
|
|
213
|
+
text,
|
|
214
|
+
id,
|
|
215
|
+
depth
|
|
216
|
+
});
|
|
217
|
+
if (1 === depth) title = text;
|
|
218
|
+
});
|
|
219
|
+
return {
|
|
220
|
+
toc,
|
|
221
|
+
title
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
const rebuildSearchIndexByHtml_replaceHtmlExt = (filepath)=>filepath.replace(node_path.extname(filepath), '.html');
|
|
225
|
+
async function extractPageDataFromHtml(routes, options) {
|
|
226
|
+
return Promise.all(routes.map(async (route)=>{
|
|
227
|
+
const { searchCodeBlocks, outputDir, defaultLang } = options;
|
|
228
|
+
const defaultIndexInfo = {
|
|
229
|
+
title: '',
|
|
230
|
+
content: '',
|
|
231
|
+
_flattenContent: '',
|
|
232
|
+
routePath: route.routePath,
|
|
233
|
+
lang: route.lang,
|
|
234
|
+
toc: [],
|
|
235
|
+
frontmatter: {},
|
|
236
|
+
version: route.version,
|
|
237
|
+
_filepath: route.absolutePath,
|
|
238
|
+
_relativePath: ''
|
|
239
|
+
};
|
|
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
|
+
let { toc: rawToc, title } = generateTocFromHtml(html);
|
|
243
|
+
let content = html;
|
|
244
|
+
content = htmlToText(html, {
|
|
245
|
+
wordwrap: 80,
|
|
246
|
+
selectors: [
|
|
247
|
+
{
|
|
248
|
+
selector: 'a',
|
|
249
|
+
options: {
|
|
250
|
+
ignoreHref: true
|
|
251
|
+
}
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
selector: 'img',
|
|
255
|
+
format: 'skip'
|
|
256
|
+
},
|
|
257
|
+
{
|
|
258
|
+
selector: 'pre > code',
|
|
259
|
+
format: searchCodeBlocks ? 'block' : 'skip'
|
|
260
|
+
},
|
|
261
|
+
...[
|
|
262
|
+
'h1',
|
|
263
|
+
'h2',
|
|
264
|
+
'h3',
|
|
265
|
+
'h4',
|
|
266
|
+
'h5',
|
|
267
|
+
'h6'
|
|
268
|
+
].map((tag)=>({
|
|
269
|
+
selector: tag,
|
|
270
|
+
options: {
|
|
271
|
+
uppercase: false
|
|
272
|
+
}
|
|
273
|
+
}))
|
|
274
|
+
],
|
|
275
|
+
tables: true,
|
|
276
|
+
longWordSplit: {
|
|
277
|
+
forceWrapOnLimit: true
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
if (content.startsWith(title)) content = content.slice(title.length);
|
|
281
|
+
const toc = rawToc.map((item)=>{
|
|
282
|
+
const match = item.id.match(/-(\d+)$/);
|
|
283
|
+
let position = -1;
|
|
284
|
+
if (match) for(let i = 0; i < Number(match[1]); i++){
|
|
285
|
+
position = content.indexOf(`\n${item.text}#\n\n`, position + 1);
|
|
286
|
+
if (-1 === position) break;
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
...item,
|
|
290
|
+
charIndex: content.indexOf(`\n${item.text}#\n\n`, position + 1)
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
return {
|
|
294
|
+
...defaultIndexInfo,
|
|
295
|
+
title,
|
|
296
|
+
toc,
|
|
297
|
+
content
|
|
298
|
+
};
|
|
299
|
+
}));
|
|
300
|
+
}
|
|
301
|
+
function deletePrivateField(obj) {
|
|
302
|
+
if ('object' != typeof obj || null === obj) return obj;
|
|
303
|
+
const newObj = {
|
|
304
|
+
...obj
|
|
305
|
+
};
|
|
306
|
+
for(const key in newObj)if (key.startsWith('_')) delete newObj[key];
|
|
307
|
+
return newObj;
|
|
308
|
+
}
|
|
309
|
+
async function rebuildSearchIndexByHtml(routes, options) {
|
|
310
|
+
const { versioned, outputDir } = options;
|
|
311
|
+
const searchFilePaths = findSearchIndexPaths(outputDir);
|
|
312
|
+
if (!searchFilePaths) {
|
|
313
|
+
src_logger.error('Cannot find search index files!');
|
|
314
|
+
process.exit(1);
|
|
315
|
+
}
|
|
316
|
+
const pages = await extractPageDataFromHtml(routes, options);
|
|
317
|
+
const groupedPages = groupBy(pages, (page)=>{
|
|
318
|
+
if (page.frontmatter?.pageType === 'home') return 'noindex';
|
|
319
|
+
const version = versioned ? page.version : '';
|
|
320
|
+
const lang = page.lang || '';
|
|
321
|
+
return `${version}###${lang}`;
|
|
322
|
+
});
|
|
323
|
+
delete groupedPages.noindex;
|
|
324
|
+
await Promise.all(Object.keys(groupedPages).map(async (group)=>{
|
|
325
|
+
const stringifiedIndex = JSON.stringify(groupedPages[group].map(deletePrivateField));
|
|
326
|
+
const [version, lang] = group.split('###');
|
|
327
|
+
const indexVersion = version ? `.${version.replace('.', '_')}` : '';
|
|
328
|
+
const indexLang = lang ? `.${lang}` : '';
|
|
329
|
+
const searchFilePath = searchFilePaths.find((filePath)=>filePath.replace(`${node_path.dirname(filePath)}/`, '').startsWith(`${SEARCH_INDEX_NAME}${indexVersion}${indexLang}`));
|
|
330
|
+
if (!searchFilePath) {
|
|
331
|
+
src_logger.error(`Cannot find search index file for version ${version} and lang ${lang}!`);
|
|
332
|
+
process.exit(1);
|
|
333
|
+
}
|
|
334
|
+
await promises.writeFile(searchFilePath, stringifiedIndex);
|
|
335
|
+
}));
|
|
336
|
+
}
|
|
337
|
+
const isDev = ()=>'development' === process.env.NODE_ENV;
|
|
338
|
+
function plugin_pluginModuleFederation(mfConfig, rspressOptions) {
|
|
339
|
+
const { autoShared = true, rebuildSearchIndex = true, rebuildLlms = true } = rspressOptions || {};
|
|
340
|
+
if (autoShared) mfConfig.shared = {
|
|
341
|
+
react: {
|
|
342
|
+
singleton: true,
|
|
343
|
+
requiredVersion: false
|
|
344
|
+
},
|
|
345
|
+
'react-dom': {
|
|
346
|
+
singleton: true,
|
|
347
|
+
requiredVersion: false
|
|
348
|
+
},
|
|
349
|
+
'react/': {
|
|
350
|
+
singleton: true,
|
|
351
|
+
requiredVersion: false
|
|
352
|
+
},
|
|
353
|
+
'react-dom/': {
|
|
354
|
+
singleton: true,
|
|
355
|
+
requiredVersion: false
|
|
356
|
+
},
|
|
357
|
+
'@mdx-js/react': {
|
|
358
|
+
singleton: true,
|
|
359
|
+
requiredVersion: false
|
|
360
|
+
},
|
|
361
|
+
'@rspress/core/runtime': {
|
|
362
|
+
singleton: true,
|
|
363
|
+
requiredVersion: false
|
|
364
|
+
},
|
|
365
|
+
'react-router-dom': {
|
|
366
|
+
singleton: true,
|
|
367
|
+
requiredVersion: false
|
|
368
|
+
},
|
|
369
|
+
...mfConfig.shared
|
|
370
|
+
};
|
|
371
|
+
mfConfig.experiments ||= {};
|
|
372
|
+
mfConfig.experiments.asyncStartup = true;
|
|
373
|
+
let enableSSG = false;
|
|
374
|
+
let outputDir = '';
|
|
375
|
+
let routes = [];
|
|
376
|
+
return {
|
|
377
|
+
name: 'plugin-module-federation',
|
|
378
|
+
async config (config) {
|
|
379
|
+
if (!isDev() && false !== config.ssg) {
|
|
380
|
+
enableSSG = true;
|
|
381
|
+
if (config.llms) {
|
|
382
|
+
src_logger.info('Detect you set "llms: true", enable experimentalWorker for ssg to enable parallel build');
|
|
383
|
+
config.ssg = {
|
|
384
|
+
...'object' == typeof config.ssg ? config.ssg : {},
|
|
385
|
+
experimentalWorker: true
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
config.builderConfig ||= {};
|
|
390
|
+
config.builderConfig.plugins ||= [];
|
|
391
|
+
config.builderConfig.plugins.push(pluginModuleFederation(mfConfig, {
|
|
392
|
+
target: enableSSG ? 'dual' : 'web',
|
|
393
|
+
ssrDir: 'mf-ssg'
|
|
394
|
+
}));
|
|
395
|
+
return config;
|
|
396
|
+
},
|
|
397
|
+
builderConfig: {
|
|
398
|
+
plugins: [],
|
|
399
|
+
tools: {
|
|
400
|
+
rspack (config) {
|
|
401
|
+
if ('node' === config.name) {
|
|
402
|
+
if (('/' === config.output.publicPath || 'auto' === config.output.publicPath) && mfConfig.exposes) {
|
|
403
|
+
logAndReport(BUILD_002, buildDescMap, {
|
|
404
|
+
publicPath: config.output.publicPath
|
|
405
|
+
}, src_logger.error.bind(src_logger), void 0, {
|
|
406
|
+
bundler: {
|
|
407
|
+
name: 'rsbuild'
|
|
408
|
+
},
|
|
409
|
+
mfConfig: {
|
|
410
|
+
name: mfConfig.name,
|
|
411
|
+
filename: mfConfig.filename,
|
|
412
|
+
exposes: mfConfig.exposes
|
|
413
|
+
}
|
|
414
|
+
});
|
|
415
|
+
process.exit(1);
|
|
416
|
+
}
|
|
417
|
+
outputDir = config.output.path;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
},
|
|
422
|
+
routeGenerated (routeMetaArr) {
|
|
423
|
+
routes = routeMetaArr;
|
|
424
|
+
},
|
|
425
|
+
async afterBuild (config) {
|
|
426
|
+
const shouldRebuildLlms = Boolean(config.llms && rebuildLlms);
|
|
427
|
+
if (!mfConfig.remotes || isDev() || !rebuildSearchIndex && !shouldRebuildLlms) return;
|
|
428
|
+
if (!enableSSG) {
|
|
429
|
+
src_logger.error('rebuildSearchIndex and rebuildLlms are only supported for ssg');
|
|
430
|
+
process.exit(1);
|
|
431
|
+
}
|
|
432
|
+
const defaultLang = config.lang || 'en';
|
|
433
|
+
if (rebuildSearchIndex) {
|
|
434
|
+
const searchConfig = config?.search || {};
|
|
435
|
+
const replaceRules = config?.replaceRules || [];
|
|
436
|
+
const domain = '';
|
|
437
|
+
const versioned = searchConfig && searchConfig.versioned;
|
|
438
|
+
const searchCodeBlocks = 'codeBlocks' in searchConfig ? Boolean(searchConfig.codeBlocks) : true;
|
|
439
|
+
await rebuildSearchIndexByHtml(routes, {
|
|
440
|
+
outputDir,
|
|
441
|
+
versioned,
|
|
442
|
+
replaceRules,
|
|
443
|
+
domain,
|
|
444
|
+
searchCodeBlocks,
|
|
445
|
+
defaultLang
|
|
446
|
+
});
|
|
447
|
+
src_logger.info('rebuildSearchIndex success!');
|
|
448
|
+
}
|
|
449
|
+
if (shouldRebuildLlms) {
|
|
450
|
+
await rebuildLlmsByHtml(routes, {
|
|
451
|
+
outputDir,
|
|
452
|
+
defaultLang,
|
|
453
|
+
base: config.base,
|
|
454
|
+
defaultVersion: config.multiVersion?.default
|
|
455
|
+
});
|
|
456
|
+
src_logger.info('rebuildLlms success!');
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
}
|
|
461
|
+
export { createModuleFederationConfig, plugin_pluginModuleFederation as pluginModuleFederation };
|
package/dist/logger.d.ts
ADDED
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { moduleFederationPlugin } from '@module-federation/sdk';
|
|
2
|
+
import type { RspressPlugin } from '@rspress/core';
|
|
3
|
+
type RspressPluginOptions = {
|
|
4
|
+
autoShared?: boolean;
|
|
5
|
+
rebuildSearchIndex?: boolean;
|
|
6
|
+
rebuildLlms?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare function pluginModuleFederation(mfConfig: moduleFederationPlugin.ModuleFederationPluginOptions, rspressOptions?: RspressPluginOptions): RspressPlugin;
|
|
9
|
+
export { createModuleFederationConfig } from '@module-federation/sdk';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { RouteMeta } from '@rspress/shared';
|
|
2
|
+
export type RebuildLlmsByHtmlOptions = {
|
|
3
|
+
outputDir: string;
|
|
4
|
+
defaultLang: string;
|
|
5
|
+
base?: string;
|
|
6
|
+
defaultVersion?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare function rebuildLlmsByHtml(routes: RouteMeta[], options: RebuildLlmsByHtmlOptions): Promise<void>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RouteMeta, ReplaceRule } from '@rspress/shared';
|
|
2
|
+
export type RebuildSearchIndexByHtmlOptions = {
|
|
3
|
+
domain: string;
|
|
4
|
+
searchCodeBlocks: boolean;
|
|
5
|
+
replaceRules: ReplaceRule[];
|
|
6
|
+
versioned?: boolean;
|
|
7
|
+
outputDir: string;
|
|
8
|
+
defaultLang: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function rebuildSearchIndexByHtml(routes: RouteMeta[], options: RebuildSearchIndexByHtmlOptions): Promise<void>;
|
package/package.json
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@module-federation/rspress-plugin",
|
|
3
|
+
"version": "0.0.0-chore-bump-node-22-20260710161714",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Module Federation plugin for Rspress",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"Module Federation",
|
|
8
|
+
"Rspress"
|
|
9
|
+
],
|
|
10
|
+
"files": [
|
|
11
|
+
"dist/",
|
|
12
|
+
"README.md"
|
|
13
|
+
],
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/module-federation/core.git",
|
|
20
|
+
"directory": "packages/rspress-plugin"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "hanric <hanric.zhang@gmail.com>",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/plugin.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"module": "./dist/index.js",
|
|
31
|
+
"types": "./dist/plugin.d.ts",
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@rslib/core": "^0.23.2",
|
|
34
|
+
"@rspress/core": "2.0.14",
|
|
35
|
+
"@types/html-to-text": "^9.0.4",
|
|
36
|
+
"@types/lodash-es": "^4.17.12",
|
|
37
|
+
"@types/react": "^18.3.11",
|
|
38
|
+
"typescript": "6.0.3"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"cheerio": "1.0.0-rc.12",
|
|
42
|
+
"html-to-text": "9.0.5",
|
|
43
|
+
"lodash-es": "4.18.1",
|
|
44
|
+
"@rspress/shared": "2.0.14",
|
|
45
|
+
"@module-federation/enhanced": "0.0.0-chore-bump-node-22-20260710161714",
|
|
46
|
+
"@module-federation/sdk": "0.0.0-chore-bump-node-22-20260710161714",
|
|
47
|
+
"@module-federation/rsbuild-plugin": "0.0.0-chore-bump-node-22-20260710161714",
|
|
48
|
+
"@module-federation/error-codes": "0.0.0-chore-bump-node-22-20260710161714"
|
|
49
|
+
},
|
|
50
|
+
"peerDependencies": {
|
|
51
|
+
"@rspress/core": "2.0.14"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"build": "rslib build",
|
|
55
|
+
"build:watch": "rslib build --watch",
|
|
56
|
+
"dev": "pnpm run build:watch",
|
|
57
|
+
"pre-release": "pnpm exec turbo run build --filter=@module-federation/webpack-bundler-runtime"
|
|
58
|
+
}
|
|
59
|
+
}
|