@rspress/plugin-preview 2.0.5 → 2.0.7
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 +53 -46
- package/package.json +7 -6
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import node_net from "node:net";
|
|
2
1
|
import node_path, { join } from "node:path";
|
|
3
|
-
import {
|
|
2
|
+
import { createLogger, createRsbuild, mergeRsbuildConfig } from "@rsbuild/core";
|
|
4
3
|
import { pluginReact } from "@rsbuild/plugin-react";
|
|
5
4
|
import { RSPRESS_TEMP_DIR, normalizePosixPath, removeTrailingSlash } from "@rspress/core";
|
|
5
|
+
import picocolors from "picocolors";
|
|
6
6
|
import { mkdir, writeFile } from "node:fs/promises";
|
|
7
7
|
import node_fs from "node:fs";
|
|
8
8
|
import { isDeepStrictEqual } from "node:util";
|
|
@@ -87,6 +87,15 @@ async function generateEntry_generateEntry(globalDemos, framework, customEntry)
|
|
|
87
87
|
};
|
|
88
88
|
return sourceEntry;
|
|
89
89
|
}
|
|
90
|
+
const prefix = picocolors.dim('[@rspress/plugin-preview]');
|
|
91
|
+
const previewLogger = createLogger({
|
|
92
|
+
level: 'info',
|
|
93
|
+
prefix: prefix
|
|
94
|
+
});
|
|
95
|
+
const pluginLogger = createLogger({
|
|
96
|
+
level: 'error',
|
|
97
|
+
prefix: prefix
|
|
98
|
+
});
|
|
90
99
|
const convert = function(test) {
|
|
91
100
|
if (null == test) return ok;
|
|
92
101
|
if ('function' == typeof test) return castFactory(test);
|
|
@@ -273,7 +282,7 @@ function parsePreviewInfoFromMeta(options) {
|
|
|
273
282
|
else result.previewMode = defaultPreviewMode;
|
|
274
283
|
return result;
|
|
275
284
|
}
|
|
276
|
-
if (meta.includes('iframe'))
|
|
285
|
+
if (meta.includes('iframe')) previewLogger.warn('The "iframe" meta is deprecated, please use \`\`\`tsx preview="iframe-fixed" or \`\`\`tsx preview="iframe-follow" instead.');
|
|
277
286
|
if ('preview' === defaultRenderMode) {
|
|
278
287
|
result.isPreview = true;
|
|
279
288
|
result.previewMode = defaultPreviewMode;
|
|
@@ -385,31 +394,27 @@ function pluginPreview(options) {
|
|
|
385
394
|
const getRouteMeta = ()=>src_routeMeta;
|
|
386
395
|
let devServer;
|
|
387
396
|
let clientConfig;
|
|
388
|
-
|
|
389
|
-
async function
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
devServer = void 0;
|
|
394
|
-
logger.info('[@rspress/plugin-preview] Restarting preview server due to demo changes...');
|
|
395
|
-
}
|
|
396
|
-
const outDir = join(config.outDir ?? 'doc_build', '~demo');
|
|
397
|
+
let port = devPort;
|
|
398
|
+
async function createDemoRsbuild() {
|
|
399
|
+
const distPath = clientConfig?.output?.distPath;
|
|
400
|
+
const distRoot = 'string' == typeof distPath ? distPath : distPath?.root ?? 'doc_build';
|
|
401
|
+
const outDir = join(distRoot, '~demo');
|
|
397
402
|
const { source, output, performance, resolve } = clientConfig ?? {};
|
|
398
403
|
const { preEntry: _, ...otherSourceOptions } = source ?? {};
|
|
399
|
-
const
|
|
404
|
+
const rsbuildInstanceConfig = mergeRsbuildConfig({
|
|
405
|
+
customLogger: pluginLogger,
|
|
400
406
|
server: {
|
|
401
407
|
host: true,
|
|
402
|
-
port
|
|
403
|
-
printUrls: ()=>void 0
|
|
404
|
-
strictPort: true
|
|
408
|
+
port,
|
|
409
|
+
printUrls: ()=>void 0
|
|
405
410
|
},
|
|
406
411
|
dev: {
|
|
407
412
|
lazyCompilation: false,
|
|
408
413
|
writeToDisk: true
|
|
409
414
|
},
|
|
410
415
|
performance: {
|
|
416
|
+
printFileSize: true,
|
|
411
417
|
...performance,
|
|
412
|
-
printFileSize: false,
|
|
413
418
|
buildCache: false
|
|
414
419
|
},
|
|
415
420
|
source: {
|
|
@@ -451,13 +456,30 @@ function pluginPreview(options) {
|
|
|
451
456
|
}, builderConfig);
|
|
452
457
|
const rsbuildInstance = await createRsbuild({
|
|
453
458
|
callerName: 'rspress',
|
|
454
|
-
rsbuildConfig
|
|
459
|
+
rsbuildConfig: rsbuildInstanceConfig
|
|
455
460
|
});
|
|
456
461
|
if ('react' === framework) rsbuildInstance.addPlugins([
|
|
457
462
|
pluginReact()
|
|
458
463
|
]);
|
|
459
|
-
|
|
460
|
-
|
|
464
|
+
return rsbuildInstance;
|
|
465
|
+
}
|
|
466
|
+
async function buildDemo() {
|
|
467
|
+
const rsbuildInstance = await createDemoRsbuild();
|
|
468
|
+
await rsbuildInstance.build();
|
|
469
|
+
}
|
|
470
|
+
async function startDemoServer() {
|
|
471
|
+
if (devServer && !isDirtyRef.current) return;
|
|
472
|
+
if (devServer) {
|
|
473
|
+
await devServer.server.close();
|
|
474
|
+
devServer = void 0;
|
|
475
|
+
previewLogger.info(picocolors.dim('Restarting dev server due to demo changes...'));
|
|
476
|
+
}
|
|
477
|
+
const rsbuildInstance = await createDemoRsbuild();
|
|
478
|
+
devServer = await rsbuildInstance.startDevServer();
|
|
479
|
+
if (devServer.port !== port) {
|
|
480
|
+
previewLogger.info(`Port ${port} is in use, using port ${devServer.port} instead.`);
|
|
481
|
+
port = devServer.port;
|
|
482
|
+
}
|
|
461
483
|
isDirtyRef.current = false;
|
|
462
484
|
}
|
|
463
485
|
return {
|
|
@@ -469,35 +491,11 @@ function pluginPreview(options) {
|
|
|
469
491
|
routeGenerated (routes) {
|
|
470
492
|
src_routeMeta = routes;
|
|
471
493
|
},
|
|
472
|
-
async beforeBuild (_, isProd) {
|
|
473
|
-
if (!isProd) try {
|
|
474
|
-
await new Promise((resolve, reject)=>{
|
|
475
|
-
const server = node_net.createServer();
|
|
476
|
-
server.unref();
|
|
477
|
-
server.on('error', reject);
|
|
478
|
-
server.listen({
|
|
479
|
-
port,
|
|
480
|
-
host: '0.0.0.0'
|
|
481
|
-
}, ()=>{
|
|
482
|
-
server.close(resolve);
|
|
483
|
-
});
|
|
484
|
-
});
|
|
485
|
-
} catch (e) {
|
|
486
|
-
if (!!e && 'object' == typeof e && 'code' in e && 'EADDRINUSE' !== e.code) throw e;
|
|
487
|
-
throw new Error(`Port "${port}" is occupied, please choose another one.`);
|
|
488
|
-
}
|
|
489
|
-
},
|
|
490
|
-
async afterBuild (config, isProd) {
|
|
491
|
-
await rsbuildStartOrBuild(config, isProd);
|
|
492
|
-
},
|
|
493
494
|
builderConfig: {
|
|
494
495
|
source: {
|
|
495
496
|
include: [
|
|
496
497
|
join(__dirname, '..')
|
|
497
|
-
]
|
|
498
|
-
define: {
|
|
499
|
-
'process.env.RSPRESS_IFRAME_DEV_PORT': JSON.stringify(devPort)
|
|
500
|
-
}
|
|
498
|
+
]
|
|
501
499
|
},
|
|
502
500
|
tools: {
|
|
503
501
|
bundlerChain (chain) {
|
|
@@ -515,10 +513,19 @@ function pluginPreview(options) {
|
|
|
515
513
|
},
|
|
516
514
|
plugins: [
|
|
517
515
|
{
|
|
518
|
-
name: '
|
|
516
|
+
name: 'iframe-sync-rsbuild-instance',
|
|
519
517
|
setup: (api)=>{
|
|
520
518
|
api.modifyRsbuildConfig((config)=>{
|
|
521
519
|
if (config.output?.target === 'web') clientConfig = config;
|
|
520
|
+
config.source ??= {};
|
|
521
|
+
config.source.define ??= {};
|
|
522
|
+
config.source.define['process.env.RSPRESS_IFRAME_DEV_PORT'] = JSON.stringify(port);
|
|
523
|
+
});
|
|
524
|
+
api.onAfterBuild(async ()=>{
|
|
525
|
+
await buildDemo();
|
|
526
|
+
});
|
|
527
|
+
api.onAfterDevCompile(async ()=>{
|
|
528
|
+
await startDemoServer();
|
|
522
529
|
});
|
|
523
530
|
api.onCloseDevServer(async ()=>{
|
|
524
531
|
await devServer?.server?.close();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rspress/plugin-preview",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.7",
|
|
4
4
|
"description": "A plugin for rspress to preview the code block in markdown/mdx file.",
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
|
6
6
|
"repository": {
|
|
@@ -23,13 +23,14 @@
|
|
|
23
23
|
"static"
|
|
24
24
|
],
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@rsbuild/core": "2.0.0-beta.
|
|
27
|
-
"@rsbuild/plugin-babel": "~1.1.
|
|
28
|
-
"@rsbuild/plugin-react": "~1.4.
|
|
26
|
+
"@rsbuild/core": "2.0.0-beta.10",
|
|
27
|
+
"@rsbuild/plugin-babel": "~1.1.2",
|
|
28
|
+
"@rsbuild/plugin-react": "~1.4.6",
|
|
29
|
+
"picocolors": "^1.1.1",
|
|
29
30
|
"qrcode.react": "^4.2.0"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"@rslib/core": "0.
|
|
33
|
+
"@rslib/core": "0.20.0",
|
|
33
34
|
"@types/mdast": "^4.0.4",
|
|
34
35
|
"@types/node": "^22.8.1",
|
|
35
36
|
"@types/react": "^19.2.14",
|
|
@@ -45,7 +46,7 @@
|
|
|
45
46
|
"unist-util-visit": "^5.0.0"
|
|
46
47
|
},
|
|
47
48
|
"peerDependencies": {
|
|
48
|
-
"@rspress/core": "^2.0.
|
|
49
|
+
"@rspress/core": "^2.0.7",
|
|
49
50
|
"react": ">=18.0.0",
|
|
50
51
|
"react-router-dom": "^6.8.1"
|
|
51
52
|
},
|