@umijs/plugins 4.0.0-rc.20 → 4.0.0-rc.21

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/analytics.js CHANGED
@@ -20,7 +20,7 @@ exports.default = (api) => {
20
20
  return `
21
21
  (function() {
22
22
  var hm = document.createElement('script');
23
- hm.src = 'https://hm.baidu.com/hm.js?${code}';
23
+ hm.src = '//hm.baidu.com/hm.js?${code}';
24
24
  var s = document.getElementsByTagName('script')[0];
25
25
  s.parentNode.insertBefore(hm, s);
26
26
  })();
package/dist/request.js CHANGED
@@ -250,7 +250,7 @@ const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
250
250
  });
251
251
  try {
252
252
  const handler =
253
- config.errorConfig?.errorHandler;
253
+ config?.errorConfig?.errorHandler;
254
254
  if(handler)
255
255
  handler(error, opts, config);
256
256
  } catch (e) {
@@ -212,11 +212,15 @@ class ModelUtils {
212
212
  const imports = [];
213
213
  const modelProps = [];
214
214
  models.forEach((model) => {
215
+ const fileWithoutExt = (0, path_1.format)({
216
+ dir: (0, path_1.dirname)(model.file),
217
+ base: (0, path_1.basename)(model.file, (0, path_1.extname)(model.file)),
218
+ });
215
219
  if (model.exportName !== 'default') {
216
- imports.push(`import { ${model.exportName} as ${model.id} } from '${model.file}';`);
220
+ imports.push(`import { ${model.exportName} as ${model.id} } from '${fileWithoutExt}';`);
217
221
  }
218
222
  else {
219
- imports.push(`import ${model.id} from '${model.file}';`);
223
+ imports.push(`import ${model.id} from '${fileWithoutExt}';`);
220
224
  }
221
225
  modelProps.push(`${model.id}: { namespace: '${model.namespace}', model: ${model.id} },`);
222
226
  });
@@ -225,7 +229,7 @@ ${imports.join('\n')}
225
229
 
226
230
  export const models = {
227
231
  ${modelProps.join('\n')}
228
- }`;
232
+ } as const`;
229
233
  }
230
234
  }
231
235
  exports.ModelUtils = ModelUtils;
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
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.0-rc.20",
3
+ "version": "4.0.0-rc.21",
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",
@@ -24,11 +24,11 @@
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.38.0",
28
- "@umijs/bundler-utils": "4.0.0-rc.20",
27
+ "@ant-design/pro-layout": "^7.0.1-beta.17",
28
+ "@umijs/bundler-utils": "4.0.0-rc.21",
29
29
  "antd-dayjs-webpack-plugin": "^1.0.6",
30
30
  "axios": "^0.27.2",
31
- "babel-plugin-import": "^1.13.3",
31
+ "babel-plugin-import": "^1.13.5",
32
32
  "dayjs": "^1.11.2",
33
33
  "dva-core": "^2.0.4",
34
34
  "dva-immer": "^1.0.0",
@@ -39,12 +39,12 @@
39
39
  "moment": "^2.29.3",
40
40
  "qiankun": "^2.7.0",
41
41
  "react-intl": "3.12.1",
42
- "react-redux": "^8.0.1",
42
+ "react-redux": "^8.0.2",
43
43
  "redux": "^4.2.0",
44
44
  "warning": "^4.0.3"
45
45
  },
46
46
  "devDependencies": {
47
- "umi": "4.0.0-rc.20"
47
+ "umi": "4.0.0-rc.21"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"