@rspress/plugin-preview 2.0.0-alpha.10 → 2.0.0-alpha.12

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
@@ -77,7 +77,18 @@ var __webpack_exports__ = {};
77
77
  import DemoBlock from ${JSON.stringify(path)};
78
78
  ${str}
79
79
  `;
80
- function generateEntry(demos, framework, position) {
80
+ const getLangFileExt = (lang)=>{
81
+ switch(lang){
82
+ case 'jsx':
83
+ case 'tsx':
84
+ return 'tsx';
85
+ case 'json':
86
+ return 'tsx';
87
+ default:
88
+ return lang;
89
+ }
90
+ };
91
+ function generateEntry(demos, framework, position, customEntry) {
81
92
  const sourceEntry = {};
82
93
  const entryCssPath = (0, external_node_path_namespaceObject.join)(staticPath, 'global-styles', 'entry.css');
83
94
  if ('follow' === position) Object.values(demos).forEach((routes)=>{
@@ -96,7 +107,10 @@ var __webpack_exports__ = {};
96
107
  import Demo from ${JSON.stringify(demoPath)};
97
108
  render(<Demo />, document.getElementById('root'));
98
109
  `;
99
- const entryContent = 'react' === framework ? reactEntry : solidEntry;
110
+ const entryContent = customEntry ? customEntry({
111
+ entryCssPath,
112
+ demoPath
113
+ }) : 'react' === framework ? reactEntry : solidEntry;
100
114
  (0, external_node_fs_namespaceObject.writeFileSync)(entry, entryContent);
101
115
  sourceEntry[id] = entry;
102
116
  });
@@ -423,14 +437,18 @@ var __webpack_exports__ = {};
423
437
  if ('hasVisited' in node) return;
424
438
  if (node.lang && previewLanguages.includes(node.lang)) {
425
439
  var _node_meta, _node_meta1, _node_meta2, _node_meta3, _node_meta4, _node_meta5;
426
- 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;
427
- const value = injectDemoBlockImport(previewCodeTransform({
440
+ 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;
441
+ const isJsx = 'jsx' === node.lang || 'tsx' === node.lang;
442
+ const value = isJsx ? injectDemoBlockImport(previewCodeTransform({
428
443
  language: node.lang,
429
444
  code: node.value
430
- }), demoBlockComponentPath);
431
- 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;
445
+ }), demoBlockComponentPath) : previewCodeTransform({
446
+ language: node.lang,
447
+ code: node.value
448
+ });
449
+ 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
450
  const id = generateId(pageName, index++);
433
- const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.tsx`);
451
+ const virtualModulePath = (0, external_node_path_namespaceObject.join)(virtualDir, `${id}.${getLangFileExt(node.lang)}`);
434
452
  constructDemoNode(id, virtualModulePath, node, isMobileMode);
435
453
  if (external_node_fs_default().existsSync(virtualModulePath)) {
436
454
  const content = external_node_fs_default().readFileSync(virtualModulePath, 'utf-8');
@@ -443,7 +461,7 @@ var __webpack_exports__ = {};
443
461
  if (remarkPlugin_demos[pageName].length > 0) data.pageMeta.haveDemos = true;
444
462
  };
445
463
  };
446
- let src_rslib_entry_routeMeta;
464
+ let src_routeMeta;
447
465
  const DEFAULT_PREVIEW_LANGUAGES = [
448
466
  'jsx',
449
467
  'tsx'
@@ -451,11 +469,11 @@ var __webpack_exports__ = {};
451
469
  function pluginPreview(options) {
452
470
  const { isMobile = false, iframeOptions = {}, iframePosition = 'follow', defaultRenderMode = 'preview', previewLanguages = DEFAULT_PREVIEW_LANGUAGES, previewCodeTransform = ({ code })=>code } = options ?? {};
453
471
  const previewMode = (null == options ? void 0 : options.previewMode) ?? (isMobile ? 'iframe' : 'internal');
454
- const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {} } = iframeOptions;
472
+ const { devPort = 7890, framework = 'react', position = iframePosition, builderConfig = {}, customEntry } = iframeOptions;
455
473
  const globalUIComponents = 'fixed' === position ? [
456
474
  (0, external_node_path_namespaceObject.join)(staticPath, 'global-components', 'Device.tsx')
457
475
  ] : [];
458
- const getRouteMeta = ()=>src_rslib_entry_routeMeta;
476
+ const getRouteMeta = ()=>src_routeMeta;
459
477
  let lastDemos;
460
478
  let devServer;
461
479
  let clientConfig;
@@ -468,7 +486,7 @@ var __webpack_exports__ = {};
468
486
  return config;
469
487
  },
470
488
  routeGenerated (routes) {
471
- src_rslib_entry_routeMeta = routes;
489
+ src_routeMeta = routes;
472
490
  },
473
491
  async beforeBuild (_, isProd) {
474
492
  if (!isProd) try {
@@ -492,9 +510,9 @@ var __webpack_exports__ = {};
492
510
  var _devServer_server;
493
511
  if ((0, external_lodash_namespaceObject.isEqual)(remarkPlugin_demos, lastDemos)) return;
494
512
  lastDemos = (0, external_lodash_namespaceObject.cloneDeep)(remarkPlugin_demos);
495
- await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
513
+ await (null == devServer ? void 0 : null == (_devServer_server = devServer.server) ? void 0 : _devServer_server.close());
496
514
  devServer = void 0;
497
- const sourceEntry = generateEntry(remarkPlugin_demos, framework, position);
515
+ const sourceEntry = generateEntry(remarkPlugin_demos, framework, position, customEntry);
498
516
  const outDir = (0, external_node_path_namespaceObject.join)(config.outDir ?? 'doc_build', '~demo');
499
517
  if (0 === Object.keys(sourceEntry).length) return;
500
518
  const { html, source, output, performance } = clientConfig ?? {};
@@ -570,11 +588,11 @@ var __webpack_exports__ = {};
570
588
  setup: (api)=>{
571
589
  api.modifyRsbuildConfig((config)=>{
572
590
  var _config_output;
573
- if ((null === (_config_output = config.output) || void 0 === _config_output ? void 0 : _config_output.target) === 'web') clientConfig = config;
591
+ if ((null == (_config_output = config.output) ? void 0 : _config_output.target) === 'web') clientConfig = config;
574
592
  });
575
593
  api.onCloseDevServer(async ()=>{
576
594
  var _devServer_server;
577
- await (null == devServer ? void 0 : null === (_devServer_server = devServer.server) || void 0 === _devServer_server ? void 0 : _devServer_server.close());
595
+ await (null == devServer ? void 0 : null == (_devServer_server = devServer.server) ? void 0 : _devServer_server.close());
578
596
  devServer = void 0;
579
597
  });
580
598
  }
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.10",
3
+ "version": "2.0.0-alpha.12",
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.4",
21
- "@rsbuild/plugin-babel": "~1.0.4",
20
+ "@rsbuild/core": "1.3.6",
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-alpha.10",
29
- "@rspress/theme-default": "2.0.0-alpha.10"
28
+ "@rspress/shared": "2.0.0-alpha.12",
29
+ "@rspress/theme-default": "2.0.0-alpha.12"
30
30
  },
31
31
  "devDependencies": {
32
- "@rslib/core": "0.6.1",
32
+ "@rslib/core": "0.6.3",
33
33
  "@types/lodash": "^4.17.16",
34
34
  "@types/mdast": "^4.0.4",
35
35
  "@types/node": "^18.11.17",
@@ -45,7 +45,7 @@
45
45
  "unist-util-visit": "^5.0.0"
46
46
  },
47
47
  "peerDependencies": {
48
- "@rspress/core": "^2.0.0-alpha.10",
48
+ "@rspress/core": "^2.0.0-alpha.12",
49
49
  "react": ">=17.0.0",
50
50
  "react-router-dom": "^6.8.1"
51
51
  },