@rspress/plugin-preview 2.0.0 → 2.0.2

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 CHANGED
@@ -398,6 +398,7 @@ function pluginPreview(options) {
398
398
  const { preEntry: _, ...otherSourceOptions } = source ?? {};
399
399
  const rsbuildConfig = mergeRsbuildConfig({
400
400
  server: {
401
+ host: true,
401
402
  port: devPort,
402
403
  printUrls: ()=>void 0,
403
404
  strictPort: true
@@ -493,7 +494,10 @@ function pluginPreview(options) {
493
494
  source: {
494
495
  include: [
495
496
  join(__dirname, '..')
496
- ]
497
+ ],
498
+ define: {
499
+ 'process.env.RSPRESS_IFRAME_DEV_PORT': JSON.stringify(devPort)
500
+ }
497
501
  },
498
502
  tools: {
499
503
  bundlerChain (chain) {
@@ -524,9 +528,6 @@ function pluginPreview(options) {
524
528
  }
525
529
  ]
526
530
  },
527
- extendPageData (pageData, isProd) {
528
- if (!isProd) pageData.devPort = port;
529
- },
530
531
  markdown: {
531
532
  remarkPlugins: [
532
533
  [
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.2",
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,13 @@
23
23
  "static"
24
24
  ],
25
25
  "dependencies": {
26
- "@rsbuild/core": "2.0.0-alpha.4",
26
+ "@rsbuild/core": "2.0.0-beta.1",
27
27
  "@rsbuild/plugin-babel": "~1.1.0",
28
- "@rsbuild/plugin-react": "~1.4.3",
28
+ "@rsbuild/plugin-react": "~1.4.5",
29
29
  "qrcode.react": "^4.2.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@rslib/core": "0.19.3",
32
+ "@rslib/core": "0.19.4",
33
33
  "@types/mdast": "^4.0.4",
34
34
  "@types/node": "^22.8.1",
35
35
  "@types/react": "^19.2.10",
@@ -38,19 +38,19 @@
38
38
  "mdast-util-mdxjs-esm": "^2.0.1",
39
39
  "react": "^19.2.4",
40
40
  "react-dom": "^19.2.4",
41
- "react-router-dom": "^7.12.0",
41
+ "react-router-dom": "^7.13.0",
42
42
  "rsbuild-plugin-publint": "^0.3.4",
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.2",
49
49
  "react": ">=18.0.0",
50
50
  "react-router-dom": "^6.8.1"
51
51
  },
52
52
  "engines": {
53
- "node": ">=20.9.0"
53
+ "node": "^20.19.0 || >=22.12.0"
54
54
  },
55
55
  "publishConfig": {
56
56
  "access": "public",
@@ -1,7 +1,8 @@
1
- import { NoSSR, useDark, usePage, withBase } from '@rspress/core/runtime';
1
+ import { useDark, usePage } from '@rspress/core/runtime';
2
2
  import { useCallback, useEffect, useRef, useState } from 'react';
3
3
  // @ts-expect-error
4
4
  import { normalizeId } from '../../dist/utils';
5
+ import { getPageFullUrl, getPageUrl } from './common/getPageUrl';
5
6
  import MobileOperation from './common/PreviewOperations';
6
7
  import './FixedDevice.css';
7
8
 
@@ -9,15 +10,8 @@ export default () => {
9
10
  const { page } = usePage();
10
11
  const pageName = `${normalizeId(page.pagePath)}`;
11
12
  const demoId = `_${pageName}`;
12
- const url = `~demo/${demoId}`;
13
13
  const { haveIframeFixedDemos } = page;
14
14
 
15
- const getPageUrl = (url: string) => {
16
- if (page?.devPort) {
17
- return `http://localhost:${page.devPort}/${demoId}`;
18
- }
19
- return withBase(url);
20
- };
21
15
  const [iframeKey, setIframeKey] = useState(0);
22
16
  const dark = useDark();
23
17
  const refresh = useCallback(() => {
@@ -50,16 +44,14 @@ export default () => {
50
44
  }
51
45
  }
52
46
  `}</style>
53
- <NoSSR>
54
- <iframe
55
- src={getPageUrl(url)}
56
- className="rp-fixed-device__iframe"
57
- key={iframeKey}
58
- ref={iframeRef}
59
- />
60
- </NoSSR>
47
+ <iframe
48
+ src={getPageUrl(demoId)}
49
+ className="rp-fixed-device__iframe"
50
+ key={iframeKey}
51
+ ref={iframeRef}
52
+ />
61
53
  <MobileOperation
62
- url={getPageUrl(url)}
54
+ url={getPageFullUrl(demoId)}
63
55
  className="rp-fixed-device__operations"
64
56
  refresh={refresh}
65
57
  />
@@ -1,4 +1,4 @@
1
- import { NoSSR, useDark, usePageData, withBase } from '@rspress/core/runtime';
1
+ import { useDark } from '@rspress/core/runtime';
2
2
  import {
3
3
  type MouseEvent,
4
4
  useCallback,
@@ -6,6 +6,7 @@ import {
6
6
  useRef,
7
7
  useState,
8
8
  } from 'react';
9
+ import { getPageFullUrl, getPageUrl } from './common/getPageUrl';
9
10
  import MobileOperation from './common/PreviewOperations';
10
11
  import IconCode from './icons/Code';
11
12
  import './Preview.css';
@@ -18,12 +19,12 @@ type PreviewProps = {
18
19
 
19
20
  interface BasePreviewProps {
20
21
  children: React.ReactNode[];
21
- getPageUrl: () => string;
22
+ demoId: string;
22
23
  }
23
24
 
24
25
  const PreviewIframeFollow: React.FC<BasePreviewProps> = ({
25
26
  children,
26
- getPageUrl,
27
+ demoId,
27
28
  }) => {
28
29
  const [iframeKey, setIframeKey] = useState(0);
29
30
  const refresh = useCallback(() => {
@@ -48,11 +49,11 @@ const PreviewIframeFollow: React.FC<BasePreviewProps> = ({
48
49
  <div className="rp-preview--iframe-follow__device">
49
50
  <iframe
50
51
  className="rp-preview--iframe-follow__device__iframe"
51
- src={getPageUrl()}
52
+ src={getPageUrl(demoId)}
52
53
  key={iframeKey}
53
54
  ref={iframeRef}
54
55
  />
55
- <MobileOperation url={getPageUrl()} refresh={refresh} />
56
+ <MobileOperation url={getPageFullUrl(demoId)} refresh={refresh} />
56
57
  </div>
57
58
  </div>
58
59
  );
@@ -104,26 +105,15 @@ const PreviewInternal: React.FC<{ children: React.ReactNode[] }> = ({
104
105
 
105
106
  const Preview: React.FC<PreviewProps> = props => {
106
107
  const { children, previewMode, demoId } = props;
107
- const { page } = usePageData();
108
-
109
- const getPageUrl = useCallback(() => {
110
- const url = `/~demo/${demoId}`;
111
- if (page?.devPort) {
112
- return `http://localhost:${page.devPort}/${demoId}`;
113
- }
114
- return withBase(url);
115
- }, [page?.devPort, demoId]);
116
108
 
117
109
  return (
118
- <NoSSR>
110
+ <>
119
111
  {previewMode === 'iframe-follow' ? (
120
- <PreviewIframeFollow getPageUrl={getPageUrl}>
121
- {children}
122
- </PreviewIframeFollow>
112
+ <PreviewIframeFollow demoId={demoId}>{children}</PreviewIframeFollow>
123
113
  ) : (
124
114
  <PreviewInternal>{children}</PreviewInternal>
125
115
  )}
126
- </NoSSR>
116
+ </>
127
117
  );
128
118
  };
129
119
 
@@ -0,0 +1,26 @@
1
+ import { withBase } from '@rspress/core/runtime';
2
+
3
+ const DEV_PORT = process.env.RSPRESS_IFRAME_DEV_PORT;
4
+
5
+ /*
6
+ * We must provide the complete address for scanning QR Code.
7
+ */
8
+ export const getPageFullUrl = (demoId: string) => {
9
+ if (DEV_PORT && import.meta.env.DEV) {
10
+ return `http://${window.location.hostname}:${DEV_PORT}/${demoId}`;
11
+ }
12
+ const url = `/~demo/${demoId}`;
13
+ if (typeof window !== 'undefined') {
14
+ return `${window.location.origin}${withBase(url)}`;
15
+ }
16
+ // do nothing in ssr
17
+ return '';
18
+ };
19
+
20
+ export const getPageUrl = (demoId: string) => {
21
+ if (DEV_PORT && import.meta.env.DEV) {
22
+ return `http://${window.location.hostname}:${DEV_PORT}/${demoId}`;
23
+ }
24
+ const url = `/~demo/${demoId}`;
25
+ return withBase(url);
26
+ };