@tarojs/api 4.0.0-canary.8 → 4.0.0-canary.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/LICENSE +14 -0
- package/dist/env.d.ts +15 -0
- package/dist/env.js +53 -0
- package/dist/index.cjs.d.ts +2 -0
- package/dist/index.cjs.js +243 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.d.ts +2 -0
- package/dist/index.esm.js +192 -297
- package/dist/index.js +21 -352
- package/dist/interceptor/chain.d.ts +17 -0
- package/dist/interceptor/chain.js +29 -0
- package/dist/interceptor/index.d.ts +12 -0
- package/dist/interceptor/index.js +29 -0
- package/dist/interceptor/interceptors.d.ts +4 -0
- package/dist/interceptor/interceptors.js +47 -0
- package/dist/taro.d.ts +2 -0
- package/dist/taro.js +196 -306
- package/dist/tools.d.ts +5 -0
- package/dist/tools.js +66 -0
- package/package.json +16 -10
- package/dist/index.esm.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/taro.js.map +0 -1
package/dist/taro.js
CHANGED
|
@@ -1,356 +1,246 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ?
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self,
|
|
5
|
-
})(this, (function (
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@tarojs/runtime'), require('@tarojs/shared')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['@tarojs/runtime', '@tarojs/shared'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Taro = factory(global.runtime, global.shared));
|
|
5
|
+
})(this, (function (runtime, shared) { 'use strict';
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
return typeof x === 'undefined';
|
|
20
|
-
}
|
|
21
|
-
function isObject(x) {
|
|
22
|
-
return x && _typeof__default["default"](x) === 'object';
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
var isBadObj = function isBadObj(x) {
|
|
26
|
-
return !isObject(x);
|
|
7
|
+
const ENV_TYPE = {
|
|
8
|
+
WEAPP: 'WEAPP',
|
|
9
|
+
SWAN: 'SWAN',
|
|
10
|
+
ALIPAY: 'ALIPAY',
|
|
11
|
+
TT: 'TT',
|
|
12
|
+
QQ: 'QQ',
|
|
13
|
+
JD: 'JD',
|
|
14
|
+
WEB: 'WEB',
|
|
15
|
+
RN: 'RN',
|
|
16
|
+
HARMONY: 'HARMONY',
|
|
17
|
+
QUICKAPP: 'QUICKAPP',
|
|
18
|
+
HARMONYHYBRID: 'HARMONYHYBRID',
|
|
27
19
|
};
|
|
28
|
-
function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (!isFunction(Object.assign)) {
|
|
32
|
-
// Must be writable: true, enumerable: false, configurable: true
|
|
33
|
-
Object.assign = function (target) {
|
|
34
|
-
// .length of function is 2
|
|
35
|
-
if (target == null) {
|
|
36
|
-
// TypeError if undefined or null
|
|
37
|
-
throwTypeError('Cannot convert undefined or null to object');
|
|
20
|
+
function getEnv() {
|
|
21
|
+
if (process.env.TARO_ENV === 'weapp') {
|
|
22
|
+
return ENV_TYPE.WEAPP;
|
|
38
23
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var nextSource = arguments[index];
|
|
42
|
-
if (nextSource != null) {
|
|
43
|
-
// Skip over if undefined or null
|
|
44
|
-
for (var nextKey in nextSource) {
|
|
45
|
-
// Avoid bugs when hasOwnProperty is shadowed
|
|
46
|
-
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
|
47
|
-
to[nextKey] = nextSource[nextKey];
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
24
|
+
else if (process.env.TARO_ENV === 'alipay') {
|
|
25
|
+
return ENV_TYPE.ALIPAY;
|
|
51
26
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
if (!isFunction(Object.defineProperties)) {
|
|
56
|
-
Object.defineProperties = function (obj, properties) {
|
|
57
|
-
function convertToDescriptor(desc) {
|
|
58
|
-
function hasProperty(obj, prop) {
|
|
59
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
60
|
-
}
|
|
61
|
-
if (isBadObj(desc)) {
|
|
62
|
-
throwTypeError('bad desc');
|
|
63
|
-
}
|
|
64
|
-
var d = {};
|
|
65
|
-
if (hasProperty(desc, 'enumerable')) d.enumerable = !!desc.enumerable;
|
|
66
|
-
if (hasProperty(desc, 'configurable')) {
|
|
67
|
-
d.configurable = !!desc.configurable;
|
|
68
|
-
}
|
|
69
|
-
if (hasProperty(desc, 'value')) d.value = desc.value;
|
|
70
|
-
if (hasProperty(desc, 'writable')) d.writable = !!desc.writable;
|
|
71
|
-
if (hasProperty(desc, 'get')) {
|
|
72
|
-
var g = desc.get;
|
|
73
|
-
if (!isFunction(g) && !isUndefined(g)) {
|
|
74
|
-
throwTypeError('bad get');
|
|
75
|
-
}
|
|
76
|
-
d.get = g;
|
|
77
|
-
}
|
|
78
|
-
if (hasProperty(desc, 'set')) {
|
|
79
|
-
var s = desc.set;
|
|
80
|
-
if (!isFunction(s) && !isUndefined(s)) {
|
|
81
|
-
throwTypeError('bad set');
|
|
82
|
-
}
|
|
83
|
-
d.set = s;
|
|
84
|
-
}
|
|
85
|
-
if (('get' in d || 'set' in d) && ('value' in d || 'writable' in d)) {
|
|
86
|
-
throwTypeError('identity-confused descriptor');
|
|
87
|
-
}
|
|
88
|
-
return d;
|
|
27
|
+
else if (process.env.TARO_ENV === 'swan') {
|
|
28
|
+
return ENV_TYPE.SWAN;
|
|
89
29
|
}
|
|
90
|
-
if (
|
|
91
|
-
|
|
92
|
-
var keys = Object.keys(properties);
|
|
93
|
-
var descs = [];
|
|
94
|
-
for (var i = 0; i < keys.length; i++) {
|
|
95
|
-
descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
|
|
30
|
+
else if (process.env.TARO_ENV === 'tt') {
|
|
31
|
+
return ENV_TYPE.TT;
|
|
96
32
|
}
|
|
97
|
-
|
|
98
|
-
|
|
33
|
+
else if (process.env.TARO_ENV === 'jd') {
|
|
34
|
+
return ENV_TYPE.JD;
|
|
35
|
+
}
|
|
36
|
+
else if (process.env.TARO_ENV === 'qq') {
|
|
37
|
+
return ENV_TYPE.QQ;
|
|
38
|
+
}
|
|
39
|
+
else if (process.env.TARO_ENV === 'harmony-hybrid') {
|
|
40
|
+
return ENV_TYPE.HARMONYHYBRID;
|
|
41
|
+
}
|
|
42
|
+
else if (process.env.TARO_ENV === 'h5' || process.env.TARO_PLATFORM === 'web') {
|
|
43
|
+
return ENV_TYPE.WEB;
|
|
44
|
+
}
|
|
45
|
+
else if (process.env.TARO_ENV === 'rn') {
|
|
46
|
+
return ENV_TYPE.RN;
|
|
47
|
+
}
|
|
48
|
+
else if (process.env.TARO_ENV === 'harmony' || process.env.TARO_PLATFORM === 'harmony') {
|
|
49
|
+
return ENV_TYPE.HARMONY;
|
|
50
|
+
}
|
|
51
|
+
else if (process.env.TARO_ENV === 'quickapp') {
|
|
52
|
+
return ENV_TYPE.QUICKAPP;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return process.env.TARO_ENV || 'Unknown';
|
|
99
56
|
}
|
|
100
|
-
return obj;
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
var ENV_TYPE = {
|
|
105
|
-
WEAPP: 'WEAPP',
|
|
106
|
-
SWAN: 'SWAN',
|
|
107
|
-
ALIPAY: 'ALIPAY',
|
|
108
|
-
TT: 'TT',
|
|
109
|
-
QQ: 'QQ',
|
|
110
|
-
JD: 'JD',
|
|
111
|
-
WEB: 'WEB',
|
|
112
|
-
RN: 'RN',
|
|
113
|
-
HARMONY: 'HARMONY',
|
|
114
|
-
QUICKAPP: 'QUICKAPP'
|
|
115
|
-
};
|
|
116
|
-
var isWeb = shared.isWebPlatform();
|
|
117
|
-
function getEnv() {
|
|
118
|
-
if (process.env.TARO_ENV === 'weapp') {
|
|
119
|
-
return ENV_TYPE.WEAPP;
|
|
120
|
-
} else if (process.env.TARO_ENV === 'alipay') {
|
|
121
|
-
return ENV_TYPE.ALIPAY;
|
|
122
|
-
} else if (process.env.TARO_ENV === 'swan') {
|
|
123
|
-
return ENV_TYPE.SWAN;
|
|
124
|
-
} else if (process.env.TARO_ENV === 'tt') {
|
|
125
|
-
return ENV_TYPE.TT;
|
|
126
|
-
} else if (process.env.TARO_ENV === 'jd') {
|
|
127
|
-
return ENV_TYPE.JD;
|
|
128
|
-
} else if (process.env.TARO_ENV === 'qq') {
|
|
129
|
-
return ENV_TYPE.QQ;
|
|
130
|
-
} else if (isWeb) {
|
|
131
|
-
return ENV_TYPE.WEB;
|
|
132
|
-
} else if (process.env.TARO_ENV === 'rn') {
|
|
133
|
-
return ENV_TYPE.RN;
|
|
134
|
-
} else if (process.env.TARO_ENV === 'harmony') {
|
|
135
|
-
return ENV_TYPE.HARMONY;
|
|
136
|
-
} else if (process.env.TARO_ENV === 'quickapp') {
|
|
137
|
-
return ENV_TYPE.QUICKAPP;
|
|
138
|
-
} else {
|
|
139
|
-
return process.env.TARO_ENV || 'Unknown';
|
|
140
|
-
}
|
|
141
57
|
}
|
|
142
58
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
var res = p.catch(function (err) {
|
|
161
|
-
return Promise.reject(err);
|
|
162
|
-
});
|
|
163
|
-
Object.keys(p).forEach(function (k) {
|
|
164
|
-
return isFunction(p[k]) && (res[k] = p[k]);
|
|
165
|
-
});
|
|
166
|
-
return res;
|
|
59
|
+
class Chain {
|
|
60
|
+
constructor(requestParams, interceptors, index) {
|
|
61
|
+
this.index = index || 0;
|
|
62
|
+
this.requestParams = requestParams || {};
|
|
63
|
+
this.interceptors = interceptors || [];
|
|
64
|
+
}
|
|
65
|
+
proceed(requestParams = {}) {
|
|
66
|
+
this.requestParams = requestParams;
|
|
67
|
+
if (this.index >= this.interceptors.length) {
|
|
68
|
+
throw new Error('chain 参数错误, 请勿直接修改 request.chain');
|
|
69
|
+
}
|
|
70
|
+
const nextInterceptor = this._getNextInterceptor();
|
|
71
|
+
const nextChain = this._getNextChain();
|
|
72
|
+
const p = nextInterceptor(nextChain);
|
|
73
|
+
const res = p.catch(err => Promise.reject(err));
|
|
74
|
+
Object.keys(p).forEach(k => shared.isFunction(p[k]) && (res[k] = p[k]));
|
|
75
|
+
return res;
|
|
167
76
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
value: function _getNextInterceptor() {
|
|
171
|
-
return this.interceptors[this.index];
|
|
77
|
+
_getNextInterceptor() {
|
|
78
|
+
return this.interceptors[this.index];
|
|
172
79
|
}
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
value: function _getNextChain() {
|
|
176
|
-
return new Chain(this.requestParams, this.interceptors, this.index + 1);
|
|
80
|
+
_getNextChain() {
|
|
81
|
+
return new Chain(this.requestParams, this.interceptors, this.index + 1);
|
|
177
82
|
}
|
|
178
|
-
|
|
179
|
-
return Chain;
|
|
180
|
-
}();
|
|
83
|
+
}
|
|
181
84
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
return interceptor !== taroInterceptor;
|
|
195
|
-
}).concat(taroInterceptor);
|
|
196
|
-
return chain.proceed(_objectSpread__default["default"]({}, requestParams));
|
|
85
|
+
class Link {
|
|
86
|
+
constructor(interceptor) {
|
|
87
|
+
this.taroInterceptor = interceptor;
|
|
88
|
+
this.chain = new Chain();
|
|
89
|
+
}
|
|
90
|
+
request(requestParams) {
|
|
91
|
+
const chain = this.chain;
|
|
92
|
+
const taroInterceptor = this.taroInterceptor;
|
|
93
|
+
chain.interceptors = chain.interceptors
|
|
94
|
+
.filter(interceptor => interceptor !== taroInterceptor)
|
|
95
|
+
.concat(taroInterceptor);
|
|
96
|
+
return chain.proceed(Object.assign({}, requestParams));
|
|
197
97
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
value: function addInterceptor(interceptor) {
|
|
201
|
-
this.chain.interceptors.push(interceptor);
|
|
98
|
+
addInterceptor(interceptor) {
|
|
99
|
+
this.chain.interceptors.push(interceptor);
|
|
202
100
|
}
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
value: function cleanInterceptors() {
|
|
206
|
-
this.chain = new Chain();
|
|
101
|
+
cleanInterceptors() {
|
|
102
|
+
this.chain = new Chain();
|
|
207
103
|
}
|
|
208
|
-
|
|
209
|
-
return Link;
|
|
210
|
-
}();
|
|
104
|
+
}
|
|
211
105
|
function interceptorify(promiseifyApi) {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
106
|
+
return new Link(function (chain) {
|
|
107
|
+
return promiseifyApi(chain.requestParams);
|
|
108
|
+
});
|
|
215
109
|
}
|
|
216
110
|
|
|
217
111
|
function timeoutInterceptor(chain) {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
112
|
+
const requestParams = chain.requestParams;
|
|
113
|
+
let p;
|
|
114
|
+
const res = new Promise((resolve, reject) => {
|
|
115
|
+
const timeout = setTimeout(() => {
|
|
116
|
+
clearTimeout(timeout);
|
|
117
|
+
reject(new Error('网络链接超时,请稍后再试!'));
|
|
118
|
+
}, (requestParams && requestParams.timeout) || 60000);
|
|
119
|
+
p = chain.proceed(requestParams);
|
|
120
|
+
p
|
|
121
|
+
.then(res => {
|
|
122
|
+
if (!timeout)
|
|
123
|
+
return;
|
|
124
|
+
clearTimeout(timeout);
|
|
125
|
+
resolve(res);
|
|
126
|
+
})
|
|
127
|
+
.catch(err => {
|
|
128
|
+
timeout && clearTimeout(timeout);
|
|
129
|
+
reject(err);
|
|
130
|
+
});
|
|
233
131
|
});
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
if (!shared.isUndefined(p) && shared.isFunction(p.abort))
|
|
134
|
+
res.abort = p.abort;
|
|
135
|
+
return res;
|
|
237
136
|
}
|
|
238
137
|
function logInterceptor(chain) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
data = requestParams.data,
|
|
242
|
-
url = requestParams.url;
|
|
243
|
-
|
|
244
|
-
// eslint-disable-next-line no-console
|
|
245
|
-
console.log("http ".concat(method || 'GET', " --> ").concat(url, " data: "), data);
|
|
246
|
-
var p = chain.proceed(requestParams);
|
|
247
|
-
var res = p.then(function (res) {
|
|
138
|
+
const requestParams = chain.requestParams;
|
|
139
|
+
const { method, data, url } = requestParams;
|
|
248
140
|
// eslint-disable-next-line no-console
|
|
249
|
-
console.log(
|
|
141
|
+
console.log(`http ${method || 'GET'} --> ${url} data: `, data);
|
|
142
|
+
const p = chain.proceed(requestParams);
|
|
143
|
+
const res = p
|
|
144
|
+
.then(res => {
|
|
145
|
+
// eslint-disable-next-line no-console
|
|
146
|
+
console.log(`http <-- ${url} result:`, res);
|
|
147
|
+
return res;
|
|
148
|
+
});
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
if (shared.isFunction(p.abort))
|
|
151
|
+
res.abort = p.abort;
|
|
250
152
|
return res;
|
|
251
|
-
});
|
|
252
|
-
if (isFunction(p.abort)) res.abort = p.abort;
|
|
253
|
-
return res;
|
|
254
153
|
}
|
|
255
154
|
|
|
256
155
|
var interceptors = /*#__PURE__*/Object.freeze({
|
|
257
156
|
__proto__: null,
|
|
258
|
-
|
|
259
|
-
|
|
157
|
+
logInterceptor: logInterceptor,
|
|
158
|
+
timeoutInterceptor: timeoutInterceptor
|
|
260
159
|
});
|
|
261
160
|
|
|
262
161
|
function Behavior(options) {
|
|
263
|
-
|
|
162
|
+
return options;
|
|
264
163
|
}
|
|
265
164
|
function getPreload(current) {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
165
|
+
return function (key, val) {
|
|
166
|
+
current.preloadData = shared.isObject(key)
|
|
167
|
+
? key
|
|
168
|
+
: {
|
|
169
|
+
[key]: val
|
|
170
|
+
};
|
|
171
|
+
};
|
|
269
172
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
173
|
+
const defaultDesignWidth = 750;
|
|
174
|
+
const defaultDesignRatio = {
|
|
175
|
+
640: 2.34 / 2,
|
|
176
|
+
750: 1,
|
|
177
|
+
828: 1.81 / 2
|
|
275
178
|
};
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
179
|
+
const defaultBaseFontSize = 20;
|
|
180
|
+
const defaultUnitPrecision = 5;
|
|
181
|
+
const defaultTargetUnit = 'rpx';
|
|
279
182
|
function getInitPxTransform(taro) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
_config$unitPrecision = config.unitPrecision,
|
|
290
|
-
unitPrecision = _config$unitPrecision === void 0 ? defaultUnitPrecision : _config$unitPrecision;
|
|
291
|
-
taro.config = taro.config || {};
|
|
292
|
-
taro.config.designWidth = designWidth;
|
|
293
|
-
taro.config.deviceRatio = deviceRatio;
|
|
294
|
-
taro.config.baseFontSize = baseFontSize;
|
|
295
|
-
taro.config.targetUnit = targetUnit;
|
|
296
|
-
taro.config.unitPrecision = unitPrecision;
|
|
297
|
-
};
|
|
183
|
+
return function (config) {
|
|
184
|
+
const { designWidth = defaultDesignWidth, deviceRatio = defaultDesignRatio, baseFontSize = defaultBaseFontSize, targetUnit = defaultTargetUnit, unitPrecision = defaultUnitPrecision, } = config;
|
|
185
|
+
taro.config = taro.config || {};
|
|
186
|
+
taro.config.designWidth = designWidth;
|
|
187
|
+
taro.config.deviceRatio = deviceRatio;
|
|
188
|
+
taro.config.baseFontSize = baseFontSize;
|
|
189
|
+
taro.config.targetUnit = targetUnit;
|
|
190
|
+
taro.config.unitPrecision = unitPrecision;
|
|
191
|
+
};
|
|
298
192
|
}
|
|
299
193
|
function getPxTransform(taro) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
};
|
|
194
|
+
return function (size) {
|
|
195
|
+
const config = taro.config || {};
|
|
196
|
+
const baseFontSize = config.baseFontSize;
|
|
197
|
+
const deviceRatio = config.deviceRatio || defaultDesignRatio;
|
|
198
|
+
const designWidth = (((input = 0) => shared.isFunction(config.designWidth)
|
|
199
|
+
? config.designWidth(input)
|
|
200
|
+
: config.designWidth || defaultDesignWidth))(size);
|
|
201
|
+
if (!(designWidth in deviceRatio)) {
|
|
202
|
+
throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
|
|
203
|
+
}
|
|
204
|
+
const targetUnit = config.targetUnit || defaultTargetUnit;
|
|
205
|
+
const unitPrecision = config.unitPrecision || defaultUnitPrecision;
|
|
206
|
+
const formatSize = ~~size;
|
|
207
|
+
let rootValue = 1 / deviceRatio[designWidth];
|
|
208
|
+
switch (targetUnit) {
|
|
209
|
+
case 'rem':
|
|
210
|
+
rootValue *= baseFontSize * 2;
|
|
211
|
+
break;
|
|
212
|
+
case 'px':
|
|
213
|
+
rootValue *= 2;
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
let val = formatSize / rootValue;
|
|
217
|
+
if (unitPrecision >= 0 && unitPrecision <= 100) {
|
|
218
|
+
val = Number(val.toFixed(unitPrecision));
|
|
219
|
+
}
|
|
220
|
+
return val + targetUnit;
|
|
221
|
+
};
|
|
329
222
|
}
|
|
330
223
|
|
|
331
224
|
/* eslint-disable camelcase */
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
225
|
+
const Taro = {
|
|
226
|
+
Behavior,
|
|
227
|
+
getEnv,
|
|
228
|
+
ENV_TYPE,
|
|
229
|
+
Link,
|
|
230
|
+
interceptors,
|
|
231
|
+
Current: runtime.Current,
|
|
232
|
+
getCurrentInstance: runtime.getCurrentInstance,
|
|
233
|
+
options: runtime.options,
|
|
234
|
+
nextTick: runtime.nextTick,
|
|
235
|
+
eventCenter: runtime.eventCenter,
|
|
236
|
+
Events: runtime.Events,
|
|
237
|
+
getInitPxTransform,
|
|
238
|
+
interceptorify
|
|
346
239
|
};
|
|
347
240
|
Taro.initPxTransform = getInitPxTransform(Taro);
|
|
348
241
|
Taro.preload = getPreload(runtime.Current);
|
|
349
242
|
Taro.pxTransform = getPxTransform(Taro);
|
|
350
243
|
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
244
|
+
return Taro;
|
|
354
245
|
|
|
355
246
|
}));
|
|
356
|
-
//# sourceMappingURL=taro.js.map
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare function Behavior(options: any): any;
|
|
2
|
+
declare function getPreload(current: any): (key: string | Record<string, unknown>, val: unknown) => void;
|
|
3
|
+
declare function getInitPxTransform(taro: any): (config: any) => void;
|
|
4
|
+
declare function getPxTransform(taro: any): (size: any) => any;
|
|
5
|
+
export { Behavior, getPreload, getInitPxTransform, getPxTransform };
|
package/dist/tools.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { isObject, isFunction } from '@tarojs/shared';
|
|
2
|
+
|
|
3
|
+
function Behavior(options) {
|
|
4
|
+
return options;
|
|
5
|
+
}
|
|
6
|
+
function getPreload(current) {
|
|
7
|
+
return function (key, val) {
|
|
8
|
+
current.preloadData = isObject(key)
|
|
9
|
+
? key
|
|
10
|
+
: {
|
|
11
|
+
[key]: val
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const defaultDesignWidth = 750;
|
|
16
|
+
const defaultDesignRatio = {
|
|
17
|
+
640: 2.34 / 2,
|
|
18
|
+
750: 1,
|
|
19
|
+
828: 1.81 / 2
|
|
20
|
+
};
|
|
21
|
+
const defaultBaseFontSize = 20;
|
|
22
|
+
const defaultUnitPrecision = 5;
|
|
23
|
+
const defaultTargetUnit = 'rpx';
|
|
24
|
+
function getInitPxTransform(taro) {
|
|
25
|
+
return function (config) {
|
|
26
|
+
const { designWidth = defaultDesignWidth, deviceRatio = defaultDesignRatio, baseFontSize = defaultBaseFontSize, targetUnit = defaultTargetUnit, unitPrecision = defaultUnitPrecision, } = config;
|
|
27
|
+
taro.config = taro.config || {};
|
|
28
|
+
taro.config.designWidth = designWidth;
|
|
29
|
+
taro.config.deviceRatio = deviceRatio;
|
|
30
|
+
taro.config.baseFontSize = baseFontSize;
|
|
31
|
+
taro.config.targetUnit = targetUnit;
|
|
32
|
+
taro.config.unitPrecision = unitPrecision;
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function getPxTransform(taro) {
|
|
36
|
+
return function (size) {
|
|
37
|
+
const config = taro.config || {};
|
|
38
|
+
const baseFontSize = config.baseFontSize;
|
|
39
|
+
const deviceRatio = config.deviceRatio || defaultDesignRatio;
|
|
40
|
+
const designWidth = (((input = 0) => isFunction(config.designWidth)
|
|
41
|
+
? config.designWidth(input)
|
|
42
|
+
: config.designWidth || defaultDesignWidth))(size);
|
|
43
|
+
if (!(designWidth in deviceRatio)) {
|
|
44
|
+
throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
|
|
45
|
+
}
|
|
46
|
+
const targetUnit = config.targetUnit || defaultTargetUnit;
|
|
47
|
+
const unitPrecision = config.unitPrecision || defaultUnitPrecision;
|
|
48
|
+
const formatSize = ~~size;
|
|
49
|
+
let rootValue = 1 / deviceRatio[designWidth];
|
|
50
|
+
switch (targetUnit) {
|
|
51
|
+
case 'rem':
|
|
52
|
+
rootValue *= baseFontSize * 2;
|
|
53
|
+
break;
|
|
54
|
+
case 'px':
|
|
55
|
+
rootValue *= 2;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
let val = formatSize / rootValue;
|
|
59
|
+
if (unitPrecision >= 0 && unitPrecision <= 100) {
|
|
60
|
+
val = Number(val.toFixed(unitPrecision));
|
|
61
|
+
}
|
|
62
|
+
return val + targetUnit;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { Behavior, getInitPxTransform, getPreload, getPxTransform };
|