@tarojs/plugin-inject 0.0.2 → 1.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/README.md +146 -5
- package/dist/apis-list.js +3 -6
- package/dist/components.js +2 -5
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime.js +0 -2
- package/dist/runtime.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +28 -8
- package/src/runtime.ts +1 -4
package/README.md
CHANGED
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
|
|
3
3
|
> 可以为小程序平台注入公共的组件、API 等逻辑
|
|
4
4
|
|
|
5
|
+
## 版本要求
|
|
6
|
+
|
|
7
|
+
### Taro 3.3+
|
|
8
|
+
|
|
9
|
+
请使用本插件的 `1.0` 或以上版本
|
|
10
|
+
|
|
11
|
+
### Taro 3.1/3.2
|
|
12
|
+
|
|
13
|
+
请使用本插件的 `0.0.2` 或以上版本
|
|
14
|
+
|
|
5
15
|
## 安装
|
|
6
16
|
|
|
7
17
|
在 Taro 项目根目录下安装
|
|
@@ -39,10 +49,15 @@ const config = {
|
|
|
39
49
|
| syncApis | array | 新增同步 API |
|
|
40
50
|
| asyncApis | array | 新增异步 API |
|
|
41
51
|
| components | object | 修改、新增组件的属性 |
|
|
52
|
+
| componentsMap | object | 新增组件时的名称映射 |
|
|
42
53
|
| voidComponents | array, function | 设置组件是否可以渲染子元素 |
|
|
43
54
|
| nestElements | object, function | 设置组件模版的循环次数 |
|
|
55
|
+
| thirdPartyComponents | object | 设置第三方自定义组件的属性的默认值 |
|
|
56
|
+
|
|
57
|
+
#### 1. syncApis
|
|
44
58
|
|
|
45
|
-
|
|
59
|
+
> Deprecated
|
|
60
|
+
> v1.0.0+ 不再需要此属性
|
|
46
61
|
|
|
47
62
|
插件支持为小程序新增**同步的** API。
|
|
48
63
|
|
|
@@ -65,7 +80,7 @@ const config = {
|
|
|
65
80
|
Taro.a()
|
|
66
81
|
```
|
|
67
82
|
|
|
68
|
-
|
|
83
|
+
#### 2. asyncApis
|
|
69
84
|
|
|
70
85
|
插件支持为小程序新增**异步的** API。
|
|
71
86
|
|
|
@@ -90,7 +105,7 @@ Taro.b()
|
|
|
90
105
|
.catch(() => {})
|
|
91
106
|
```
|
|
92
107
|
|
|
93
|
-
|
|
108
|
+
#### 3. components
|
|
94
109
|
|
|
95
110
|
插件支持为小程序的组件**修改属性默认值**或**新增属性**。
|
|
96
111
|
|
|
@@ -107,14 +122,48 @@ const config = {
|
|
|
107
122
|
Text: {
|
|
108
123
|
'x-props': "'hello'",
|
|
109
124
|
bindYEvent: ''
|
|
125
|
+
},
|
|
126
|
+
// 新增一个组件
|
|
127
|
+
ShareElement: {
|
|
128
|
+
key: "",
|
|
129
|
+
transform: "true",
|
|
130
|
+
duration: "300",
|
|
131
|
+
"easing-function": ""
|
|
110
132
|
}
|
|
133
|
+
},
|
|
134
|
+
// 新增的组件需要写映射
|
|
135
|
+
componentsMap: {
|
|
136
|
+
ShareElement: 'share-element'
|
|
111
137
|
}
|
|
112
138
|
}]
|
|
113
139
|
]
|
|
114
140
|
}
|
|
115
141
|
```
|
|
116
142
|
|
|
117
|
-
|
|
143
|
+
新增事件属性:
|
|
144
|
+
> 新增一个事件属性有两种方式,一种是像上面例子那样属性以`bind`开头,一种是像下面例子这样将属性值设为`eh` [查看详细PR](https://github.com/NervJS/taro/pull/11478)
|
|
145
|
+
```js
|
|
146
|
+
const config = {
|
|
147
|
+
plugins: [
|
|
148
|
+
[
|
|
149
|
+
'@tarojs/plugin-inject',
|
|
150
|
+
{
|
|
151
|
+
components: {
|
|
152
|
+
// 新增一个 'CustomComponent' 组件并支持 'catchtouchend' 事件
|
|
153
|
+
CustomComponent: {
|
|
154
|
+
catchtouchend: 'eh',
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
componentsMap: {
|
|
158
|
+
CustomComponent: 'custom-component',
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
],
|
|
162
|
+
],
|
|
163
|
+
}
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### 4. voidComponents
|
|
118
167
|
|
|
119
168
|
在 `voidComponents` 里的组件**不可以渲染子组件**。
|
|
120
169
|
|
|
@@ -156,7 +205,7 @@ const config = {
|
|
|
156
205
|
}
|
|
157
206
|
```
|
|
158
207
|
|
|
159
|
-
|
|
208
|
+
#### 5. nestElements
|
|
160
209
|
|
|
161
210
|
对于不支持模板递归的小程序(如微信、QQ、京东小程序),Taro3 默认下述组件的模板能递归自身:
|
|
162
211
|
|
|
@@ -205,3 +254,95 @@ const config = {
|
|
|
205
254
|
]
|
|
206
255
|
}
|
|
207
256
|
```
|
|
257
|
+
|
|
258
|
+
#### 6. thirdPartyComponents
|
|
259
|
+
|
|
260
|
+
> v1.0.2+ 开始支持,且需要 Taro v3.4.10+
|
|
261
|
+
|
|
262
|
+
在默认情况下,第三方自定义组件的属性会被编译为形如:`<van-empty image="{{i.image}}" />`。
|
|
263
|
+
|
|
264
|
+
这时自定义组件声明的默认值会失效(详情请浏览 [#11575](https://github.com/NervJS/taro/issues/11575))。
|
|
265
|
+
|
|
266
|
+
```js
|
|
267
|
+
Component({
|
|
268
|
+
props: {
|
|
269
|
+
image: {
|
|
270
|
+
type: String,
|
|
271
|
+
value: 'default'
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
})
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
所以我们需要为此属性增加默认值,把它编译为形如:`<van-empty image="{{i.image===undefined?'default':i.image}}" />`。
|
|
278
|
+
|
|
279
|
+
用法:
|
|
280
|
+
|
|
281
|
+
```js
|
|
282
|
+
const config = {
|
|
283
|
+
plugins: [
|
|
284
|
+
['@tarojs/plugin-inject', {
|
|
285
|
+
thirdPartyComponents: {
|
|
286
|
+
// 为 `van-empty` 组件的 image 属性设置默认值 'default'
|
|
287
|
+
'van-empty': {
|
|
288
|
+
'image': "'default'"
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}]
|
|
292
|
+
]
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
### 模块补充
|
|
297
|
+
|
|
298
|
+
在前面的 components 示例中,给Text组件添加了新属性x-props后,@tarojs/components的类型文件中没有新定义的属性,typescript无法识别Text组件的x-props属性,导致vscode提示属性不存在,可以通过[模块补充](https://docs.taro.zone/docs/platform-plugin-how#%E7%B1%BB%E5%9E%8B)的方式来修正
|
|
299
|
+
|
|
300
|
+
```typescript
|
|
301
|
+
//tsconfig.json
|
|
302
|
+
{
|
|
303
|
+
"compilerOptions": {
|
|
304
|
+
"typeRoots": ["src/global.d.ts"],
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// global.d.ts
|
|
309
|
+
declare module '@tarojs/components' {
|
|
310
|
+
export * from '@tarojs/components/types/index';
|
|
311
|
+
|
|
312
|
+
// 下面示例是react的定义,vue下可能有所不同,原理是一样的
|
|
313
|
+
import { ComponentType } from 'react';
|
|
314
|
+
import { TextProps as OldTextProps } from '@tarojs/components/types/Text';
|
|
315
|
+
|
|
316
|
+
// 修改的Props
|
|
317
|
+
interface TextProps extends OldTextProps {
|
|
318
|
+
xProps?: string;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export const Text: ComponentType<TextProps>;
|
|
322
|
+
}
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
## License
|
|
327
|
+
|
|
328
|
+
MIT License
|
|
329
|
+
|
|
330
|
+
Copyright (c) O2Team
|
|
331
|
+
|
|
332
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
333
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
334
|
+
in the Software without restriction, including without limitation the rights
|
|
335
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
336
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
337
|
+
furnished to do so, subject to the following conditions:
|
|
338
|
+
|
|
339
|
+
The above copyright notice and this permission notice shall be included in all
|
|
340
|
+
copies or substantial portions of the Software.
|
|
341
|
+
|
|
342
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
343
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
344
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
345
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
346
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
347
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
348
|
+
SOFTWARE.
|
package/dist/apis-list.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.noPromiseApis = new Set([]);
|
|
5
|
-
exports.needPromiseApis = new Set([]);
|
|
6
|
-
//# sourceMappingURL=apis-list.js.map
|
|
1
|
+
|
|
2
|
+
export const noPromiseApis = new Set([]);
|
|
3
|
+
export const needPromiseApis = new Set([]);
|
package/dist/components.js
CHANGED
package/dist/index.js
CHANGED
|
@@ -7,7 +7,7 @@ exports.default = (ctx, options) => {
|
|
|
7
7
|
ctx.registerMethod({
|
|
8
8
|
name: 'onSetupClose',
|
|
9
9
|
fn(platform) {
|
|
10
|
-
const { voidComponents, nestElements, components, syncApis, asyncApis } = options;
|
|
10
|
+
const { voidComponents, nestElements, components, syncApis, asyncApis, componentsMap, thirdPartyComponents } = options;
|
|
11
11
|
const template = platform.template;
|
|
12
12
|
if (shared_1.isArray(voidComponents)) {
|
|
13
13
|
voidComponents.forEach(el => template.voidElements.add(el));
|
|
@@ -23,14 +23,21 @@ exports.default = (ctx, options) => {
|
|
|
23
23
|
else if (shared_1.isFunction(nestElements)) {
|
|
24
24
|
template.nestElements = nestElements(template.nestElements);
|
|
25
25
|
}
|
|
26
|
-
if (components || syncApis || asyncApis) {
|
|
26
|
+
if (components || syncApis || asyncApis || componentsMap) {
|
|
27
27
|
injectRuntimePath(platform);
|
|
28
28
|
if (components) {
|
|
29
29
|
template.mergeComponents(ctx, components);
|
|
30
30
|
}
|
|
31
|
+
if (componentsMap) {
|
|
32
|
+
injectComponentsReact(fs, platform.taroComponentsPath, componentsMap);
|
|
33
|
+
platform.taroComponentsPath = `@tarojs/plugin-inject/dist/components-react`;
|
|
34
|
+
}
|
|
31
35
|
injectComponents(fs, components);
|
|
32
36
|
injectApis(fs, syncApis, asyncApis);
|
|
33
37
|
}
|
|
38
|
+
if (thirdPartyComponents) {
|
|
39
|
+
template.mergeThirdPartyComponents(thirdPartyComponents);
|
|
40
|
+
}
|
|
34
41
|
}
|
|
35
42
|
});
|
|
36
43
|
};
|
|
@@ -43,6 +50,12 @@ function injectRuntimePath(platform) {
|
|
|
43
50
|
platform.runtimePath = [platform.runtimePath, injectedPath];
|
|
44
51
|
}
|
|
45
52
|
}
|
|
53
|
+
function injectComponentsReact(fs, taroComponentsPath, componentsMap) {
|
|
54
|
+
fs.writeFileSync(path.resolve(__dirname, '../dist/components-react.js'), `
|
|
55
|
+
export * from '${taroComponentsPath}'
|
|
56
|
+
${Object.keys(componentsMap).map((key) => `export const ${key} = '${componentsMap[key]}'`).join('\n')}
|
|
57
|
+
`);
|
|
58
|
+
}
|
|
46
59
|
function injectComponents(fs, components) {
|
|
47
60
|
fs.writeFileSync(path.resolve(__dirname, '../dist/components.js'), `
|
|
48
61
|
export const components = ${components ? JSON.stringify(components) : JSON.stringify({})};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAEA,2CAAwE;AACxE,6BAA4B;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","s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AAS,EAAE,uBAAuB,CAAC,EAAE;4BACzC,UAAU,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;CACvF,CAAC,CAAA;AACF,CAAC;AAED,SAAS,UAAU,CAAC,EAAE,EAAE,QAAQ,EAAE,SAAS;IACzC,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,sBAAsB,CAAC,EAAE;uCAC7B,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;yCACtD,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;CAClG,CAAC,CAAA;AACF,CAAC"}
|
package/dist/runtime.js
CHANGED
|
@@ -3,10 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
const shared_1 = require("@tarojs/shared");
|
|
4
4
|
const components_1 = require("./components");
|
|
5
5
|
const apis_list_1 = require("./apis-list");
|
|
6
|
-
const originFn = shared_1.defaultReconciler.initNativeApi;
|
|
7
6
|
const hostConfig = {
|
|
8
7
|
initNativeApi(taro) {
|
|
9
|
-
originFn === null || originFn === void 0 ? void 0 : originFn(taro);
|
|
10
8
|
const global = taro.miniGlobal;
|
|
11
9
|
shared_1.processApis(taro, global, {
|
|
12
10
|
noPromiseApis: apis_list_1.noPromiseApis,
|
package/dist/runtime.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":";;AAAA,
|
|
1
|
+
{"version":3,"file":"runtime.js","sourceRoot":"","sources":["../src/runtime.ts"],"names":[],"mappings":";;AAAA,2CAAsF;AACtF,6CAAyC;AACzC,2CAA4D;AAE5D,MAAM,UAAU,GAAG;IACjB,aAAa,CAAE,IAAI;QACjB,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAA;QAC9B,oBAAW,CAAC,IAAI,EAAE,MAAM,EAAE;YACxB,aAAa,EAAb,yBAAa;YACb,eAAe,EAAf,2BAAe;YACf,eAAe,EAAE,IAAI;SACtB,CAAC,CAAA;IACJ,CAAC;CACF,CAAA;AAGD,wBAAe,CAAC,UAAU,CAAC,CAAA;AAC3B,gCAAuB,CAAC,uBAAU,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tarojs/plugin-inject",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "Taro 小程序端平台中间层插件",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -30,14 +30,14 @@
|
|
|
30
30
|
"url": "https://github.com/NervJS/tarojs-plugin-inject/issues"
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/NervJS/tarojs-plugin-inject#readme",
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@tarojs/shared": "^3.1.3"
|
|
35
|
+
},
|
|
33
36
|
"devDependencies": {
|
|
34
37
|
"@tarojs/service": "^3.1.3",
|
|
35
38
|
"@types/node": "^13.9.8",
|
|
36
39
|
"typescript": "^3.8.3",
|
|
37
40
|
"webpack": "^4.41.0",
|
|
38
41
|
"webpack-dev-server": "^3.8.2"
|
|
39
|
-
},
|
|
40
|
-
"dependencies": {
|
|
41
|
-
"@tarojs/shared": "^3.1.3"
|
|
42
42
|
}
|
|
43
43
|
}
|
package/src/index.ts
CHANGED
|
@@ -10,8 +10,10 @@ interface IOptions {
|
|
|
10
10
|
voidComponents: string[] | ((list: VoidComponents) => VoidComponents)
|
|
11
11
|
nestElements: Record<string, number> | ((elem: NestElements) => NestElements)
|
|
12
12
|
components: Record<string, Record<string, any>>
|
|
13
|
+
componentsMap: Record<string, string>
|
|
13
14
|
syncApis: string[]
|
|
14
15
|
asyncApis: string[]
|
|
16
|
+
thirdPartyComponents: Record<string, Record<string, any>>
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export default (ctx: IPluginContext, options: IOptions) => {
|
|
@@ -19,13 +21,15 @@ export default (ctx: IPluginContext, options: IOptions) => {
|
|
|
19
21
|
|
|
20
22
|
ctx.registerMethod({
|
|
21
23
|
name: 'onSetupClose',
|
|
22
|
-
fn
|
|
24
|
+
fn(platform: TaroPlatformBase) {
|
|
23
25
|
const {
|
|
24
26
|
voidComponents,
|
|
25
27
|
nestElements,
|
|
26
28
|
components,
|
|
27
29
|
syncApis,
|
|
28
|
-
asyncApis
|
|
30
|
+
asyncApis,
|
|
31
|
+
componentsMap,
|
|
32
|
+
thirdPartyComponents
|
|
29
33
|
} = options
|
|
30
34
|
|
|
31
35
|
const template = platform.template
|
|
@@ -44,21 +48,30 @@ export default (ctx: IPluginContext, options: IOptions) => {
|
|
|
44
48
|
template.nestElements = nestElements(template.nestElements)
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
if (components || syncApis || asyncApis) {
|
|
51
|
+
if (components || syncApis || asyncApis || componentsMap) {
|
|
48
52
|
injectRuntimePath(platform)
|
|
49
53
|
|
|
50
54
|
if (components) {
|
|
51
55
|
template.mergeComponents(ctx, components)
|
|
52
56
|
}
|
|
53
57
|
|
|
58
|
+
if (componentsMap) {
|
|
59
|
+
injectComponentsReact(fs, platform.taroComponentsPath, componentsMap)
|
|
60
|
+
platform.taroComponentsPath = `@tarojs/plugin-inject/dist/components-react`
|
|
61
|
+
}
|
|
62
|
+
|
|
54
63
|
injectComponents(fs, components)
|
|
55
64
|
injectApis(fs, syncApis, asyncApis)
|
|
56
65
|
}
|
|
66
|
+
|
|
67
|
+
if (thirdPartyComponents) {
|
|
68
|
+
template.mergeThirdPartyComponents(thirdPartyComponents)
|
|
69
|
+
}
|
|
57
70
|
}
|
|
58
71
|
})
|
|
59
72
|
}
|
|
60
73
|
|
|
61
|
-
function injectRuntimePath
|
|
74
|
+
function injectRuntimePath(platform: TaroPlatformBase) {
|
|
62
75
|
const injectedPath = `@tarojs/plugin-inject/dist/runtime`
|
|
63
76
|
if (isArray(platform.runtimePath)) {
|
|
64
77
|
platform.runtimePath.push(injectedPath)
|
|
@@ -67,15 +80,22 @@ function injectRuntimePath (platform: TaroPlatformBase) {
|
|
|
67
80
|
}
|
|
68
81
|
}
|
|
69
82
|
|
|
70
|
-
function
|
|
83
|
+
function injectComponentsReact(fs, taroComponentsPath, componentsMap) {
|
|
84
|
+
fs.writeFileSync(path.resolve(__dirname, '../dist/components-react.js'), `
|
|
85
|
+
export * from '${taroComponentsPath}'
|
|
86
|
+
${Object.keys(componentsMap).map((key) => `export const ${key} = '${componentsMap[key]}'`).join('\n')}
|
|
87
|
+
`)
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function injectComponents(fs, components) {
|
|
71
91
|
fs.writeFileSync(path.resolve(__dirname, '../dist/components.js'), `
|
|
72
92
|
export const components = ${components ? JSON.stringify(components) : JSON.stringify({})};
|
|
73
93
|
`)
|
|
74
94
|
}
|
|
75
95
|
|
|
76
|
-
function injectApis
|
|
96
|
+
function injectApis(fs, syncApis, asyncApis) {
|
|
77
97
|
fs.writeFileSync(path.resolve(__dirname, '../dist/apis-list.js'), `
|
|
78
|
-
export const noPromiseApis = new Set(${syncApis ? JSON.stringify(syncApis): JSON.stringify([])});
|
|
79
|
-
export const needPromiseApis = new Set(${asyncApis ? JSON.stringify(asyncApis): JSON.stringify([])});
|
|
98
|
+
export const noPromiseApis = new Set(${syncApis ? JSON.stringify(syncApis) : JSON.stringify([])});
|
|
99
|
+
export const needPromiseApis = new Set(${asyncApis ? JSON.stringify(asyncApis) : JSON.stringify([])});
|
|
80
100
|
`)
|
|
81
101
|
}
|
package/src/runtime.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mergeReconciler, mergeInternalComponents, processApis } from '@tarojs/shared'
|
|
2
2
|
import { components } from './components'
|
|
3
3
|
import { noPromiseApis, needPromiseApis } from './apis-list'
|
|
4
4
|
|
|
5
|
-
const originFn = (defaultReconciler as any).initNativeApi
|
|
6
|
-
|
|
7
5
|
const hostConfig = {
|
|
8
6
|
initNativeApi (taro) {
|
|
9
|
-
originFn?.(taro)
|
|
10
7
|
const global = taro.miniGlobal
|
|
11
8
|
processApis(taro, global, {
|
|
12
9
|
noPromiseApis,
|