@hhfenpm/micro-app 1.0.2 → 1.0.3

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.esm.js CHANGED
@@ -1,7 +1,62 @@
1
- const BRIDGE$1 = {
2
- INVOKE: 'bridge-invoke',
3
- CALLBACK: 'bridge-callback',
4
- };
1
+ var uiHandler = vm => ({
2
+ $message: vm.$message,
3
+ $success: msg => vm.$message.success(msg),
4
+ $warning: msg => vm.$message.warning(msg),
5
+ $error: msg => vm.$message.error(msg),
6
+ $notify: options => vm.$notify(options),
7
+ $confirm: (...args) => vm.$confirm(...args).then(() => true).catch(() => false),
8
+ $alert: (...args) => vm.$alert(...args).then(() => true).catch(() => false),
9
+ $prompt: (...args) => vm.$prompt(...args).then(res => res.value).catch(() => null),
10
+ $loading: options => {
11
+ const loading = vm.$loading(options);
12
+ return () => loading.close()
13
+ },
14
+ });
15
+
16
+ const noop = () => console.warn('[bridge] electron 不可用');
17
+
18
+ const KEYS = [
19
+ 'renderer_listen',
20
+ 'web_send',
21
+ 'web_send_path',
22
+ 'show',
23
+ 'hide',
24
+ 'quit',
25
+ 'showAlertDialog',
26
+ 'closeAlertDialog',
27
+ 'closeAlertDialogAndJump',
28
+ 'set_bounds',
29
+ 'get_bounds',
30
+ 'workarea',
31
+ 'center',
32
+ 'maximize',
33
+ 'unmaximize',
34
+ 'is_maximized',
35
+ 'minimize',
36
+ 'open_top',
37
+ 'close_top',
38
+ 'update',
39
+ 'collapse',
40
+ 'unfold',
41
+ 'unfoldMax',
42
+ 'get_version',
43
+ ];
44
+
45
+ function createCs(electron = {}) {
46
+ const base = Object.fromEntries(
47
+ KEYS.map(k => [k, typeof electron[k] === 'function' ? electron[k] : noop])
48
+ );
49
+ return {
50
+ ...electron,
51
+ ...base,
52
+ is_app:
53
+ typeof electron.is_app === 'function'
54
+ ? electron.is_app()
55
+ : !!electron.is_app,
56
+ }
57
+ }
58
+
59
+ const BRIDGE$1 = { INVOKE: 'bridge-invoke', CALLBACK: 'bridge-callback' };
5
60
 
6
61
  const post = (target, message) =>
7
62
  target?.postMessage(message, window.location.origin);
@@ -19,6 +74,17 @@ const safeClone = value => {
19
74
  }
20
75
  };
21
76
 
77
+ function createRegisterHandlers(getElectron) {
78
+ const electron = (() => {
79
+ try {
80
+ return typeof getElectron === 'function' ? getElectron() : {}
81
+ } catch {
82
+ return {}
83
+ }
84
+ })();
85
+ return vm => ({ ui: uiHandler(vm), cs: createCs(electron) })
86
+ }
87
+
22
88
  const initBridge = ({
23
89
  isParent = false,
24
90
  vm,
@@ -79,7 +145,7 @@ const initBridge = ({
79
145
  result: safeClone(payload),
80
146
  });
81
147
 
82
- result?.then ? result.then(done).catch(done) : done(result);
148
+ Promise.resolve(result).then(done).catch(done);
83
149
  } catch (err) {
84
150
  post(iframeWindow, {
85
151
  type: BRIDGE$1.CALLBACK,
@@ -106,88 +172,6 @@ const initBridge = ({
106
172
  });
107
173
  };
108
174
 
109
- var uiHandler = vm => ({
110
- $message: (...args) => vm.$message(...args),
111
- $success: msg => vm.$message.success(msg),
112
- $warning: msg => vm.$message.warning(msg),
113
- $error: msg => vm.$message.error(msg),
114
- $notify: options => vm.$notify(options),
115
- $confirm: (...args) =>
116
- vm
117
- .$confirm(...args)
118
- .then(() => true)
119
- .catch(() => false),
120
- $alert: (...args) =>
121
- vm
122
- .$alert(...args)
123
- .then(() => true)
124
- .catch(() => false),
125
- $prompt: (...args) =>
126
- vm
127
- .$prompt(...args)
128
- .then(res => res.value)
129
- .catch(() => null),
130
- $loading: options => {
131
- const loading = vm.$loading(options);
132
- return () => loading.close()
133
- },
134
- });
135
-
136
- const noop = () => console.warn('[bridge] electron 不可用');
137
-
138
- const KEYS = [
139
- 'renderer_listen',
140
- 'web_send',
141
- 'web_send_path',
142
- 'show',
143
- 'hide',
144
- 'quit',
145
- 'showAlertDialog',
146
- 'closeAlertDialog',
147
- 'closeAlertDialogAndJump',
148
- 'set_bounds',
149
- 'get_bounds',
150
- 'workarea',
151
- 'center',
152
- 'maximize',
153
- 'unmaximize',
154
- 'is_maximized',
155
- 'minimize',
156
- 'open_top',
157
- 'close_top',
158
- 'update',
159
- 'collapse',
160
- 'unfold',
161
- 'unfoldMax',
162
- 'get_version',
163
- ];
164
-
165
- function createCs(electron = {}) {
166
- const base = Object.fromEntries(
167
- KEYS.map(k => [k, typeof electron[k] === 'function' ? electron[k] : noop])
168
- );
169
- return {
170
- ...electron,
171
- ...base,
172
- is_app:
173
- typeof electron.is_app === 'function'
174
- ? electron.is_app()
175
- : !!electron.is_app,
176
- }
177
- }
178
-
179
- function createRegisterHandlers(getElectron) {
180
- let electron = {};
181
- try {
182
- if (typeof getElectron === 'function') electron = getElectron();
183
- } catch {}
184
-
185
- return vm => ({
186
- ui: uiHandler(vm),
187
- cs: createCs(electron),
188
- })
189
- }
190
-
191
175
  function createMicroAppCore({
192
176
  modelMap,
193
177
  enabledModules,
@@ -213,9 +197,9 @@ function createMicroAppCore({
213
197
 
214
198
  const microAppModule = path => {
215
199
  const value = path.split('?')[0];
216
- const key = Object.keys(MAP).find(key => MAP[key].includes(value));
217
- const DATA = getEnabledModules();
218
- return DATA?.includes(key) ? key : null
200
+ const key = Object.keys(MAP).find(k => MAP[k].includes(value));
201
+ const mods = getEnabledModules();
202
+ return mods?.includes(key) ? key : null
219
203
  };
220
204
 
221
205
  const microAppSrc = path => {
@@ -226,12 +210,9 @@ function createMicroAppCore({
226
210
 
227
211
  const microAppDeployed = async module => {
228
212
  try {
229
- const result = await fetch(`/${module}`, {
230
- method: 'GET',
231
- cache: 'no-store',
232
- });
233
- return result.ok
234
- } catch (error) {
213
+ const res = await fetch(`/${module}`, { method: 'GET', cache: 'no-store' });
214
+ return res.ok
215
+ } catch {
235
216
  return false
236
217
  }
237
218
  };
@@ -261,11 +242,11 @@ const BRIDGE = { INVOKE: 'bridge-invoke', CALLBACK: 'bridge-callback' };
261
242
  const LIFE_CYCLE_TYPES = Object.values(LIFE_CYCLE);
262
243
  const BRIDGE_TYPES = Object.values(BRIDGE);
263
244
 
264
- const getIframe = selector => document.querySelector(selector);
265
-
266
245
  const postToChild = (iframeSelector, message) => {
267
- const iframe = getIframe(iframeSelector);
268
- iframe?.contentWindow?.postMessage(message, window.location.origin);
246
+ document.querySelector(iframeSelector)?.contentWindow?.postMessage(
247
+ message,
248
+ window.location.origin
249
+ );
269
250
  };
270
251
 
271
252
  const postToParent = message => {
package/dist/index.js CHANGED
@@ -2,10 +2,65 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- const BRIDGE$1 = {
6
- INVOKE: 'bridge-invoke',
7
- CALLBACK: 'bridge-callback',
8
- };
5
+ var uiHandler = vm => ({
6
+ $message: vm.$message,
7
+ $success: msg => vm.$message.success(msg),
8
+ $warning: msg => vm.$message.warning(msg),
9
+ $error: msg => vm.$message.error(msg),
10
+ $notify: options => vm.$notify(options),
11
+ $confirm: (...args) => vm.$confirm(...args).then(() => true).catch(() => false),
12
+ $alert: (...args) => vm.$alert(...args).then(() => true).catch(() => false),
13
+ $prompt: (...args) => vm.$prompt(...args).then(res => res.value).catch(() => null),
14
+ $loading: options => {
15
+ const loading = vm.$loading(options);
16
+ return () => loading.close()
17
+ },
18
+ });
19
+
20
+ const noop = () => console.warn('[bridge] electron 不可用');
21
+
22
+ const KEYS = [
23
+ 'renderer_listen',
24
+ 'web_send',
25
+ 'web_send_path',
26
+ 'show',
27
+ 'hide',
28
+ 'quit',
29
+ 'showAlertDialog',
30
+ 'closeAlertDialog',
31
+ 'closeAlertDialogAndJump',
32
+ 'set_bounds',
33
+ 'get_bounds',
34
+ 'workarea',
35
+ 'center',
36
+ 'maximize',
37
+ 'unmaximize',
38
+ 'is_maximized',
39
+ 'minimize',
40
+ 'open_top',
41
+ 'close_top',
42
+ 'update',
43
+ 'collapse',
44
+ 'unfold',
45
+ 'unfoldMax',
46
+ 'get_version',
47
+ ];
48
+
49
+ function createCs(electron = {}) {
50
+ const base = Object.fromEntries(
51
+ KEYS.map(k => [k, typeof electron[k] === 'function' ? electron[k] : noop])
52
+ );
53
+ return {
54
+ ...electron,
55
+ ...base,
56
+ is_app:
57
+ typeof electron.is_app === 'function'
58
+ ? electron.is_app()
59
+ : !!electron.is_app,
60
+ }
61
+ }
62
+
63
+ const BRIDGE$1 = { INVOKE: 'bridge-invoke', CALLBACK: 'bridge-callback' };
9
64
 
10
65
  const post = (target, message) =>
11
66
  target?.postMessage(message, window.location.origin);
@@ -23,6 +78,17 @@ const safeClone = value => {
23
78
  }
24
79
  };
25
80
 
81
+ function createRegisterHandlers(getElectron) {
82
+ const electron = (() => {
83
+ try {
84
+ return typeof getElectron === 'function' ? getElectron() : {}
85
+ } catch {
86
+ return {}
87
+ }
88
+ })();
89
+ return vm => ({ ui: uiHandler(vm), cs: createCs(electron) })
90
+ }
91
+
26
92
  const initBridge = ({
27
93
  isParent = false,
28
94
  vm,
@@ -83,7 +149,7 @@ const initBridge = ({
83
149
  result: safeClone(payload),
84
150
  });
85
151
 
86
- result?.then ? result.then(done).catch(done) : done(result);
152
+ Promise.resolve(result).then(done).catch(done);
87
153
  } catch (err) {
88
154
  post(iframeWindow, {
89
155
  type: BRIDGE$1.CALLBACK,
@@ -110,88 +176,6 @@ const initBridge = ({
110
176
  });
111
177
  };
112
178
 
113
- var uiHandler = vm => ({
114
- $message: (...args) => vm.$message(...args),
115
- $success: msg => vm.$message.success(msg),
116
- $warning: msg => vm.$message.warning(msg),
117
- $error: msg => vm.$message.error(msg),
118
- $notify: options => vm.$notify(options),
119
- $confirm: (...args) =>
120
- vm
121
- .$confirm(...args)
122
- .then(() => true)
123
- .catch(() => false),
124
- $alert: (...args) =>
125
- vm
126
- .$alert(...args)
127
- .then(() => true)
128
- .catch(() => false),
129
- $prompt: (...args) =>
130
- vm
131
- .$prompt(...args)
132
- .then(res => res.value)
133
- .catch(() => null),
134
- $loading: options => {
135
- const loading = vm.$loading(options);
136
- return () => loading.close()
137
- },
138
- });
139
-
140
- const noop = () => console.warn('[bridge] electron 不可用');
141
-
142
- const KEYS = [
143
- 'renderer_listen',
144
- 'web_send',
145
- 'web_send_path',
146
- 'show',
147
- 'hide',
148
- 'quit',
149
- 'showAlertDialog',
150
- 'closeAlertDialog',
151
- 'closeAlertDialogAndJump',
152
- 'set_bounds',
153
- 'get_bounds',
154
- 'workarea',
155
- 'center',
156
- 'maximize',
157
- 'unmaximize',
158
- 'is_maximized',
159
- 'minimize',
160
- 'open_top',
161
- 'close_top',
162
- 'update',
163
- 'collapse',
164
- 'unfold',
165
- 'unfoldMax',
166
- 'get_version',
167
- ];
168
-
169
- function createCs(electron = {}) {
170
- const base = Object.fromEntries(
171
- KEYS.map(k => [k, typeof electron[k] === 'function' ? electron[k] : noop])
172
- );
173
- return {
174
- ...electron,
175
- ...base,
176
- is_app:
177
- typeof electron.is_app === 'function'
178
- ? electron.is_app()
179
- : !!electron.is_app,
180
- }
181
- }
182
-
183
- function createRegisterHandlers(getElectron) {
184
- let electron = {};
185
- try {
186
- if (typeof getElectron === 'function') electron = getElectron();
187
- } catch {}
188
-
189
- return vm => ({
190
- ui: uiHandler(vm),
191
- cs: createCs(electron),
192
- })
193
- }
194
-
195
179
  function createMicroAppCore({
196
180
  modelMap,
197
181
  enabledModules,
@@ -217,9 +201,9 @@ function createMicroAppCore({
217
201
 
218
202
  const microAppModule = path => {
219
203
  const value = path.split('?')[0];
220
- const key = Object.keys(MAP).find(key => MAP[key].includes(value));
221
- const DATA = getEnabledModules();
222
- return DATA?.includes(key) ? key : null
204
+ const key = Object.keys(MAP).find(k => MAP[k].includes(value));
205
+ const mods = getEnabledModules();
206
+ return mods?.includes(key) ? key : null
223
207
  };
224
208
 
225
209
  const microAppSrc = path => {
@@ -230,12 +214,9 @@ function createMicroAppCore({
230
214
 
231
215
  const microAppDeployed = async module => {
232
216
  try {
233
- const result = await fetch(`/${module}`, {
234
- method: 'GET',
235
- cache: 'no-store',
236
- });
237
- return result.ok
238
- } catch (error) {
217
+ const res = await fetch(`/${module}`, { method: 'GET', cache: 'no-store' });
218
+ return res.ok
219
+ } catch {
239
220
  return false
240
221
  }
241
222
  };
@@ -265,11 +246,11 @@ const BRIDGE = { INVOKE: 'bridge-invoke', CALLBACK: 'bridge-callback' };
265
246
  const LIFE_CYCLE_TYPES = Object.values(LIFE_CYCLE);
266
247
  const BRIDGE_TYPES = Object.values(BRIDGE);
267
248
 
268
- const getIframe = selector => document.querySelector(selector);
269
-
270
249
  const postToChild = (iframeSelector, message) => {
271
- const iframe = getIframe(iframeSelector);
272
- iframe?.contentWindow?.postMessage(message, window.location.origin);
250
+ document.querySelector(iframeSelector)?.contentWindow?.postMessage(
251
+ message,
252
+ window.location.origin
253
+ );
273
254
  };
274
255
 
275
256
  const postToParent = message => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hhfenpm/micro-app",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "微前端通信桥接和状态同步工具,支持父子应用通信、状态同步、生命周期管理",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",