@umijs/plugins 4.0.0-canary.20220429.2 → 4.0.0-canary.20220506.1
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.d.ts +3 -0
- package/dist/access.js +139 -0
- package/dist/analytics.d.ts +3 -0
- package/dist/analytics.js +67 -0
- package/dist/antd.d.ts +3 -0
- package/dist/antd.js +137 -0
- package/dist/dva.d.ts +6 -0
- package/dist/dva.js +198 -0
- package/dist/icons.d.ts +3 -0
- package/dist/icons.js +5 -0
- package/dist/initial-state.d.ts +3 -0
- package/dist/initial-state.js +116 -0
- package/dist/layout.d.ts +3 -0
- package/dist/layout.js +538 -0
- package/dist/locale.d.ts +4 -0
- package/dist/locale.js +199 -0
- package/dist/model.d.ts +3 -0
- package/dist/model.js +101 -0
- package/dist/moment2dayjs.d.ts +3 -0
- package/dist/moment2dayjs.js +96 -0
- package/dist/qiankun/constants.d.ts +5 -0
- package/dist/qiankun/constants.js +8 -0
- package/dist/qiankun/master.d.ts +6 -0
- package/dist/qiankun/master.js +134 -0
- package/dist/qiankun/slave.d.ts +3 -0
- package/dist/qiankun/slave.js +158 -0
- package/dist/qiankun.d.ts +3 -0
- package/dist/qiankun.js +19 -0
- package/dist/request.d.ts +3 -0
- package/dist/request.js +311 -0
- package/dist/tailwindcss.d.ts +3 -0
- package/dist/tailwindcss.js +40 -0
- package/dist/unocss.d.ts +3 -0
- package/dist/unocss.js +39 -0
- package/dist/utils/astUtils.d.ts +3 -0
- package/dist/utils/astUtils.js +38 -0
- package/dist/utils/localeUtils.d.ts +33 -0
- package/dist/utils/localeUtils.js +126 -0
- package/dist/utils/modelUtils.d.ts +39 -0
- package/dist/utils/modelUtils.js +231 -0
- package/dist/utils/resolveProjectDep.d.ts +5 -0
- package/dist/utils/resolveProjectDep.js +15 -0
- package/dist/utils/withTmpPath.d.ts +6 -0
- package/dist/utils/withTmpPath.js +11 -0
- package/libs/qiankun/master/MicroApp.tsx +7 -0
- package/libs/qiankun/master/common.ts +13 -10
- package/libs/qiankun/master/getMicroAppRouteComponent.tsx.tpl +5 -5
- package/libs/qiankun/master/masterRuntimePlugin.tsx +5 -5
- package/libs/qiankun/slave/connectMaster.tsx +0 -1
- package/libs/qiankun/slave/lifecycles.ts +6 -8
- package/libs/qiankun/slave/qiankunModel.ts +0 -1
- package/libs/qiankun/slave/slaveRuntimePlugin.ts +9 -15
- package/package.json +3 -3
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const assert_1 = __importDefault(require("assert"));
|
|
7
|
+
const fs_1 = require("fs");
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const plugin_utils_1 = require("umi/plugin-utils");
|
|
10
|
+
const withTmpPath_1 = require("../utils/withTmpPath");
|
|
11
|
+
const constants_1 = require("./constants");
|
|
12
|
+
// BREAK CHANGE: 需要手动配置 slave: {},不能留空
|
|
13
|
+
function isSlaveEnable(opts) {
|
|
14
|
+
var _a, _b;
|
|
15
|
+
const slaveCfg = (_b = (_a = opts.userConfig) === null || _a === void 0 ? void 0 : _a.qiankun) === null || _b === void 0 ? void 0 : _b.slave;
|
|
16
|
+
if (slaveCfg) {
|
|
17
|
+
return slaveCfg.enable !== false;
|
|
18
|
+
}
|
|
19
|
+
return !!process.env.INITIAL_QIANKUN_SLAVE_OPTIONS;
|
|
20
|
+
}
|
|
21
|
+
exports.default = (api) => {
|
|
22
|
+
api.describe({
|
|
23
|
+
key: 'qiankun-slave',
|
|
24
|
+
enableBy: isSlaveEnable,
|
|
25
|
+
});
|
|
26
|
+
api.addRuntimePlugin(() => {
|
|
27
|
+
return [(0, withTmpPath_1.withTmpPath)({ api, path: 'slaveRuntimePlugin.ts' })];
|
|
28
|
+
});
|
|
29
|
+
api.register({
|
|
30
|
+
key: 'addExtraModels',
|
|
31
|
+
fn() {
|
|
32
|
+
return [
|
|
33
|
+
`@@/plugin-qiankun-slave/qiankunModel#{"namespace":"${constants_1.qiankunStateFromMasterModelNamespace}"}`,
|
|
34
|
+
];
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
api.modifyDefaultConfig((memo) => {
|
|
38
|
+
var _a, _b, _c, _d;
|
|
39
|
+
const initialSlaveOptions = {
|
|
40
|
+
devSourceMap: true,
|
|
41
|
+
...JSON.parse(process.env.INITIAL_QIANKUN_SLAVE_OPTIONS || '{}'),
|
|
42
|
+
...(memo.qiankun || {}).slave,
|
|
43
|
+
};
|
|
44
|
+
const modifiedDefaultConfig = {
|
|
45
|
+
...memo,
|
|
46
|
+
// 默认开启 runtimePublicPath,避免出现 dynamic import 场景子应用资源地址出问题
|
|
47
|
+
runtimePublicPath: true,
|
|
48
|
+
qiankun: {
|
|
49
|
+
...memo.qiankun,
|
|
50
|
+
slave: initialSlaveOptions,
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
const shouldNotModifyDefaultBase = (_c = (_b = (_a = api.userConfig.qiankun) === null || _a === void 0 ? void 0 : _a.slave) === null || _b === void 0 ? void 0 : _b.shouldNotModifyDefaultBase) !== null && _c !== void 0 ? _c : initialSlaveOptions.shouldNotModifyDefaultBase;
|
|
54
|
+
const historyType = ((_d = api.userConfig.history) === null || _d === void 0 ? void 0 : _d.type) || 'browser';
|
|
55
|
+
if (!shouldNotModifyDefaultBase && historyType !== 'hash') {
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
modifiedDefaultConfig.base = `/${api.pkg.name}`;
|
|
58
|
+
}
|
|
59
|
+
return modifiedDefaultConfig;
|
|
60
|
+
});
|
|
61
|
+
api.modifyConfig((config) => {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
// mfsu 场景默认给子应用增加 mfName 配置,从而避免冲突
|
|
64
|
+
if (config.mfsu !== false) {
|
|
65
|
+
config.mfsu = {
|
|
66
|
+
...config.mfsu,
|
|
67
|
+
mfName: ((_a = config.mfsu) === null || _a === void 0 ? void 0 : _a.mfName) ||
|
|
68
|
+
`mf_${(_b = api.pkg.name) === null || _b === void 0 ? void 0 : _b.replace(/^@/, '').replace(/\W/g, '_')}`,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return config;
|
|
72
|
+
});
|
|
73
|
+
api.addHTMLHeadScripts(() => {
|
|
74
|
+
var _a;
|
|
75
|
+
const dontModify = (_a = api.config.qiankun) === null || _a === void 0 ? void 0 : _a.shouldNotModifyRuntimePublicPath;
|
|
76
|
+
return dontModify
|
|
77
|
+
? []
|
|
78
|
+
: [
|
|
79
|
+
`window.publicPath = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__ || "${api.config.publicPath || '/'}";`,
|
|
80
|
+
];
|
|
81
|
+
});
|
|
82
|
+
api.chainWebpack((config) => {
|
|
83
|
+
(0, assert_1.default)(api.pkg.name, 'You should have name in package.json.');
|
|
84
|
+
const { shouldNotAddLibraryChunkName } = (api.config.qiankun || {}).slave;
|
|
85
|
+
config.output
|
|
86
|
+
.libraryTarget('umd')
|
|
87
|
+
.library(shouldNotAddLibraryChunkName ? api.pkg.name : `${api.pkg.name}-[name]`);
|
|
88
|
+
// TODO: SOCKET_SERVER
|
|
89
|
+
// TODO: devSourceMap
|
|
90
|
+
return config;
|
|
91
|
+
});
|
|
92
|
+
// umi bundle 添加 entry 标记
|
|
93
|
+
api.modifyHTML(($) => {
|
|
94
|
+
$('script').each((_, el) => {
|
|
95
|
+
var _a;
|
|
96
|
+
const scriptEl = $(el);
|
|
97
|
+
const umiEntry = /\/?umi(\.\w+)?\.js$/g;
|
|
98
|
+
if (umiEntry.test((_a = scriptEl.attr('src')) !== null && _a !== void 0 ? _a : '')) {
|
|
99
|
+
scriptEl.attr('entry', '');
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
return $;
|
|
103
|
+
});
|
|
104
|
+
api.addEntryImports(() => {
|
|
105
|
+
return [
|
|
106
|
+
{
|
|
107
|
+
source: '@@/plugin-qiankun-slave/lifecycles',
|
|
108
|
+
specifier: '{ genMount as qiankun_genMount, genBootstrap as qiankun_genBootstrap, genUnmount as qiankun_genUnmount, genUpdate as qiankun_genUpdate }',
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
});
|
|
112
|
+
api.addEntryCode(() => [
|
|
113
|
+
`
|
|
114
|
+
export const bootstrap = qiankun_genBootstrap(render);
|
|
115
|
+
export const mount = qiankun_genMount('${api.config.mountElementId}');
|
|
116
|
+
export const unmount = qiankun_genUnmount('${api.config.mountElementId}');
|
|
117
|
+
export const update = qiankun_genUpdate();
|
|
118
|
+
if (!window.__POWERED_BY_QIANKUN__) {
|
|
119
|
+
bootstrap().then(mount);
|
|
120
|
+
}
|
|
121
|
+
`,
|
|
122
|
+
]);
|
|
123
|
+
function getFileContent(file) {
|
|
124
|
+
return (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../libs/qiankun/slave', file), 'utf-8');
|
|
125
|
+
}
|
|
126
|
+
api.onGenerateFiles(() => {
|
|
127
|
+
// api.writeTmpFile({
|
|
128
|
+
// path: 'slaveOptions.ts',
|
|
129
|
+
// content: `
|
|
130
|
+
// let options = ${JSON.stringify((api.config.qiankun || {}).slave || {})};
|
|
131
|
+
// export const getSlaveOptions = () => options;
|
|
132
|
+
// export const setSlaveOptions = (newOpts) => options = ({ ...options, ...newOpts });
|
|
133
|
+
// `,
|
|
134
|
+
// });
|
|
135
|
+
[
|
|
136
|
+
'qiankunModel.ts',
|
|
137
|
+
'connectMaster.tsx',
|
|
138
|
+
'slaveRuntimePlugin.ts',
|
|
139
|
+
'lifecycles.ts',
|
|
140
|
+
].forEach((file) => {
|
|
141
|
+
api.writeTmpFile({
|
|
142
|
+
path: file.replace(/\.tpl$/, ''),
|
|
143
|
+
content: getFileContent(file)
|
|
144
|
+
.replace('__USE_MODEL__', api.isPluginEnable('model')
|
|
145
|
+
? `import { useModel } from '@@/plugin-model'`
|
|
146
|
+
: `const useModel = null;`)
|
|
147
|
+
.replace(/from 'qiankun'/g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('qiankun/package')))}'`)
|
|
148
|
+
.replace(/from 'lodash\//g, `from '${(0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('lodash/package')))}/`),
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
api.writeTmpFile({
|
|
152
|
+
path: 'index.ts',
|
|
153
|
+
content: `
|
|
154
|
+
export { connectMaster } from './connectMaster';
|
|
155
|
+
`,
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
};
|
package/dist/qiankun.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = (api) => {
|
|
4
|
+
api.describe({
|
|
5
|
+
config: {
|
|
6
|
+
schema(joi) {
|
|
7
|
+
return joi.object().keys({
|
|
8
|
+
slave: joi.object(),
|
|
9
|
+
master: joi.object(),
|
|
10
|
+
});
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
api.addRuntimePluginKey(() => ['qiankun']);
|
|
15
|
+
api.registerPlugins([
|
|
16
|
+
require.resolve('./qiankun/master'),
|
|
17
|
+
require.resolve('./qiankun/slave'),
|
|
18
|
+
]);
|
|
19
|
+
};
|
package/dist/request.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path_1 = require("path");
|
|
4
|
+
const plugin_utils_1 = require("umi/plugin-utils");
|
|
5
|
+
exports.default = (api) => {
|
|
6
|
+
api.describe({
|
|
7
|
+
key: 'request',
|
|
8
|
+
config: {
|
|
9
|
+
schema: (joi) => {
|
|
10
|
+
return joi.object({
|
|
11
|
+
dataField: joi
|
|
12
|
+
.string()
|
|
13
|
+
.pattern(/^[a-zA-Z]*$/)
|
|
14
|
+
.allow(''),
|
|
15
|
+
});
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
enableBy: api.EnableBy.config,
|
|
19
|
+
});
|
|
20
|
+
api.addRuntimePluginKey(() => ['request']);
|
|
21
|
+
const requestTpl = `
|
|
22
|
+
import axios, {
|
|
23
|
+
type AxiosInstance,
|
|
24
|
+
type AxiosRequestConfig,
|
|
25
|
+
type AxiosResponse,
|
|
26
|
+
} from '{{{axiosPath}}}';
|
|
27
|
+
import useUmiRequest, { UseRequestProvider } from '{{{umiRequestPath}}}';
|
|
28
|
+
import { ApplyPluginsType } from 'umi';
|
|
29
|
+
import { getPluginManager } from '../core/plugin';
|
|
30
|
+
|
|
31
|
+
import {
|
|
32
|
+
BaseOptions,
|
|
33
|
+
BasePaginatedOptions,
|
|
34
|
+
BaseResult,
|
|
35
|
+
CombineService,
|
|
36
|
+
LoadMoreFormatReturn,
|
|
37
|
+
LoadMoreOptions,
|
|
38
|
+
LoadMoreOptionsWithFormat,
|
|
39
|
+
LoadMoreParams,
|
|
40
|
+
LoadMoreResult,
|
|
41
|
+
OptionsWithFormat,
|
|
42
|
+
PaginatedFormatReturn,
|
|
43
|
+
PaginatedOptionsWithFormat,
|
|
44
|
+
PaginatedParams,
|
|
45
|
+
PaginatedResult,
|
|
46
|
+
} from '{{{umiRequestPath}}}/es/types';
|
|
47
|
+
|
|
48
|
+
type ResultWithData< T = any > = { data?: T; [key: string]: any };
|
|
49
|
+
|
|
50
|
+
function useRequest<
|
|
51
|
+
R = any,
|
|
52
|
+
P extends any[] = any,
|
|
53
|
+
U = any,
|
|
54
|
+
UU extends U = any,
|
|
55
|
+
>(
|
|
56
|
+
service: CombineService<R, P>,
|
|
57
|
+
options: OptionsWithFormat<R, P, U, UU>,
|
|
58
|
+
): BaseResult<U, P>;
|
|
59
|
+
function useRequest<R extends ResultWithData = any, P extends any[] = any>(
|
|
60
|
+
service: CombineService<R, P>,
|
|
61
|
+
options?: BaseOptions<R['data'], P>,
|
|
62
|
+
): BaseResult<R['data'], P>;
|
|
63
|
+
function useRequest<R extends LoadMoreFormatReturn = any, RR = any>(
|
|
64
|
+
service: CombineService<RR, LoadMoreParams<R>>,
|
|
65
|
+
options: LoadMoreOptionsWithFormat<R, RR>,
|
|
66
|
+
): LoadMoreResult<R>;
|
|
67
|
+
function useRequest<
|
|
68
|
+
R extends ResultWithData<LoadMoreFormatReturn | any> = any,
|
|
69
|
+
RR extends R = any,
|
|
70
|
+
>(
|
|
71
|
+
service: CombineService<R, LoadMoreParams<R['data']>>,
|
|
72
|
+
options: LoadMoreOptions<RR['data']>,
|
|
73
|
+
): LoadMoreResult<R['data']>;
|
|
74
|
+
|
|
75
|
+
function useRequest<R = any, Item = any, U extends Item = any>(
|
|
76
|
+
service: CombineService<R, PaginatedParams>,
|
|
77
|
+
options: PaginatedOptionsWithFormat<R, Item, U>,
|
|
78
|
+
): PaginatedResult<Item>;
|
|
79
|
+
function useRequest<Item = any, U extends Item = any>(
|
|
80
|
+
service: CombineService<
|
|
81
|
+
ResultWithData<PaginatedFormatReturn<Item>>,
|
|
82
|
+
PaginatedParams
|
|
83
|
+
>,
|
|
84
|
+
options: BasePaginatedOptions<U>,
|
|
85
|
+
): PaginatedResult<Item>;
|
|
86
|
+
function useRequest(service: any, options: any = {}) {
|
|
87
|
+
return useUmiRequest(service, {
|
|
88
|
+
formatResult: {{{formatResult}}},
|
|
89
|
+
requestMethod: (requestOptions: any) => {
|
|
90
|
+
if (typeof requestOptions === 'string') {
|
|
91
|
+
return request(requestOptions);
|
|
92
|
+
}
|
|
93
|
+
if (typeof requestOptions === 'object') {
|
|
94
|
+
const { url, ...rest } = requestOptions;
|
|
95
|
+
return request(url, rest);
|
|
96
|
+
}
|
|
97
|
+
throw new Error('request options error');
|
|
98
|
+
},
|
|
99
|
+
...options,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// request 方法 opts 参数的接口
|
|
104
|
+
interface IRequestOptions extends AxiosRequestConfig {
|
|
105
|
+
skipErrorHandler?: boolean;
|
|
106
|
+
requestInterceptors?: IRequestInterceptorTuple[];
|
|
107
|
+
responseInterceptors?: IResponseInterceptorTuple[];
|
|
108
|
+
[key: string]: any;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
interface IRequestOptionsWithResponse extends IRequestOptions {
|
|
112
|
+
getResponse: true;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
interface IRequestOptionsWithoutResponse extends IRequestOptions{
|
|
116
|
+
getResponse: false;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface IRequest{
|
|
120
|
+
<T = any>(url: string, opts: IRequestOptionsWithResponse): Promise<AxiosResponse<T>>;
|
|
121
|
+
<T = any>(url: string, opts: IRequestOptionsWithoutResponse): Promise<T>;
|
|
122
|
+
<T = any>(url: string, opts: IRequestOptions): Promise<T>; // getResponse 默认是 false, 因此不提供该参数时,只返回 data
|
|
123
|
+
<T = any>(url: string): Promise<T>; // 不提供 opts 时,默认使用 'GET' method,并且默认返回 data
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
interface IErrorHandler {
|
|
127
|
+
(error: RequestError, opts: IRequestOptions): void;
|
|
128
|
+
}
|
|
129
|
+
type IRequestInterceptorAxios = (config: RequestOptions) => RequestOptions;
|
|
130
|
+
type IRequestInterceptorUmiRequest = (url: string, config : RequestOptions) => { url: string, options: RequestOptions };
|
|
131
|
+
type IRequestInterceptor = IRequestInterceptorAxios;
|
|
132
|
+
type IErrorInterceptor = (error: Error) => Promise<Error>;
|
|
133
|
+
type IResponseInterceptor = <T = any>(response : AxiosResponse<T>) => AxiosResponse<T> ;
|
|
134
|
+
type IRequestInterceptorTuple = [IRequestInterceptor , IErrorInterceptor] | [ IRequestInterceptor ] | IRequestInterceptor
|
|
135
|
+
type IResponseInterceptorTuple = [IResponseInterceptor, IErrorInterceptor] | [IResponseInterceptor] | IResponseInterceptor
|
|
136
|
+
|
|
137
|
+
export interface RequestConfig extends AxiosRequestConfig {
|
|
138
|
+
errorConfig?: {
|
|
139
|
+
errorHandler?: IErrorHandler;
|
|
140
|
+
errorThrower?: <T = any>( res: T ) => void
|
|
141
|
+
};
|
|
142
|
+
requestInterceptors?: IRequestInterceptorTuple[];
|
|
143
|
+
responseInterceptors?: IResponseInterceptorTuple[];
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let requestInstance: AxiosInstance;
|
|
147
|
+
let config: RequestConfig;
|
|
148
|
+
const getConfig = (): RequestConfig => {
|
|
149
|
+
if (config) return config;
|
|
150
|
+
config = getPluginManager().applyPlugins({
|
|
151
|
+
key: 'request',
|
|
152
|
+
type: ApplyPluginsType.modify,
|
|
153
|
+
initialValue: {},
|
|
154
|
+
});
|
|
155
|
+
return config;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const getRequestInstance = (): AxiosInstance => {
|
|
159
|
+
if (requestInstance) return requestInstance;
|
|
160
|
+
const config = getConfig();
|
|
161
|
+
requestInstance = axios.create(config);
|
|
162
|
+
|
|
163
|
+
config?.requestInterceptors?.forEach((interceptor) => {
|
|
164
|
+
if(interceptor instanceof Array){
|
|
165
|
+
requestInstance.interceptors.request.use((config) => {
|
|
166
|
+
const { url } = config;
|
|
167
|
+
if(interceptor[0].length === 2){
|
|
168
|
+
const { url: newUrl, options } = interceptor[0](url, config);
|
|
169
|
+
return { ...options, url: newUrl };
|
|
170
|
+
}
|
|
171
|
+
return interceptor[0](config);
|
|
172
|
+
}, interceptor[1]);
|
|
173
|
+
} else {
|
|
174
|
+
requestInstance.interceptors.request.use((config) => {
|
|
175
|
+
const { url } = config;
|
|
176
|
+
if(interceptor.length === 2){
|
|
177
|
+
const { url: newUrl, options } = interceptor(url, config);
|
|
178
|
+
return { ...options, url: newUrl };
|
|
179
|
+
}
|
|
180
|
+
return interceptor(config);
|
|
181
|
+
})
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
config?.responseInterceptors?.forEach((interceptor) => {
|
|
186
|
+
interceptor instanceof Array ?
|
|
187
|
+
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
188
|
+
requestInstance.interceptors.response.use(interceptor);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// 当响应的数据 success 是 false 的时候,抛出 error 以供 errorHandler 处理。
|
|
192
|
+
requestInstance.interceptors.response.use((response)=>{
|
|
193
|
+
const { data } = response;
|
|
194
|
+
if(config?.errorConfig?.errorThrower){
|
|
195
|
+
config.errorConfig.errorThrower(data);
|
|
196
|
+
}
|
|
197
|
+
return response;
|
|
198
|
+
})
|
|
199
|
+
return requestInstance;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
const request: IRequest = (url: string, opts: any = { method: 'GET' }) => {
|
|
203
|
+
const requestInstance = getRequestInstance();
|
|
204
|
+
const config = getConfig();
|
|
205
|
+
const { getResponse = false, requestInterceptors, responseInterceptors } = opts;
|
|
206
|
+
const requestInterceptorsToEject = requestInterceptors?.map((interceptor) => {
|
|
207
|
+
if(interceptor instanceof Array){
|
|
208
|
+
return requestInstance.interceptors.request.use((config) => {
|
|
209
|
+
const { url } = config;
|
|
210
|
+
if(interceptor[0].length === 2){
|
|
211
|
+
const { url: newUrl, options } = interceptor[0](url, config);
|
|
212
|
+
return { ...options, url: newUrl };
|
|
213
|
+
}
|
|
214
|
+
return interceptor[0](config);
|
|
215
|
+
}, interceptor[1]);
|
|
216
|
+
} else {
|
|
217
|
+
return requestInstance.interceptors.request.use((config) => {
|
|
218
|
+
const { url } = config;
|
|
219
|
+
if(interceptor.length === 2){
|
|
220
|
+
const { url: newUrl, options } = interceptor(url, config);
|
|
221
|
+
return { ...options, url: newUrl };
|
|
222
|
+
}
|
|
223
|
+
return interceptor(config);
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
const responseInterceptorsToEject = responseInterceptors?.map((interceptor) => {
|
|
228
|
+
return interceptor instanceof Array ?
|
|
229
|
+
requestInstance.interceptors.response.use(interceptor[0], interceptor[1]):
|
|
230
|
+
requestInstance.interceptors.response.use(interceptor);
|
|
231
|
+
});
|
|
232
|
+
return new Promise((resolve, reject)=>{
|
|
233
|
+
requestInstance
|
|
234
|
+
.request({...opts, url})
|
|
235
|
+
.then((res)=>{
|
|
236
|
+
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
237
|
+
requestInstance.interceptors.request.eject(interceptor);
|
|
238
|
+
});
|
|
239
|
+
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
240
|
+
requestInstance.interceptors.response.eject(interceptor);
|
|
241
|
+
});
|
|
242
|
+
resolve(getResponse ? res : res.data);
|
|
243
|
+
})
|
|
244
|
+
.catch((error)=>{
|
|
245
|
+
requestInterceptorsToEject?.forEach((interceptor) => {
|
|
246
|
+
requestInstance.interceptors.request.eject(interceptor);
|
|
247
|
+
});
|
|
248
|
+
responseInterceptorsToEject?.forEach((interceptor) => {
|
|
249
|
+
requestInstance.interceptors.response.eject(interceptor);
|
|
250
|
+
});
|
|
251
|
+
try {
|
|
252
|
+
const handler =
|
|
253
|
+
config.errorConfig?.errorHandler;
|
|
254
|
+
if(handler)
|
|
255
|
+
handler(error, opts, config);
|
|
256
|
+
} catch (e) {
|
|
257
|
+
reject(e);
|
|
258
|
+
}
|
|
259
|
+
reject(error);
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export {
|
|
265
|
+
useRequest,
|
|
266
|
+
UseRequestProvider,
|
|
267
|
+
request,
|
|
268
|
+
getRequestInstance,
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
export type {
|
|
272
|
+
AxiosInstance,
|
|
273
|
+
AxiosRequestConfig,
|
|
274
|
+
AxiosResponse,
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
`;
|
|
278
|
+
api.onGenerateFiles(() => {
|
|
279
|
+
var _a;
|
|
280
|
+
const umiRequestPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('@ahooksjs/use-request/package.json')));
|
|
281
|
+
const axiosPath = (0, plugin_utils_1.winPath)((0, path_1.dirname)(require.resolve('axios/package.json')));
|
|
282
|
+
let dataField = (_a = api.config.request) === null || _a === void 0 ? void 0 : _a.dataField;
|
|
283
|
+
if (dataField === undefined)
|
|
284
|
+
dataField = 'data';
|
|
285
|
+
const formatResult = dataField === '' ? `result => result` : `result => result?.${dataField}`;
|
|
286
|
+
api.writeTmpFile({
|
|
287
|
+
path: 'request.ts',
|
|
288
|
+
content: plugin_utils_1.Mustache.render(requestTpl, {
|
|
289
|
+
umiRequestPath,
|
|
290
|
+
axiosPath,
|
|
291
|
+
formatResult,
|
|
292
|
+
}),
|
|
293
|
+
});
|
|
294
|
+
api.writeTmpFile({
|
|
295
|
+
path: 'types.d.ts',
|
|
296
|
+
content: `
|
|
297
|
+
export type { RequestConfig } from './request';
|
|
298
|
+
`,
|
|
299
|
+
});
|
|
300
|
+
api.writeTmpFile({
|
|
301
|
+
path: 'index.ts',
|
|
302
|
+
content: `
|
|
303
|
+
export {
|
|
304
|
+
useRequest,
|
|
305
|
+
UseRequestProvider,
|
|
306
|
+
request,
|
|
307
|
+
} from './request';
|
|
308
|
+
`,
|
|
309
|
+
});
|
|
310
|
+
});
|
|
311
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const path_1 = require("path");
|
|
4
|
+
const plugin_utils_1 = require("umi/plugin-utils");
|
|
5
|
+
exports.default = (api) => {
|
|
6
|
+
api.describe({
|
|
7
|
+
key: 'tailwindcss',
|
|
8
|
+
config: {
|
|
9
|
+
schema(Joi) {
|
|
10
|
+
return Joi.object();
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
enableBy: api.EnableBy.config,
|
|
14
|
+
});
|
|
15
|
+
let tailwind = null;
|
|
16
|
+
const outputPath = 'plugin-tailwindcss/tailwind.css';
|
|
17
|
+
api.onBeforeCompiler(() => {
|
|
18
|
+
const inputPath = (0, path_1.join)(api.cwd, 'tailwind.css');
|
|
19
|
+
const generatedPath = (0, path_1.join)(api.paths.absTmpPath, outputPath);
|
|
20
|
+
const binPath = (0, path_1.join)(api.cwd, 'node_modules/.bin/tailwind');
|
|
21
|
+
/** 透过子进程建立 tailwindcss 服务,将生成的 css 写入 generatedPath */
|
|
22
|
+
tailwind = (0, plugin_utils_1.crossSpawn)(`${binPath}`, [
|
|
23
|
+
'-i',
|
|
24
|
+
inputPath,
|
|
25
|
+
'-o',
|
|
26
|
+
generatedPath,
|
|
27
|
+
api.env === 'development' ? '--watch' : '',
|
|
28
|
+
], {
|
|
29
|
+
stdio: 'inherit',
|
|
30
|
+
});
|
|
31
|
+
tailwind.on('error', (m) => {
|
|
32
|
+
api.logger.error('tailwindcss service encounter an error: ' + m);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
/** 将生成的 css 文件加入到 import 中 */
|
|
36
|
+
api.addEntryImports(() => {
|
|
37
|
+
const generatedPath = (0, plugin_utils_1.winPath)((0, path_1.join)(api.paths.absTmpPath, outputPath));
|
|
38
|
+
return [{ source: generatedPath }];
|
|
39
|
+
});
|
|
40
|
+
};
|
package/dist/unocss.d.ts
ADDED
package/dist/unocss.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const child_process_1 = require("child_process");
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
const plugin_utils_1 = require("umi/plugin-utils");
|
|
7
|
+
exports.default = (api) => {
|
|
8
|
+
api.describe({
|
|
9
|
+
key: 'unocss',
|
|
10
|
+
config: {
|
|
11
|
+
schema(Joi) {
|
|
12
|
+
return Joi.object({
|
|
13
|
+
watch: Joi.array(),
|
|
14
|
+
});
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
enableBy: api.EnableBy.config,
|
|
18
|
+
});
|
|
19
|
+
const outputPath = 'uno.css';
|
|
20
|
+
api.onBeforeCompiler(() => {
|
|
21
|
+
/** 由于 @unocss/cli 对设置文件进行了检查,因此加入需要 unocss.config.ts 设置的提示
|
|
22
|
+
* https://github.com/antfu/unocss/blob/main/packages/cli/src/index.ts#L93 */
|
|
23
|
+
if (!(0, fs_1.existsSync)((0, path_1.join)(api.paths.cwd, 'unocss.config.ts')))
|
|
24
|
+
api.logger.warn('请在项目目录中添加 unocss.config.ts 文件,并配置需要的 unocss presets,否则插件将没有效果!');
|
|
25
|
+
const generatedPath = (0, path_1.join)(api.paths.absTmpPath, outputPath);
|
|
26
|
+
const binPath = (0, path_1.join)(api.cwd, 'node_modules/.bin/unocss');
|
|
27
|
+
const watchDirs = api.config.unocss.watch;
|
|
28
|
+
/** 透过子进程建立 unocss 服务,将生成的 css 写入 generatedPath */
|
|
29
|
+
const unocss = (0, child_process_1.exec)(`${binPath} ${watchDirs.join(' ')} --out-file ${generatedPath} ${api.env === 'development' ? '--watch' : ''}`, { cwd: api.cwd });
|
|
30
|
+
unocss.on('error', (m) => {
|
|
31
|
+
api.logger.error('unocss service encounter an error: ' + m);
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
/** 将生成的 css 文件加入到 import 中 */
|
|
35
|
+
api.addEntryImports(() => {
|
|
36
|
+
const generatedPath = (0, plugin_utils_1.winPath)((0, path_1.join)(api.paths.absTmpPath, outputPath));
|
|
37
|
+
return [{ source: generatedPath }];
|
|
38
|
+
});
|
|
39
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
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;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.getIdentifierDeclaration = void 0;
|
|
27
|
+
const t = __importStar(require("@umijs/bundler-utils/compiled/babel/types"));
|
|
28
|
+
function getIdentifierDeclaration(node, path) {
|
|
29
|
+
if (t.isIdentifier(node) && path.scope.hasBinding(node.name)) {
|
|
30
|
+
let bindingNode = path.scope.getBinding(node.name).path.node;
|
|
31
|
+
if (t.isVariableDeclarator(bindingNode)) {
|
|
32
|
+
bindingNode = bindingNode.init;
|
|
33
|
+
}
|
|
34
|
+
return bindingNode;
|
|
35
|
+
}
|
|
36
|
+
return node;
|
|
37
|
+
}
|
|
38
|
+
exports.getIdentifierDeclaration = getIdentifierDeclaration;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export declare type IAddAntdLocales = (args: {
|
|
2
|
+
lang: string;
|
|
3
|
+
country: string;
|
|
4
|
+
}) => Promise<string[]>;
|
|
5
|
+
export interface IGetLocaleFileListOpts {
|
|
6
|
+
localeFolder: string;
|
|
7
|
+
separator?: string;
|
|
8
|
+
absSrcPath?: string;
|
|
9
|
+
absPagesPath?: string;
|
|
10
|
+
addAntdLocales: IAddAntdLocales;
|
|
11
|
+
resolveKey: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* 获取 moment 包的 locale 名称
|
|
15
|
+
* @param lang 语言
|
|
16
|
+
* @param country 国家
|
|
17
|
+
* @param resolveKey 用来resolve的key,moment 或者 dayjs,为了使 dayjs 可以替换 moment
|
|
18
|
+
*/
|
|
19
|
+
export declare const getMomentLocale: (lang: string, country: string, resolveKey: string) => {
|
|
20
|
+
momentLocale: string;
|
|
21
|
+
};
|
|
22
|
+
export declare const getAntdLocale: (lang: string, country: string) => string;
|
|
23
|
+
export interface IGetLocaleFileListResult {
|
|
24
|
+
lang: string;
|
|
25
|
+
country: string;
|
|
26
|
+
name: string;
|
|
27
|
+
paths: string[];
|
|
28
|
+
antdLocale: string[];
|
|
29
|
+
momentLocale: string;
|
|
30
|
+
}
|
|
31
|
+
export declare const getLocaleList: (opts: IGetLocaleFileListOpts) => Promise<IGetLocaleFileListResult[]>;
|
|
32
|
+
export declare const exactLocalePaths: (data: IGetLocaleFileListResult[]) => string[];
|
|
33
|
+
export declare function isNeedPolyfill(targets?: {}): boolean;
|