@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/dist/access.js CHANGED
@@ -1,14 +1,9 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
10
4
  };
11
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ const fs_1 = __importDefault(require("fs"));
12
7
  const path_1 = require("path");
13
8
  const withTmpPath_1 = require("./utils/withTmpPath");
14
9
  exports.default = (api) => {
@@ -20,19 +15,29 @@ exports.default = (api) => {
20
15
  },
21
16
  enableBy: api.EnableBy.config,
22
17
  });
23
- api.onGenerateFiles(() => __awaiter(void 0, void 0, void 0, function* () {
18
+ api.onGenerateFiles(async () => {
19
+ // allow enable access without access file
20
+ const hasAccessFile = ['js', 'jsx', 'ts', 'tsx'].some((ext) => fs_1.default.existsSync((0, path_1.join)(api.paths.absSrcPath, `access.${ext}`)));
24
21
  // runtime.tsx
25
22
  api.writeTmpFile({
26
23
  path: 'runtime.tsx',
27
24
  content: `
28
- import React from 'react';
29
- import accessFactory from '@/access';
25
+ import React from 'react';${hasAccessFile
26
+ ? `
27
+ import accessFactory from '@/access'
30
28
  import { useModel } from '@@/plugin-model';
29
+ `
30
+ : ''}
31
31
  import { AccessContext } from './context';
32
32
 
33
- function Provider(props) {
33
+ function Provider(props) {${hasAccessFile
34
+ ? `
34
35
  const { initialState } = useModel('@@initialState');
35
36
  const access = React.useMemo(() => accessFactory(initialState), [initialState]);
37
+ `
38
+ : `
39
+ const access = {};
40
+ `}
36
41
  return (
37
42
  <AccessContext.Provider value={access}>
38
43
  { props.children }
@@ -45,11 +50,11 @@ export function accessProvider(container) {
45
50
  }
46
51
  `,
47
52
  });
48
- // index.ts
53
+ // index.tsx
49
54
  api.writeTmpFile({
50
- path: 'index.ts',
55
+ path: 'index.tsx',
51
56
  content: `
52
- import React from 'react';
57
+ import React, { PropsWithChildren } from 'react';
53
58
  import { AccessContext } from './context';
54
59
  import type { IRoute } from 'umi';
55
60
 
@@ -61,12 +66,12 @@ export interface AccessProps {
61
66
  accessible: boolean;
62
67
  fallback?: React.ReactNode;
63
68
  }
64
- export const Access: React.FC<AccessProps> = (props) => {
69
+ export const Access: React.FC<PropsWithChildren<AccessProps>> = (props) => {
65
70
  if (process.env.NODE_ENV === 'development' && typeof props.accessible !== 'boolean') {
66
71
  throw new Error('[access] the \`accessible\` property on <Access /> should be a boolean');
67
72
  }
68
73
 
69
- return props.accessible ? props.children : props.fallback;
74
+ return <>{ props.accessible ? props.children : props.fallback }</>;
70
75
  };
71
76
 
72
77
  export const useAccessMarkedRoutes = (routes: IRoute[]) => {
@@ -80,7 +85,7 @@ export const useAccessMarkedRoutes = (routes: IRoute[]) => {
80
85
 
81
86
  // check access code
82
87
  if (typeof accessCode === 'string') {
83
- const detector = access[route.access];
88
+ const detector = access[accessCode];
84
89
 
85
90
  if (typeof detector === 'function') {
86
91
  route.unaccessible = !detector(route);
@@ -92,8 +97,8 @@ export const useAccessMarkedRoutes = (routes: IRoute[]) => {
92
97
  }
93
98
 
94
99
  // check children access code
95
- if (route.routes) {
96
- const isNoAccessibleChild = !route.routes.reduce((hasAccessibleChild, child) => {
100
+ if (route.children?.length) {
101
+ const isNoAccessibleChild = !route.children.reduce((hasAccessibleChild, child) => {
97
102
  process(child, accessCode);
98
103
 
99
104
  return hasAccessibleChild || !child.unaccessible;
@@ -123,7 +128,7 @@ import React from 'react';
123
128
  export const AccessContext = React.createContext<any>(null);
124
129
  `,
125
130
  });
126
- }));
131
+ });
127
132
  api.addRuntimePlugin(() => {
128
133
  return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
129
134
  });
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/antd.js CHANGED
@@ -30,7 +30,12 @@ exports.default = (api) => {
30
30
  });
31
31
  },
32
32
  },
33
- enableBy: api.EnableBy.config,
33
+ enableBy({ userConfig }) {
34
+ // 由于本插件有 api.modifyConfig 的调用,以及 Umi 框架的限制
35
+ // 在其他插件中通过 api.modifyDefaultConfig 设置 antd 并不能让 api.modifyConfig 生效
36
+ // 所以这里通过环境变量来判断是否启用
37
+ return process.env.UMI_PLUGIN_ANTD_ENABLE || userConfig.antd;
38
+ },
34
39
  });
35
40
  function checkPkgPath() {
36
41
  if (!pkgPath) {
@@ -48,21 +53,31 @@ exports.default = (api) => {
48
53
  });
49
54
  api.modifyConfig((memo) => {
50
55
  checkPkgPath();
56
+ const antd = memo.antd || {};
57
+ // defaultConfig 的取值在 config 之后,所以改用环境变量传默认值
58
+ if (process.env.UMI_PLUGIN_ANTD_ENABLE) {
59
+ const { defaultConfig } = JSON.parse(process.env.UMI_PLUGIN_ANTD_ENABLE);
60
+ Object.assign(antd, defaultConfig);
61
+ }
51
62
  // antd import
52
63
  memo.alias.antd = pkgPath;
53
64
  // moment > dayjs
54
- if (memo.antd.dayjs) {
65
+ if (antd.dayjs) {
55
66
  memo.alias.moment = (0, path_1.dirname)(require.resolve('dayjs/package.json'));
56
67
  }
57
68
  // dark mode & compact mode
58
- if (memo.antd.dark || memo.antd.compact) {
69
+ if (antd.dark || antd.compact) {
59
70
  const { getThemeVariables } = require('antd/dist/theme');
60
- memo.theme = Object.assign(Object.assign({}, getThemeVariables(memo.antd)), memo.theme);
71
+ memo.theme = {
72
+ ...getThemeVariables(antd),
73
+ ...memo.theme,
74
+ };
61
75
  }
62
- return memo;
63
- });
64
- api.modifyConfig((memo) => {
65
- memo.theme = Object.assign({ 'root-entry-name': 'default' }, memo.theme);
76
+ // antd theme
77
+ memo.theme = {
78
+ 'root-entry-name': 'default',
79
+ ...memo.theme,
80
+ };
66
81
  return memo;
67
82
  });
68
83
  // babel-plugin-import
@@ -77,6 +92,7 @@ exports.default = (api) => {
77
92
  libraryDirectory: 'es',
78
93
  style: style === 'less' ? true : 'css',
79
94
  },
95
+ 'antd',
80
96
  ],
81
97
  ]
82
98
  : [];
@@ -88,6 +104,7 @@ exports.default = (api) => {
88
104
  api.writeTmpFile({
89
105
  path: `runtime.tsx`,
90
106
  content: plugin_utils_1.Mustache.render(`
107
+ import React from 'react';
91
108
  import { ConfigProvider, Modal, message, notification } from 'antd';
92
109
 
93
110
  export function rootContainer(container) {
package/dist/dva.js CHANGED
@@ -25,11 +25,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.getAllModels = exports.getModelUtil = void 0;
27
27
  const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
28
+ const utils_1 = require("@umijs/utils");
28
29
  const path_1 = require("path");
29
30
  const plugin_utils_1 = require("umi/plugin-utils");
30
31
  const modelUtils_1 = require("./utils/modelUtils");
31
32
  const withTmpPath_1 = require("./utils/withTmpPath");
32
- const utils_1 = require("@umijs/utils");
33
33
  exports.default = (api) => {
34
34
  const pkgPath = (0, path_1.join)(__dirname, '../libs/dva.ts');
35
35
  api.describe({
@@ -80,15 +80,24 @@ import dvaImmer, { enableES5, enableAllPlugins } from '${(0, utils_1.winPath)(re
80
80
  `
81
81
  : ''}
82
82
  import React, { useRef } from 'react';
83
- import { history } from 'umi';
83
+ import { history, ApplyPluginsType, useAppData } from 'umi';
84
84
  import { models } from './models';
85
85
 
86
+ let dvaApp: any;
87
+
86
88
  export function RootContainer(props: any) {
89
+ const { pluginManager } = useAppData();
87
90
  const app = useRef<any>();
91
+ const runtimeDva = pluginManager.applyPlugins({
92
+ key: 'dva',
93
+ type: ApplyPluginsType.modify,
94
+ initialValue: {},
95
+ });
88
96
  if (!app.current) {
89
97
  app.current = create(
90
98
  {
91
99
  history,
100
+ ...(runtimeDva.config || {}),
92
101
  },
93
102
  {
94
103
  initialReducer: {},
@@ -100,17 +109,28 @@ export function RootContainer(props: any) {
100
109
  },
101
110
  },
102
111
  );
112
+ dvaApp = app.current;
103
113
  app.current.use(createLoading());
104
114
  ${((_b = api.config.dva) === null || _b === void 0 ? void 0 : _b.immer) ? `app.current.use(dvaImmer());` : ''}
105
115
  ${((_d = (_c = api.config.dva) === null || _c === void 0 ? void 0 : _c.immer) === null || _d === void 0 ? void 0 : _d.enableES5) ? `enableES5();` : ''}
106
116
  ${((_f = (_e = api.config.dva) === null || _e === void 0 ? void 0 : _e.immer) === null || _f === void 0 ? void 0 : _f.enableAllPlugins) ? `enableAllPlugins();` : ''}
117
+ (runtimeDva.plugins || []).forEach((p) => {
118
+ app.current.use(p);
119
+ });
107
120
  for (const id of Object.keys(models)) {
108
- app.current.model(models[id].model);
121
+ app.current.model({
122
+ namespace: models[id].namespace,
123
+ ...models[id].model,
124
+ });
109
125
  }
110
126
  app.current.start();
111
127
  }
112
128
  return <Provider store={app.current!._store}>{props.children}</Provider>;
113
129
  }
130
+
131
+ export function getDvaApp() {
132
+ return dvaApp;
133
+ }
114
134
  `,
115
135
  context: {},
116
136
  });
@@ -130,7 +150,9 @@ export function dataflowProvider(container, opts) {
130
150
  api.writeTmpFile({
131
151
  path: 'index.ts',
132
152
  content: `
133
- export { connect, useDispatch, useStore, useSelector } from 'dva';`,
153
+ export { connect, useDispatch, useStore, useSelector } from 'dva';
154
+ export { getDvaApp } from './dva';
155
+ `,
134
156
  });
135
157
  });
136
158
  api.addTmpGenerateWatcherPaths(() => {
@@ -139,6 +161,7 @@ export { connect, useDispatch, useStore, useSelector } from 'dva';`,
139
161
  api.addRuntimePlugin(() => {
140
162
  return [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })];
141
163
  });
164
+ api.addRuntimePluginKey(() => ['dva']);
142
165
  // dva list model
143
166
  api.registerCommand({
144
167
  name: 'dva',
@@ -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(() => {
@@ -30,8 +35,8 @@ exports.default = (api) => {
30
35
  import React from 'react';
31
36
  import { useModel } from '@@/plugin-model';
32
37
  ${loading
33
- ? `import Loading from ${loading}`
34
- : `function Loading() { return <div>loading</div>; }`}
38
+ ? `import Loading from '${loading}'`
39
+ : `function Loading() { return <div />; }`}
35
40
  export default function InitialStateProvider(props: any) {
36
41
  const appLoaded = React.useRef(false);
37
42
  const { loading = false } = useModel("@@initialState") || {};
@@ -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,20 +78,21 @@ 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
- const setInitialState = useCallback(async (initialState) => {
79
- setState((s) => {
80
- if (typeof initialState === 'function') {
81
- return { ...s, initialState: initialState(s.initialState), loading: false };
82
- }
83
- return { ...s, initialState, loading: false };
84
- });
85
- // [?]
86
- // await sleep(10)
87
- }, []);
83
+ const setInitialState = useCallback(
84
+ async (
85
+ initialState: InitialStateType | ((initialState: InitialStateType) => InitialStateType),
86
+ ) => {
87
+ setState((s) => {
88
+ if (typeof initialState === 'function') {
89
+ return { ...s, initialState: initialState(s.initialState), loading: false };
90
+ }
91
+ return { ...s, initialState, loading: false };
92
+ });
93
+ },
94
+ [],
95
+ );
88
96
 
89
97
  useEffect(() => {
90
98
  refresh();
@@ -107,7 +115,7 @@ export default () => ({ loading: false, refresh: () => {} })
107
115
  content: `
108
116
  import React from 'react';
109
117
  import Provider from './Provider';
110
- export function innerProvider(container) {
118
+ export function dataflowProvider(container) {
111
119
  return <Provider>{ container }</Provider>;
112
120
  }
113
121
  `,
package/dist/layout.js CHANGED
@@ -22,15 +22,11 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
25
  Object.defineProperty(exports, "__esModule", { value: true });
29
26
  const allIcons = __importStar(require("@ant-design/icons"));
30
- const assert_1 = __importDefault(require("assert"));
27
+ const fs_1 = require("fs");
31
28
  const path_1 = require("path");
32
29
  const plugin_utils_1 = require("umi/plugin-utils");
33
- const resolveProjectDep_1 = require("./utils/resolveProjectDep");
34
30
  const withTmpPath_1 = require("./utils/withTmpPath");
35
31
  exports.default = (api) => {
36
32
  api.describe({
@@ -43,11 +39,35 @@ exports.default = (api) => {
43
39
  },
44
40
  enableBy: api.EnableBy.config,
45
41
  });
46
- const pkgPath = (0, resolveProjectDep_1.resolveProjectDep)({
47
- pkg: api.pkg,
48
- cwd: api.cwd,
49
- dep: '@ant-design/pro-layout',
50
- }) || (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
+ 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'));
69
+ };
70
+ const pkgPath = (0, plugin_utils_1.winPath)(getPkgPath());
51
71
  api.modifyAppData((memo) => {
52
72
  const version = require(`${pkgPath}/package.json`).version;
53
73
  memo.pluginLayout = {
@@ -57,8 +77,11 @@ exports.default = (api) => {
57
77
  return memo;
58
78
  });
59
79
  api.modifyConfig((memo) => {
60
- // import from @ant-design/pro-layout
61
- memo.alias['@ant-design/pro-layout'] = pkgPath;
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
+ }
62
85
  return memo;
63
86
  });
64
87
  api.onGenerateFiles(() => {
@@ -68,10 +91,10 @@ exports.default = (api) => {
68
91
  path: 'Layout.tsx',
69
92
  content: `
70
93
  import { Link, useLocation, useNavigate, Outlet, useAppData, useRouteData, matchRoutes } from 'umi';
71
- import { useMemo } from 'react';
72
- import ProLayout, {
73
- PageLoading,
74
- } from '@ant-design/pro-layout';
94
+ import React, { useMemo } from 'react';
95
+ import {
96
+ ProLayout,
97
+ } from "${pkgPath || '@ant-design/pro-layout'}";
75
98
  import './Layout.less';
76
99
  import Logo from './Logo';
77
100
  import Exception from './Exception';
@@ -137,7 +160,8 @@ const { formatMessage } = useIntl();
137
160
  }
138
161
  if (menuItemProps.path && location.pathname !== menuItemProps.path) {
139
162
  return (
140
- <Link to={menuItemProps.path} target={menuItemProps.target}>
163
+ // handle wildcard route path, for example /slave/* from qiankun
164
+ <Link to={menuItemProps.path.replace('/*', '')} target={menuItemProps.target}>
141
165
  {defaultDom}
142
166
  </Link>
143
167
  );
@@ -185,14 +209,36 @@ const { formatMessage } = useIntl();
185
209
  );
186
210
  }
187
211
  `,
212
+ });
213
+ api.writeTmpFile({
214
+ path: 'index.ts',
215
+ content: `export type TempType = string`,
216
+ });
217
+ // 写入类型, RunTimeLayoutConfig 是 app.tsx 中 layout 配置的类型
218
+ // 对于动态 layout 配置很有用
219
+ api.writeTmpFile({
220
+ path: 'types.d.ts',
221
+ content: `
222
+ import type { ProLayoutProps } from "${pkgPath || '@ant-design/pro-layout'}";
223
+ ${hasInitialStatePlugin
224
+ ? `import type InitialStateType from '@@/plugin-initialState/@@initialState';
225
+ type InitDataType = ReturnType<typeof InitialStateType>;
226
+ `
227
+ : 'type InitDataType = any;'}
228
+
229
+ export type RunTimeLayoutConfig = (
230
+ initData: InitDataType,
231
+ ) => ProLayoutProps & {
232
+ childrenRender?: (dom: JSX.Element, props: ProLayoutProps) => React.ReactNode,
233
+ unAccessible?: JSX.Element,
234
+ noFound?: JSX.Element,
235
+ };
236
+ `,
188
237
  });
189
238
  const iconsMap = Object.keys(api.appData.routes).reduce((memo, id) => {
190
239
  const { icon } = api.appData.routes[id];
191
240
  if (icon) {
192
241
  const upperIcon = plugin_utils_1.lodash.upperFirst(plugin_utils_1.lodash.camelCase(icon));
193
- (0, assert_1.default)(
194
- // @ts-ignore
195
- allIcons[upperIcon] || allIcons[`${upperIcon}Outlined`], `Icon ${upperIcon} is not found`);
196
242
  // @ts-ignore
197
243
  if (allIcons[upperIcon]) {
198
244
  memo[upperIcon] = true;
@@ -237,7 +283,9 @@ export function patchRoutes({ routes }) {
237
283
  const { icon } = routes[key];
238
284
  if (icon && typeof icon === 'string') {
239
285
  const upperIcon = formatIcon(icon);
240
- routes[key].icon = React.createElement(icons[upperIcon] || icons[upperIcon + 'Outlined']);
286
+ if (icons[upperIcon] || icons[upperIcon + 'Outlined']) {
287
+ routes[key].icon = React.createElement(icons[upperIcon] || icons[upperIcon + 'Outlined']);
288
+ }
241
289
  }
242
290
  });
243
291
  }
package/dist/locale.js CHANGED
@@ -1,13 +1,4 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.packageNormalize = void 0;
13
4
  const fs_1 = require("fs");
@@ -58,9 +49,9 @@ exports.default = (api) => {
58
49
  },
59
50
  ]
60
51
  : []);
61
- const addAntdLocales = (args) => __awaiter(void 0, void 0, void 0, function* () {
52
+ const addAntdLocales = async (args) => {
62
53
  var _a;
63
- return yield api.applyPlugins({
54
+ return await api.applyPlugins({
64
55
  key: 'addAntdLocales',
65
56
  type: api.ApplyPluginsType.add,
66
57
  initialValue: [
@@ -68,29 +59,32 @@ exports.default = (api) => {
68
59
  ],
69
60
  args,
70
61
  });
71
- });
72
- const getList = (resolveKey) => __awaiter(void 0, void 0, void 0, function* () {
73
- var _b;
62
+ };
63
+ const getList = async (resolveKey) => {
64
+ var _a;
74
65
  const { paths } = api;
75
66
  return (0, localeUtils_1.getLocaleList)({
76
67
  localeFolder: 'locales',
77
- separator: (_b = api.config.locale) === null || _b === void 0 ? void 0 : _b.baseSeparator,
68
+ separator: (_a = api.config.locale) === null || _a === void 0 ? void 0 : _a.baseSeparator,
78
69
  absSrcPath: paths.absSrcPath,
79
70
  absPagesPath: paths.absPagesPath,
80
71
  addAntdLocales,
81
72
  resolveKey,
82
73
  });
83
- });
84
- api.onGenerateFiles(() => __awaiter(void 0, void 0, void 0, function* () {
85
- var _c, _d, _e, _f;
74
+ };
75
+ api.onGenerateFiles(async () => {
76
+ var _a, _b, _c, _d;
86
77
  const localeTpl = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../libs/locale/locale.tpl'), 'utf-8');
87
78
  // moment2dayjs
88
79
  const resolveKey = api.config.moment2dayjs ? 'dayjs' : 'moment';
89
80
  const momentPkgPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve(`${resolveKey}/package.json`)));
90
81
  const EventEmitterPkg = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('event-emitter/package')));
91
- const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = Object.assign(Object.assign({}, defaultConfig), api.config.locale);
92
- const defaultLocale = ((_c = api.config.locale) === null || _c === void 0 ? void 0 : _c.default) || `zh${baseSeparator}CN`;
93
- const localeList = yield getList(resolveKey);
82
+ const { baseSeparator, baseNavigator, antd, title, useLocalStorage } = {
83
+ ...defaultConfig,
84
+ ...api.config.locale,
85
+ };
86
+ const defaultLocale = ((_a = api.config.locale) === null || _a === void 0 ? void 0 : _a.default) || `zh${baseSeparator}CN`;
87
+ const localeList = await getList(resolveKey);
94
88
  const momentLocales = localeList
95
89
  .map(({ momentLocale }) => momentLocale)
96
90
  .filter((locale) => locale);
@@ -100,7 +94,7 @@ exports.default = (api) => {
100
94
  let MomentLocales = momentLocales;
101
95
  let DefaultMomentLocale = '';
102
96
  // set moment default accounding to locale.default
103
- if (!MomentLocales.length && ((_d = api.config.locale) === null || _d === void 0 ? void 0 : _d.default)) {
97
+ if (!MomentLocales.length && ((_b = api.config.locale) === null || _b === void 0 ? void 0 : _b.default)) {
104
98
  const [lang, country = ''] = defaultLocale.split(baseSeparator);
105
99
  const { momentLocale } = (0, localeUtils_1.getMomentLocale)(lang, country, resolveKey);
106
100
  if (momentLocale) {
@@ -110,9 +104,9 @@ exports.default = (api) => {
110
104
  }
111
105
  let DefaultAntdLocales = [];
112
106
  // set antd default locale
113
- if (!antdLocales.length && ((_e = api.config.locale) === null || _e === void 0 ? void 0 : _e.antd)) {
107
+ if (!antdLocales.length && ((_c = api.config.locale) === null || _c === void 0 ? void 0 : _c.antd)) {
114
108
  const [lang, country = ''] = defaultLocale.split(baseSeparator);
115
- DefaultAntdLocales = plugin_utils_1.lodash.uniq(yield addAntdLocales({
109
+ DefaultAntdLocales = plugin_utils_1.lodash.uniq(await addAntdLocales({
116
110
  lang,
117
111
  country,
118
112
  }));
@@ -148,16 +142,20 @@ exports.default = (api) => {
148
142
  UseLocalStorage: !!useLocalStorage,
149
143
  LocaleDir: localeDirName,
150
144
  ExistLocaleDir: (0, fs_1.existsSync)(localeDirPath),
151
- LocaleList: localeList.map((locale) => (Object.assign(Object.assign({}, locale), { antdLocale: locale.antdLocale.map((antdLocale, index) => ({
145
+ LocaleList: localeList.map((locale) => ({
146
+ ...locale,
147
+ antdLocale: locale.antdLocale.map((antdLocale, index) => ({
152
148
  locale: antdLocale,
153
149
  index: index,
154
- })), paths: locale.paths.map((path, index) => ({
150
+ })),
151
+ paths: locale.paths.map((path, index) => ({
155
152
  path,
156
153
  index,
157
- })) }))),
154
+ })),
155
+ })),
158
156
  Antd: !!antd,
159
157
  DefaultLocale: JSON.stringify(defaultLocale),
160
- warningPkgPath: (0, plugin_utils_1.winPath)(require.resolve('warning/package')),
158
+ warningPkgPath: (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('warning/package'))),
161
159
  reactIntlPkgPath,
162
160
  }),
163
161
  });
@@ -177,25 +175,25 @@ exports.default = (api) => {
177
175
  Antd: !!antd,
178
176
  LocaleList: localeList,
179
177
  ShowSelectLang: localeList.length > 1 && !!antd,
180
- antdFiles: ((_f = api.config) === null || _f === void 0 ? void 0 : _f.ssr) ? 'lib' : 'es',
178
+ antdFiles: ((_d = api.config) === null || _d === void 0 ? void 0 : _d.ssr) ? 'lib' : 'es',
181
179
  }),
182
180
  });
183
181
  // index.ts
184
182
  api.writeTmpFile({
185
183
  path: 'index.ts',
186
184
  content: `
187
- export { setLocale, getLocale, useIntl, formatMessage, FormattedMessage } from './localeExports.ts';
185
+ export { setLocale, getLocale, useIntl, injectIntl, formatMessage, FormattedMessage } from './localeExports.ts';
188
186
  export { SelectLang } from './SelectLang.tsx';
189
187
  `,
190
188
  });
191
- }));
189
+ });
192
190
  // Runtime Plugin
193
191
  api.addRuntimePlugin(() => [(0, withTmpPath_1.withTmpPath)({ api, path: 'runtime.tsx' })]);
194
192
  api.addRuntimePluginKey(() => ['locale']);
195
193
  // watch locale files
196
- api.addTmpGenerateWatcherPaths(() => __awaiter(void 0, void 0, void 0, function* () {
194
+ api.addTmpGenerateWatcherPaths(async () => {
197
195
  const resolveKey = api.config.moment2dayjs ? 'dayjs' : 'moment';
198
- const localeList = yield getList(resolveKey);
196
+ const localeList = await getList(resolveKey);
199
197
  return (0, localeUtils_1.exactLocalePaths)(localeList);
200
- }));
198
+ });
201
199
  };