@rspress/plugin-preview 2.0.0-alpha.8 → 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 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
- function generateEntry(demos, framework, position) {
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 { render } from 'react-dom';
109
+ import { createRoot } from 'react-dom/client';
95
110
  import ${JSON.stringify(entryCssPath)};
96
111
  import Demo from ${JSON.stringify(demoPath)};
97
- render(<Demo />, document.getElementById('root'));
112
+ const container = document.getElementById('root');
113
+ createRoot(container).render(<Demo />);
98
114
  `;
99
- const entryContent = 'react' === framework ? reactEntry : solidEntry;
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 { render } from 'react-dom';
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
- render(<App /> , document.getElementById('root'));
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';
@@ -142,9 +162,9 @@ var __webpack_exports__ = {};
142
162
  const node_utils_namespaceObject = require("@rspress/shared/node-utils");
143
163
  const convert = function(test) {
144
164
  if (null == test) return ok;
145
- if ('string' == typeof test) return typeFactory(test);
146
- if ('object' == typeof test) return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
147
165
  if ('function' == typeof test) return castFactory(test);
166
+ if ('object' == typeof test) return Array.isArray(test) ? anyFactory(test) : propsFactory(test);
167
+ if ('string' == typeof test) return typeFactory(test);
148
168
  throw new Error('Expected function, string, or object as test');
149
169
  };
150
170
  function anyFactory(tests) {
@@ -154,15 +174,17 @@ var __webpack_exports__ = {};
154
174
  return castFactory(any);
155
175
  function any(...parameters) {
156
176
  let index = -1;
157
- while(++index < checks.length)if (checks[index].call(this, ...parameters)) return true;
177
+ while(++index < checks.length)if (checks[index].apply(this, parameters)) return true;
158
178
  return false;
159
179
  }
160
180
  }
161
181
  function propsFactory(check) {
182
+ const checkAsRecord = check;
162
183
  return castFactory(all);
163
184
  function all(node) {
185
+ const nodeAsRecord = node;
164
186
  let key;
165
- for(key in check)if (node[key] !== check[key]) return false;
187
+ for(key in check)if (nodeAsRecord[key] !== checkAsRecord[key]) return false;
166
188
  return true;
167
189
  }
168
190
  }
@@ -172,28 +194,32 @@ var __webpack_exports__ = {};
172
194
  return node && node.type === check;
173
195
  }
174
196
  }
175
- function castFactory(check) {
176
- return assertion;
177
- function assertion(node, ...parameters) {
178
- return Boolean(node && 'object' == typeof node && 'type' in node && Boolean(check.call(this, node, ...parameters)));
197
+ function castFactory(testFunction) {
198
+ return check;
199
+ function check(value, index, parent) {
200
+ return Boolean(looksLikeANode(value) && testFunction.call(this, value, 'number' == typeof index ? index : void 0, parent || void 0));
179
201
  }
180
202
  }
181
203
  function ok() {
182
204
  return true;
183
205
  }
206
+ function looksLikeANode(value) {
207
+ return null !== value && 'object' == typeof value && 'type' in value;
208
+ }
184
209
  function color(d) {
185
210
  return '\u001B[33m' + d + '\u001B[39m';
186
211
  }
212
+ const empty = [];
187
213
  const CONTINUE = true;
188
214
  const EXIT = false;
189
215
  const SKIP = 'skip';
190
- const visitParents = function(tree, test, visitor, reverse) {
216
+ function visitParents(tree, test, visitor, reverse) {
217
+ let check;
191
218
  if ('function' == typeof test && 'function' != typeof visitor) {
192
219
  reverse = visitor;
193
220
  visitor = test;
194
- test = null;
195
- }
196
- const is = convert(test);
221
+ } else check = test;
222
+ const is = convert(check);
197
223
  const step = reverse ? -1 : 1;
198
224
  factory(tree, void 0, [])();
199
225
  function factory(node, index, parents) {
@@ -206,49 +232,61 @@ var __webpack_exports__ = {};
206
232
  }
207
233
  return visit;
208
234
  function visit() {
209
- let result = [];
235
+ let result = empty;
210
236
  let subresult;
211
237
  let offset;
212
238
  let grandparents;
213
- if (!test || is(node, index, parents[parents.length - 1] || null)) {
239
+ if (!test || is(node, index, parents[parents.length - 1] || void 0)) {
214
240
  result = toResult(visitor(node, parents));
215
241
  if (result[0] === EXIT) return result;
216
242
  }
217
- if (node.children && result[0] !== SKIP) {
218
- offset = (reverse ? node.children.length : -1) + step;
219
- grandparents = parents.concat(node);
220
- while(offset > -1 && offset < node.children.length){
221
- subresult = factory(node.children[offset], offset, grandparents)();
222
- if (subresult[0] === EXIT) return subresult;
223
- offset = 'number' == typeof subresult[1] ? subresult[1] : offset + step;
243
+ if ('children' in node && node.children) {
244
+ const nodeAsParent = node;
245
+ if (nodeAsParent.children && result[0] !== SKIP) {
246
+ offset = (reverse ? nodeAsParent.children.length : -1) + step;
247
+ grandparents = parents.concat(nodeAsParent);
248
+ while(offset > -1 && offset < nodeAsParent.children.length){
249
+ const child = nodeAsParent.children[offset];
250
+ subresult = factory(child, offset, grandparents)();
251
+ if (subresult[0] === EXIT) return subresult;
252
+ offset = 'number' == typeof subresult[1] ? subresult[1] : offset + step;
253
+ }
224
254
  }
225
255
  }
226
256
  return result;
227
257
  }
228
258
  }
229
- };
259
+ }
230
260
  function toResult(value) {
231
261
  if (Array.isArray(value)) return value;
232
262
  if ('number' == typeof value) return [
233
263
  CONTINUE,
234
264
  value
235
265
  ];
236
- return [
266
+ return null == value ? empty : [
237
267
  value
238
268
  ];
239
269
  }
240
- const lib_visit = function(tree, test, visitor, reverse) {
241
- if ('function' == typeof test && 'function' != typeof visitor) {
242
- reverse = visitor;
243
- visitor = test;
244
- test = null;
270
+ function lib_visit(tree, testOrVisitor, visitorOrReverse, maybeReverse) {
271
+ let reverse;
272
+ let test;
273
+ let visitor;
274
+ if ('function' == typeof testOrVisitor && 'function' != typeof visitorOrReverse) {
275
+ test = void 0;
276
+ visitor = testOrVisitor;
277
+ reverse = visitorOrReverse;
278
+ } else {
279
+ test = testOrVisitor;
280
+ visitor = visitorOrReverse;
281
+ reverse = maybeReverse;
245
282
  }
246
283
  visitParents(tree, test, overload, reverse);
247
284
  function overload(node, parents) {
248
285
  const parent = parents[parents.length - 1];
249
- return visitor(node, parent ? parent.children.indexOf(node) : null, parent);
286
+ const index = parent ? parent.children.indexOf(node) : void 0;
287
+ return visitor(node, index, parent);
250
288
  }
251
- };
289
+ }
252
290
  const getASTNodeImport = (name, from)=>({
253
291
  type: 'mdxjsEsm',
254
292
  value: `import ${name} from ${JSON.stringify(from)}`,
@@ -405,14 +443,18 @@ var __webpack_exports__ = {};
405
443
  if ('hasVisited' in node) return;
406
444
  if (node.lang && previewLanguages.includes(node.lang)) {
407
445
  var _node_meta, _node_meta1, _node_meta2, _node_meta3, _node_meta4, _node_meta5;
408
- if ((null === (_node_meta = node.meta) || void 0 === _node_meta ? void 0 : _node_meta.includes('pure')) || !(null === (_node_meta1 = node.meta) || void 0 === _node_meta1 ? void 0 : _node_meta1.includes('preview')) && 'pure' === defaultRenderMode) return;
409
- const value = injectDemoBlockImport(previewCodeTransform({
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({
410
449
  language: node.lang,
411
450
  code: node.value
412
- }), demoBlockComponentPath);
413
- const isMobileMode = (null === (_node_meta2 = node.meta) || void 0 === _node_meta2 ? void 0 : _node_meta2.includes('mobile')) || (null === (_node_meta3 = node.meta) || void 0 === _node_meta3 ? void 0 : _node_meta3.includes('iframe')) || !(null === (_node_meta4 = node.meta) || void 0 === _node_meta4 ? void 0 : _node_meta4.includes('web')) && !(null === (_node_meta5 = node.meta) || void 0 === _node_meta5 ? void 0 : _node_meta5.includes('internal')) && 'iframe' === previewMode;
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;
414
456
  const id = generateId(pageName, index++);
415
- const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.tsx`);
457
+ const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.${getLangFileExt(node.lang)}`);
416
458
  constructDemoNode(id, virtualModulePath, node, isMobileMode);
417
459
  if (external_node_fs_default().existsSync(virtualModulePath)) {
418
460
  const content = external_node_fs_default().readFileSync(virtualModulePath, 'utf-8');
@@ -425,7 +467,7 @@ var __webpack_exports__ = {};
425
467
  if (remarkPlugin_demos[pageName].length > 0) data.pageMeta.haveDemos = true;
426
468
  };
427
469
  };
428
- let src_rslib_entry_routeMeta;
470
+ let src_routeMeta;
429
471
  const DEFAULT_PREVIEW_LANGUAGES = [
430
472
  'jsx',
431
473
  'tsx'
@@ -433,11 +475,11 @@ var __webpack_exports__ = {};
433
475
  function pluginPreview(options) {
434
476
  const { isMobile = false, iframeOptions = {}, iframePosition = 'follow', defaultRenderMode = 'preview', previewLanguages = DEFAULT_PREVIEW_LANGUAGES, previewCodeTransform = ({ code })=>code } = options ?? {};
435
477
  const previewMode = (null == options ? void 0 : options.previewMode) ?? (isMobile ? 'iframe' : 'internal');
436
- const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {} } = iframeOptions;
478
+ const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {}, customEntry } = iframeOptions;
437
479
  const globalUIComponents = 'fixed' === position ? [
438
480
  (0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Device.tsx')
439
481
  ] : [];
440
- const getRouteMeta = ()=>src_rslib_entry_routeMeta;
482
+ const getRouteMeta = ()=>src_routeMeta;
441
483
  let lastDemos;
442
484
  let devServer;
443
485
  let clientConfig;
@@ -450,7 +492,7 @@ var __webpack_exports__ = {};
450
492
  return config;
451
493
  },
452
494
  routeGenerated (routes) {
453
- src_rslib_entry_routeMeta = routes;
495
+ src_routeMeta = routes;
454
496
  },
455
497
  async beforeBuild (_, isProd) {
456
498
  if (!isProd) try {
@@ -474,9 +516,9 @@ var __webpack_exports__ = {};
474
516
  var _devServer_server;
475
517
  if ((0, external_lodash_namespaceObject.isEqual)(remarkPlugin_demos, lastDemos)) return;
476
518
  lastDemos = (0, external_lodash_namespaceObject.cloneDeep)(remarkPlugin_demos);
477
- await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
519
+ await (null == devServer ? void 0 : null == (_devServer_server = devServer.server) ? void 0 : _devServer_server.close());
478
520
  devServer = void 0;
479
- const sourceEntry = generateEntry(remarkPlugin_demos, framework, position);
521
+ const sourceEntry = generateEntry(remarkPlugin_demos, framework, position, customEntry);
480
522
  const outDir = (0, external_node_path_namespaceObject.join)(config.outDir ?? 'doc_build', '~demo');
481
523
  if (0 === Object.keys(sourceEntry).length) return;
482
524
  const { html, source, output, performance } = clientConfig ?? {};
@@ -552,11 +594,11 @@ var __webpack_exports__ = {};
552
594
  setup: (api)=>{
553
595
  api.modifyRsbuildConfig((config)=>{
554
596
  var _config_output;
555
- if ((null === (_config_output = config.output) || void 0 === _config_output ? void 0 : _config_output.target) === 'web') clientConfig = config;
597
+ if ((null == (_config_output = config.output) ? void 0 : _config_output.target) === 'web') clientConfig = config;
556
598
  });
557
599
  api.onCloseDevServer(async ()=>{
558
600
  var _devServer_server;
559
- await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
601
+ await (null == devServer ? void 0 : null == (_devServer_server = devServer.server) ? void 0 : _devServer_server.close());
560
602
  devServer = void 0;
561
603
  });
562
604
  }
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,7 @@
1
1
  export declare const generateId: (pageName: string, index: number) => string;
2
2
 
3
+ export declare const getLangFileExt: (lang: string) => string;
4
+
3
5
  export declare const injectDemoBlockImport: (str: string, path: string) => string;
4
6
 
5
7
  /**
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-alpha.8",
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,35 +17,35 @@
17
17
  "static"
18
18
  ],
19
19
  "dependencies": {
20
- "@rsbuild/core": "1.3.0",
21
- "@rsbuild/plugin-babel": "~1.0.4",
22
- "@rsbuild/plugin-less": "~1.2.1",
20
+ "@rsbuild/core": "1.3.9",
21
+ "@rsbuild/plugin-babel": "~1.0.5",
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-alpha.8",
29
- "@rspress/theme-default": "2.0.0-alpha.8"
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.0",
32
+ "@rslib/core": "0.6.5",
33
33
  "@types/lodash": "^4.17.16",
34
- "@types/mdast": "^3.0.15",
34
+ "@types/mdast": "^4.0.4",
35
35
  "@types/node": "^18.11.17",
36
36
  "@types/react": "^18.3.20",
37
- "@types/react-dom": "^18.3.5",
38
- "mdast-util-mdx-jsx": "^2.1.4",
39
- "mdast-util-mdxjs-esm": "^1.3.1",
40
- "react": "^18.3.1",
41
- "react-dom": "^18.3.1",
37
+ "@types/react-dom": "^18.3.6",
38
+ "mdast-util-mdx-jsx": "^3.2.0",
39
+ "mdast-util-mdxjs-esm": "^2.0.1",
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
- "unified": "^10.1.2",
45
- "unist-util-visit": "^4.1.2"
44
+ "unified": "^11.0.5",
45
+ "unist-util-visit": "^5.0.0"
46
46
  },
47
47
  "peerDependencies": {
48
- "@rspress/core": "^2.0.0-alpha.8",
48
+ "@rspress/core": "^2.0.0-beta.0",
49
49
  "react": ">=17.0.0",
50
50
  "react-router-dom": "^6.8.1"
51
51
  },