@xingtukeji/micro 1.0.1 → 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/index.ts DELETED
@@ -1,291 +0,0 @@
1
- import qs from 'qs'
2
- import { ref } from 'vue'
3
-
4
- export interface AppOption {
5
- name: string
6
- url: string
7
- el?: HTMLElement | string
8
- preload?: boolean
9
- token?: ()=>string | string
10
- params?: { [key: string]: any }
11
- }
12
- export interface App extends AppOption {
13
- instance: MicroApp
14
- }
15
-
16
- const _apps = []
17
- const CONSTANT = {
18
- KEY: '$xtm', // 父应用注入子应用实例的key
19
- IS_MICRO: '__XT_MICRO', // 是否在微应用中
20
- MICRO_NAME: '__XT_MICRO_NAME', // 微应用名称
21
- MICRO_TOKEN: '__XT_MICRO_TOKEN', // 微应用token
22
- MICRO_APP: '__XT_MICRO_APP', // 微应用实例
23
- DOM_ID_PREFIX: 'xt-micro-ifr-' // dom id前缀
24
- }
25
- //dom id前缀
26
- // export const DOM_ID_PREFIX = 'xt-ifr-'
27
-
28
- /**
29
- * 判断是否在微应用中
30
- */
31
- const _isMicro = ref(false)
32
- Object.defineProperty(window, CONSTANT.IS_MICRO, {
33
- get: function () {
34
- return _isMicro.value
35
- },
36
- set: function (newValue) {
37
- _isMicro.value = newValue
38
- }
39
- })
40
- export const isMicro = _isMicro
41
-
42
- /**
43
- * 生成微应用id
44
- * 增加前缀
45
- * @param name 微应用名称(唯一)
46
- * @returns 返回dom id
47
- */
48
- const generateId = (name: string) => {
49
- return `${CONSTANT.DOM_ID_PREFIX}${name}`
50
- }
51
- /**
52
- * 处理微应用url
53
- * @param url 微应用url
54
- * @param params 携带参数
55
- * @returns 拼接后的url
56
- */
57
- const generateURL = (app: AppOption) => {
58
- function isObject(variable) {
59
- return variable !== null && typeof variable === 'object' && !Array.isArray(variable)
60
- }
61
- const _parmas = typeof app.params === 'function' ? app.params() : isObject(app.params) ? app.params : {}
62
- const _token = typeof app.token === 'function' ? app.token() : isObject(app.token) ? app.token : {}
63
- return `${app.url}?${CONSTANT.IS_MICRO}=true
64
- &${CONSTANT.MICRO_NAME}=${app.name}
65
- &${CONSTANT.MICRO_TOKEN}=${_token}
66
- &${qs.stringify(_parmas)}`
67
- }
68
-
69
- class MicroApp {
70
- app: App | AppOption
71
- dom: HTMLIFrameElement
72
- panel: HTMLElement
73
-
74
- constructor(app: AppOption) {
75
- this.app = app
76
- app.preload && this.preload()
77
- return this
78
- }
79
-
80
- mount(el: HTMLElement | string, isHide = false) {
81
- let panel: HTMLElement | null
82
- if (el) {
83
- if (typeof el === 'string') {
84
- panel = document.querySelector(el)
85
- } else {
86
- panel = el
87
- }
88
- //这里清空了容器的内容,为了方便,如果还有其他元素需要特别处理,而且也影响元素再复用
89
- panel.innerHTML = ''
90
- } else {
91
- panel = document.body
92
- }
93
-
94
- if (panel) {
95
- let ifr = <HTMLIFrameElement>document.getElementById(generateId(this.app.name))
96
- if (!ifr) {
97
- ifr = document.createElement('iframe')
98
- ifr.id = generateId(this.app.name)
99
- const url = generateURL(this.app)
100
- ifr.src = url
101
- ifr.classList.add('micro-iframe')
102
- ifr.addEventListener('load', (event)=>{
103
- this.onLoad.call(this, event)
104
- })
105
- ifr.addEventListener('error', this.onError)
106
- ifr.addEventListener('unload', this.onUnload)
107
- this.dom = ifr
108
- }
109
- isHide ? (ifr.style.display = 'none') : ifr.style.removeProperty('display')
110
- this.panel = panel
111
- panel.appendChild(ifr)
112
-
113
- } else {
114
- throw new Error('el not found')
115
- }
116
- }
117
-
118
- unmount() {
119
- const appDom = document.getElementById(generateId(this.app.name))
120
- if (appDom) {
121
- if (this.preload) {
122
- appDom.style.display = 'none'
123
- document.body.appendChild(appDom)
124
- } else {
125
- this.panel.removeChild(appDom)
126
- }
127
- } else {
128
- throw new Error('app dom not found')
129
- }
130
- }
131
-
132
- /**
133
- * 预加载渲染
134
- * TODO 还差用户切走之后,如何将ifr还原回去,等待下次载入
135
- * @param el 挂载dom对象
136
- */
137
- preloadRender(el: HTMLElement | string) {
138
-
139
- let appDom: HTMLElement | null = this.dom
140
- this.mount(el)
141
-
142
- // const appDom = document.getElementById(generateId(this.app.name))
143
- // if (!appDom) {
144
- //
145
- // this.render(el)
146
- // return
147
- // }
148
- //
149
- // let panel
150
- // if (typeof el === 'string') {
151
- // panel = document.querySelector(el)
152
- // } else {
153
- // panel = el
154
- // }
155
- // appDom!.style.removeProperty('display')
156
- // panel?.appendChild(appDom)
157
- }
158
-
159
- /**
160
- * 预加载app,先放到body,待启动时,挪过来
161
- */
162
- preload() {
163
- this.mount(undefined, true)
164
- }
165
-
166
- changeOptions(options: Partial<AppOption>) {
167
- this.app = {
168
- ...this.app,
169
- ...options,
170
- }
171
- }
172
-
173
- onLoad(event: Event) {
174
- console.log('micro app load', this, event)
175
- _postMicro(this.app)
176
- }
177
-
178
- onUnload() {
179
-
180
- }
181
-
182
- onError() {
183
-
184
- }
185
- }
186
-
187
- /**
188
- * 安装微应用
189
- * @param app
190
- */
191
- export const setupApp = (app: AppOption) => {
192
- //在子应用加载完成后,发送消息给子应用
193
- // setTimeout(() => {
194
- // _postMicro(app)
195
- // },0)
196
- return (_apps[app.name] = {
197
- ...app,
198
- instance: new MicroApp(app),
199
- })
200
- }
201
-
202
- export const getApp = (name: string) => {
203
- return _apps[name]
204
- }
205
-
206
- const setApp = (app: App, options?: Partial<AppOption>) => {
207
- return (_apps[app.name] = {
208
- ...app,
209
- ...options,
210
- })
211
- }
212
-
213
- /**
214
- * 启动微应用
215
- * @param name 应用名称
216
- * @param options 应用配置
217
- * @returns
218
- */
219
- export const startApp = (name: string, options?: Partial<AppOption>) => {
220
- return new Promise((resolve, reject) => {
221
- let app = getApp(name)
222
- if (app) {
223
- if (options) {
224
- app.instance.changeOptions(options)
225
- app = setApp(app, options)
226
- }
227
- if (app.preload) {
228
- app.instance.preloadRender(app.el)
229
- } else {
230
- app.instance.mount(app.el)
231
- }
232
- // _postMicro(app)
233
- resolve(app)
234
- } else {
235
- reject('app not found')
236
- }
237
- })
238
- }
239
-
240
- export const stopApp = (name: string) => {
241
- let app = getApp(name)
242
- if (app) {
243
- app.instance.unmount()
244
- }
245
- }
246
-
247
- function getQueryParameter(name, url = window.location.href) {
248
- name = name.replace(/[\[\]]/g, "\\$&");
249
- var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
250
- results = regex.exec(url);
251
- if (!results) return null;
252
- if (!results[2]) return '';
253
- return decodeURIComponent(results[2].replace(/\+/g, " "));
254
- }
255
-
256
- interface MicroInitOptions{
257
- authHandler?: (token: string) => void
258
- }
259
- /**
260
- * 微应用-子应用初始化
261
- * 非必须,但建议使用
262
- */
263
- export const microInit = (options: MicroInitOptions = {}) => {
264
- //处理url参数
265
- const micro = getQueryParameter(CONSTANT.IS_MICRO)
266
- const microName = getQueryParameter(CONSTANT.MICRO_NAME)
267
- //设置环境变量
268
- if (micro) {
269
- window[CONSTANT.IS_MICRO] = true
270
- }
271
- //监听message,设置环境变量
272
- window.addEventListener('message', (e) => {
273
- if (e.data.cmd === `$xt/micro/${microName}`) {
274
- console.log('micro', e.data.data)
275
- window[CONSTANT.MICRO_APP] = e.data.data
276
- }
277
- })
278
-
279
- //处理token
280
- const token = getQueryParameter(CONSTANT.MICRO_TOKEN)
281
- options.authHandler && options.authHandler(token)
282
- }
283
-
284
- const _postMicro = (app: AppOption) => {
285
- window.postMessage({ cmd: `$xt/micro/${app.name}`, data: {
286
- name: app.name,
287
- url: app.url,
288
- preload: app.preload,
289
- // params: app.params
290
- } }, '*')
291
- }