@umijs/plugins 4.0.0-beta.13 → 4.0.0-beta.17
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 +4 -1
- package/dist/antd.js +2 -2
- package/dist/dva.js +4 -5
- package/dist/layout.js +47 -13
- package/dist/locale.d.ts +1 -0
- package/dist/locale.js +199 -1
- package/dist/qiankun/constants.d.ts +5 -0
- package/dist/qiankun/constants.js +8 -0
- package/dist/qiankun/master.d.ts +6 -0
- package/dist/qiankun/master.js +114 -0
- package/dist/qiankun/slave.d.ts +3 -0
- package/dist/qiankun/slave.js +141 -0
- package/dist/qiankun.js +15 -1
- package/dist/request.js +297 -1
- package/dist/tailwindcss.d.ts +3 -0
- package/dist/tailwindcss.js +38 -0
- package/dist/unocss.d.ts +3 -0
- package/dist/unocss.js +57 -0
- package/dist/utils/localeUtils.d.ts +33 -0
- package/dist/utils/localeUtils.js +135 -0
- package/dist/utils/modelUtils.d.ts +1 -0
- package/dist/utils/modelUtils.js +17 -3
- package/libs/locale/SelectLang.tpl +478 -0
- package/libs/locale/locale.tpl +82 -0
- package/libs/locale/localeExports.tpl +271 -0
- package/libs/locale/runtime.tpl +33 -0
- package/libs/qiankun/master/AntdErrorBoundary.tsx +34 -0
- package/libs/qiankun/master/AntdLoader.tsx +15 -0
- package/libs/qiankun/master/ErrorBoundary.tsx +7 -0
- package/libs/qiankun/master/MicroApp.tsx +262 -0
- package/libs/qiankun/master/MicroAppWithMemoHistory.tsx +43 -0
- package/libs/qiankun/master/common.ts +133 -0
- package/libs/qiankun/master/constants.ts +7 -0
- package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +45 -0
- package/libs/qiankun/master/masterRuntimePlugin.tsx +130 -0
- package/libs/qiankun/master/types.ts +44 -0
- package/libs/qiankun/slave/connectMaster.tsx +15 -0
- package/libs/qiankun/slave/lifecycles.ts +149 -0
- package/libs/qiankun/slave/qiankunModel.ts +19 -0
- package/libs/qiankun/slave/slaveRuntimePlugin.ts +21 -0
- package/package.json +12 -4
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
import React, { useCallback, useEffect, useRef } from 'react';
|
|
5
|
+
import { MicroApp, Props as MicroAppProps } from './MicroApp';
|
|
6
|
+
|
|
7
|
+
export interface Props extends MicroAppProps {
|
|
8
|
+
history?: never;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function MicroAppWithMemoHistory(componentProps: Props) {
|
|
12
|
+
const { url, ...rest } = componentProps;
|
|
13
|
+
const history = useRef();
|
|
14
|
+
// url 的变更不会透传给下游,组件内自己会处理掉,所以这里直接用 ref 来存
|
|
15
|
+
const historyOpts = useRef({
|
|
16
|
+
type: 'memory',
|
|
17
|
+
initialEntries: [url],
|
|
18
|
+
initialIndex: 1,
|
|
19
|
+
});
|
|
20
|
+
const historyInitHandler = useCallback((h) => (history.current = h), []);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
// push history for slave app when url property changed
|
|
24
|
+
// the initial url will be ignored because the history has not been initialized
|
|
25
|
+
if (history.current && url) {
|
|
26
|
+
history.current.push(url);
|
|
27
|
+
}
|
|
28
|
+
}, [url]);
|
|
29
|
+
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
// reset the history when name changed
|
|
32
|
+
historyOpts.current.initialEntries = [url];
|
|
33
|
+
historyOpts.current.initialIndex = 1;
|
|
34
|
+
}, [componentProps.name]);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<MicroApp
|
|
38
|
+
{...rest}
|
|
39
|
+
history={historyOpts.current}
|
|
40
|
+
onHistoryInit={historyInitHandler}
|
|
41
|
+
/>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
/**
|
|
4
|
+
* @author Kuitos
|
|
5
|
+
* @since 2019-06-20
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { ReactComponentElement } from 'react';
|
|
9
|
+
import type { IRouteProps } from 'umi';
|
|
10
|
+
|
|
11
|
+
export const defaultMountContainerId = 'root-subapp';
|
|
12
|
+
|
|
13
|
+
// @formatter:off
|
|
14
|
+
export const noop = () => {};
|
|
15
|
+
// @formatter:on
|
|
16
|
+
|
|
17
|
+
export function toArray<T>(source: T | T[]): T[] {
|
|
18
|
+
return Array.isArray(source) ? source : [source];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function testPathWithStaticPrefix(pathPrefix: string, realPath: string) {
|
|
22
|
+
if (pathPrefix.endsWith('/')) {
|
|
23
|
+
return realPath.startsWith(pathPrefix);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const pathRegex = new RegExp(`^${pathPrefix}([/?])+.*$`, 'g');
|
|
27
|
+
const normalizedPath = `${realPath}/`;
|
|
28
|
+
return pathRegex.test(normalizedPath);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// function testPathWithDynamicRoute(dynamicRoute: string, realPath: string) {
|
|
32
|
+
// // FIXME 这个是旧的使用方式才会调到的 api,先临时这么苟一下消除报错,引导用户去迁移吧
|
|
33
|
+
// const pathToRegexp = require('path-to-regexp');
|
|
34
|
+
// return pathToRegexp(dynamicRoute, { strict: true, end: false }).test(
|
|
35
|
+
// realPath,
|
|
36
|
+
// );
|
|
37
|
+
// }
|
|
38
|
+
//
|
|
39
|
+
// export function testPathWithPrefix(pathPrefix: string, realPath: string) {
|
|
40
|
+
// return (
|
|
41
|
+
// testPathWithStaticPrefix(pathPrefix, realPath) ||
|
|
42
|
+
// testPathWithDynamicRoute(pathPrefix, realPath)
|
|
43
|
+
// );
|
|
44
|
+
// }
|
|
45
|
+
|
|
46
|
+
export function patchMicroAppRoute(
|
|
47
|
+
route: any,
|
|
48
|
+
getMicroAppRouteComponent: (opts: {
|
|
49
|
+
appName: string;
|
|
50
|
+
base: string;
|
|
51
|
+
masterHistoryType: string;
|
|
52
|
+
routeProps?: any;
|
|
53
|
+
}) => string | ReactComponentElement<any>,
|
|
54
|
+
masterOptions: {
|
|
55
|
+
base: string;
|
|
56
|
+
masterHistoryType: string;
|
|
57
|
+
routeBindingAlias: string;
|
|
58
|
+
},
|
|
59
|
+
) {
|
|
60
|
+
const { base, masterHistoryType, routeBindingAlias } = masterOptions;
|
|
61
|
+
// 当配置了 routeBindingAlias 时,优先从 routeBindingAlias 里取配置,但同时也兼容使用了默认的 microApp 方式
|
|
62
|
+
const microAppName = route[routeBindingAlias] || route.microApp;
|
|
63
|
+
const microAppProps =
|
|
64
|
+
route[`${routeBindingAlias}Props`] || route.microAppProps || {};
|
|
65
|
+
if (microAppName) {
|
|
66
|
+
if (route.routes?.length) {
|
|
67
|
+
const childrenRouteHasComponent = route.routes.some(
|
|
68
|
+
(r: any) => r.component,
|
|
69
|
+
);
|
|
70
|
+
if (childrenRouteHasComponent) {
|
|
71
|
+
throw new Error(
|
|
72
|
+
`[@umijs/plugin-qiankun]: You can not attach micro app ${microAppName} to route ${route.path} whose children has own component!`,
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
route.exact = false;
|
|
78
|
+
|
|
79
|
+
const { settings = {}, ...componentProps } = microAppProps;
|
|
80
|
+
const routeProps = {
|
|
81
|
+
// 兼容以前的 settings 配置
|
|
82
|
+
settings: route.settings || settings || {},
|
|
83
|
+
...componentProps,
|
|
84
|
+
};
|
|
85
|
+
const opts = {
|
|
86
|
+
appName: microAppName,
|
|
87
|
+
base,
|
|
88
|
+
masterHistoryType,
|
|
89
|
+
routeProps,
|
|
90
|
+
};
|
|
91
|
+
route.component = getMicroAppRouteComponent(opts);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const recursiveSearch = (
|
|
96
|
+
routes: IRouteProps[],
|
|
97
|
+
path: string,
|
|
98
|
+
): IRouteProps | null => {
|
|
99
|
+
for (let i = 0; i < routes.length; i++) {
|
|
100
|
+
if (routes[i].path === path) {
|
|
101
|
+
return routes[i];
|
|
102
|
+
}
|
|
103
|
+
if (routes[i].routes && routes[i].routes?.length) {
|
|
104
|
+
const found = recursiveSearch(routes[i].routes || [], path);
|
|
105
|
+
if (found) {
|
|
106
|
+
return found;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
export function insertRoute(routes: IRouteProps[], microAppRoute: IRouteProps) {
|
|
114
|
+
const found = recursiveSearch(routes, microAppRoute.insert);
|
|
115
|
+
if (found) {
|
|
116
|
+
if (
|
|
117
|
+
!microAppRoute.path ||
|
|
118
|
+
!found.path ||
|
|
119
|
+
!microAppRoute.path.startsWith(found.path)
|
|
120
|
+
) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`[plugin-qiankun]: path "${microAppRoute.path}" need to starts with "${found.path}"`,
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
found.exact = false;
|
|
126
|
+
found.routes = found.routes || [];
|
|
127
|
+
found.routes.push(microAppRoute);
|
|
128
|
+
} else {
|
|
129
|
+
throw new Error(
|
|
130
|
+
`[plugin-qiankun]: path "${microAppRoute.insert}" not found`,
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
export const defaultMasterRootId = 'root-master';
|
|
5
|
+
export const defaultHistoryType = 'browser';
|
|
6
|
+
export const qiankunStateForSlaveModelNamespace = '@@qiankunStateForSlave';
|
|
7
|
+
export const qiankunStateFromMasterModelNamespace = '@@qiankunStateFromMaster';
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { MicroApp } from './MicroApp';
|
|
3
|
+
{{#runtimeHistory}}
|
|
4
|
+
import { getCreateHistoryOptions } from 'umi';
|
|
5
|
+
{{/runtimeHistory}}
|
|
6
|
+
import { useLocation } from 'umi';
|
|
7
|
+
|
|
8
|
+
export function getMicroAppRouteComponent(opts: {
|
|
9
|
+
appName: string;
|
|
10
|
+
base: string;
|
|
11
|
+
masterHistoryType: string;
|
|
12
|
+
routeProps?: any;
|
|
13
|
+
}) {
|
|
14
|
+
const { base, masterHistoryType, appName, routeProps } = opts;
|
|
15
|
+
const RouteComponent = ({ match }: any) => {
|
|
16
|
+
const url = useLocation().pathname;
|
|
17
|
+
|
|
18
|
+
// 默认取静态配置的 base
|
|
19
|
+
let umiConfigBase = base === '/' ? '' : base;
|
|
20
|
+
|
|
21
|
+
{{#runtimeHistory}}
|
|
22
|
+
// 存在 getCreateHistoryOptions 说明当前应用开启了 runtimeHistory,此时取运行时的 history 配置的 basename
|
|
23
|
+
const { basename = '/' } = getCreateHistoryOptions();
|
|
24
|
+
umiConfigBase = basename === '/' ? '' : basename;
|
|
25
|
+
{{/runtimeHistory}}
|
|
26
|
+
|
|
27
|
+
let runtimeMatchedBase =
|
|
28
|
+
umiConfigBase + (url.endsWith('/') ? url.substr(0, url.length - 1) : url);
|
|
29
|
+
|
|
30
|
+
{{#dynamicRoot}}
|
|
31
|
+
// @see https://github.com/umijs/umi/blob/master/packages/preset-built-in/src/plugins/commands/htmlUtils.ts#L102
|
|
32
|
+
runtimeMatchedBase = window.routerBase || location.pathname.split('/').slice(0, -(path.split('/').length - 1)).concat('').join('/');
|
|
33
|
+
{{/dynamicRoot}}
|
|
34
|
+
|
|
35
|
+
const componentProps = {
|
|
36
|
+
name: appName,
|
|
37
|
+
base: runtimeMatchedBase,
|
|
38
|
+
history: masterHistoryType,
|
|
39
|
+
...routeProps,
|
|
40
|
+
};
|
|
41
|
+
return <MicroApp {...componentProps} />;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return RouteComponent;
|
|
45
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
import { getPluginManager } from '@@/core/plugin';
|
|
5
|
+
import { prefetchApps } from 'qiankun';
|
|
6
|
+
import { ApplyPluginsType } from 'umi';
|
|
7
|
+
import { insertRoute, noop, patchMicroAppRoute } from './common';
|
|
8
|
+
import { getMicroAppRouteComponent } from './getMicroAppRouteComponent';
|
|
9
|
+
import { getMasterOptions, setMasterOptions } from './masterOptions';
|
|
10
|
+
import { MasterOptions, MicroAppRoute } from './types';
|
|
11
|
+
|
|
12
|
+
let microAppRuntimeRoutes: MicroAppRoute[];
|
|
13
|
+
|
|
14
|
+
async function getMasterRuntime() {
|
|
15
|
+
const config = await getPluginManager().applyPlugins({
|
|
16
|
+
key: 'qiankun',
|
|
17
|
+
type: ApplyPluginsType.modify,
|
|
18
|
+
initialValue: {},
|
|
19
|
+
async: true,
|
|
20
|
+
});
|
|
21
|
+
const { master } = config;
|
|
22
|
+
return master || config;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// modify route with "microApp" attribute to use real component
|
|
26
|
+
function patchMicroAppRouteComponent(routes: any[]) {
|
|
27
|
+
const insertRoutes = microAppRuntimeRoutes.filter((r) => r.insert);
|
|
28
|
+
// 先处理 insert 配置
|
|
29
|
+
insertRoutes.forEach((route) => {
|
|
30
|
+
insertRoute(routes, route);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const getRootRoutes = (routes: any[]) => {
|
|
34
|
+
const rootRoute = routes.find((route) => route.path === '/');
|
|
35
|
+
if (rootRoute) {
|
|
36
|
+
// 如果根路由是叶子节点,则直接返回其父节点
|
|
37
|
+
if (!rootRoute.routes) {
|
|
38
|
+
return routes;
|
|
39
|
+
}
|
|
40
|
+
return getRootRoutes(rootRoute.routes);
|
|
41
|
+
}
|
|
42
|
+
return routes;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const rootRoutes = getRootRoutes(routes);
|
|
46
|
+
if (rootRoutes) {
|
|
47
|
+
const { routeBindingAlias, base, masterHistoryType } =
|
|
48
|
+
getMasterOptions() as MasterOptions;
|
|
49
|
+
microAppRuntimeRoutes.reverse().forEach((microAppRoute) => {
|
|
50
|
+
const patchRoute = (route: any) => {
|
|
51
|
+
patchMicroAppRoute(route, getMicroAppRouteComponent, {
|
|
52
|
+
base,
|
|
53
|
+
masterHistoryType,
|
|
54
|
+
routeBindingAlias,
|
|
55
|
+
});
|
|
56
|
+
if (route.routes?.length) {
|
|
57
|
+
route.routes.forEach(patchRoute);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
patchRoute(microAppRoute);
|
|
62
|
+
!microAppRoute.insert && rootRoutes.unshift(microAppRoute);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function render(oldRender: typeof noop) {
|
|
68
|
+
const runtimeOptions = await getMasterRuntime();
|
|
69
|
+
let masterOptions: MasterOptions = {
|
|
70
|
+
...getMasterOptions(),
|
|
71
|
+
...runtimeOptions,
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
const masterApps = masterOptions.apps || [];
|
|
75
|
+
const credentialsApps = masterApps.filter((app) => app.credentials);
|
|
76
|
+
if (credentialsApps.length) {
|
|
77
|
+
const defaultFetch = masterOptions.fetch || window.fetch;
|
|
78
|
+
const fetchWithCredentials = (url: string, init?: RequestInit) => {
|
|
79
|
+
// 如果当前 url 为 credentials 应用的 entry,则为其加上 cors 相关配置
|
|
80
|
+
if (credentialsApps.some((app) => app.entry === url)) {
|
|
81
|
+
return defaultFetch(url, {
|
|
82
|
+
...init,
|
|
83
|
+
mode: 'cors',
|
|
84
|
+
credentials: 'include',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return defaultFetch(url, init);
|
|
88
|
+
};
|
|
89
|
+
// 设置新的 fetch
|
|
90
|
+
masterOptions = { ...masterOptions, fetch: fetchWithCredentials };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// 更新 master options
|
|
94
|
+
setMasterOptions(masterOptions);
|
|
95
|
+
|
|
96
|
+
const { apps = [], routes, ...options } = masterOptions;
|
|
97
|
+
microAppRuntimeRoutes = routes;
|
|
98
|
+
|
|
99
|
+
// 主应用相关的配置注册完毕后即可开启渲染
|
|
100
|
+
oldRender();
|
|
101
|
+
|
|
102
|
+
// 未使用 base 配置的可以认为是路由关联或者使用标签装载的应用
|
|
103
|
+
const loadableApps = apps.filter((app) => !app.base);
|
|
104
|
+
if (loadableApps.length) {
|
|
105
|
+
const { prefetch, ...importEntryOpts } = options;
|
|
106
|
+
if (prefetch === 'all') {
|
|
107
|
+
prefetchApps(loadableApps, importEntryOpts);
|
|
108
|
+
} else if (Array.isArray(prefetch)) {
|
|
109
|
+
const specialPrefetchApps = loadableApps.filter(
|
|
110
|
+
(app) => prefetch.indexOf(app.name) !== -1,
|
|
111
|
+
);
|
|
112
|
+
prefetchApps(specialPrefetchApps, importEntryOpts);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// 使用了 base 配置的应用为可注册应用
|
|
117
|
+
// 不再支持
|
|
118
|
+
const registrableApps = apps.filter((app) => app.base);
|
|
119
|
+
if (registrableApps.length) {
|
|
120
|
+
console.error(
|
|
121
|
+
'[plugins/qiankun] 检测到还在使用旧版配置,该配置已移除,请尽快升级到最新配置方式以获得更好的开发体验,详见 https://umijs.org/plugins/plugin-qiankun#%E5%8D%87%E7%BA%A7%E6%8C%87%E5%8D%97',
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function patchRoutes({ routes }: { routes: any[] }) {
|
|
127
|
+
if (microAppRuntimeRoutes) {
|
|
128
|
+
patchMicroAppRouteComponent(routes);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import { FrameworkConfiguration, FrameworkLifeCycles } from 'qiankun';
|
|
4
|
+
|
|
5
|
+
type BaseIConfig = any;
|
|
6
|
+
|
|
7
|
+
export type HistoryType = 'browser' | 'hash';
|
|
8
|
+
export type App = {
|
|
9
|
+
name: string;
|
|
10
|
+
entry: string | { scripts: string[]; styles: string[] };
|
|
11
|
+
base?: string | string[];
|
|
12
|
+
history?: HistoryType;
|
|
13
|
+
// 取 entry 时是否需要开启跨域 credentials
|
|
14
|
+
credentials?: boolean;
|
|
15
|
+
props?: any;
|
|
16
|
+
} & Pick<BaseIConfig, 'mountElementId'>;
|
|
17
|
+
|
|
18
|
+
export type MicroAppRoute = {
|
|
19
|
+
path: string;
|
|
20
|
+
microApp: string;
|
|
21
|
+
} & Record<string, any>;
|
|
22
|
+
|
|
23
|
+
export type MasterOptions = {
|
|
24
|
+
enable?: boolean;
|
|
25
|
+
apps?: App[];
|
|
26
|
+
routes?: MicroAppRoute[];
|
|
27
|
+
lifeCycles?: FrameworkLifeCycles<object>;
|
|
28
|
+
masterHistoryType?: HistoryType;
|
|
29
|
+
base?: string;
|
|
30
|
+
// 关联路由标记的别名,默认 microApp
|
|
31
|
+
routeBindingAlias?: string;
|
|
32
|
+
// 导出的组件别名,默认 MicroApp
|
|
33
|
+
exportComponentAlias?: string;
|
|
34
|
+
} & FrameworkConfiguration;
|
|
35
|
+
|
|
36
|
+
export type SlaveOptions = {
|
|
37
|
+
enable?: boolean;
|
|
38
|
+
devSourceMap?: boolean;
|
|
39
|
+
keepOriginalRoutes?: boolean | string;
|
|
40
|
+
shouldNotModifyRuntimePublicPath?: boolean;
|
|
41
|
+
shouldNotModifyDefaultBase?: boolean;
|
|
42
|
+
// library name 是否增加 -[name] 应对多 chunk 场景
|
|
43
|
+
shouldNotAddLibraryChunkName?: boolean;
|
|
44
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
__USE_MODEL__;
|
|
4
|
+
import React from 'react';
|
|
5
|
+
|
|
6
|
+
const noop = () => {};
|
|
7
|
+
|
|
8
|
+
const connectMaster = <T extends object>(Component: React.ComponentType<T>) => {
|
|
9
|
+
return (props: T, ...rest: any[]) => {
|
|
10
|
+
const masterProps = (useModel || noop)('@@qiankunStateFromMaster') || {};
|
|
11
|
+
return <Component {...props} {...rest} {...masterProps} />;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { connectMaster };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import { getPluginManager } from '@@/core/plugin';
|
|
4
|
+
import ReactDOM from 'react-dom';
|
|
5
|
+
import { ApplyPluginsType } from 'umi';
|
|
6
|
+
import { setModelState } from './qiankunModel';
|
|
7
|
+
|
|
8
|
+
const noop = () => {};
|
|
9
|
+
|
|
10
|
+
type Defer = {
|
|
11
|
+
promise: Promise<any>;
|
|
12
|
+
resolve(value?: any): void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
const defer: Defer = {};
|
|
17
|
+
defer.promise = new Promise((resolve) => {
|
|
18
|
+
defer.resolve = resolve;
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
let render = noop;
|
|
22
|
+
let hasMountedAtLeastOnce = false;
|
|
23
|
+
|
|
24
|
+
export default () => defer.promise;
|
|
25
|
+
export const clientRenderOptsStack: any[] = [];
|
|
26
|
+
|
|
27
|
+
// function normalizeHistory(
|
|
28
|
+
// history?: 'string' | Record<string, any>,
|
|
29
|
+
// base?: string,
|
|
30
|
+
// ) {
|
|
31
|
+
// let normalizedHistory: Record<string, any> = {};
|
|
32
|
+
// if (base) normalizedHistory.basename = base;
|
|
33
|
+
// if (history) {
|
|
34
|
+
// if (typeof history === 'string') {
|
|
35
|
+
// normalizedHistory.type = history;
|
|
36
|
+
// } else {
|
|
37
|
+
// normalizedHistory = history;
|
|
38
|
+
// }
|
|
39
|
+
// }
|
|
40
|
+
//
|
|
41
|
+
// return normalizedHistory;
|
|
42
|
+
// }
|
|
43
|
+
|
|
44
|
+
async function getSlaveRuntime() {
|
|
45
|
+
const config = await getPluginManager().applyPlugins({
|
|
46
|
+
key: 'qiankun',
|
|
47
|
+
type: ApplyPluginsType.modify,
|
|
48
|
+
initialValue: {},
|
|
49
|
+
async: true,
|
|
50
|
+
});
|
|
51
|
+
// 应用既是 master 又是 slave 的场景,运行时 slave 配置方式为 export const qiankun = { slave: {} }
|
|
52
|
+
const { slave } = config;
|
|
53
|
+
return slave || config;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function genBootstrap(oldRender: typeof noop) {
|
|
57
|
+
return async (props: any) => {
|
|
58
|
+
const slaveRuntime = await getSlaveRuntime();
|
|
59
|
+
if (slaveRuntime.bootstrap) {
|
|
60
|
+
await slaveRuntime.bootstrap(props);
|
|
61
|
+
}
|
|
62
|
+
render = oldRender;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function genMount(mountElementId: string) {
|
|
67
|
+
return async (props?: any) => {
|
|
68
|
+
// props 有值时说明应用是通过 lifecycle 被主应用唤醒的,而不是独立运行时自己 mount
|
|
69
|
+
if (typeof props !== 'undefined') {
|
|
70
|
+
setModelState(props);
|
|
71
|
+
|
|
72
|
+
const slaveRuntime = await getSlaveRuntime();
|
|
73
|
+
if (slaveRuntime.mount) {
|
|
74
|
+
await slaveRuntime.mount(props);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 更新 clientRender 配置
|
|
78
|
+
const clientRenderOpts = {
|
|
79
|
+
// 默认开启
|
|
80
|
+
// 如果需要手动控制 loading,通过主应用配置 props.autoSetLoading false 可以关闭
|
|
81
|
+
callback: () => {
|
|
82
|
+
if (
|
|
83
|
+
props?.autoSetLoading &&
|
|
84
|
+
typeof props?.setLoading === 'function'
|
|
85
|
+
) {
|
|
86
|
+
props.setLoading(false);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// // 支持将子应用的 history 回传给父应用
|
|
90
|
+
// if (typeof props?.onHistoryInit === 'function') {
|
|
91
|
+
// props.onHistoryInit(history);
|
|
92
|
+
// }
|
|
93
|
+
},
|
|
94
|
+
// 支持通过 props 注入 container 来限定子应用 mountElementId 的查找范围
|
|
95
|
+
// 避免多个子应用出现在同一主应用时出现 mount 冲突
|
|
96
|
+
rootElement:
|
|
97
|
+
props?.container?.querySelector(`#${mountElementId}`) ||
|
|
98
|
+
mountElementId,
|
|
99
|
+
|
|
100
|
+
// 当存在同一个 umi 子应用在同一个页面被多实例渲染的场景时(比如一个页面里,同时展示了这个子应用的多个路由页面)
|
|
101
|
+
// mount 钩子会被调用多次,但是具体什么时候对应的实例开始 render 则是不定的,即它调用 applyPlugins('modifyClientRenderOpts') 的时机是不确定的
|
|
102
|
+
// 为了保证每次 applyPlugins('modifyClientRenderOpts') 调用是生成正确的 history,我们需要这里通过闭包上下文维持 mount 调用时的一些配置信息
|
|
103
|
+
// FIXME 由于 umi history 是全局的,通过 import { history } from 'umi' 调用的永远都是最后一个调用 createHistory 产生的对象,所以这种场景下会存在子应用内部获取 history 时,获取到的是同一个 history 的问题。这种场景下就不能直接从 umi import history,而应该从组件的 props 中取
|
|
104
|
+
// getHistory() {
|
|
105
|
+
// // 动态改变 history
|
|
106
|
+
// const historyOptions = normalizeHistory(props.history, props.base);
|
|
107
|
+
// setCreateHistoryOptions(historyOptions);
|
|
108
|
+
//
|
|
109
|
+
// // FIXME 子应用嵌入模式下不支持热更
|
|
110
|
+
// return createHistory();
|
|
111
|
+
// },
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
clientRenderOptsStack.push(clientRenderOpts);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 第一次 mount defer 被 resolve 后umi 会自动触发 render,非第一次 mount 则需手动触发
|
|
118
|
+
if (hasMountedAtLeastOnce) {
|
|
119
|
+
render();
|
|
120
|
+
} else {
|
|
121
|
+
defer.resolve();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
hasMountedAtLeastOnce = true;
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export function genUpdate() {
|
|
129
|
+
return async (props: any) => {
|
|
130
|
+
setModelState(props);
|
|
131
|
+
const slaveRuntime = await getSlaveRuntime();
|
|
132
|
+
if (slaveRuntime.update) {
|
|
133
|
+
await slaveRuntime.update(props);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function genUnmount(mountElementId: string) {
|
|
139
|
+
return async (props: any) => {
|
|
140
|
+
const container = props?.container
|
|
141
|
+
? props.container.querySelector(`#${mountElementId}`)
|
|
142
|
+
: document.getElementById(mountElementId);
|
|
143
|
+
if (container) {
|
|
144
|
+
ReactDOM.unmountComponentAtNode(container);
|
|
145
|
+
}
|
|
146
|
+
const slaveRuntime = await getSlaveRuntime();
|
|
147
|
+
if (slaveRuntime.unmount) await slaveRuntime.unmount(props);
|
|
148
|
+
};
|
|
149
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import { useState } from 'react';
|
|
4
|
+
|
|
5
|
+
let initState: any;
|
|
6
|
+
let setModelState = (val: any) => {
|
|
7
|
+
initState = val;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export default () => {
|
|
11
|
+
const [state, setState] = useState(initState);
|
|
12
|
+
setModelState = (val: any) => {
|
|
13
|
+
initState = val;
|
|
14
|
+
setState(val);
|
|
15
|
+
};
|
|
16
|
+
return state;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { setModelState };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
import qiankunRender from './lifecycles';
|
|
4
|
+
|
|
5
|
+
export function render(oldRender: any) {
|
|
6
|
+
return qiankunRender().then(oldRender);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// export function modifyClientRenderOpts(memo: any) {
|
|
10
|
+
// // 每次应用 render 的时候会调 modifyClientRenderOpts,这时尝试从队列中取 render 的配置
|
|
11
|
+
// const clientRenderOpts = clientRenderOptsStack.shift();
|
|
12
|
+
// if (clientRenderOpts) {
|
|
13
|
+
// const history = clientRenderOpts.getHistory();
|
|
14
|
+
// delete clientRenderOpts.getHistory;
|
|
15
|
+
// clientRenderOpts.history = history;
|
|
16
|
+
// }
|
|
17
|
+
// return {
|
|
18
|
+
// ...memo,
|
|
19
|
+
// ...clientRenderOpts,
|
|
20
|
+
// };
|
|
21
|
+
// }
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umijs/plugins",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.17",
|
|
4
4
|
"description": "@umijs/plugins",
|
|
5
5
|
"homepage": "https://github.com/umijs/umi-next/tree/master/packages/plugins#readme",
|
|
6
6
|
"bugs": "https://github.com/umijs/umi-next/issues",
|
|
@@ -21,20 +21,28 @@
|
|
|
21
21
|
"dev": "pnpm build -- --watch"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
+
"@ahooksjs/use-request": "^2.0.0",
|
|
24
25
|
"@ant-design/icons": "^4.7.0",
|
|
25
26
|
"@ant-design/pro-layout": "^6.31.7",
|
|
26
|
-
"@umijs/bundler-utils": "4.0.0-beta.
|
|
27
|
+
"@umijs/bundler-utils": "4.0.0-beta.17",
|
|
27
28
|
"antd": "^4.17.3",
|
|
28
29
|
"antd-dayjs-webpack-plugin": "^1.0.6",
|
|
30
|
+
"axios": "^0.24.0",
|
|
29
31
|
"babel-plugin-import": "^1.13.3",
|
|
30
32
|
"dayjs": "^1.10.7",
|
|
31
33
|
"dva-core": "^2.0.4",
|
|
34
|
+
"event-emitter": "~0.3.5",
|
|
32
35
|
"fast-deep-equal": "3.1.3",
|
|
36
|
+
"lodash": "^4.17.21",
|
|
37
|
+
"moment": "^2.29.1",
|
|
38
|
+
"qiankun": "^2.6.3",
|
|
39
|
+
"react-intl": "3.12.1",
|
|
33
40
|
"react-redux": "^7.2.6",
|
|
34
|
-
"redux": "^4.1.2"
|
|
41
|
+
"redux": "^4.1.2",
|
|
42
|
+
"warning": "^4.0.3"
|
|
35
43
|
},
|
|
36
44
|
"devDependencies": {
|
|
37
|
-
"umi": "4.0.0-beta.
|
|
45
|
+
"umi": "4.0.0-beta.17"
|
|
38
46
|
},
|
|
39
47
|
"publishConfig": {
|
|
40
48
|
"access": "public"
|