@lark-apaas/miaoda-core 0.1.0-alpha.21 → 0.1.0-alpha.23

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.
@@ -1,3 +1,4 @@
1
1
  import React from 'react';
2
+ import './utils/listenHot';
2
3
  import './LogInterceptor';
3
4
  export default function IframeBridge(): React.JSX.Element;
@@ -5,7 +5,8 @@ import { connectToParent } from "penpal";
5
5
  import { useUpdatingRef } from "../../hooks/useUpdatingRef.js";
6
6
  import { postMessage } from "../../utils/postMessage.js";
7
7
  import { getPreviewParentOrigin } from "../../utils/getParentOrigin.js";
8
- import { childApi } from "./childApi.js";
8
+ import { childApi } from "./utils/childApi.js";
9
+ import "./utils/listenHot.js";
9
10
  import "./LogInterceptor.js";
10
11
  var IframeBridge_RouteMessageType = /*#__PURE__*/ function(RouteMessageType) {
11
12
  RouteMessageType["RouteChange"] = "RouteChange";
@@ -17,6 +18,10 @@ function isRouteMessageType(type) {
17
18
  return Object.values(IframeBridge_RouteMessageType).includes(type);
18
19
  }
19
20
  async function connectParent() {
21
+ postMessage({
22
+ type: 'PreviewReady',
23
+ data: {}
24
+ });
20
25
  const connection = connectToParent({
21
26
  parentOrigin: getPreviewParentOrigin(),
22
27
  methods: {
@@ -25,6 +30,7 @@ async function connectParent() {
25
30
  });
26
31
  await connection.promise;
27
32
  }
33
+ 'production' !== process.env.NODE_ENV && connectParent();
28
34
  function IframeBridge() {
29
35
  const location = useLocation();
30
36
  const navigate = useNavigate();
@@ -51,13 +57,6 @@ function IframeBridge() {
51
57
  historyForward,
52
58
  navigateRef
53
59
  ]);
54
- useEffect(()=>{
55
- postMessage({
56
- type: 'PreviewReady',
57
- data: {}
58
- });
59
- connectParent();
60
- }, []);
61
60
  useEffect(()=>{
62
61
  if (isActive.current) {
63
62
  isActive.current = false;
@@ -1,4 +1,4 @@
1
- import { ChildApi } from '../../types/iframe-events';
1
+ import { ChildApi } from '../../../types/iframe-events';
2
2
  /**
3
3
  * 子级方法,提供给父级调用
4
4
  */
@@ -1,4 +1,4 @@
1
- import { normalizeBasePath } from "../../utils/utils.js";
1
+ import { normalizeBasePath } from "../../../utils/utils.js";
2
2
  async function getRoutes() {
3
3
  let routes = [
4
4
  {
@@ -0,0 +1 @@
1
+ export declare function connectDevServer(): WebSocket;
@@ -0,0 +1,43 @@
1
+ import sockjs_client from "sockjs-client";
2
+ import { postMessage } from "../../../utils/postMessage.js";
3
+ import { getWsPath } from "../../../utils/utils.js";
4
+ let hotInited = false;
5
+ function handleDevServerMessage(msg) {
6
+ if ('hash' === msg.type) {
7
+ if (!hotInited) {
8
+ hotInited = true;
9
+ return;
10
+ }
11
+ postMessage({
12
+ type: 'HmrMessage',
13
+ msg: {
14
+ type: 'hot'
15
+ },
16
+ data: null
17
+ });
18
+ } else if ('errors' === msg.type) postMessage({
19
+ type: 'HmrMessage',
20
+ msg: {
21
+ type: 'errors',
22
+ data: JSON.stringify(msg.data)
23
+ },
24
+ data: null
25
+ });
26
+ }
27
+ function connectDevServer() {
28
+ const sockUrl = getWsPath();
29
+ const sock = new sockjs_client(sockUrl);
30
+ sock.onopen = ()=>console.log("✅ connect DevServer SockJS");
31
+ sock.onmessage = (event)=>{
32
+ try {
33
+ const msg = JSON.parse(event.data);
34
+ console.log("hmr 消息:", msg);
35
+ handleDevServerMessage(msg);
36
+ } catch (err) {
37
+ console.error("解析 hmr 消息失败:", event.data);
38
+ }
39
+ };
40
+ return sock;
41
+ }
42
+ 'production' !== process.env.NODE_ENV && connectDevServer();
43
+ export { connectDevServer };
@@ -30,8 +30,13 @@ export interface UpdateRoutesMessage extends IframeMessage<{
30
30
  }> {
31
31
  type: 'UpdateRoutes';
32
32
  }
33
+ /** 热更新消息 */
33
34
  export interface HmrMessage extends IframeMessage<Record<string, never>> {
34
- type: 'Hmr';
35
+ type: 'HmrMessage';
36
+ msg: {
37
+ type: 'hot' | 'errors';
38
+ data?: any;
39
+ };
35
40
  }
36
41
  export type OutgoingMessage = PreviewReadyMessage | HmrMessage | ConsoleMessage | ChildLocationChangeMessage | CreatePageMessage | RenderErrorMessage | RenderErrorRepairMessage | PageScreenshotMessage | UpdateRoutesMessage;
37
42
  export interface GetRoutesMessage extends IframeMessage<Record<string, never>> {
@@ -15,5 +15,6 @@ export declare function isPreview(): boolean;
15
15
  * 移除末尾的斜杠,确保路径不以斜杠结尾
16
16
  */
17
17
  export declare function normalizeBasePath(basePath?: string): string;
18
+ export declare function getWsPath(): string;
18
19
  /** 判断是否是沙箱模式 */
19
20
  export declare function isSparkRuntime(): boolean;
@@ -10,7 +10,13 @@ function normalizeBasePath(basePath) {
10
10
  if (!basePath || '/' === basePath) return '';
11
11
  return basePath.replace(/\/+$/, '');
12
12
  }
13
+ function getWsPath() {
14
+ const rawBasePath = process.env.CLIENT_BASE_PATH || '/';
15
+ const normalizedBasePath = rawBasePath.startsWith('/') ? rawBasePath : `/${rawBasePath}`;
16
+ const basePathWithoutTrailingSlash = normalizedBasePath.endsWith('/') ? normalizedBasePath.slice(0, -1) : normalizedBasePath;
17
+ return `${basePathWithoutTrailingSlash}/ws`;
18
+ }
13
19
  function isSparkRuntime() {
14
- return window._IS_Spark_RUNTIME ?? 'true' === process.env.fullstack;
20
+ return window._IS_Spark_RUNTIME ?? 'fullstack' === process.env.runtimeMode;
15
21
  }
16
- export { clsxWithTw, isPreview, isSparkRuntime, normalizeBasePath };
22
+ export { clsxWithTw, getWsPath, isPreview, isSparkRuntime, normalizeBasePath };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/miaoda-core",
3
- "version": "0.1.0-alpha.21",
3
+ "version": "0.1.0-alpha.23",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -83,6 +83,7 @@
83
83
  "html2canvas-pro": "^1.5.11",
84
84
  "lodash": "^4.17.21",
85
85
  "penpal": "^6.2.2",
86
+ "sockjs-client": "^1.6.1",
86
87
  "sonner": "~2.0.0",
87
88
  "tailwind-merge": "~2.0.0",
88
89
  "tailwind-variants": "0.3.1",
@@ -107,6 +108,7 @@
107
108
  "@types/node": "^22.10.2",
108
109
  "@types/react": "^18.3.23",
109
110
  "@types/react-dom": "^18.3.7",
111
+ "@types/sockjs-client": "^1",
110
112
  "antd": "^5.26.6",
111
113
  "eslint": "^8.57.0",
112
114
  "jsdom": "^26.1.0",