@umijs/plugins 4.0.0-rc.19 → 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/layout.js CHANGED
@@ -24,9 +24,9 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const allIcons = __importStar(require("@ant-design/icons"));
27
+ const fs_1 = require("fs");
27
28
  const path_1 = require("path");
28
29
  const plugin_utils_1 = require("umi/plugin-utils");
29
- const resolveProjectDep_1 = require("./utils/resolveProjectDep");
30
30
  const withTmpPath_1 = require("./utils/withTmpPath");
31
31
  exports.default = (api) => {
32
32
  api.describe({
@@ -39,11 +39,28 @@ exports.default = (api) => {
39
39
  },
40
40
  enableBy: api.EnableBy.config,
41
41
  });
42
- const pkgPath = (0, resolveProjectDep_1.resolveProjectDep)({
43
- pkg: api.pkg,
44
- cwd: api.cwd,
45
- dep: '@ant-design/pro-layout',
46
- }) || (0, path_1.dirname)(require.resolve('@ant-design/pro-layout/package.json'));
42
+ /**
43
+ * 优先去找 '@alipay/tech-ui',保证稳定性
44
+ */
45
+ const depList = ['@alipay/tech-ui', '@ant-design/pro-layout'];
46
+ const pkgHasDep = depList.find((dep) => {
47
+ var _a, _b;
48
+ const { pkg } = api;
49
+ if (((_a = pkg.dependencies) === null || _a === void 0 ? void 0 : _a[dep]) || ((_b = pkg.devDependencies) === null || _b === void 0 ? void 0 : _b[dep])) {
50
+ return true;
51
+ }
52
+ return false;
53
+ });
54
+ const getPkgPath = () => {
55
+ // 如果 layout 和 techui至少有一个在,找到他们的地址
56
+ if (pkgHasDep &&
57
+ (0, fs_1.existsSync)((0, path_1.join)(api.cwd, 'node_modules', pkgHasDep, 'package.json'))) {
58
+ return (0, path_1.join)(api.cwd, 'node_modules', pkgHasDep);
59
+ }
60
+ // 如果项目中没有去找插件以来的
61
+ return (0, path_1.dirname)(require.resolve('@ant-design/pro-layout/package.json'));
62
+ };
63
+ const pkgPath = getPkgPath();
47
64
  api.modifyAppData((memo) => {
48
65
  const version = require(`${pkgPath}/package.json`).version;
49
66
  memo.pluginLayout = {
@@ -53,8 +70,11 @@ exports.default = (api) => {
53
70
  return memo;
54
71
  });
55
72
  api.modifyConfig((memo) => {
56
- // import from @ant-design/pro-layout
57
- memo.alias['@ant-design/pro-layout'] = pkgPath;
73
+ // 只在没有自行依赖 @ant-design/pro-layout 或 @alipay/tech-ui 时
74
+ // 才使用插件中提供的 @ant-design/pro-layout
75
+ if (!pkgHasDep) {
76
+ memo.alias['@ant-design/pro-layout'] = pkgPath;
77
+ }
58
78
  return memo;
59
79
  });
60
80
  api.onGenerateFiles(() => {
@@ -65,9 +85,9 @@ exports.default = (api) => {
65
85
  content: `
66
86
  import { Link, useLocation, useNavigate, Outlet, useAppData, useRouteData, matchRoutes } from 'umi';
67
87
  import { useMemo } from 'react';
68
- import ProLayout, {
69
- PageLoading,
70
- } from '@ant-design/pro-layout';
88
+ import {
89
+ ProLayout,
90
+ } from "${pkgHasDep || '@ant-design/pro-layout'}";
71
91
  import './Layout.less';
72
92
  import Logo from './Logo';
73
93
  import Exception from './Exception';
@@ -34,16 +34,14 @@ exports.default = (api) => {
34
34
  },
35
35
  },
36
36
  }));
37
- // TODO: modify routes
38
37
  api.modifyRoutes((memo) => {
39
38
  Object.keys(memo).forEach((id) => {
39
+ var _a;
40
40
  const route = memo[id];
41
41
  if (route.microApp) {
42
42
  const appName = route.microApp;
43
- // TODO: config base
44
- const base = '/';
45
- // TODO: config masterHistoryType
46
- const masterHistoryType = 'browser';
43
+ const base = api.config.base || '/';
44
+ const masterHistoryType = ((_a = api.config.history) === null || _a === void 0 ? void 0 : _a.type) || 'browser';
47
45
  const routeProps = route.microAppProps || {};
48
46
  const normalizedRouteProps = JSON.stringify(routeProps).replace(/"/g, "'");
49
47
  route.file = `(async () => {
@@ -74,8 +74,8 @@ exports.default = (api) => {
74
74
  return config;
75
75
  });
76
76
  api.addHTMLHeadScripts(() => {
77
- var _a;
78
- const dontModify = (_a = api.config.qiankun) === null || _a === void 0 ? void 0 : _a.shouldNotModifyRuntimePublicPath;
77
+ var _a, _b;
78
+ const dontModify = (_b = (_a = api.config.qiankun) === null || _a === void 0 ? void 0 : _a.slave) === null || _b === void 0 ? void 0 : _b.shouldNotModifyRuntimePublicPath;
79
79
  return dontModify
80
80
  ? []
81
81
  : [
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.19",
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.19",
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.19"
47
+ "umi": "4.0.0-rc.21"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"