@rspress/plugin-preview 2.0.0-alpha.9 → 2.0.0-beta.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.d.ts +9 -0
- package/dist/index.js +45 -21
- package/dist/utils.d.ts +2 -0
- package/dist/utils.js +14 -0
- package/package.json +9 -9
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
import type { RsbuildConfig } from '@rsbuild/core';
|
2
2
|
import { RspressPlugin } from '@rspress/shared';
|
3
3
|
|
4
|
+
declare interface CustomEntry {
|
5
|
+
entryCssPath: string;
|
6
|
+
demoPath: string;
|
7
|
+
}
|
8
|
+
|
4
9
|
declare type IframeOptions = {
|
5
10
|
/**
|
6
11
|
* framework in the iframe
|
@@ -18,6 +23,10 @@ declare type IframeOptions = {
|
|
18
23
|
*/
|
19
24
|
devPort?: number;
|
20
25
|
builderConfig?: RsbuildConfig;
|
26
|
+
/**
|
27
|
+
* custom support for other web frameworks
|
28
|
+
*/
|
29
|
+
customEntry?: (meta: CustomEntry) => string;
|
21
30
|
};
|
22
31
|
|
23
32
|
export declare type Options = {
|
package/dist/index.js
CHANGED
@@ -1,10 +1,14 @@
|
|
1
1
|
"use strict";
|
2
2
|
var __webpack_modules__ = {
|
3
3
|
"@rsbuild/plugin-less": function(module) {
|
4
|
-
module.exports = import("@rsbuild/plugin-less")
|
4
|
+
module.exports = import("@rsbuild/plugin-less").then(function(module) {
|
5
|
+
return module;
|
6
|
+
});
|
5
7
|
},
|
6
8
|
"@rsbuild/plugin-sass": function(module) {
|
7
|
-
module.exports = import("@rsbuild/plugin-sass")
|
9
|
+
module.exports = import("@rsbuild/plugin-sass").then(function(module) {
|
10
|
+
return module;
|
11
|
+
});
|
8
12
|
}
|
9
13
|
};
|
10
14
|
var __webpack_module_cache__ = {};
|
@@ -77,7 +81,18 @@ var __webpack_exports__ = {};
|
|
77
81
|
import DemoBlock from ${JSON.stringify(path)};
|
78
82
|
${str}
|
79
83
|
`;
|
80
|
-
|
84
|
+
const getLangFileExt = (lang)=>{
|
85
|
+
switch(lang){
|
86
|
+
case 'jsx':
|
87
|
+
case 'tsx':
|
88
|
+
return 'tsx';
|
89
|
+
case 'json':
|
90
|
+
return 'tsx';
|
91
|
+
default:
|
92
|
+
return lang;
|
93
|
+
}
|
94
|
+
};
|
95
|
+
function generateEntry(demos, framework, position, customEntry) {
|
81
96
|
const sourceEntry = {};
|
82
97
|
const entryCssPath = (0, external_node_path_namespaceObject.join)(staticPath, 'global-styles', 'entry.css');
|
83
98
|
if ('follow' === position) Object.values(demos).forEach((routes)=>{
|
@@ -91,12 +106,16 @@ var __webpack_exports__ = {};
|
|
91
106
|
render(() => <Demo />, document.getElementById('root'));
|
92
107
|
`;
|
93
108
|
const reactEntry = `
|
94
|
-
import {
|
109
|
+
import { createRoot } from 'react-dom/client';
|
95
110
|
import ${JSON.stringify(entryCssPath)};
|
96
111
|
import Demo from ${JSON.stringify(demoPath)};
|
97
|
-
|
112
|
+
const container = document.getElementById('root');
|
113
|
+
createRoot(container).render(<Demo />);
|
98
114
|
`;
|
99
|
-
const entryContent =
|
115
|
+
const entryContent = customEntry ? customEntry({
|
116
|
+
entryCssPath,
|
117
|
+
demoPath
|
118
|
+
}) : 'react' === framework ? reactEntry : solidEntry;
|
100
119
|
(0, external_node_fs_namespaceObject.writeFileSync)(entry, entryContent);
|
101
120
|
sourceEntry[id] = entry;
|
102
121
|
});
|
@@ -104,7 +123,7 @@ var __webpack_exports__ = {};
|
|
104
123
|
else Object.entries(demos).forEach(([key, routes])=>{
|
105
124
|
if (0 === routes.length) return;
|
106
125
|
const reactContent = `
|
107
|
-
import {
|
126
|
+
import { createRoot } from 'react-dom/client';
|
108
127
|
import ${JSON.stringify(entryCssPath)};
|
109
128
|
${routes.map((demo, index)=>`import Demo_${index} from ${JSON.stringify(demo.path)}`).join('\n')}
|
110
129
|
function App() {
|
@@ -115,7 +134,8 @@ var __webpack_exports__ = {};
|
|
115
134
|
</div>
|
116
135
|
)
|
117
136
|
}
|
118
|
-
|
137
|
+
const container = document.getElementById('root');
|
138
|
+
createRoot(container).render(<App />);
|
119
139
|
`;
|
120
140
|
const solidContent = `
|
121
141
|
import { render } from 'solid-js/web';
|
@@ -423,14 +443,18 @@ var __webpack_exports__ = {};
|
|
423
443
|
if ('hasVisited' in node) return;
|
424
444
|
if (node.lang && previewLanguages.includes(node.lang)) {
|
425
445
|
var _node_meta, _node_meta1, _node_meta2, _node_meta3, _node_meta4, _node_meta5;
|
426
|
-
if ((null
|
427
|
-
const
|
446
|
+
if ((null == (_node_meta = node.meta) ? void 0 : _node_meta.includes('pure')) || !(null == (_node_meta1 = node.meta) ? void 0 : _node_meta1.includes('preview')) && 'pure' === defaultRenderMode) return;
|
447
|
+
const isJsx = 'jsx' === node.lang || 'tsx' === node.lang;
|
448
|
+
const value = isJsx ? injectDemoBlockImport(previewCodeTransform({
|
428
449
|
language: node.lang,
|
429
450
|
code: node.value
|
430
|
-
}), demoBlockComponentPath)
|
431
|
-
|
451
|
+
}), demoBlockComponentPath) : previewCodeTransform({
|
452
|
+
language: node.lang,
|
453
|
+
code: node.value
|
454
|
+
});
|
455
|
+
const isMobileMode = (null == (_node_meta2 = node.meta) ? void 0 : _node_meta2.includes('mobile')) || (null == (_node_meta3 = node.meta) ? void 0 : _node_meta3.includes('iframe')) || !(null == (_node_meta4 = node.meta) ? void 0 : _node_meta4.includes('web')) && !(null == (_node_meta5 = node.meta) ? void 0 : _node_meta5.includes('internal')) && 'iframe' === previewMode;
|
432
456
|
const id = generateId(pageName, index++);
|
433
|
-
const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.
|
457
|
+
const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.${getLangFileExt(node.lang)}`);
|
434
458
|
constructDemoNode(id, virtualModulePath, node, isMobileMode);
|
435
459
|
if (external_node_fs_default().existsSync(virtualModulePath)) {
|
436
460
|
const content = external_node_fs_default().readFileSync(virtualModulePath, 'utf-8');
|
@@ -443,7 +467,7 @@ var __webpack_exports__ = {};
|
|
443
467
|
if (remarkPlugin_demos[pageName].length > 0) data.pageMeta.haveDemos = true;
|
444
468
|
};
|
445
469
|
};
|
446
|
-
let
|
470
|
+
let src_routeMeta;
|
447
471
|
const DEFAULT_PREVIEW_LANGUAGES = [
|
448
472
|
'jsx',
|
449
473
|
'tsx'
|
@@ -451,11 +475,11 @@ var __webpack_exports__ = {};
|
|
451
475
|
function pluginPreview(options) {
|
452
476
|
const { isMobile = false, iframeOptions = {}, iframePosition = 'follow', defaultRenderMode = 'preview', previewLanguages = DEFAULT_PREVIEW_LANGUAGES, previewCodeTransform = ({ code })=>code } = options ?? {};
|
453
477
|
const previewMode = (null == options ? void 0 : options.previewMode) ?? (isMobile ? 'iframe' : 'internal');
|
454
|
-
const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {} } = iframeOptions;
|
478
|
+
const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {}, customEntry } = iframeOptions;
|
455
479
|
const globalUIComponents = 'fixed' === position ? [
|
456
480
|
(0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Device.tsx')
|
457
481
|
] : [];
|
458
|
-
const getRouteMeta = ()=>
|
482
|
+
const getRouteMeta = ()=>src_routeMeta;
|
459
483
|
let lastDemos;
|
460
484
|
let devServer;
|
461
485
|
let clientConfig;
|
@@ -468,7 +492,7 @@ var __webpack_exports__ = {};
|
|
468
492
|
return config;
|
469
493
|
},
|
470
494
|
routeGenerated (routes) {
|
471
|
-
|
495
|
+
src_routeMeta = routes;
|
472
496
|
},
|
473
497
|
async beforeBuild (_, isProd) {
|
474
498
|
if (!isProd) try {
|
@@ -492,9 +516,9 @@ var __webpack_exports__ = {};
|
|
492
516
|
var _devServer_server;
|
493
517
|
if ((0, external_lodash_namespaceObject.isEqual)(remarkPlugin_demos, lastDemos)) return;
|
494
518
|
lastDemos = (0, external_lodash_namespaceObject.cloneDeep)(remarkPlugin_demos);
|
495
|
-
await (null == devServer ? void 0 : null
|
519
|
+
await (null == devServer ? void 0 : null == (_devServer_server = devServer.server) ? void 0 : _devServer_server.close());
|
496
520
|
devServer = void 0;
|
497
|
-
const sourceEntry = generateEntry(remarkPlugin_demos, framework, position);
|
521
|
+
const sourceEntry = generateEntry(remarkPlugin_demos, framework, position, customEntry);
|
498
522
|
const outDir = (0, external_node_path_namespaceObject.join)(config.outDir ?? 'doc_build', '~demo');
|
499
523
|
if (0 === Object.keys(sourceEntry).length) return;
|
500
524
|
const { html, source, output, performance } = clientConfig ?? {};
|
@@ -570,11 +594,11 @@ var __webpack_exports__ = {};
|
|
570
594
|
setup: (api)=>{
|
571
595
|
api.modifyRsbuildConfig((config)=>{
|
572
596
|
var _config_output;
|
573
|
-
if ((null
|
597
|
+
if ((null == (_config_output = config.output) ? void 0 : _config_output.target) === 'web') clientConfig = config;
|
574
598
|
});
|
575
599
|
api.onCloseDevServer(async ()=>{
|
576
600
|
var _devServer_server;
|
577
|
-
await (null == devServer ? void 0 : null
|
601
|
+
await (null == devServer ? void 0 : null == (_devServer_server = devServer.server) ? void 0 : _devServer_server.close());
|
578
602
|
devServer = void 0;
|
579
603
|
});
|
580
604
|
}
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
@@ -25,6 +25,7 @@ var __webpack_exports__ = {};
|
|
25
25
|
__webpack_require__.r(__webpack_exports__);
|
26
26
|
__webpack_require__.d(__webpack_exports__, {
|
27
27
|
generateId: ()=>generateId,
|
28
|
+
getLangFileExt: ()=>getLangFileExt,
|
28
29
|
injectDemoBlockImport: ()=>injectDemoBlockImport,
|
29
30
|
normalizeId: ()=>normalizeId,
|
30
31
|
toValidVarName: ()=>toValidVarName
|
@@ -42,12 +43,25 @@ const injectDemoBlockImport = (str, path)=>`
|
|
42
43
|
import DemoBlock from ${JSON.stringify(path)};
|
43
44
|
${str}
|
44
45
|
`;
|
46
|
+
const getLangFileExt = (lang)=>{
|
47
|
+
switch(lang){
|
48
|
+
case 'jsx':
|
49
|
+
case 'tsx':
|
50
|
+
return 'tsx';
|
51
|
+
case 'json':
|
52
|
+
return 'tsx';
|
53
|
+
default:
|
54
|
+
return lang;
|
55
|
+
}
|
56
|
+
};
|
45
57
|
exports.generateId = __webpack_exports__.generateId;
|
58
|
+
exports.getLangFileExt = __webpack_exports__.getLangFileExt;
|
46
59
|
exports.injectDemoBlockImport = __webpack_exports__.injectDemoBlockImport;
|
47
60
|
exports.normalizeId = __webpack_exports__.normalizeId;
|
48
61
|
exports.toValidVarName = __webpack_exports__.toValidVarName;
|
49
62
|
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
50
63
|
"generateId",
|
64
|
+
"getLangFileExt",
|
51
65
|
"injectDemoBlockImport",
|
52
66
|
"normalizeId",
|
53
67
|
"toValidVarName"
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@rspress/plugin-preview",
|
3
|
-
"version": "2.0.0-
|
3
|
+
"version": "2.0.0-beta.0",
|
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": {
|
@@ -17,19 +17,19 @@
|
|
17
17
|
"static"
|
18
18
|
],
|
19
19
|
"dependencies": {
|
20
|
-
"@rsbuild/core": "1.3.
|
21
|
-
"@rsbuild/plugin-babel": "~1.0.
|
20
|
+
"@rsbuild/core": "1.3.9",
|
21
|
+
"@rsbuild/plugin-babel": "~1.0.5",
|
22
22
|
"@rsbuild/plugin-less": "~1.2.2",
|
23
23
|
"@rsbuild/plugin-react": "~1.1.1",
|
24
24
|
"@rsbuild/plugin-sass": "~1.3.1",
|
25
25
|
"@rsbuild/plugin-solid": "~1.0.5",
|
26
26
|
"lodash": "4.17.21",
|
27
27
|
"qrcode.react": "^4.2.0",
|
28
|
-
"@rspress/shared": "2.0.0-
|
29
|
-
"@rspress/theme-default": "2.0.0-
|
28
|
+
"@rspress/shared": "2.0.0-beta.0",
|
29
|
+
"@rspress/theme-default": "2.0.0-beta.0"
|
30
30
|
},
|
31
31
|
"devDependencies": {
|
32
|
-
"@rslib/core": "0.6.
|
32
|
+
"@rslib/core": "0.6.5",
|
33
33
|
"@types/lodash": "^4.17.16",
|
34
34
|
"@types/mdast": "^4.0.4",
|
35
35
|
"@types/node": "^18.11.17",
|
@@ -37,15 +37,15 @@
|
|
37
37
|
"@types/react-dom": "^18.3.6",
|
38
38
|
"mdast-util-mdx-jsx": "^3.2.0",
|
39
39
|
"mdast-util-mdxjs-esm": "^2.0.1",
|
40
|
-
"react": "^
|
41
|
-
"react-dom": "^
|
40
|
+
"react": "^19.1.0",
|
41
|
+
"react-dom": "^19.1.0",
|
42
42
|
"react-router-dom": "^6.29.0",
|
43
43
|
"typescript": "^5.8.2",
|
44
44
|
"unified": "^11.0.5",
|
45
45
|
"unist-util-visit": "^5.0.0"
|
46
46
|
},
|
47
47
|
"peerDependencies": {
|
48
|
-
"@rspress/core": "^2.0.0-
|
48
|
+
"@rspress/core": "^2.0.0-beta.0",
|
49
49
|
"react": ">=17.0.0",
|
50
50
|
"react-router-dom": "^6.8.1"
|
51
51
|
},
|