@hhfenpm/micro-app 1.0.5 → 1.0.7

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/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  基于 iframe + postMessage 的微前端工具:父子应用**通信桥接**、**Vuex 状态同步**、**生命周期**与**路由同步**。
4
4
 
5
+ **兼容 IE9+**
6
+
5
7
  ## 安装
6
8
 
7
9
  ```bash
@@ -192,15 +194,18 @@ store.callMicroAppLifeCycle('unmount')
192
194
  1. **同源**:postMessage 仅在同源下使用,需校验 `event.origin`。
193
195
  2. **base 模块**:`baseSyncPlugin` 通过 `base/SYNC_STATE` 同步;`base` 的 state 结构需与约定一致。
194
196
  3. **子应用必调**:`store.attachRouterSync(router)`,否则子应用路由不会回写到父应用 URL。
195
- 4. **Proxy**:`vm.$base` 依赖 Proxy,不支持 IE11
197
+ 4. **IE9 兼容**:本包已针对 IE9 进行了兼容处理,需要确保项目配置了 Babel 转译和 core-js polyfill
196
198
 
197
199
  ---
198
200
 
199
201
  ## 兼容性
200
202
 
201
- - 现代浏览器(Chrome、Firefox、Safari、Edge)
202
- - 依赖 `postMessage`、`Proxy`
203
- - Vue 2.6+ / 3.x,Vuex 3.x / 4.x(peerDependencies)
203
+ - **浏览器支持**:IE9+、现代浏览器(Chrome、Firefox、Safari、Edge)
204
+ - **依赖要求**:
205
+ - Vue 2.6+ / 3.x,Vuex 3.x / 4.x(peerDependencies)
206
+ - **项目配置要求**:
207
+ - 需要配置 Babel 转译(`transpileDependencies: true`)
208
+ - 需要配置 core-js polyfill(Promise、Object.fromEntries、Array.includes 等)
204
209
 
205
210
  ---
206
211
 
package/dist/index.esm.js CHANGED
@@ -86,6 +86,68 @@ const safeClone = value => {
86
86
  }
87
87
  };
88
88
 
89
+ const createBaseObject = pending => {
90
+ const namespaces = {};
91
+ const hasProxy = typeof Proxy !== 'undefined';
92
+
93
+ const createMethod = (namespace, method) => {
94
+ return params =>
95
+ new Promise(resolve => {
96
+ const callbackId = genId();
97
+ pending[callbackId] = resolve;
98
+
99
+ post(window.parent, {
100
+ type: BRIDGE$1.INVOKE,
101
+ action: `${namespace}.${method}`,
102
+ params,
103
+ callbackId,
104
+ });
105
+ })
106
+ };
107
+
108
+ if (hasProxy) {
109
+ const base = {};
110
+ return new Proxy(base, {
111
+ get(target, namespace) {
112
+ if (typeof namespace === 'string') {
113
+ if (!namespaces[namespace]) {
114
+ namespaces[namespace] = {};
115
+ }
116
+ return new Proxy(namespaces[namespace], {
117
+ get(target2, method) {
118
+ if (typeof method === 'string') {
119
+ if (!namespaces[namespace][method]) {
120
+ namespaces[namespace][method] = createMethod(namespace, method);
121
+ }
122
+ return namespaces[namespace][method]
123
+ }
124
+ return undefined
125
+ }
126
+ })
127
+ }
128
+ return undefined
129
+ }
130
+ })
131
+ } else {
132
+ const base = {};
133
+ const commonNamespaces = ['ui', 'cs'];
134
+
135
+ commonNamespaces.forEach(ns => {
136
+ namespaces[ns] = {};
137
+ const nsObj = {};
138
+
139
+ Object.defineProperty(base, ns, {
140
+ get() {
141
+ return nsObj
142
+ },
143
+ configurable: true
144
+ });
145
+ });
146
+
147
+ return base
148
+ }
149
+ };
150
+
89
151
  function createRegisterHandlers(getElectron) {
90
152
  const electron = (() => {
91
153
  try {
@@ -107,33 +169,8 @@ const initBridge = ({
107
169
  const pending = Object.create(null);
108
170
 
109
171
  if (!isParent && vm) {
110
- vm.$base = new Proxy(
111
- {},
112
- {
113
- get(_, namespace) {
114
- return new Proxy(
115
- {},
116
- {
117
- get(_, method) {
118
- return params =>
119
- new Promise(resolve => {
120
- const callbackId = genId();
121
- pending[callbackId] = resolve;
122
-
123
- post(window.parent, {
124
- type: BRIDGE$1.INVOKE,
125
- action: `${namespace}.${method}`,
126
- params,
127
- callbackId,
128
- });
129
- })
130
- },
131
- }
132
- )
133
- },
134
- }
135
- );
136
-
172
+ const baseObj = createBaseObject(pending);
173
+ vm.$base = baseObj;
137
174
  vm.$baseReady = true;
138
175
  }
139
176
 
@@ -389,6 +426,22 @@ function baseSyncPlugin({
389
426
  }
390
427
  }
391
428
 
392
- var index = { initBridge, baseSyncPlugin };
429
+ function syncState(watch = {}) {
430
+ return function SYNC_STATE(state, payload) {
431
+ if (!payload || typeof payload !== 'object') return
432
+
433
+ Object.keys(watch).forEach(key => {
434
+ if (state[key] !== payload[key] && typeof watch[key] === 'function')
435
+ watch[key]({
436
+ store: this,
437
+ value: payload[key],
438
+ valueOld: state[key],
439
+ });
440
+ });
441
+ Object.keys(payload).forEach(key => {
442
+ if (state[key] !== payload[key]) state[key] = payload[key];
443
+ });
444
+ }
445
+ }
393
446
 
394
- export { baseSyncPlugin, createMicroAppCore, createRegisterHandlers, index as default, initBridge };
447
+ export { baseSyncPlugin, createMicroAppCore, createRegisterHandlers, initBridge, syncState };
package/dist/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  var uiHandler = vm => ({
6
4
  $message: (...args) => vm.$message(...args),
7
5
  $success: msg => vm.$message.success(msg),
@@ -90,6 +88,68 @@ const safeClone = value => {
90
88
  }
91
89
  };
92
90
 
91
+ const createBaseObject = pending => {
92
+ const namespaces = {};
93
+ const hasProxy = typeof Proxy !== 'undefined';
94
+
95
+ const createMethod = (namespace, method) => {
96
+ return params =>
97
+ new Promise(resolve => {
98
+ const callbackId = genId();
99
+ pending[callbackId] = resolve;
100
+
101
+ post(window.parent, {
102
+ type: BRIDGE$1.INVOKE,
103
+ action: `${namespace}.${method}`,
104
+ params,
105
+ callbackId,
106
+ });
107
+ })
108
+ };
109
+
110
+ if (hasProxy) {
111
+ const base = {};
112
+ return new Proxy(base, {
113
+ get(target, namespace) {
114
+ if (typeof namespace === 'string') {
115
+ if (!namespaces[namespace]) {
116
+ namespaces[namespace] = {};
117
+ }
118
+ return new Proxy(namespaces[namespace], {
119
+ get(target2, method) {
120
+ if (typeof method === 'string') {
121
+ if (!namespaces[namespace][method]) {
122
+ namespaces[namespace][method] = createMethod(namespace, method);
123
+ }
124
+ return namespaces[namespace][method]
125
+ }
126
+ return undefined
127
+ }
128
+ })
129
+ }
130
+ return undefined
131
+ }
132
+ })
133
+ } else {
134
+ const base = {};
135
+ const commonNamespaces = ['ui', 'cs'];
136
+
137
+ commonNamespaces.forEach(ns => {
138
+ namespaces[ns] = {};
139
+ const nsObj = {};
140
+
141
+ Object.defineProperty(base, ns, {
142
+ get() {
143
+ return nsObj
144
+ },
145
+ configurable: true
146
+ });
147
+ });
148
+
149
+ return base
150
+ }
151
+ };
152
+
93
153
  function createRegisterHandlers(getElectron) {
94
154
  const electron = (() => {
95
155
  try {
@@ -111,33 +171,8 @@ const initBridge = ({
111
171
  const pending = Object.create(null);
112
172
 
113
173
  if (!isParent && vm) {
114
- vm.$base = new Proxy(
115
- {},
116
- {
117
- get(_, namespace) {
118
- return new Proxy(
119
- {},
120
- {
121
- get(_, method) {
122
- return params =>
123
- new Promise(resolve => {
124
- const callbackId = genId();
125
- pending[callbackId] = resolve;
126
-
127
- post(window.parent, {
128
- type: BRIDGE$1.INVOKE,
129
- action: `${namespace}.${method}`,
130
- params,
131
- callbackId,
132
- });
133
- })
134
- },
135
- }
136
- )
137
- },
138
- }
139
- );
140
-
174
+ const baseObj = createBaseObject(pending);
175
+ vm.$base = baseObj;
141
176
  vm.$baseReady = true;
142
177
  }
143
178
 
@@ -393,10 +428,26 @@ function baseSyncPlugin({
393
428
  }
394
429
  }
395
430
 
396
- var index = { initBridge, baseSyncPlugin };
431
+ function syncState(watch = {}) {
432
+ return function SYNC_STATE(state, payload) {
433
+ if (!payload || typeof payload !== 'object') return
434
+
435
+ Object.keys(watch).forEach(key => {
436
+ if (state[key] !== payload[key] && typeof watch[key] === 'function')
437
+ watch[key]({
438
+ store: this,
439
+ value: payload[key],
440
+ valueOld: state[key],
441
+ });
442
+ });
443
+ Object.keys(payload).forEach(key => {
444
+ if (state[key] !== payload[key]) state[key] = payload[key];
445
+ });
446
+ }
447
+ }
397
448
 
398
449
  exports.baseSyncPlugin = baseSyncPlugin;
399
450
  exports.createMicroAppCore = createMicroAppCore;
400
451
  exports.createRegisterHandlers = createRegisterHandlers;
401
- exports.default = index;
402
452
  exports.initBridge = initBridge;
453
+ exports.syncState = syncState;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hhfenpm/micro-app",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "微前端通信桥接和状态同步工具,支持父子应用通信、状态同步、生命周期管理",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.esm.js",