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

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/access.js CHANGED
@@ -54,7 +54,7 @@ export function accessProvider(container) {
54
54
  api.writeTmpFile({
55
55
  path: 'index.tsx',
56
56
  content: `
57
- import React from 'react';
57
+ import React, { PropsWithChildren } from 'react';
58
58
  import { AccessContext } from './context';
59
59
  import type { IRoute } from 'umi';
60
60
 
@@ -66,7 +66,7 @@ export interface AccessProps {
66
66
  accessible: boolean;
67
67
  fallback?: React.ReactNode;
68
68
  }
69
- export const Access: React.FC<AccessProps> = (props) => {
69
+ export const Access: React.FC<PropsWithChildren<AccessProps>> = (props) => {
70
70
  if (process.env.NODE_ENV === 'development' && typeof props.accessible !== 'boolean') {
71
71
  throw new Error('[access] the \`accessible\` property on <Access /> should be a boolean');
72
72
  }
@@ -85,7 +85,7 @@ export const useAccessMarkedRoutes = (routes: IRoute[]) => {
85
85
 
86
86
  // check access code
87
87
  if (typeof accessCode === 'string') {
88
- const detector = access[route.access];
88
+ const detector = access[accessCode];
89
89
 
90
90
  if (typeof detector === 'function') {
91
91
  route.unaccessible = !detector(route);
@@ -97,8 +97,8 @@ export const useAccessMarkedRoutes = (routes: IRoute[]) => {
97
97
  }
98
98
 
99
99
  // check children access code
100
- if (route.routes) {
101
- const isNoAccessibleChild = !route.routes.reduce((hasAccessibleChild, child) => {
100
+ if (route.children?.length) {
101
+ const isNoAccessibleChild = !route.children.reduce((hasAccessibleChild, child) => {
102
102
  process(child, accessCode);
103
103
 
104
104
  return hasAccessibleChild || !child.unaccessible;
package/dist/dva.js CHANGED
@@ -112,7 +112,10 @@ export function RootContainer(props: any) {
112
112
  ${((_d = (_c = api.config.dva) === null || _c === void 0 ? void 0 : _c.immer) === null || _d === void 0 ? void 0 : _d.enableES5) ? `enableES5();` : ''}
113
113
  ${((_f = (_e = api.config.dva) === null || _e === void 0 ? void 0 : _e.immer) === null || _f === void 0 ? void 0 : _f.enableAllPlugins) ? `enableAllPlugins();` : ''}
114
114
  for (const id of Object.keys(models)) {
115
- app.current.model(models[id].model);
115
+ app.current.model({
116
+ namespace: models[id].namespace,
117
+ ...models[id].model,
118
+ });
116
119
  }
117
120
  app.current.start();
118
121
  }
@@ -14,7 +14,12 @@ exports.default = (api) => {
14
14
  });
15
15
  api.register({
16
16
  key: 'addExtraModels',
17
- fn: () => [(0, withTmpPath_1.withTmpPath)({ api, path: '@@initialState.ts' })],
17
+ fn: () => [
18
+ (0, withTmpPath_1.withTmpPath)({
19
+ api,
20
+ path: '@@initialState.ts#{"namespace":"@@initialState"}',
21
+ }),
22
+ ],
18
23
  });
19
24
  api.addRuntimePluginKey(() => ['getInitialState']);
20
25
  api.addRuntimePlugin(() => {
@@ -55,8 +60,10 @@ export default function InitialStateProvider(props: any) {
55
60
  import { useState, useEffect, useCallback } from 'react';
56
61
  import { getInitialState } from '@/app';
57
62
 
63
+ export type InitialStateType = Awaited<ReturnType<typeof getInitialState>> | undefined;
64
+
58
65
  const initState = {
59
- initialState: undefined,
66
+ initialState: undefined as InitialStateType,
60
67
  loading: true,
61
68
  error: undefined,
62
69
  };
@@ -71,8 +78,6 @@ export default () => {
71
78
  } catch (e) {
72
79
  setState((s) => ({ ...s, error: e, loading: false }));
73
80
  }
74
- // [?]
75
- // await sleep(10);
76
81
  }, []);
77
82
 
78
83
  const setInitialState = useCallback(async (initialState) => {
@@ -82,8 +87,6 @@ export default () => {
82
87
  }
83
88
  return { ...s, initialState, loading: false };
84
89
  });
85
- // [?]
86
- // await sleep(10)
87
90
  }, []);
88
91
 
89
92
  useEffect(() => {
package/dist/layout.js CHANGED
@@ -52,7 +52,7 @@ exports.default = (api) => {
52
52
  return false;
53
53
  });
54
54
  const getPkgPath = () => {
55
- // 如果 layout 和 techui至少有一个在,找到他们的地址
55
+ // 如果 layout 和 techui 至少有一个在,找到他们的地址
56
56
  if (pkgHasDep &&
57
57
  (0, fs_1.existsSync)((0, path_1.join)(api.cwd, 'node_modules', pkgHasDep, 'package.json'))) {
58
58
  return (0, path_1.join)(api.cwd, 'node_modules', pkgHasDep);
@@ -87,7 +87,7 @@ import { Link, useLocation, useNavigate, Outlet, useAppData, useRouteData, match
87
87
  import { useMemo } from 'react';
88
88
  import {
89
89
  ProLayout,
90
- } from "${pkgHasDep || '@ant-design/pro-layout'}";
90
+ } from "${pkgPath || '@ant-design/pro-layout'}";
91
91
  import './Layout.less';
92
92
  import Logo from './Logo';
93
93
  import Exception from './Exception';
@@ -202,6 +202,26 @@ const { formatMessage } = useIntl();
202
202
  );
203
203
  }
204
204
  `,
205
+ });
206
+ // 写入类型, RunTimeLayoutConfig 是 app.tsx 中 layout 配置的类型
207
+ // 对于动态 layout 配置很有用
208
+ api.writeTmpFile({
209
+ path: 'index.ts',
210
+ content: `
211
+ import type { ProLayoutProps } from "${pkgPath || '@ant-design/pro-layout'}";
212
+ ${hasInitialStatePlugin
213
+ ? `import { Models } from '@@/plugin-model/useModel';
214
+ type InitDataType = Models<'@@initialState'>;
215
+ `
216
+ : 'type InitDataType = any;'}
217
+
218
+ export type RunTimeLayoutConfig = (
219
+ initData: InitDataType,
220
+ ) => BasicLayoutProps & {
221
+ childrenRender?: (dom: JSX.Element, props: ProLayoutProps) => React.ReactNode,
222
+ unAccessible?: JSX.Element,
223
+ noFound?: JSX.Element,
224
+ };`,
205
225
  });
206
226
  const iconsMap = Object.keys(api.appData.routes).reduce((memo, id) => {
207
227
  const { icon } = api.appData.routes[id];
package/dist/request.js CHANGED
@@ -272,6 +272,8 @@ export type {
272
272
  AxiosInstance,
273
273
  AxiosRequestConfig,
274
274
  AxiosResponse,
275
+ IRequestOptions as RequestOptions,
276
+ IRequest as Request,
275
277
  };
276
278
 
277
279
  `;
@@ -7,13 +7,14 @@ interface IOpts {
7
7
  content: string;
8
8
  }) => Boolean;
9
9
  }
10
+ export declare function getNamespace(absFilePath: string, absSrcPath: string): string;
10
11
  export declare class Model {
11
12
  file: string;
12
13
  namespace: string;
13
14
  id: string;
14
15
  exportName: string;
15
16
  deps: string[];
16
- constructor(file: string, sort: {} | undefined, id: number);
17
+ constructor(file: string, absSrcPath: string, sort: {} | undefined, id: number);
17
18
  findDeps(sort: object): string[];
18
19
  }
19
20
  export declare class ModelUtils {
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.ModelUtils = exports.Model = void 0;
29
+ exports.ModelUtils = exports.Model = exports.getNamespace = void 0;
30
30
  const parser = __importStar(require("@umijs/bundler-utils/compiled/babel/parser"));
31
31
  const traverse_1 = __importDefault(require("@umijs/bundler-utils/compiled/babel/traverse"));
32
32
  const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
@@ -35,8 +35,24 @@ const fs_1 = require("fs");
35
35
  const path_1 = require("path");
36
36
  const plugin_utils_1 = require("umi/plugin-utils");
37
37
  const astUtils_1 = require("./astUtils");
38
+ function getNamespace(absFilePath, absSrcPath) {
39
+ const relPath = (0, plugin_utils_1.winPath)((0, path_1.relative)((0, plugin_utils_1.winPath)(absSrcPath), (0, plugin_utils_1.winPath)(absFilePath)));
40
+ const parts = relPath.split('/');
41
+ const dirs = parts.slice(0, -1);
42
+ const file = parts[parts.length - 1];
43
+ // src/pages/foo/models/bar > foo/bar
44
+ const validDirs = dirs.filter((dir) => !['src', 'pages', 'models'].includes(dir));
45
+ let normalizedFile = file;
46
+ normalizedFile = (0, path_1.basename)(file, (0, path_1.extname)(file));
47
+ // foo.model > foo
48
+ if (normalizedFile.endsWith('.model')) {
49
+ normalizedFile = normalizedFile.split('.').slice(0, -1).join('.');
50
+ }
51
+ return [...validDirs, normalizedFile].join('.');
52
+ }
53
+ exports.getNamespace = getNamespace;
38
54
  class Model {
39
- constructor(file, sort, id) {
55
+ constructor(file, absSrcPath, sort, id) {
40
56
  let namespace;
41
57
  let exportName;
42
58
  const [_file, meta] = file.split('#');
@@ -47,7 +63,7 @@ class Model {
47
63
  }
48
64
  this.file = _file;
49
65
  this.id = `model_${id}`;
50
- this.namespace = namespace || (0, path_1.basename)(file, (0, path_1.extname)(file));
66
+ this.namespace = namespace || getNamespace(_file, absSrcPath);
51
67
  this.exportName = exportName || 'default';
52
68
  this.deps = sort ? this.findDeps(sort) : [];
53
69
  }
@@ -107,7 +123,7 @@ class ModelUtils {
107
123
  }),
108
124
  ...opts.extraModels,
109
125
  ].map((file) => {
110
- return new Model(file, opts.sort, this.count++);
126
+ return new Model(file, this.api.paths.absSrcPath, opts.sort, this.count++);
111
127
  });
112
128
  // check duplicate
113
129
  const namespaces = models.map((model) => model.namespace);
@@ -212,10 +228,10 @@ class ModelUtils {
212
228
  const imports = [];
213
229
  const modelProps = [];
214
230
  models.forEach((model) => {
215
- const fileWithoutExt = (0, path_1.format)({
231
+ const fileWithoutExt = (0, plugin_utils_1.winPath)((0, path_1.format)({
216
232
  dir: (0, path_1.dirname)(model.file),
217
233
  base: (0, path_1.basename)(model.file, (0, path_1.extname)(model.file)),
218
- });
234
+ }));
219
235
  if (model.exportName !== 'default') {
220
236
  imports.push(`import { ${model.exportName} as ${model.id} } from '${fileWithoutExt}';`);
221
237
  }
@@ -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(
@@ -174,13 +178,11 @@ export const MicroApp = forwardRef(
174
178
  if (noneMounted) {
175
179
  if (Array.isArray(prefetch)) {
176
180
  const specialPrefetchApps = apps.filter(
177
- (app) => app.name !== name && prefetch.indexOf(app.name) !== -1,
181
+ (app) => !isCurrentApp(app) && (prefetch.indexOf(app[appNameKeyAlias]) !== -1 || prefetch.indexOf(app.name) !== -1)
178
182
  );
179
183
  prefetchApps(specialPrefetchApps, configuration);
180
184
  } else {
181
- const otherNotMountedApps = apps.filter(
182
- (app) => app.name !== name,
183
- );
185
+ const otherNotMountedApps = apps.filter((app) => !isCurrentApp(app));
184
186
  prefetchApps(otherNotMountedApps, configuration);
185
187
  }
186
188
  noneMounted = false;
@@ -34,7 +34,7 @@ function patchMicroAppRouteComponent(routes: any[]) {
34
34
  const rootRoute = routes.find((route) => route.path === '/');
35
35
  if (rootRoute) {
36
36
  // 如果根路由是叶子节点,则直接返回其父节点
37
- if (!rootRoute.children) {
37
+ if (!rootRoute.children?.length) {
38
38
  return routes;
39
39
  }
40
40
  return getRootRoutes(rootRoute.children);
@@ -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 = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/plugins",
3
- "version": "4.0.0-rc.21",
3
+ "version": "4.0.0-rc.24",
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",
@@ -25,7 +25,7 @@
25
25
  "@ahooksjs/use-request": "^2.0.0",
26
26
  "@ant-design/icons": "^4.7.0",
27
27
  "@ant-design/pro-layout": "^7.0.1-beta.17",
28
- "@umijs/bundler-utils": "4.0.0-rc.21",
28
+ "@umijs/bundler-utils": "4.0.0-rc.24",
29
29
  "antd-dayjs-webpack-plugin": "^1.0.6",
30
30
  "axios": "^0.27.2",
31
31
  "babel-plugin-import": "^1.13.5",
@@ -44,7 +44,7 @@
44
44
  "warning": "^4.0.3"
45
45
  },
46
46
  "devDependencies": {
47
- "umi": "4.0.0-rc.21"
47
+ "umi": "4.0.0-rc.24"
48
48
  },
49
49
  "publishConfig": {
50
50
  "access": "public"