@umijs/plugins 4.0.0-rc.9 → 4.0.2

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/libs/model.tsx CHANGED
@@ -1,14 +1,25 @@
1
1
  // @ts-ignore
2
+ import type { models as rawModels } from '@@/plugin-model/model';
2
3
  import isEqual from 'fast-deep-equal';
3
4
  import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
4
5
 
6
+ type Models = typeof rawModels;
7
+
8
+ type GetNamespaces<M> = {
9
+ [K in keyof M]: M[K] extends { namespace: string }
10
+ ? M[K]['namespace']
11
+ : never;
12
+ }[keyof M];
13
+
14
+ type Namespaces = GetNamespaces<Models>;
15
+
5
16
  // @ts-ignore
6
17
  const Context = React.createContext<{ dispatcher: Dispatcher }>(null);
7
18
 
8
19
  class Dispatcher {
9
- callbacks: Record<string, Set<Function>> = {};
10
- data: Record<string, unknown> = {};
11
- update = (namespace: string) => {
20
+ callbacks: Record<Namespaces, Set<Function>> = {};
21
+ data: Record<Namespaces, unknown> = {};
22
+ update = (namespace: Namespaces) => {
12
23
  if (this.callbacks[namespace]) {
13
24
  this.callbacks[namespace].forEach((cb) => {
14
25
  try {
@@ -87,7 +98,34 @@ export function Provider(props: {
87
98
  );
88
99
  }
89
100
 
90
- export function useModel(namespace: string, selector?: any) {
101
+ type GetModelByNamespace<M, N> = {
102
+ [K in keyof M]: M[K] extends { namespace: string; model: unknown }
103
+ ? M[K]['namespace'] extends N
104
+ ? M[K]['model'] extends (...args: any) => any
105
+ ? ReturnType<M[K]['model']>
106
+ : never
107
+ : never
108
+ : never;
109
+ }[keyof M];
110
+
111
+ type Model<N> = GetModelByNamespace<Models, N>;
112
+ type Selector<N, S> = (model: Model<N>) => S;
113
+
114
+ type SelectedModel<N, T> = T extends (...args: any) => any
115
+ ? ReturnType<NonNullable<T>>
116
+ : Model<N>;
117
+
118
+ export function useModel<N extends Namespaces>(namespace: N): Model<N>;
119
+
120
+ export function useModel<N extends Namespaces, S>(
121
+ namespace: N,
122
+ selector: Selector<N, S>,
123
+ ): SelectedModel<N, typeof selector>;
124
+
125
+ export function useModel<N extends Namespaces, S>(
126
+ namespace: N,
127
+ selector?: Selector<N, S>,
128
+ ): SelectedModel<N, typeof selector> {
91
129
  const { dispatcher } = useContext<{ dispatcher: Dispatcher }>(Context);
92
130
  const selectorRef = useRef(selector);
93
131
  selectorRef.current = selector;
@@ -127,7 +165,7 @@ export function useModel(namespace: string, selector?: any) {
127
165
  }
128
166
  };
129
167
 
130
- dispatcher.callbacks[namespace] ||= new Set();
168
+ dispatcher.callbacks[namespace] ||= new Set() as any; // rawModels 是 umi 动态生成的文件,导致前面 callback[namespace] 的类型无法推导出来,所以用 as any 来忽略掉
131
169
  dispatcher.callbacks[namespace].add(handler);
132
170
  dispatcher.update(namespace);
133
171
 
@@ -74,11 +74,11 @@ export const MicroApp = forwardRef(
74
74
  apps = [],
75
75
  lifeCycles: globalLifeCycles,
76
76
  prefetch = true,
77
+ appNameKeyAlias = 'name',
77
78
  ...globalSettings
78
79
  } = getMasterOptions() as MasterOptions;
79
80
 
80
81
  const {
81
- name,
82
82
  settings: settingsFromProps = {},
83
83
  loader,
84
84
  errorBoundary,
@@ -88,6 +88,10 @@ export const MicroApp = forwardRef(
88
88
  ...propsFromParams
89
89
  } = componentProps;
90
90
 
91
+ // 优先使用 alias 名匹配,fallback 到 name 匹配
92
+ const name = componentProps[appNameKeyAlias] || componentProps.name;
93
+ const isCurrentApp = (app: any) => app[appNameKeyAlias] === name || app.name === name;
94
+
91
95
  const [loading, setLoading] = useState(true);
92
96
  const [error, setError] = useState<any>(null);
93
97
  // 未配置自定义 errorBoundary 且开启了 autoCaptureError 场景下,使用插件默认的 errorBoundary,否则使用自定义 errorBoundary
@@ -117,7 +121,7 @@ export const MicroApp = forwardRef(
117
121
 
118
122
  useImperativeHandle(componentRef, () => microAppRef.current);
119
123
 
120
- const appConfig = apps.find((app: any) => app.name === name);
124
+ const appConfig = apps.find((app: any) => isCurrentApp(app));
121
125
  useEffect(() => {
122
126
  if (!appConfig) {
123
127
  setComponentError(
@@ -139,6 +143,13 @@ export const MicroApp = forwardRef(
139
143
  setComponentError(null);
140
144
  setLoading(true);
141
145
  const configuration = {
146
+ fetch(url) {
147
+ return window.fetch(url, {
148
+ headers: {
149
+ accept: 'text/html',
150
+ },
151
+ });
152
+ },
142
153
  globalContext: window,
143
154
  ...globalSettings,
144
155
  ...settingsFromProps,
@@ -167,13 +178,11 @@ export const MicroApp = forwardRef(
167
178
  if (noneMounted) {
168
179
  if (Array.isArray(prefetch)) {
169
180
  const specialPrefetchApps = apps.filter(
170
- (app) => app.name !== name && prefetch.indexOf(app.name) !== -1,
181
+ (app) => !isCurrentApp(app) && (prefetch.indexOf(app[appNameKeyAlias]) !== -1 || prefetch.indexOf(app.name) !== -1)
171
182
  );
172
183
  prefetchApps(specialPrefetchApps, configuration);
173
184
  } else {
174
- const otherNotMountedApps = apps.filter(
175
- (app) => app.name !== name,
176
- );
185
+ const otherNotMountedApps = apps.filter((app) => !isCurrentApp(app));
177
186
  prefetchApps(otherNotMountedApps, configuration);
178
187
  }
179
188
  noneMounted = false;
@@ -5,7 +5,7 @@
5
5
  * @since 2019-06-20
6
6
  */
7
7
 
8
- import { ReactComponentElement } from 'react';
8
+ import React, { ReactComponentElement } from 'react';
9
9
  import type { IRouteProps } from 'umi';
10
10
 
11
11
  export const defaultMountContainerId = 'root-subapp';
@@ -48,6 +48,7 @@ export function patchMicroAppRoute(
48
48
  getMicroAppRouteComponent: (opts: {
49
49
  appName: string;
50
50
  base: string;
51
+ routePath: string;
51
52
  masterHistoryType: string;
52
53
  routeProps?: any;
53
54
  }) => string | ReactComponentElement<any>,
@@ -63,9 +64,9 @@ export function patchMicroAppRoute(
63
64
  const microAppProps =
64
65
  route[`${routeBindingAlias}Props`] || route.microAppProps || {};
65
66
  if (microAppName) {
66
- if (route.routes?.length) {
67
- const childrenRouteHasComponent = route.routes.some(
68
- (r: any) => r.component,
67
+ if (route.children?.length) {
68
+ const childrenRouteHasComponent = route.children.some(
69
+ (r: any) => r.element,
69
70
  );
70
71
  if (childrenRouteHasComponent) {
71
72
  throw new Error(
@@ -74,7 +75,10 @@ export function patchMicroAppRoute(
74
75
  }
75
76
  }
76
77
 
77
- route.exact = false;
78
+ // 自动追加通配符,匹配子应用的路由
79
+ if (!route.path.endsWith('/*')) {
80
+ route.path = route.path.replace(/\/?$/, '/*');
81
+ }
78
82
 
79
83
  const { settings = {}, ...componentProps } = microAppProps;
80
84
  const routeProps = {
@@ -85,10 +89,11 @@ export function patchMicroAppRoute(
85
89
  const opts = {
86
90
  appName: microAppName,
87
91
  base,
92
+ routePath: route.path,
88
93
  masterHistoryType,
89
94
  routeProps,
90
95
  };
91
- route.component = getMicroAppRouteComponent(opts);
96
+ route.element = React.createElement(getMicroAppRouteComponent(opts), null);
92
97
  }
93
98
  }
94
99
 
@@ -100,8 +105,8 @@ const recursiveSearch = (
100
105
  if (routes[i].path === path) {
101
106
  return routes[i];
102
107
  }
103
- if (routes[i].routes && routes[i].routes?.length) {
104
- const found = recursiveSearch(routes[i].routes || [], path);
108
+ if (routes[i].children && routes[i].children?.length) {
109
+ const found = recursiveSearch(routes[i].children || [], path);
105
110
  if (found) {
106
111
  return found;
107
112
  }
@@ -123,8 +128,8 @@ export function insertRoute(routes: IRouteProps[], microAppRoute: IRouteProps) {
123
128
  );
124
129
  }
125
130
  found.exact = false;
126
- found.routes = found.routes || [];
127
- found.routes.push(microAppRoute);
131
+ found.children = found.children || [];
132
+ found.children.push(microAppRoute);
128
133
  } else {
129
134
  throw new Error(
130
135
  `[plugin-qiankun]: path "${microAppRoute.insert}" not found`,
@@ -1,31 +1,20 @@
1
1
  import React from 'react';
2
2
  import { MicroApp } from './MicroApp';
3
- {{#runtimeHistory}}
4
- import { getCreateHistoryOptions } from 'umi';
5
- {{/runtimeHistory}}
6
- import { useLocation } from 'umi';
7
3
 
8
4
  export function getMicroAppRouteComponent(opts: {
9
5
  appName: string;
10
6
  base: string;
7
+ routePath: string;
11
8
  masterHistoryType: string;
12
9
  routeProps?: any;
13
10
  }) {
14
- const { base, masterHistoryType, appName, routeProps } = opts;
15
- const RouteComponent = ({ match }: any) => {
16
- const url = useLocation().pathname;
17
-
11
+ const { base, masterHistoryType, appName, routeProps, routePath } = opts;
12
+ const RouteComponent = () => {
18
13
  // 默认取静态配置的 base
19
14
  let umiConfigBase = base === '/' ? '' : base;
20
15
 
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);
16
+ // 拼接子应用挂载路由
17
+ let runtimeMatchedBase = umiConfigBase + routePath.replace('/*', '');
29
18
 
30
19
  {{#dynamicRoot}}
31
20
  // @see https://github.com/umijs/umi/blob/master/packages/preset-built-in/src/plugins/commands/htmlUtils.ts#L102
@@ -34,10 +34,10 @@ function patchMicroAppRouteComponent(routes: any[]) {
34
34
  const rootRoute = routes.find((route) => route.path === '/');
35
35
  if (rootRoute) {
36
36
  // 如果根路由是叶子节点,则直接返回其父节点
37
- if (!rootRoute.routes) {
37
+ if (!rootRoute.children?.length) {
38
38
  return routes;
39
39
  }
40
- return getRootRoutes(rootRoute.routes);
40
+ return getRootRoutes(rootRoute.children);
41
41
  }
42
42
  return routes;
43
43
  };
@@ -50,11 +50,12 @@ function patchMicroAppRouteComponent(routes: any[]) {
50
50
  const patchRoute = (route: any) => {
51
51
  patchMicroAppRoute(route, getMicroAppRouteComponent, {
52
52
  base,
53
+ routePath: route.path,
53
54
  masterHistoryType,
54
55
  routeBindingAlias,
55
56
  });
56
- if (route.routes?.length) {
57
- route.routes.forEach(patchRoute);
57
+ if (route.children?.length) {
58
+ route.children.forEach(patchRoute);
58
59
  }
59
60
  };
60
61
 
@@ -123,7 +124,7 @@ export async function render(oldRender: typeof noop) {
123
124
  }
124
125
  }
125
126
 
126
- export function patchRoutes({ routes }: { routes: any[] }) {
127
+ export function patchClientRoutes({ routes }: { routes: any[] }) {
127
128
  if (microAppRuntimeRoutes) {
128
129
  patchMicroAppRouteComponent(routes);
129
130
  }
@@ -31,6 +31,8 @@ export type MasterOptions = {
31
31
  routeBindingAlias?: string;
32
32
  // 导出的组件别名,默认 MicroApp
33
33
  exportComponentAlias?: string;
34
+ // MicroApp 寻址时使用的应用名唯一键,默认是 name
35
+ appNameKeyAlias?: string;
34
36
  } & FrameworkConfiguration;
35
37
 
36
38
  export type SlaveOptions = {
@@ -1,5 +1,4 @@
1
1
  // @ts-nocheck
2
- /* eslint-disable */
3
2
  __USE_MODEL__;
4
3
  import React from 'react';
5
4
 
@@ -1,5 +1,4 @@
1
1
  // @ts-nocheck
2
- /* eslint-disable */
3
2
  import { getPluginManager } from '@@/core/plugin';
4
3
  import ReactDOM from 'react-dom';
5
4
  import { ApplyPluginsType } from 'umi';
@@ -22,7 +21,7 @@ let render = noop;
22
21
  let hasMountedAtLeastOnce = false;
23
22
 
24
23
  export default () => defer.promise;
25
- export const clientRenderOptsStack: any[] = [];
24
+ export const contextOptsStack: any[] = [];
26
25
 
27
26
  // function normalizeHistory(
28
27
  // history?: 'string' | Record<string, any>,
@@ -79,10 +78,7 @@ export function genMount(mountElementId: string) {
79
78
  // 默认开启
80
79
  // 如果需要手动控制 loading,通过主应用配置 props.autoSetLoading false 可以关闭
81
80
  callback: () => {
82
- if (
83
- props?.autoSetLoading &&
84
- typeof props?.setLoading === 'function'
85
- ) {
81
+ if (props.autoSetLoading && typeof props.setLoading === 'function') {
86
82
  props.setLoading(false);
87
83
  }
88
84
 
@@ -94,9 +90,11 @@ export function genMount(mountElementId: string) {
94
90
  // 支持通过 props 注入 container 来限定子应用 mountElementId 的查找范围
95
91
  // 避免多个子应用出现在同一主应用时出现 mount 冲突
96
92
  rootElement:
97
- props?.container?.querySelector(`#${mountElementId}`) ||
93
+ props.container?.querySelector(`#${mountElementId}`) ||
98
94
  mountElementId,
99
95
 
96
+ basename: props.base,
97
+
100
98
  // 当存在同一个 umi 子应用在同一个页面被多实例渲染的场景时(比如一个页面里,同时展示了这个子应用的多个路由页面)
101
99
  // mount 钩子会被调用多次,但是具体什么时候对应的实例开始 render 则是不定的,即它调用 applyPlugins('modifyClientRenderOpts') 的时机是不确定的
102
100
  // 为了保证每次 applyPlugins('modifyClientRenderOpts') 调用是生成正确的 history,我们需要这里通过闭包上下文维持 mount 调用时的一些配置信息
@@ -111,7 +109,7 @@ export function genMount(mountElementId: string) {
111
109
  // },
112
110
  };
113
111
 
114
- clientRenderOptsStack.push(clientRenderOpts);
112
+ contextOptsStack.push(clientRenderOpts);
115
113
  }
116
114
 
117
115
  // 第一次 mount defer 被 resolve 后umi 会自动触发 render,非第一次 mount 则需手动触发
@@ -121,6 +119,14 @@ export function genMount(mountElementId: string) {
121
119
  defer.resolve();
122
120
  }
123
121
 
122
+ // 如果需要手动控制 loading,通过主应用配置 props.autoSetLoading false 可以关闭
123
+ // 考虑到 react 18 之后 callback 不再准
124
+ // 所以在这里直接返回,而不使用 ReactDOM.render 的第三个参数
125
+ if (typeof props !== 'undefined') {
126
+ if (props.autoSetLoading && typeof props.setLoading === 'function') {
127
+ props.setLoading(false);
128
+ }
129
+ }
124
130
  hasMountedAtLeastOnce = true;
125
131
  };
126
132
  }
@@ -1,5 +1,4 @@
1
1
  // @ts-nocheck
2
- /* eslint-disable */
3
2
  import { useState } from 'react';
4
3
 
5
4
  let initState: any;
@@ -1,21 +1,15 @@
1
1
  // @ts-nocheck
2
- /* eslint-disable */
3
- import qiankunRender from './lifecycles';
2
+ import qiankunRender, { contextOptsStack } from './lifecycles';
4
3
 
5
4
  export function render(oldRender: any) {
6
5
  return qiankunRender().then(oldRender);
7
6
  }
8
7
 
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
- // }
8
+ export function modifyContextOpts(memo: any) {
9
+ // 每次应用 render 的时候会调 modifyClientRenderOpts,这时尝试从队列中取 render 的配置
10
+ const clientRenderOpts = contextOptsStack.shift();
11
+ return {
12
+ ...memo,
13
+ ...clientRenderOpts,
14
+ };
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.0-rc.9",
3
+ "version": "4.0.2",
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",
@@ -17,34 +17,34 @@
17
17
  ],
18
18
  "scripts": {
19
19
  "build": "pnpm tsc",
20
- "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
21
- "dev": "pnpm build -- --watch",
22
- "test": "jest -c ../../jest.turbo.config.ts"
20
+ "build:deps": "umi-scripts bundleDeps",
21
+ "dev": "pnpm build --watch",
22
+ "test": "umi-scripts jest-turbo"
23
23
  },
24
24
  "dependencies": {
25
25
  "@ahooksjs/use-request": "^2.0.0",
26
26
  "@ant-design/icons": "^4.7.0",
27
- "@ant-design/pro-layout": "^6.34.6",
28
- "@umijs/bundler-utils": "4.0.0-rc.9",
27
+ "@ant-design/pro-layout": "^7.0.1-beta.20",
28
+ "@umijs/bundler-utils": "4.0.2",
29
29
  "antd-dayjs-webpack-plugin": "^1.0.6",
30
- "axios": "^0.26.1",
31
- "babel-plugin-import": "^1.13.3",
32
- "dayjs": "^1.11.0",
30
+ "axios": "^0.27.2",
31
+ "babel-plugin-import": "^1.13.5",
32
+ "dayjs": "^1.11.2",
33
33
  "dva-core": "^2.0.4",
34
34
  "dva-immer": "^1.0.0",
35
35
  "dva-loading": "^3.0.22",
36
36
  "event-emitter": "~0.3.5",
37
37
  "fast-deep-equal": "3.1.3",
38
38
  "lodash": "^4.17.21",
39
- "moment": "^2.29.1",
40
- "qiankun": "^2.6.3",
39
+ "moment": "^2.29.3",
40
+ "qiankun": "^2.7.0",
41
41
  "react-intl": "3.12.1",
42
- "react-redux": "^7.2.6",
43
- "redux": "^4.1.2",
42
+ "react-redux": "^8.0.2",
43
+ "redux": "^4.2.0",
44
44
  "warning": "^4.0.3"
45
45
  },
46
46
  "devDependencies": {
47
- "umi": "4.0.0-rc.9"
47
+ "umi": "4.0.2"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"