@umijs/plugins 4.0.6 → 4.0.9

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/layout.js CHANGED
@@ -1,120 +1,104 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
24
10
  };
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- const allIcons = __importStar(require("@ant-design/icons"));
27
- const fs_1 = require("fs");
28
- const path_1 = require("path");
29
- const plugin_utils_1 = require("umi/plugin-utils");
30
- const withTmpPath_1 = require("./utils/withTmpPath");
31
- exports.default = (api) => {
32
- api.describe({
33
- key: 'layout',
34
- config: {
35
- schema(joi) {
36
- return joi.object();
37
- },
38
- onChange: api.ConfigChangeType.regenerateTmpFiles,
39
- },
40
- enableBy: api.EnableBy.config,
41
- });
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
- const cwd = process.cwd();
61
- // support APP_ROOT
62
- if (pkgHasDep &&
63
- api.cwd !== cwd &&
64
- (0, fs_1.existsSync)((0, path_1.join)(cwd, 'node_modules', pkgHasDep, 'package.json'))) {
65
- return (0, path_1.join)(cwd, 'node_modules', pkgHasDep);
66
- }
67
- // 如果项目中没有去找插件以来的
68
- return (0, path_1.dirname)(require.resolve('@ant-design/pro-layout/package.json'));
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // layout.ts
23
+ var layout_exports = {};
24
+ __export(layout_exports, {
25
+ default: () => layout_default
26
+ });
27
+ module.exports = __toCommonJS(layout_exports);
28
+ var allIcons = __toESM(require("@ant-design/icons"));
29
+ var import_fs = require("fs");
30
+ var import_path = require("path");
31
+ var import_plugin_utils = require("umi/plugin-utils");
32
+ var import_withTmpPath = require("./utils/withTmpPath");
33
+ var layout_default = (api) => {
34
+ api.describe({
35
+ key: "layout",
36
+ config: {
37
+ schema(Joi) {
38
+ return Joi.alternatives().try(Joi.object(), Joi.boolean().invalid(true));
39
+ },
40
+ onChange: api.ConfigChangeType.regenerateTmpFiles
41
+ },
42
+ enableBy: api.EnableBy.config
43
+ });
44
+ const depList = ["@alipay/tech-ui", "@ant-design/pro-layout"];
45
+ const pkgHasDep = depList.find((dep) => {
46
+ var _a, _b;
47
+ const { pkg } = api;
48
+ if (((_a = pkg.dependencies) == null ? void 0 : _a[dep]) || ((_b = pkg.devDependencies) == null ? void 0 : _b[dep])) {
49
+ return true;
50
+ }
51
+ return false;
52
+ });
53
+ const getPkgPath = () => {
54
+ if (pkgHasDep && (0, import_fs.existsSync)((0, import_path.join)(api.cwd, "node_modules", pkgHasDep, "package.json"))) {
55
+ return (0, import_path.join)(api.cwd, "node_modules", pkgHasDep);
56
+ }
57
+ const cwd = process.cwd();
58
+ if (pkgHasDep && api.cwd !== cwd && (0, import_fs.existsSync)((0, import_path.join)(cwd, "node_modules", pkgHasDep, "package.json"))) {
59
+ return (0, import_path.join)(cwd, "node_modules", pkgHasDep);
60
+ }
61
+ return (0, import_path.dirname)(require.resolve("@ant-design/pro-layout/package.json"));
62
+ };
63
+ const pkgPath = (0, import_plugin_utils.winPath)(getPkgPath());
64
+ api.modifyAppData((memo) => {
65
+ const version = require(`${pkgPath}/package.json`).version;
66
+ memo.pluginLayout = {
67
+ pkgPath,
68
+ version
69
69
  };
70
- const pkgPath = (0, plugin_utils_1.winPath)(getPkgPath());
71
- api.modifyAppData((memo) => {
72
- const version = require(`${pkgPath}/package.json`).version;
73
- memo.pluginLayout = {
74
- pkgPath,
75
- version,
76
- };
77
- return memo;
78
- });
79
- api.modifyConfig((memo) => {
80
- // 只在没有自行依赖 @ant-design/pro-layout 或 @alipay/tech-ui 时
81
- // 才使用插件中提供的 @ant-design/pro-layout
82
- if (!pkgHasDep) {
83
- memo.alias['@ant-design/pro-layout'] = pkgPath;
84
- }
85
- return memo;
86
- });
87
- api.onGenerateFiles(() => {
88
- const hasInitialStatePlugin = api.config.initialState;
89
- // Layout.tsx
90
- api.writeTmpFile({
91
- path: 'Layout.tsx',
92
- content: `
70
+ return memo;
71
+ });
72
+ api.modifyConfig((memo) => {
73
+ if (!pkgHasDep) {
74
+ memo.alias["@ant-design/pro-layout"] = pkgPath;
75
+ }
76
+ return memo;
77
+ });
78
+ api.onGenerateFiles(() => {
79
+ const hasInitialStatePlugin = api.config.initialState;
80
+ api.writeTmpFile({
81
+ path: "Layout.tsx",
82
+ content: `
93
83
  import { Link, useLocation, useNavigate, Outlet, useAppData, useRouteData, matchRoutes } from 'umi';
94
84
  import type { IRoute } from 'umi';
95
85
  import React, { useMemo } from 'react';
96
86
  import {
97
87
  ProLayout,
98
- } from "${pkgPath || '@ant-design/pro-layout'}";
88
+ } from "${pkgPath || "@ant-design/pro-layout"}";
99
89
  import './Layout.less';
100
90
  import Logo from './Logo';
101
91
  import Exception from './Exception';
102
92
  import { getRightRenderContent } from './rightRender';
103
- ${hasInitialStatePlugin
104
- ? `import { useModel } from '@@/plugin-model';`
105
- : 'const useModel = null;'}
106
- ${api.config.access
107
- ? `
93
+ ${hasInitialStatePlugin ? `import { useModel } from '@@/plugin-model';` : "const useModel = null;"}
94
+ ${api.config.access ? `
108
95
  import { useAccessMarkedRoutes } from '@@/plugin-access';
109
- `.trim()
110
- : 'const useAccessMarkedRoutes = (r) => r;'}
111
- ${api.config.locale
112
- ? `
96
+ `.trim() : "const useAccessMarkedRoutes = (r) => r;"}
97
+ ${api.config.locale ? `
113
98
  import { useIntl } from '@@/plugin-locale';
114
- `.trim()
115
- : ''}
99
+ `.trim() : ""}
116
100
 
117
- // 过滤出需要显示的路由, 这里的filterFn 不希望显示的层级
101
+ // \u8FC7\u6EE4\u51FA\u9700\u8981\u663E\u793A\u7684\u8DEF\u7531, \u8FD9\u91CC\u7684filterFn \u6307 \u4E0D\u5E0C\u671B\u663E\u793A\u7684\u5C42\u7EA7
118
102
  const filterRoutes = (routes: IRoute[], filterFn: (route: IRoute) => boolean) => {
119
103
  if (routes.length === 0) {
120
104
  return []
@@ -137,6 +121,26 @@ const filterRoutes = (routes: IRoute[], filterFn: (route: IRoute) => boolean) =>
137
121
  return newRoutes;
138
122
  }
139
123
 
124
+ // \u683C\u5F0F\u5316\u8DEF\u7531 \u5904\u7406\u56E0 wrapper \u5BFC\u81F4\u7684 \u83DC\u5355 path \u4E0D\u4E00\u81F4
125
+ const mapRoutes = (routes: IRoute[]) => {
126
+ if (routes.length === 0) {
127
+ return []
128
+ }
129
+ return routes.map(route => {
130
+ // \u9700\u8981 copy \u4E00\u4EFD, \u5426\u5219\u4F1A\u6C61\u67D3\u539F\u59CB\u6570\u636E
131
+ const newRoute = {...route}
132
+ if (route.originPath) {
133
+ newRoute.path = route.originPath
134
+ }
135
+
136
+ if (Array.isArray(route.routes)) {
137
+ newRoute.routes = mapRoutes(route.routes);
138
+ }
139
+
140
+ return newRoute
141
+ })
142
+ }
143
+
140
144
  export default (props: any) => {
141
145
  const location = useLocation();
142
146
  const navigate = useNavigate();
@@ -148,11 +152,9 @@ export default (props: any) => {
148
152
  };
149
153
  const { initialState, loading, setInitialState } = initialInfo;
150
154
  const userConfig = ${JSON.stringify(api.config.layout, null, 2)};
151
- ${api.config.locale
152
- ? `
155
+ ${api.config.locale ? `
153
156
  const { formatMessage } = useIntl();
154
- `.trim()
155
- : 'const formatMessage = undefined;'}
157
+ `.trim() : "const formatMessage = undefined;"}
156
158
  const runtimeConfig = pluginManager.applyPlugins({
157
159
  key: 'layout',
158
160
  type: 'modify',
@@ -163,9 +165,9 @@ const { formatMessage } = useIntl();
163
165
 
164
166
  const matchedRoute = useMemo(() => matchRoutes(clientRoutes, location.pathname).pop()?.route, [location.pathname]);
165
167
  const newRoutes = filterRoutes(clientRoutes.filter(route => route.id === 'ant-design-pro-layout'), (route) => {
166
- return !!route.isLayout && route.id !== 'ant-design-pro-layout';
168
+ return (!!route.isLayout && route.id !== 'ant-design-pro-layout') || !!route.isWrapper;
167
169
  })
168
- const [route] = useAccessMarkedRoutes(newRoutes);
170
+ const [route] = useAccessMarkedRoutes(mapRoutes(newRoutes));
169
171
 
170
172
  return (
171
173
  <ProLayout
@@ -236,72 +238,63 @@ const { formatMessage } = useIntl();
236
238
  </ProLayout>
237
239
  );
238
240
  }
239
- `,
240
- });
241
- api.writeTmpFile({
242
- path: 'index.ts',
243
- content: `export type TempType = string`,
244
- });
245
- // 写入类型, RunTimeLayoutConfig 是 app.tsx 中 layout 配置的类型
246
- // 对于动态 layout 配置很有用
247
- api.writeTmpFile({
248
- path: 'types.d.ts',
249
- content: `
250
- import type { ProLayoutProps } from "${pkgPath || '@ant-design/pro-layout'}";
251
- ${hasInitialStatePlugin
252
- ? `import type InitialStateType from '@@/plugin-initialState/@@initialState';
241
+ `
242
+ });
243
+ api.writeTmpFile({
244
+ path: "index.ts",
245
+ content: `export type TempType = string`
246
+ });
247
+ api.writeTmpFile({
248
+ path: "types.d.ts",
249
+ content: `
250
+ import type { ProLayoutProps } from "${pkgPath || "@ant-design/pro-layout"}";
251
+ ${hasInitialStatePlugin ? `import type InitialStateType from '@@/plugin-initialState/@@initialState';
253
252
  type InitDataType = ReturnType<typeof InitialStateType>;
254
- `
255
- : 'type InitDataType = any;'}
253
+ ` : "type InitDataType = any;"}
256
254
 
257
255
  export type RunTimeLayoutConfig = (
258
256
  initData: InitDataType,
259
257
  ) => ProLayoutProps & {
260
258
  childrenRender?: (dom: JSX.Element, props: ProLayoutProps) => React.ReactNode,
261
- unAccessible?: JSX.Element,
262
- noFound?: JSX.Element,
259
+ noAccessible?: JSX.Element,
260
+ notFound?: JSX.Element,
263
261
  };
264
- `,
265
- });
266
- const iconsMap = Object.keys(api.appData.routes).reduce((memo, id) => {
267
- const { icon } = api.appData.routes[id];
268
- if (icon) {
269
- const upperIcon = plugin_utils_1.lodash.upperFirst(plugin_utils_1.lodash.camelCase(icon));
270
- // @ts-ignore
271
- if (allIcons[upperIcon]) {
272
- memo[upperIcon] = true;
273
- }
274
- // @ts-ignore
275
- if (allIcons[`${upperIcon}Outlined`]) {
276
- memo[`${upperIcon}Outlined`] = true;
277
- }
278
- }
279
- return memo;
280
- }, {});
281
- const icons = Object.keys(iconsMap);
282
- const antIconsPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('@ant-design/icons/package')));
283
- api.writeTmpFile({
284
- path: 'icons.tsx',
285
- content: `
286
- ${icons
287
- .map((icon) => {
288
- return `import ${icon} from '${antIconsPath}/es/icons/${icon}';`;
289
- })
290
- .join('\n')}
291
- export default { ${icons.join(', ')} };
292
- `,
293
- });
294
- // runtime.tsx
295
- api.writeTmpFile({
296
- path: 'runtime.tsx',
297
- content: `
262
+ `
263
+ });
264
+ const iconsMap = Object.keys(api.appData.routes).reduce((memo, id) => {
265
+ const { icon } = api.appData.routes[id];
266
+ if (icon) {
267
+ const upperIcon = import_plugin_utils.lodash.upperFirst(import_plugin_utils.lodash.camelCase(icon));
268
+ if (allIcons[upperIcon]) {
269
+ memo[upperIcon] = true;
270
+ }
271
+ if (allIcons[`${upperIcon}Outlined`]) {
272
+ memo[`${upperIcon}Outlined`] = true;
273
+ }
274
+ }
275
+ return memo;
276
+ }, {});
277
+ const icons = Object.keys(iconsMap);
278
+ const antIconsPath = (0, import_plugin_utils.winPath)((0, import_path.dirname)(require.resolve("@ant-design/icons/package")));
279
+ api.writeTmpFile({
280
+ path: "icons.tsx",
281
+ content: `
282
+ ${icons.map((icon) => {
283
+ return `import ${icon} from '${antIconsPath}/es/icons/${icon}';`;
284
+ }).join("\n")}
285
+ export default { ${icons.join(", ")} };
286
+ `
287
+ });
288
+ api.writeTmpFile({
289
+ path: "runtime.tsx",
290
+ content: `
298
291
  import React from 'react';
299
292
  import icons from './icons';
300
293
 
301
294
  function formatIcon(name: string) {
302
295
  return name
303
296
  .replace(name[0], name[0].toUpperCase())
304
- .replace(/-(\w)/g, function(all, letter) {
297
+ .replace(/-(w)/g, function(all, letter) {
305
298
  return letter.toUpperCase();
306
299
  });
307
300
  }
@@ -317,9 +310,9 @@ export function patchRoutes({ routes }) {
317
310
  }
318
311
  });
319
312
  }
320
- `,
321
- });
322
- const rightRenderContent = `
313
+ `
314
+ });
315
+ const rightRenderContent = `
323
316
  import React from 'react';
324
317
  import { Avatar, Dropdown, Menu, Spin } from 'antd';
325
318
  import { LogoutOutlined } from '@ant-design/icons';
@@ -350,7 +343,7 @@ export function getRightRenderContent (opts: {
350
343
  }
351
344
  >
352
345
  <LogoutOutlined />
353
- 退出登录
346
+ \u9000\u51FA\u767B\u5F55
354
347
  </Menu.Item>
355
348
  </Menu>
356
349
  );
@@ -394,22 +387,20 @@ export function getRightRenderContent (opts: {
394
387
  );
395
388
  }
396
389
  `;
397
- const Locale = api.isPluginEnable('locale');
398
- // rightRender.tsx
399
- api.writeTmpFile({
400
- path: 'rightRender.tsx',
401
- content: plugin_utils_1.Mustache.render(rightRenderContent, {
402
- Locale,
403
- }),
404
- });
405
- // Layout.less
406
- api.writeTmpFile({
407
- path: 'Layout.less',
408
- content: `
390
+ const Locale = api.isPluginEnable("locale");
391
+ api.writeTmpFile({
392
+ path: "rightRender.tsx",
393
+ content: import_plugin_utils.Mustache.render(rightRenderContent, {
394
+ Locale
395
+ })
396
+ });
397
+ api.writeTmpFile({
398
+ path: "Layout.less",
399
+ content: `
409
400
  @import '~antd/es/style/themes/default.less';
410
401
  @pro-header-hover-bg: rgba(0, 0, 0, 0.025);
411
402
  @media screen and (max-width: @screen-xs) {
412
- // 在小屏幕的时候可以有更好的体验
403
+ // \u5728\u5C0F\u5C4F\u5E55\u7684\u65F6\u5019\u53EF\u4EE5\u6709\u66F4\u597D\u7684\u4F53\u9A8C
413
404
  .umi-plugin-layout-container {
414
405
  width: 100% !important;
415
406
  }
@@ -459,12 +450,11 @@ export function getRightRenderContent (opts: {
459
450
  .umi-plugin-layout-name {
460
451
  margin-left: 8px;
461
452
  }
462
- `,
463
- });
464
- // Logo.tsx
465
- api.writeTmpFile({
466
- path: 'Logo.tsx',
467
- content: `
453
+ `
454
+ });
455
+ api.writeTmpFile({
456
+ path: "Logo.tsx",
457
+ content: `
468
458
  import React from 'react';
469
459
 
470
460
  const LogoIcon: React.FC = () => {
@@ -556,11 +546,11 @@ const LogoIcon: React.FC = () => {
556
546
  };
557
547
 
558
548
  export default LogoIcon;
559
- `,
560
- });
561
- api.writeTmpFile({
562
- path: 'Exception.tsx',
563
- content: `
549
+ `
550
+ });
551
+ api.writeTmpFile({
552
+ path: "Exception.tsx",
553
+ content: `
564
554
  import React from 'react';
565
555
  import { history, type IRoute } from 'umi';
566
556
  import { Result, Button } from 'antd';
@@ -580,10 +570,10 @@ const Exception: React.FC<{
580
570
  <Result
581
571
  status={props.route ? '403' : '404'}
582
572
  title={props.route ? '403' : '404'}
583
- subTitle={props.route ? '抱歉,你无权访问该页面' : '抱歉,你访问的页面不存在'}
573
+ subTitle={props.route ? '\u62B1\u6B49\uFF0C\u4F60\u65E0\u6743\u8BBF\u95EE\u8BE5\u9875\u9762' : '\u62B1\u6B49\uFF0C\u4F60\u8BBF\u95EE\u7684\u9875\u9762\u4E0D\u5B58\u5728'}
584
574
  extra={
585
575
  <Button type="primary" onClick={() => history.push('/')}>
586
- 返回首页
576
+ \u8FD4\u56DE\u9996\u9875
587
577
  </Button>
588
578
  }
589
579
  />
@@ -593,22 +583,24 @@ const Exception: React.FC<{
593
583
  );
594
584
 
595
585
  export default Exception;
596
- `,
597
- });
598
- });
599
- api.addLayouts(() => {
600
- return [
601
- {
602
- id: 'ant-design-pro-layout',
603
- file: (0, withTmpPath_1.withTmpPath)({ api, path: 'Layout.tsx' }),
604
- test: (route) => {
605
- return route.layout !== false;
606
- },
607
- },
608
- ];
609
- });
610
- api.addRuntimePluginKey(() => ['layout']);
611
- api.addRuntimePlugin(() => {
612
- return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
586
+ `
613
587
  });
588
+ });
589
+ api.addLayouts(() => {
590
+ return [
591
+ {
592
+ id: "ant-design-pro-layout",
593
+ file: (0, import_withTmpPath.withTmpPath)({ api, path: "Layout.tsx" }),
594
+ test: (route) => {
595
+ return route.layout !== false;
596
+ }
597
+ }
598
+ ];
599
+ });
600
+ api.addRuntimePluginKey(() => ["layout"]);
601
+ api.addRuntimePlugin(() => {
602
+ return [(0, import_withTmpPath.withTmpPath)({ api, path: "runtime.tsx" })];
603
+ });
614
604
  };
605
+ // Annotate the CommonJS export names for ESM import in node:
606
+ 0 && (module.exports = {});