zmdms-utils 0.0.1
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/.vscode/settings.json +6 -0
- package/REAMME.md +1 -0
- package/dist/es/_virtual/_tslib.js +65 -0
- package/dist/es/auth.d.ts +7 -0
- package/dist/es/auth.js +22 -0
- package/dist/es/mainApp.d.ts +35 -0
- package/dist/es/mainApp.js +46 -0
- package/dist/es/microAppCreator.d.ts +23 -0
- package/dist/es/microAppCreator.js +69 -0
- package/dist/es/qiankunUtils.d.ts +9 -0
- package/dist/es/qiankunUtils.js +11 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/package.json +30 -0
- package/rollup.esm.config.js +96 -0
- package/src/auth.ts +19 -0
- package/src/index.ts +4 -0
- package/src/mainApp.ts +97 -0
- package/src/microAppCreator.ts +76 -0
- package/src/qiankunUtils.ts +10 -0
- package/tsconfig.json +32 -0
package/REAMME.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
## 公用方法,用于存放一些与框架、平台无关的方法
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
|
|
16
|
+
var __assign = function() {
|
|
17
|
+
__assign = Object.assign || function __assign(t) {
|
|
18
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
19
|
+
s = arguments[i];
|
|
20
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
|
|
21
|
+
}
|
|
22
|
+
return t;
|
|
23
|
+
};
|
|
24
|
+
return __assign.apply(this, arguments);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
28
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
29
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
30
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
31
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
32
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
33
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function __generator(thisArg, body) {
|
|
38
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
39
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
+
function step(op) {
|
|
42
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
44
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
45
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
+
switch (op[0]) {
|
|
47
|
+
case 0: case 1: t = op; break;
|
|
48
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
+
default:
|
|
52
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
+
if (t[2]) _.ops.pop();
|
|
57
|
+
_.trys.pop(); continue;
|
|
58
|
+
}
|
|
59
|
+
op = body.call(thisArg, _);
|
|
60
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { __assign, __awaiter, __generator };
|
package/dist/es/auth.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 权限相关
|
|
3
|
+
*/
|
|
4
|
+
function getToken() {
|
|
5
|
+
try {
|
|
6
|
+
return window.localStorage.getItem("system-token");
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
console.error("获取token失败", err);
|
|
10
|
+
return "";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function setToken(token) {
|
|
14
|
+
try {
|
|
15
|
+
window.localStorage.setItem("system-token", token);
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
console.error("设置token失败", err);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { getToken, setToken };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
interface IEntry {
|
|
2
|
+
scripts?: string[];
|
|
3
|
+
styles?: string[];
|
|
4
|
+
html?: string;
|
|
5
|
+
}
|
|
6
|
+
type IActiveRule = (location: Location) => boolean;
|
|
7
|
+
interface IMicroApp {
|
|
8
|
+
name: string;
|
|
9
|
+
entry: string | IEntry;
|
|
10
|
+
container: string | HTMLElement;
|
|
11
|
+
activeRule: string | string[] | IActiveRule | IActiveRule[];
|
|
12
|
+
loader?: (loading: boolean) => void;
|
|
13
|
+
props?: object;
|
|
14
|
+
}
|
|
15
|
+
interface IOtherOptions {
|
|
16
|
+
/** 过滤掉的一些资源的关键词,必须保证不过滤掉一些不该过滤的东西 */
|
|
17
|
+
whiteWords?: string[];
|
|
18
|
+
/** loading 发生变化触发事件 */
|
|
19
|
+
loader?: (loading: boolean) => void;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 主应用初始化逻辑
|
|
23
|
+
* @param microApps 子应用列表
|
|
24
|
+
* @param defaultAppLink 设置主应用启动后默认进入的微应用
|
|
25
|
+
* @param otherOptions
|
|
26
|
+
*/
|
|
27
|
+
declare function initMainApp(microApps: IMicroApp[], defaultAppLink: string, otherOptions?: IOtherOptions): void;
|
|
28
|
+
/**
|
|
29
|
+
* 全局错误处理
|
|
30
|
+
* @param handle 错误函数
|
|
31
|
+
* @returns 清空监听
|
|
32
|
+
*/
|
|
33
|
+
declare function initGlobalError(handle: (event: any) => void): () => void;
|
|
34
|
+
|
|
35
|
+
export { IMicroApp, initGlobalError, initMainApp };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { __assign } from './_virtual/_tslib.js';
|
|
2
|
+
import { registerMicroApps, setDefaultMountApp, start, runAfterFirstMounted, addGlobalUncaughtErrorHandler, removeGlobalUncaughtErrorHandler } from 'qiankun';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 主应用初始化逻辑
|
|
6
|
+
* @param microApps 子应用列表
|
|
7
|
+
* @param defaultAppLink 设置主应用启动后默认进入的微应用
|
|
8
|
+
* @param otherOptions
|
|
9
|
+
*/
|
|
10
|
+
function initMainApp(microApps, defaultAppLink, otherOptions) {
|
|
11
|
+
if (otherOptions === void 0) { otherOptions = {}; }
|
|
12
|
+
var _a = otherOptions.whiteWords, whiteWords = _a === void 0 ? [] : _a, loader = otherOptions.loader;
|
|
13
|
+
/**
|
|
14
|
+
* Step2 注册子应用
|
|
15
|
+
*/
|
|
16
|
+
registerMicroApps(microApps.map(function (item) { return (__assign(__assign({}, item), { loader: loader })); }));
|
|
17
|
+
/**
|
|
18
|
+
* Setp3 设置默认进入的子应用
|
|
19
|
+
*/
|
|
20
|
+
setDefaultMountApp(defaultAppLink);
|
|
21
|
+
/**
|
|
22
|
+
* Setp4 启动应用
|
|
23
|
+
*/
|
|
24
|
+
start({
|
|
25
|
+
excludeAssetFilter: function (urls) {
|
|
26
|
+
// 指定部分特殊的动态加载的微应用资源(css/js) 不被 qiankun 劫持处理
|
|
27
|
+
return whiteWords.some(function (w) { return urls.includes(w); });
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
runAfterFirstMounted(function () {
|
|
31
|
+
console.log("第一个微应用加载成功!");
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 全局错误处理
|
|
36
|
+
* @param handle 错误函数
|
|
37
|
+
* @returns 清空监听
|
|
38
|
+
*/
|
|
39
|
+
function initGlobalError(handle) {
|
|
40
|
+
addGlobalUncaughtErrorHandler(handle);
|
|
41
|
+
return function () {
|
|
42
|
+
removeGlobalUncaughtErrorHandler(handle);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { initGlobalError, initMainApp };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 微应用初始化
|
|
3
|
+
* @param render 加载应用方法
|
|
4
|
+
* @param microAppOptions 微应用的一些配置
|
|
5
|
+
*/
|
|
6
|
+
interface IMicroAppOptions {
|
|
7
|
+
microAppContainerId: string;
|
|
8
|
+
microAppKey: string;
|
|
9
|
+
microAppRoot: any;
|
|
10
|
+
microAppMountHandle?: (props: any) => void;
|
|
11
|
+
microAppUnMountHandle?: (props: any) => void;
|
|
12
|
+
}
|
|
13
|
+
declare function initMicroApp(render: any, microAppOptions: IMicroAppOptions): {
|
|
14
|
+
bootstrap?: undefined;
|
|
15
|
+
mount?: undefined;
|
|
16
|
+
unmount?: undefined;
|
|
17
|
+
} | {
|
|
18
|
+
bootstrap: () => Promise<void>;
|
|
19
|
+
mount: (props: any) => Promise<void>;
|
|
20
|
+
unmount: (props: any) => Promise<void>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export { IMicroAppOptions, initMicroApp };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { __awaiter, __generator } from './_virtual/_tslib.js';
|
|
2
|
+
import { isQiankun } from './qiankunUtils.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 可选生命周期钩子,仅使用 loadMicroApp 方式加载微应用时生效
|
|
6
|
+
*/
|
|
7
|
+
function update(props) {
|
|
8
|
+
console.log("可以在update声明周期中做的一些公共处理的事!", props);
|
|
9
|
+
}
|
|
10
|
+
function initMicroApp(render, microAppOptions) {
|
|
11
|
+
// 初始加载应用
|
|
12
|
+
// 1. 如果不是微前端环境,那么应用直接运行
|
|
13
|
+
if (!isQiankun()) {
|
|
14
|
+
render({});
|
|
15
|
+
return {};
|
|
16
|
+
}
|
|
17
|
+
// 2. 进入微前端环境的运行逻辑
|
|
18
|
+
var microAppKey = microAppOptions.microAppKey, microAppRoot = microAppOptions.microAppRoot, microAppMountHandle = microAppOptions.microAppMountHandle, microAppUnMountHandle = microAppOptions.microAppUnMountHandle;
|
|
19
|
+
// 唯一key没传 给出开发提示
|
|
20
|
+
if (!microAppKey) {
|
|
21
|
+
throw Error("\u6CA1\u6709\u914D\u7F6E\u5FAE\u5E94\u7528\u7684\u552F\u4E00key\u503C\uFF0C\u8FD9\u4F1A\u5BFC\u81F4\u5FAE\u524D\u7AEF\u8FD0\u884C\u51FA\u9519\uFF01");
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* bootstrap 只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子,不会再重复触发 bootstrap。
|
|
25
|
+
* 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
|
|
26
|
+
*/
|
|
27
|
+
function bootstrap() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
29
|
+
return __generator(this, function (_a) {
|
|
30
|
+
console.log("微应用 bootstraped");
|
|
31
|
+
return [2 /*return*/];
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
|
|
37
|
+
*/
|
|
38
|
+
function mount(props) {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
40
|
+
return __generator(this, function (_a) {
|
|
41
|
+
console.log("微应用 mount");
|
|
42
|
+
microAppMountHandle && microAppMountHandle(props);
|
|
43
|
+
render(props);
|
|
44
|
+
return [2 /*return*/];
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
|
|
50
|
+
*/
|
|
51
|
+
function unmount(props) {
|
|
52
|
+
var _a;
|
|
53
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
54
|
+
return __generator(this, function (_b) {
|
|
55
|
+
console.log("微应用 unmount");
|
|
56
|
+
microAppUnMountHandle && microAppUnMountHandle(props);
|
|
57
|
+
(_a = microAppRoot === null || microAppRoot === void 0 ? void 0 : microAppRoot.unmount) === null || _a === void 0 ? void 0 : _a.call(microAppRoot);
|
|
58
|
+
return [2 /*return*/];
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
bootstrap: bootstrap,
|
|
64
|
+
mount: mount,
|
|
65
|
+
unmount: unmount,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { initMicroApp, update };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "zmdms-utils",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"clean": "rimraf ./dist",
|
|
10
|
+
"build": "npm run clean && npm run build-es",
|
|
11
|
+
"build-es": "npm run clean && rollup --config ./rollup.esm.config.js"
|
|
12
|
+
},
|
|
13
|
+
"author": "",
|
|
14
|
+
"license": "ISC",
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"qiankun": ">=2.10.5"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@rollup/plugin-commonjs": "^24.0.1",
|
|
21
|
+
"@rollup/plugin-json": "^6.0.0",
|
|
22
|
+
"@rollup/plugin-node-resolve": "^15.0.1",
|
|
23
|
+
"rimraf": "^4.4.1",
|
|
24
|
+
"rollup": "^3.20.2",
|
|
25
|
+
"rollup-plugin-dts": "^5.3.0",
|
|
26
|
+
"rollup-plugin-peer-deps-external": "^2.2.4",
|
|
27
|
+
"rollup-plugin-typescript2": "^0.34.1",
|
|
28
|
+
"typescript": "^5.0.2"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import typescript from "rollup-plugin-typescript2";
|
|
2
|
+
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
|
3
|
+
import commonjs from "@rollup/plugin-commonjs";
|
|
4
|
+
import json from "@rollup/plugin-json";
|
|
5
|
+
import dts from "rollup-plugin-dts";
|
|
6
|
+
import peerDepsExternal from "rollup-plugin-peer-deps-external";
|
|
7
|
+
import path from "path";
|
|
8
|
+
import fs from "fs";
|
|
9
|
+
|
|
10
|
+
// 覆盖一些ts配置
|
|
11
|
+
const overrides = {
|
|
12
|
+
compilerOptions: {
|
|
13
|
+
// declaration: true, // 生成.d.ts文件
|
|
14
|
+
lib: ["dom", "dom.iterable", "esnext"],
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// 读取组件目录
|
|
19
|
+
const utilsDir = "src";
|
|
20
|
+
const utilsName = fs.readdirSync(path.resolve(utilsDir), {
|
|
21
|
+
withFileTypes: true,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function getEsModuleEntryDir() {
|
|
25
|
+
// 定义入口目录
|
|
26
|
+
const entryDirObj = {};
|
|
27
|
+
utilsName.forEach((dirent) => {
|
|
28
|
+
if (dirent.isFile()) {
|
|
29
|
+
const fileName = dirent.name.substring(0, dirent.name.lastIndexOf("."));
|
|
30
|
+
entryDirObj[`${fileName}`] = `${utilsDir}/${dirent.name}`;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return entryDirObj;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getEsModuleOutput() {
|
|
37
|
+
return {
|
|
38
|
+
preserveModules: true, // 使用原始模块名称作为文件名为所有模块创建单独的块
|
|
39
|
+
entryFileNames: ({ name }) => {
|
|
40
|
+
if (name === "index") {
|
|
41
|
+
return "[name].js";
|
|
42
|
+
}
|
|
43
|
+
// 将业务代码
|
|
44
|
+
return "es/[name].js";
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getDts() {
|
|
50
|
+
return {
|
|
51
|
+
preserveModules: true, // 使用原始模块名称作为文件名为所有模块创建单独的块
|
|
52
|
+
entryFileNames: ({ name }) => {
|
|
53
|
+
const { dir, base } = path.parse(name);
|
|
54
|
+
if (name === "index") {
|
|
55
|
+
return "[name].d.ts";
|
|
56
|
+
}
|
|
57
|
+
return "es/[name].d.ts";
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const componentsEntryObj = getEsModuleEntryDir();
|
|
63
|
+
// es Module打包输出
|
|
64
|
+
const esmOutput = getEsModuleOutput();
|
|
65
|
+
// d.ts 打包输出
|
|
66
|
+
const esmOutputDts = getDts();
|
|
67
|
+
|
|
68
|
+
const config = [
|
|
69
|
+
{
|
|
70
|
+
input: componentsEntryObj,
|
|
71
|
+
output: {
|
|
72
|
+
...esmOutput,
|
|
73
|
+
dir: "dist",
|
|
74
|
+
format: "es",
|
|
75
|
+
},
|
|
76
|
+
plugins: [
|
|
77
|
+
peerDepsExternal(),
|
|
78
|
+
nodeResolve(),
|
|
79
|
+
commonjs(),
|
|
80
|
+
json(),
|
|
81
|
+
typescript({ tsconfigOverride: overrides }),
|
|
82
|
+
],
|
|
83
|
+
},
|
|
84
|
+
// 打包d.ts文件
|
|
85
|
+
{
|
|
86
|
+
input: "src/index.ts",
|
|
87
|
+
output: {
|
|
88
|
+
...esmOutputDts,
|
|
89
|
+
dir: "dist",
|
|
90
|
+
format: "es",
|
|
91
|
+
},
|
|
92
|
+
plugins: [dts({})],
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
export default config;
|
package/src/auth.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 权限相关
|
|
3
|
+
*/
|
|
4
|
+
export function getToken() {
|
|
5
|
+
try {
|
|
6
|
+
return window.localStorage.getItem("system-token");
|
|
7
|
+
} catch (err) {
|
|
8
|
+
console.error("获取token失败", err);
|
|
9
|
+
return "";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function setToken(token: string) {
|
|
14
|
+
try {
|
|
15
|
+
window.localStorage.setItem("system-token", token);
|
|
16
|
+
} catch (err) {
|
|
17
|
+
console.error("设置token失败", err);
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/index.ts
ADDED
package/src/mainApp.ts
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
registerMicroApps,
|
|
3
|
+
setDefaultMountApp,
|
|
4
|
+
start,
|
|
5
|
+
runAfterFirstMounted,
|
|
6
|
+
addGlobalUncaughtErrorHandler,
|
|
7
|
+
removeGlobalUncaughtErrorHandler,
|
|
8
|
+
} from "qiankun";
|
|
9
|
+
|
|
10
|
+
interface IEntry {
|
|
11
|
+
scripts?: string[];
|
|
12
|
+
styles?: string[];
|
|
13
|
+
html?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
type IActiveRule = (location: Location) => boolean;
|
|
17
|
+
|
|
18
|
+
export interface IMicroApp {
|
|
19
|
+
/* 微应用的名称,微应用之间必须确保唯一 */
|
|
20
|
+
name: string;
|
|
21
|
+
/*
|
|
22
|
+
微应用的入口
|
|
23
|
+
为字符串时,标识微应用的访问地址
|
|
24
|
+
为对象时,html的值是微应用的html内容字符串,而不是微应用的访问地址。微应用的publicPath将回被设置为 /
|
|
25
|
+
*/
|
|
26
|
+
entry: string | IEntry;
|
|
27
|
+
/* 微应用的容器节点的选择器或者Element实例。如container: '#root' 或者 document.querySelector('#root') */
|
|
28
|
+
container: string | HTMLElement;
|
|
29
|
+
/*
|
|
30
|
+
微应用的激活规则
|
|
31
|
+
支持直接配置字符串或字符串数组 如activeRule: '/app1' 或 ['/app1', '/app2']。当配置为字符串时会直接跟 url 中的路径部分做前缀匹配,匹配成功表明当前应用会被激活
|
|
32
|
+
支持配置一个active function函数或一组。函数会传入当前location作为参数,函数返回true时,表明当前微应用被激活
|
|
33
|
+
*/
|
|
34
|
+
activeRule: string | string[] | IActiveRule | IActiveRule[];
|
|
35
|
+
/* loading状态发生变化时会调用的方法 */
|
|
36
|
+
loader?: (loading: boolean) => void;
|
|
37
|
+
/* 主应用需要传递给微应用的数据 */
|
|
38
|
+
props?: object;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface IOtherOptions {
|
|
42
|
+
/** 过滤掉的一些资源的关键词,必须保证不过滤掉一些不该过滤的东西 */
|
|
43
|
+
whiteWords?: string[];
|
|
44
|
+
/** loading 发生变化触发事件 */
|
|
45
|
+
loader?: (loading: boolean) => void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 主应用初始化逻辑
|
|
50
|
+
* @param microApps 子应用列表
|
|
51
|
+
* @param defaultAppLink 设置主应用启动后默认进入的微应用
|
|
52
|
+
* @param otherOptions
|
|
53
|
+
*/
|
|
54
|
+
export function initMainApp(
|
|
55
|
+
microApps: IMicroApp[],
|
|
56
|
+
defaultAppLink: string,
|
|
57
|
+
otherOptions: IOtherOptions = {}
|
|
58
|
+
) {
|
|
59
|
+
const { whiteWords = [], loader } = otherOptions;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Step2 注册子应用
|
|
63
|
+
*/
|
|
64
|
+
registerMicroApps(microApps.map((item) => ({ ...item, loader })));
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Setp3 设置默认进入的子应用
|
|
68
|
+
*/
|
|
69
|
+
setDefaultMountApp(defaultAppLink);
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Setp4 启动应用
|
|
73
|
+
*/
|
|
74
|
+
start({
|
|
75
|
+
excludeAssetFilter: (urls: string) => {
|
|
76
|
+
// 指定部分特殊的动态加载的微应用资源(css/js) 不被 qiankun 劫持处理
|
|
77
|
+
return whiteWords.some((w) => urls.includes(w));
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
runAfterFirstMounted(() => {
|
|
82
|
+
console.log("第一个微应用加载成功!");
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 全局错误处理
|
|
88
|
+
* @param handle 错误函数
|
|
89
|
+
* @returns 清空监听
|
|
90
|
+
*/
|
|
91
|
+
export function initGlobalError(handle: (event: any) => void) {
|
|
92
|
+
addGlobalUncaughtErrorHandler(handle);
|
|
93
|
+
|
|
94
|
+
return () => {
|
|
95
|
+
removeGlobalUncaughtErrorHandler(handle);
|
|
96
|
+
};
|
|
97
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description 初始化微应用程序
|
|
3
|
+
*/
|
|
4
|
+
import { isQiankun } from "./qiankunUtils";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 可选生命周期钩子,仅使用 loadMicroApp 方式加载微应用时生效
|
|
8
|
+
*/
|
|
9
|
+
export function update(props: any) {
|
|
10
|
+
console.log("可以在update声明周期中做的一些公共处理的事!", props);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @description 微应用初始化
|
|
15
|
+
* @param render 加载应用方法
|
|
16
|
+
* @param microAppOptions 微应用的一些配置
|
|
17
|
+
*/
|
|
18
|
+
export interface IMicroAppOptions {
|
|
19
|
+
microAppContainerId: string; // 微应用加载的dom容器 id
|
|
20
|
+
microAppKey: string; // 微应用的唯一key值 用途:1. qiankun下的路由前缀;2. 主、子通信时的标识
|
|
21
|
+
microAppRoot: any; // 微应用根节点实例
|
|
22
|
+
microAppMountHandle?: (props: any) => void; // 微应用每次进入都会调用的方法
|
|
23
|
+
microAppUnMountHandle?: (props: any) => void; // 微应用每次切出/卸载 都会调用的方法
|
|
24
|
+
}
|
|
25
|
+
export function initMicroApp(render: any, microAppOptions: IMicroAppOptions) {
|
|
26
|
+
// 初始加载应用
|
|
27
|
+
// 1. 如果不是微前端环境,那么应用直接运行
|
|
28
|
+
if (!isQiankun()) {
|
|
29
|
+
render({});
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
// 2. 进入微前端环境的运行逻辑
|
|
33
|
+
const {
|
|
34
|
+
microAppKey,
|
|
35
|
+
microAppRoot,
|
|
36
|
+
microAppMountHandle,
|
|
37
|
+
microAppUnMountHandle,
|
|
38
|
+
} = microAppOptions;
|
|
39
|
+
|
|
40
|
+
// 唯一key没传 给出开发提示
|
|
41
|
+
if (!microAppKey) {
|
|
42
|
+
throw Error(`没有配置微应用的唯一key值,这会导致微前端运行出错!`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* bootstrap 只会在微应用初始化的时候调用一次,下次微应用重新进入时会直接调用 mount 钩子,不会再重复触发 bootstrap。
|
|
47
|
+
* 通常我们可以在这里做一些全局变量的初始化,比如不会在 unmount 阶段被销毁的应用级别的缓存等。
|
|
48
|
+
*/
|
|
49
|
+
async function bootstrap() {
|
|
50
|
+
console.log("微应用 bootstraped");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* 应用每次进入都会调用 mount 方法,通常我们在这里触发应用的渲染方法
|
|
55
|
+
*/
|
|
56
|
+
async function mount(props: any) {
|
|
57
|
+
console.log("微应用 mount");
|
|
58
|
+
microAppMountHandle && microAppMountHandle(props);
|
|
59
|
+
render(props);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 应用每次 切出/卸载 会调用的方法,通常在这里我们会卸载微应用的应用实例
|
|
64
|
+
*/
|
|
65
|
+
async function unmount(props: any) {
|
|
66
|
+
console.log("微应用 unmount");
|
|
67
|
+
microAppUnMountHandle && microAppUnMountHandle(props);
|
|
68
|
+
microAppRoot?.unmount?.();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
bootstrap,
|
|
73
|
+
mount,
|
|
74
|
+
unmount,
|
|
75
|
+
};
|
|
76
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5", // 指定ECMAScript目标版本
|
|
4
|
+
// 编译过程中需要映入的库文件列表
|
|
5
|
+
"lib": ["dom", "dom.iterable", "esnext"],
|
|
6
|
+
"allowJs": true, // 允许编译JS文件
|
|
7
|
+
"skipLibCheck": true, // 忽略所有的声明文件(*.d.ts)的类型检查
|
|
8
|
+
// https://zhuanlan.zhihu.com/p/148081795
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
// 允许从没有设置默认导出的模块中默认导入。
|
|
11
|
+
"allowSyntheticDefaultImports": true,
|
|
12
|
+
// 启用所有严格类型检查选项
|
|
13
|
+
"strict": true,
|
|
14
|
+
// 禁止对同一个文件的不一致的引用
|
|
15
|
+
"forceConsistentCasingInFileNames": true,
|
|
16
|
+
// 报告switch语句的fallthrough错误。(即,不允许switch的case语句贯穿)
|
|
17
|
+
"noFallthroughCasesInSwitch": true,
|
|
18
|
+
// 指定生成哪个模块系统代码
|
|
19
|
+
"module": "esnext",
|
|
20
|
+
// 决定如何处理模块
|
|
21
|
+
"moduleResolution": "node",
|
|
22
|
+
// 解析json模块
|
|
23
|
+
"resolveJsonModule": true,
|
|
24
|
+
// 将每个文件作为单独的模块
|
|
25
|
+
"isolatedModules": true,
|
|
26
|
+
// 不生成输出文件。仅用tsc做类型检查时,当一些其他工具(webpack、rollup)负责编译你的代码时
|
|
27
|
+
"noEmit": true,
|
|
28
|
+
"jsx": "react-jsx",
|
|
29
|
+
"outDir": "dist"
|
|
30
|
+
},
|
|
31
|
+
"include": ["src"]
|
|
32
|
+
}
|