@xingtukeji/micro 1.0.2 → 1.0.4
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.js +1913 -0
- package/dist/style.css +1 -0
- package/dist/types/api.d.ts +21 -0
- package/dist/types/child.d.ts +16 -0
- package/dist/types/components/XTMicroView.vue.d.ts +6 -0
- package/dist/types/index.d.ts +27 -0
- package/dist/types/parent.d.ts +28 -0
- package/dist/types/types.d.ts +1 -0
- package/dist/types/utils.d.ts +8 -0
- package/package.json +9 -3
- package/postcss.config.js +23 -0
- package/tsconfig.json +24 -0
- package/vite.config.ts +27 -0
- package/index.ts +0 -291
package/dist/style.css
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
.xt-micro{background-color:#fff}.xt-micro:before{align-items:center;content:"加载中";display:flex;justify-content:center;inset:0;position:absolute;z-index:-1}.micro-iframe{border:none;height:100%;margin:0;padding:0;width:100%}.micro-iframe:after{align-items:center;background:#fff;content:"加载中";display:flex;justify-content:center;inset:0;position:absolute}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AppOption, App } from './index.d';
|
|
2
|
+
import { microInit } from './child';
|
|
3
|
+
/**
|
|
4
|
+
* 安装微应用
|
|
5
|
+
* @param app
|
|
6
|
+
*/
|
|
7
|
+
export declare const setupApp: (app: AppOption) => App | undefined;
|
|
8
|
+
export declare const getApp: (name: string) => App;
|
|
9
|
+
/**
|
|
10
|
+
* 启动微应用
|
|
11
|
+
* @param name 应用名称
|
|
12
|
+
* @param options 应用配置
|
|
13
|
+
* @returns
|
|
14
|
+
*/
|
|
15
|
+
export declare const startApp: (name: string, options?: Partial<AppOption>) => Promise<unknown>;
|
|
16
|
+
/**
|
|
17
|
+
* 停止指定名称的应用程序
|
|
18
|
+
* @param {string} name - 应用程序的名称
|
|
19
|
+
*/
|
|
20
|
+
export declare const stopApp: (name: string) => void;
|
|
21
|
+
export { microInit };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MicroInitOptions } from './index.d';
|
|
2
|
+
/**
|
|
3
|
+
* 微应用-子应用初始化
|
|
4
|
+
* 此时方法上下文是子应用
|
|
5
|
+
* 非必须,但建议使用
|
|
6
|
+
* @param options
|
|
7
|
+
*/
|
|
8
|
+
export declare const microInit: (options?: MicroInitOptions) => Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* 同步子应用路由到主应用路由
|
|
11
|
+
*/
|
|
12
|
+
export declare function syncUrlToWindow(): void;
|
|
13
|
+
/**
|
|
14
|
+
* 同步主应用路由到子应用
|
|
15
|
+
*/
|
|
16
|
+
export declare function syncUrlToIframe(iframeWindow: Window): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
declare const _default: import('vue').DefineComponent<{
|
|
2
|
+
appId: StringConstructor;
|
|
3
|
+
}, {}, unknown, {}, {}, import('vue').ComponentOptionsMixin, import('vue').ComponentOptionsMixin, {}, string, import('vue').VNodeProps & import('vue').AllowedComponentProps & import('vue').ComponentCustomProps, Readonly<import('vue').ExtractPropTypes<{
|
|
4
|
+
appId: StringConstructor;
|
|
5
|
+
}>>, {}, {}>;
|
|
6
|
+
export default _default;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { MicroApp } from './parent';
|
|
2
|
+
export interface AppOption {
|
|
3
|
+
name: string
|
|
4
|
+
url: string
|
|
5
|
+
el?: HTMLElement | string
|
|
6
|
+
preload?: boolean
|
|
7
|
+
token?: () => string | string
|
|
8
|
+
params?: { [key: string]: any }
|
|
9
|
+
sync?: boolean
|
|
10
|
+
}
|
|
11
|
+
export interface App extends AppOption {
|
|
12
|
+
instance: MicroApp
|
|
13
|
+
}
|
|
14
|
+
export interface MicroInitOptions {
|
|
15
|
+
hideHeaderCssSelector?: string
|
|
16
|
+
authHandler?: (token: string) => void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// 扩展 Window 接口,防止ts报错
|
|
20
|
+
declare global {
|
|
21
|
+
interface Window {
|
|
22
|
+
[key: string]: any
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
export * from './api'
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { AppOption, App } from './index.d';
|
|
2
|
+
export declare const isMicro: import('vue').Ref<boolean>;
|
|
3
|
+
export declare class MicroApp {
|
|
4
|
+
app: App | AppOption;
|
|
5
|
+
dom: HTMLIFrameElement | undefined;
|
|
6
|
+
panel: HTMLElement | undefined;
|
|
7
|
+
constructor(app: AppOption);
|
|
8
|
+
mount(el: HTMLElement | string | undefined, isHide?: boolean): void;
|
|
9
|
+
unmount(): void;
|
|
10
|
+
/**
|
|
11
|
+
* 预加载渲染
|
|
12
|
+
* TODO 还差用户切走之后,如何将ifr还原回去,等待下次载入
|
|
13
|
+
* @param el 挂载dom对象
|
|
14
|
+
*/
|
|
15
|
+
preloadRender(el: HTMLElement | string | undefined): void;
|
|
16
|
+
/**
|
|
17
|
+
* 预加载app,先放到body,待启动时,挪过来
|
|
18
|
+
*/
|
|
19
|
+
preload(): void;
|
|
20
|
+
changeOptions(options: Partial<AppOption>): void;
|
|
21
|
+
onLoad(_event: Event): void;
|
|
22
|
+
onUnload(_event: Event): void;
|
|
23
|
+
onError(): void;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 初始化主应用微前端
|
|
27
|
+
*/
|
|
28
|
+
export declare const mainMicroInit: () => void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const CONSTANT: Record<string, string>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare function getQueryParameter(name: string, url?: string): string | null;
|
|
2
|
+
export declare function getAbsolutePath(url: string | undefined, base: string, hash?: boolean): string | undefined;
|
|
3
|
+
export declare function anchorElementGenerator(url: string): HTMLAnchorElement;
|
|
4
|
+
export declare function getAnchorElementQueryMap(anchorElement: HTMLAnchorElement): {
|
|
5
|
+
[key: string]: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function debounce<T extends (...args: any[]) => any>(func: T, delay: number): (...args: Parameters<T>) => void;
|
|
8
|
+
export declare function addOrReplaceUrlParam(url: string, paramName: string, paramValue: string): string;
|
package/package.json
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xingtukeji/micro",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
|
-
"main": "index.
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/types/index.d.ts",
|
|
7
8
|
"author": "",
|
|
8
9
|
"license": "ISC",
|
|
9
10
|
"publishConfig": {
|
|
10
11
|
"access": "public",
|
|
11
12
|
"registry": "https://registry.npmjs.org/"
|
|
12
13
|
},
|
|
13
|
-
"
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"vite-plugin-dts": "^4.5.0"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "vite build"
|
|
19
|
+
}
|
|
14
20
|
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
const defaultList = [
|
|
2
|
+
require('postcss-import'), // 使css支持@import相互引入
|
|
3
|
+
// require('postcss-simple-vars'), // 使css支持变量($blue: #056ef0;),老项目如果用到了sass里的变量,需要加载该插件
|
|
4
|
+
require('postcss-utilities'), // 支持常用工具(具体参照: https://github.com/ismamz/postcss-utilities#:~:text=postcss-utilities%20is%20a%20PostCSS%20plugin%20that%20includes%20the,started%20using%20postcss-utilities%20Try%20it%20in%20the%20browser%21)
|
|
5
|
+
require('postcss-nested'), // 支持嵌套语法,貌似postcss-preset-env里含语法嵌套,出现嵌套问题再尝试加载此插件
|
|
6
|
+
require('postcss-preset-env')({
|
|
7
|
+
stage: 0
|
|
8
|
+
}), // 对css4语法进行降阶(如自定义选择器),已经包含autoprefixer
|
|
9
|
+
]
|
|
10
|
+
const developmentList = [
|
|
11
|
+
|
|
12
|
+
]
|
|
13
|
+
|
|
14
|
+
const productionList = [
|
|
15
|
+
require('cssnano'), // css压缩
|
|
16
|
+
]
|
|
17
|
+
module.exports = {
|
|
18
|
+
parser: 'postcss-scss',
|
|
19
|
+
plugins: [
|
|
20
|
+
...defaultList,
|
|
21
|
+
...(process.env.NODE_ENV === 'development' ? developmentList : productionList)
|
|
22
|
+
]
|
|
23
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "esnext",
|
|
4
|
+
"useDefineForClassFields": true,
|
|
5
|
+
"module": "esnext",
|
|
6
|
+
"moduleResolution": "node",
|
|
7
|
+
"strict": false,
|
|
8
|
+
"jsx": "preserve",
|
|
9
|
+
"sourceMap": false,
|
|
10
|
+
"resolveJsonModule": true,
|
|
11
|
+
"esModuleInterop": true,
|
|
12
|
+
"lib": ["esnext", "dom"],
|
|
13
|
+
"paths": {
|
|
14
|
+
"@/*": ["./src/*"]
|
|
15
|
+
},
|
|
16
|
+
"types": ["element-plus/global"],
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"declarationDir": "dist",
|
|
19
|
+
"outDir": "./dist",
|
|
20
|
+
"rootDir": "./src",
|
|
21
|
+
},
|
|
22
|
+
"exclude": ["node_modules", "dist"],
|
|
23
|
+
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
|
|
24
|
+
}
|
package/vite.config.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineConfig } from 'vite'
|
|
2
|
+
import vue from '@vitejs/plugin-vue'
|
|
3
|
+
import dts from 'vite-plugin-dts';
|
|
4
|
+
// import { name, version } from './package.json'
|
|
5
|
+
|
|
6
|
+
export default defineConfig(({ mode }) => {
|
|
7
|
+
return {
|
|
8
|
+
base: './',
|
|
9
|
+
build: {
|
|
10
|
+
lib: {
|
|
11
|
+
entry: ['src/index.ts'],
|
|
12
|
+
name: '__xt_micro__',
|
|
13
|
+
formats: ['es'],
|
|
14
|
+
fileName: (format, entryName) => `index.js`,
|
|
15
|
+
},
|
|
16
|
+
target: 'es2015',
|
|
17
|
+
emptyOutDir: true, //去掉打包到项目外的警告
|
|
18
|
+
outDir: `./dist`,
|
|
19
|
+
},
|
|
20
|
+
plugins: [
|
|
21
|
+
vue(),
|
|
22
|
+
dts({
|
|
23
|
+
outDir: 'dist/types'
|
|
24
|
+
})
|
|
25
|
+
],
|
|
26
|
+
}
|
|
27
|
+
})
|
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
|
-
}
|