@lark-apaas/client-toolkit 1.2.8-alpha.0 → 1.2.8-alpha.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.
@@ -1,3 +1,2 @@
1
1
  import React from 'react';
2
- import './utils/listenHot';
3
2
  export default function IframeBridge(): React.JSX.Element;
@@ -1,12 +1,8 @@
1
1
  import { jsx } from "react/jsx-runtime";
2
2
  import { useCallback, useEffect, useMemo, useRef } from "react";
3
3
  import { useLocation, useNavigate } from "react-router-dom";
4
- import { connectToParent } from "penpal";
5
4
  import { useUpdatingRef } from "../../hooks/useUpdatingRef.js";
6
- import { resolveParentOrigin, submitPostMessage } from "../../utils/postMessage.js";
7
- import { childApi } from "./utils/childApi.js";
8
- import { batchLogInfo } from "../../logger/batch-logger.js";
9
- import "./utils/listenHot.js";
5
+ import { submitPostMessage } from "../../utils/postMessage.js";
10
6
  var IframeBridge_RouteMessageType = /*#__PURE__*/ function(RouteMessageType) {
11
7
  RouteMessageType["RouteChange"] = "RouteChange";
12
8
  RouteMessageType["RouteBack"] = "RouteBack";
@@ -16,27 +12,6 @@ var IframeBridge_RouteMessageType = /*#__PURE__*/ function(RouteMessageType) {
16
12
  function isRouteMessageType(type) {
17
13
  return Object.values(IframeBridge_RouteMessageType).includes(type);
18
14
  }
19
- async function connectParent() {
20
- submitPostMessage({
21
- type: 'PreviewReady',
22
- data: {}
23
- });
24
- batchLogInfo('info', JSON.stringify({
25
- type: 'PreviewReady',
26
- timestamp: Date.now(),
27
- url: window.location.href
28
- }));
29
- const parentOrigin = resolveParentOrigin();
30
- if (!parentOrigin) return;
31
- const connection = connectToParent({
32
- parentOrigin,
33
- methods: {
34
- ...childApi
35
- }
36
- });
37
- await connection.promise;
38
- }
39
- 'production' !== process.env.NODE_ENV && connectParent();
40
15
  function IframeBridge() {
41
16
  const location = useLocation();
42
17
  const navigate = useNavigate();
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Iframe Bridge Runtime
3
+ *
4
+ * 开发环境下的 iframe 通信初始化:
5
+ * - connectParent - 通过 penpal 连接父页面,建立双向通信
6
+ */
7
+ /**
8
+ * 初始化 iframe bridge(仅开发环境)
9
+ * - 连接父页面
10
+ */
11
+ export declare function initIframeBridge(): void;
@@ -0,0 +1,29 @@
1
+ import { connectToParent } from "penpal";
2
+ import { resolveParentOrigin, submitPostMessage } from "../utils/postMessage.js";
3
+ import { batchLogInfo } from "../logger/batch-logger.js";
4
+ import { childApi } from "../components/AppContainer/utils/childApi.js";
5
+ async function connectParent() {
6
+ submitPostMessage({
7
+ type: 'PreviewReady',
8
+ data: {}
9
+ });
10
+ batchLogInfo('info', JSON.stringify({
11
+ type: 'PreviewReady',
12
+ timestamp: Date.now(),
13
+ url: window.location.href
14
+ }));
15
+ const parentOrigin = resolveParentOrigin();
16
+ if (!parentOrigin) return;
17
+ const connection = connectToParent({
18
+ parentOrigin,
19
+ methods: {
20
+ ...childApi
21
+ }
22
+ });
23
+ await connection.promise;
24
+ }
25
+ function initIframeBridge() {
26
+ if (window.parent === window) return;
27
+ connectParent();
28
+ }
29
+ export { initIframeBridge };
@@ -3,10 +3,14 @@ import { registerDayjsPlugins } from "./dayjs.js";
3
3
  import { initAxiosConfig } from "./axios.js";
4
4
  import { initObservable } from "./observable.js";
5
5
  import { initServerLog } from "./server-log.js";
6
+ import { initIframeBridge } from "./iframe-bridge.js";
6
7
  if (!window.__FULLSTACK_RUNTIME_INITIALIZED__) {
7
8
  window.__FULLSTACK_RUNTIME_INITIALIZED__ = true;
8
9
  registerDayjsPlugins();
9
10
  initAxiosConfig();
10
11
  initObservable();
11
- if ('production' !== process.env.NODE_ENV) initServerLog();
12
+ if ('production' !== process.env.NODE_ENV) {
13
+ initServerLog();
14
+ initIframeBridge();
15
+ }
12
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lark-apaas/client-toolkit",
3
- "version": "1.2.8-alpha.0",
3
+ "version": "1.2.8-alpha.2",
4
4
  "types": "./lib/index.d.ts",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -115,7 +115,6 @@
115
115
  "lodash": "^4.17.21",
116
116
  "network-idle": "^0.2.0",
117
117
  "penpal": "^6.2.2",
118
- "sockjs-client": "^1.6.1",
119
118
  "sonner": "~2.0.0",
120
119
  "source-map": "^0.7.6",
121
120
  "stacktrace-js": "^2.0.2",
@@ -148,7 +147,6 @@
148
147
  "@types/node": "^22.10.2",
149
148
  "@types/react": "^18.3.23",
150
149
  "@types/react-dom": "^18.3.7",
151
- "@types/sockjs-client": "^1",
152
150
  "antd": "^5.26.6",
153
151
  "eslint": "^8.57.0",
154
152
  "jsdom": "^26.1.0",
@@ -1 +0,0 @@
1
- export declare function connectDevServer(): WebSocket;
@@ -1,79 +0,0 @@
1
- import sockjs_client from "sockjs-client";
2
- import { submitPostMessage, submitSlardarEvent } from "../../../utils/postMessage.js";
3
- import { batchLogInfo } from "../../../logger/batch-logger.js";
4
- import { getWsPath } from "../../../utils/utils.js";
5
- let hotInited = false;
6
- function handleDevServerMessage(msg) {
7
- if ('hash' === msg.type) {
8
- if (!hotInited) {
9
- hotInited = true;
10
- return;
11
- }
12
- submitPostMessage({
13
- type: 'HmrMessage',
14
- msg: {
15
- type: 'hot'
16
- },
17
- data: null
18
- });
19
- batchLogInfo('info', JSON.stringify({
20
- type: 'HmrMessage',
21
- subType: 'hot',
22
- timestamp: Date.now(),
23
- hash: msg.data
24
- }));
25
- } else if ('errors' === msg.type) {
26
- submitPostMessage({
27
- type: 'HmrMessage',
28
- msg: {
29
- type: 'errors',
30
- data: JSON.stringify(msg.data)
31
- },
32
- data: null
33
- });
34
- batchLogInfo('info', JSON.stringify({
35
- type: 'HmrMessage',
36
- subType: 'errors',
37
- timestamp: Date.now(),
38
- errorCount: msg.data?.length
39
- }));
40
- } else if ('hmr-timing' === msg.type) {
41
- const { duration, fileCount, fileTotalSize } = msg.data;
42
- submitSlardarEvent({
43
- name: 'runTiming',
44
- metrics: {
45
- duration
46
- },
47
- categories: {
48
- type: 'sandbox-hmr-timing',
49
- fileCount,
50
- fileTotalSize
51
- }
52
- });
53
- batchLogInfo('info', JSON.stringify({
54
- type: 'HmrMessage',
55
- subType: 'hmr-timing',
56
- timestamp: Date.now(),
57
- duration,
58
- fileCount,
59
- fileTotalSize
60
- }));
61
- }
62
- }
63
- function connectDevServer() {
64
- const sockUrl = getWsPath();
65
- const sock = new sockjs_client(sockUrl);
66
- sock.onopen = ()=>console.log('✅ connect DevServer SockJS');
67
- sock.onmessage = (event)=>{
68
- try {
69
- const msg = JSON.parse(event.data);
70
- console.log('hmr 消息:', msg);
71
- handleDevServerMessage(msg);
72
- } catch (err) {
73
- console.error('解析 hmr 消息失败:', event.data);
74
- }
75
- };
76
- return sock;
77
- }
78
- 'production' !== process.env.NODE_ENV && connectDevServer();
79
- export { connectDevServer };