@tarojs/api 4.0.0-canary.8 → 4.0.0
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 +17 -3
- package/dist/env.d.ts +14 -0
- package/dist/env.js +54 -0
- package/dist/env.js.map +1 -0
- package/dist/index.cjs.js +246 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +193 -297
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +21 -351
- package/dist/index.js.map +1 -1
- package/dist/interceptor/chain.d.ts +16 -0
- package/dist/interceptor/chain.js +30 -0
- package/dist/interceptor/chain.js.map +1 -0
- package/dist/interceptor/index.d.ts +11 -0
- package/dist/interceptor/index.js +30 -0
- package/dist/interceptor/index.js.map +1 -0
- package/dist/interceptor/interceptors.d.ts +3 -0
- package/dist/interceptor/interceptors.js +48 -0
- package/dist/interceptor/interceptors.js.map +1 -0
- package/dist/taro.js +222 -329
- package/dist/taro.js.map +1 -1
- package/dist/tools.d.ts +4 -0
- package/dist/tools.js +67 -0
- package/dist/tools.js.map +1 -0
- package/package.json +24 -24
package/dist/taro.js
CHANGED
|
@@ -1,356 +1,249 @@
|
|
|
1
1
|
(function (global, factory) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
})(this, (function (exports,
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tarojs/runtime'), require('@tarojs/shared')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', '@tarojs/runtime', '@tarojs/shared'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Taro = {}, global.runtime, global.shared));
|
|
5
|
+
})(this, (function (exports, 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);
|
|
27
|
-
};
|
|
28
|
-
function throwTypeError(s) {
|
|
29
|
-
throw new TypeError(s);
|
|
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');
|
|
38
|
-
}
|
|
39
|
-
var to = Object(target);
|
|
40
|
-
for (var index = 1; index < arguments.length; index++) {
|
|
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
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return to;
|
|
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',
|
|
53
19
|
};
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
function convertToDescriptor(desc) {
|
|
58
|
-
function hasProperty(obj, prop) {
|
|
59
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
20
|
+
function getEnv() {
|
|
21
|
+
if (process.env.TARO_ENV === 'weapp') {
|
|
22
|
+
return ENV_TYPE.WEAPP;
|
|
60
23
|
}
|
|
61
|
-
if (
|
|
62
|
-
|
|
24
|
+
else if (process.env.TARO_ENV === 'alipay') {
|
|
25
|
+
return ENV_TYPE.ALIPAY;
|
|
63
26
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (hasProperty(desc, 'configurable')) {
|
|
67
|
-
d.configurable = !!desc.configurable;
|
|
27
|
+
else if (process.env.TARO_ENV === 'swan') {
|
|
28
|
+
return ENV_TYPE.SWAN;
|
|
68
29
|
}
|
|
69
|
-
if (
|
|
70
|
-
|
|
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;
|
|
30
|
+
else if (process.env.TARO_ENV === 'tt') {
|
|
31
|
+
return ENV_TYPE.TT;
|
|
77
32
|
}
|
|
78
|
-
if (
|
|
79
|
-
|
|
80
|
-
if (!isFunction(s) && !isUndefined(s)) {
|
|
81
|
-
throwTypeError('bad set');
|
|
82
|
-
}
|
|
83
|
-
d.set = s;
|
|
33
|
+
else if (process.env.TARO_ENV === 'jd') {
|
|
34
|
+
return ENV_TYPE.JD;
|
|
84
35
|
}
|
|
85
|
-
if (
|
|
86
|
-
|
|
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';
|
|
87
56
|
}
|
|
88
|
-
return d;
|
|
89
|
-
}
|
|
90
|
-
if (isBadObj(obj)) throwTypeError('bad obj');
|
|
91
|
-
properties = Object(properties);
|
|
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]])]);
|
|
96
|
-
}
|
|
97
|
-
for (var _i = 0; _i < descs.length; _i++) {
|
|
98
|
-
Object.defineProperty(obj, descs[_i][0], descs[_i][1]);
|
|
99
|
-
}
|
|
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
57
|
}
|
|
141
|
-
}
|
|
142
58
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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;
|
|
76
|
+
}
|
|
77
|
+
_getNextInterceptor() {
|
|
78
|
+
return this.interceptors[this.index];
|
|
79
|
+
}
|
|
80
|
+
_getNextChain() {
|
|
81
|
+
return new Chain(this.requestParams, this.interceptors, this.index + 1);
|
|
82
|
+
}
|
|
149
83
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
84
|
+
|
|
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));
|
|
97
|
+
}
|
|
98
|
+
addInterceptor(interceptor) {
|
|
99
|
+
this.chain.interceptors.push(interceptor);
|
|
100
|
+
}
|
|
101
|
+
cleanInterceptors() {
|
|
102
|
+
this.chain = new Chain();
|
|
156
103
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return Promise.reject(err);
|
|
104
|
+
}
|
|
105
|
+
function interceptorify(promiseifyApi) {
|
|
106
|
+
return new Link(function (chain) {
|
|
107
|
+
return promiseifyApi(chain.requestParams);
|
|
162
108
|
});
|
|
163
|
-
|
|
164
|
-
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function timeoutInterceptor(chain) {
|
|
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
|
+
});
|
|
165
131
|
});
|
|
132
|
+
// @ts-ignore
|
|
133
|
+
if (!shared.isUndefined(p) && shared.isFunction(p.abort))
|
|
134
|
+
res.abort = p.abort;
|
|
135
|
+
return res;
|
|
136
|
+
}
|
|
137
|
+
function logInterceptor(chain) {
|
|
138
|
+
const requestParams = chain.requestParams;
|
|
139
|
+
const { method, data, url } = requestParams;
|
|
140
|
+
// eslint-disable-next-line no-console
|
|
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;
|
|
166
152
|
return res;
|
|
167
|
-
}
|
|
168
|
-
}, {
|
|
169
|
-
key: "_getNextInterceptor",
|
|
170
|
-
value: function _getNextInterceptor() {
|
|
171
|
-
return this.interceptors[this.index];
|
|
172
|
-
}
|
|
173
|
-
}, {
|
|
174
|
-
key: "_getNextChain",
|
|
175
|
-
value: function _getNextChain() {
|
|
176
|
-
return new Chain(this.requestParams, this.interceptors, this.index + 1);
|
|
177
|
-
}
|
|
178
|
-
}]);
|
|
179
|
-
return Chain;
|
|
180
|
-
}();
|
|
181
|
-
|
|
182
|
-
var Link = /*#__PURE__*/function () {
|
|
183
|
-
function Link(interceptor) {
|
|
184
|
-
_classCallCheck__default["default"](this, Link);
|
|
185
|
-
this.taroInterceptor = interceptor;
|
|
186
|
-
this.chain = new Chain();
|
|
187
153
|
}
|
|
188
|
-
_createClass__default["default"](Link, [{
|
|
189
|
-
key: "request",
|
|
190
|
-
value: function request(requestParams) {
|
|
191
|
-
var chain = this.chain;
|
|
192
|
-
var taroInterceptor = this.taroInterceptor;
|
|
193
|
-
chain.interceptors = chain.interceptors.filter(function (interceptor) {
|
|
194
|
-
return interceptor !== taroInterceptor;
|
|
195
|
-
}).concat(taroInterceptor);
|
|
196
|
-
return chain.proceed(_objectSpread__default["default"]({}, requestParams));
|
|
197
|
-
}
|
|
198
|
-
}, {
|
|
199
|
-
key: "addInterceptor",
|
|
200
|
-
value: function addInterceptor(interceptor) {
|
|
201
|
-
this.chain.interceptors.push(interceptor);
|
|
202
|
-
}
|
|
203
|
-
}, {
|
|
204
|
-
key: "cleanInterceptors",
|
|
205
|
-
value: function cleanInterceptors() {
|
|
206
|
-
this.chain = new Chain();
|
|
207
|
-
}
|
|
208
|
-
}]);
|
|
209
|
-
return Link;
|
|
210
|
-
}();
|
|
211
|
-
function interceptorify(promiseifyApi) {
|
|
212
|
-
return new Link(function (chain) {
|
|
213
|
-
return promiseifyApi(chain.requestParams);
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
function timeoutInterceptor(chain) {
|
|
218
|
-
var requestParams = chain.requestParams;
|
|
219
|
-
var p;
|
|
220
|
-
var res = new Promise(function (resolve, reject) {
|
|
221
|
-
var timeout = setTimeout(function () {
|
|
222
|
-
timeout = null;
|
|
223
|
-
reject(new Error('网络链接超时,请稍后再试!'));
|
|
224
|
-
}, requestParams && requestParams.timeout || 60000);
|
|
225
|
-
p = chain.proceed(requestParams);
|
|
226
|
-
p.then(function (res) {
|
|
227
|
-
if (!timeout) return;
|
|
228
|
-
clearTimeout(timeout);
|
|
229
|
-
resolve(res);
|
|
230
|
-
}).catch(function (err) {
|
|
231
|
-
timeout && clearTimeout(timeout);
|
|
232
|
-
reject(err);
|
|
233
|
-
});
|
|
234
|
-
});
|
|
235
|
-
if (!isUndefined(p) && isFunction(p.abort)) res.abort = p.abort;
|
|
236
|
-
return res;
|
|
237
|
-
}
|
|
238
|
-
function logInterceptor(chain) {
|
|
239
|
-
var requestParams = chain.requestParams;
|
|
240
|
-
var method = requestParams.method,
|
|
241
|
-
data = requestParams.data,
|
|
242
|
-
url = requestParams.url;
|
|
243
154
|
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
// eslint-disable-next-line no-console
|
|
249
|
-
console.log("http <-- ".concat(url, " result:"), res);
|
|
250
|
-
return res;
|
|
155
|
+
var interceptors = /*#__PURE__*/Object.freeze({
|
|
156
|
+
__proto__: null,
|
|
157
|
+
logInterceptor: logInterceptor,
|
|
158
|
+
timeoutInterceptor: timeoutInterceptor
|
|
251
159
|
});
|
|
252
|
-
if (isFunction(p.abort)) res.abort = p.abort;
|
|
253
|
-
return res;
|
|
254
|
-
}
|
|
255
|
-
|
|
256
|
-
var interceptors = /*#__PURE__*/Object.freeze({
|
|
257
|
-
__proto__: null,
|
|
258
|
-
timeoutInterceptor: timeoutInterceptor,
|
|
259
|
-
logInterceptor: logInterceptor
|
|
260
|
-
});
|
|
261
160
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
function getInitPxTransform(taro) {
|
|
280
|
-
return function (config) {
|
|
281
|
-
var _config$designWidth = config.designWidth,
|
|
282
|
-
designWidth = _config$designWidth === void 0 ? defaultDesignWidth : _config$designWidth,
|
|
283
|
-
_config$deviceRatio = config.deviceRatio,
|
|
284
|
-
deviceRatio = _config$deviceRatio === void 0 ? defaultDesignRatio : _config$deviceRatio,
|
|
285
|
-
_config$baseFontSize = config.baseFontSize,
|
|
286
|
-
baseFontSize = _config$baseFontSize === void 0 ? defaultBaseFontSize : _config$baseFontSize,
|
|
287
|
-
_config$targetUnit = config.targetUnit,
|
|
288
|
-
targetUnit = _config$targetUnit === void 0 ? defaultTargetUnit : _config$targetUnit,
|
|
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
|
-
};
|
|
298
|
-
}
|
|
299
|
-
function getPxTransform(taro) {
|
|
300
|
-
return function (size) {
|
|
301
|
-
var config = taro.config || {};
|
|
302
|
-
var baseFontSize = config.baseFontSize;
|
|
303
|
-
var deviceRatio = config.deviceRatio || defaultDesignRatio;
|
|
304
|
-
var designWidth = function () {
|
|
305
|
-
var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
|
|
306
|
-
return isFunction(config.designWidth) ? config.designWidth(input) : config.designWidth || defaultDesignWidth;
|
|
307
|
-
}(size);
|
|
308
|
-
if (!(designWidth in deviceRatio)) {
|
|
309
|
-
throw new Error("deviceRatio \u914D\u7F6E\u4E2D\u4E0D\u5B58\u5728 ".concat(designWidth, " \u7684\u8BBE\u7F6E\uFF01"));
|
|
310
|
-
}
|
|
311
|
-
var targetUnit = config.targetUnit || defaultTargetUnit;
|
|
312
|
-
var unitPrecision = config.unitPrecision || defaultUnitPrecision;
|
|
313
|
-
var formatSize = ~~size;
|
|
314
|
-
var rootValue = 1 / deviceRatio[designWidth];
|
|
315
|
-
switch (targetUnit) {
|
|
316
|
-
case 'rem':
|
|
317
|
-
rootValue *= baseFontSize * 2;
|
|
318
|
-
break;
|
|
319
|
-
case 'px':
|
|
320
|
-
rootValue *= 2;
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
var val = formatSize / rootValue;
|
|
324
|
-
if (unitPrecision >= 0 && unitPrecision <= 100) {
|
|
325
|
-
val = Number(val.toFixed(unitPrecision));
|
|
326
|
-
}
|
|
327
|
-
return val + targetUnit;
|
|
161
|
+
function Behavior(options) {
|
|
162
|
+
return options;
|
|
163
|
+
}
|
|
164
|
+
function getPreload(current) {
|
|
165
|
+
return function (key, val) {
|
|
166
|
+
current.preloadData = shared.isObject(key)
|
|
167
|
+
? key
|
|
168
|
+
: {
|
|
169
|
+
[key]: val
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const defaultDesignWidth = 750;
|
|
174
|
+
const defaultDesignRatio = {
|
|
175
|
+
640: 2.34 / 2,
|
|
176
|
+
750: 1,
|
|
177
|
+
828: 1.81 / 2
|
|
328
178
|
};
|
|
329
|
-
|
|
179
|
+
const defaultBaseFontSize = 20;
|
|
180
|
+
const defaultUnitPrecision = 5;
|
|
181
|
+
const defaultTargetUnit = 'rpx';
|
|
182
|
+
function getInitPxTransform(taro) {
|
|
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
|
+
};
|
|
192
|
+
}
|
|
193
|
+
function getPxTransform(taro) {
|
|
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
|
+
};
|
|
222
|
+
}
|
|
330
223
|
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
224
|
+
/* eslint-disable camelcase */
|
|
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
|
|
239
|
+
};
|
|
240
|
+
Taro.initPxTransform = getInitPxTransform(Taro);
|
|
241
|
+
Taro.preload = getPreload(runtime.Current);
|
|
242
|
+
Taro.pxTransform = getPxTransform(Taro);
|
|
350
243
|
|
|
351
|
-
|
|
244
|
+
exports.default = Taro;
|
|
352
245
|
|
|
353
|
-
|
|
246
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
354
247
|
|
|
355
248
|
}));
|
|
356
249
|
//# sourceMappingURL=taro.js.map
|