Skip to content

Commit 9fa90d3

Browse files
committed
feature: improve usingComponents options
1 parent 0a35ed1 commit 9fa90d3

File tree

17 files changed

+470
-356
lines changed

17 files changed

+470
-356
lines changed

build.js

Lines changed: 346 additions & 349 deletions
Large diffs are not rendered by default.

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ const id2 = simulate.load('/path/to/component', 'custom-comp', {
8181
| id | String | 可选字段,如果传了此字段,则表明注册为全局组件,其他组件可直接在 template 中使用而无需在 usingComponents 里引入 |
8282
| tagName | String | 可选字段,指定组件对应的 dom 节点的 tagName,默认取 usingComponents 里的定义或组件自身的 id |
8383
| template | String | 组件模板,即组件对应的 wxml 内容 |
84-
| usingComponents | Object | 使用到的自定义组件映射表 |
84+
| usingComponents | Object | 使用到的自定义组件映射表,子孙组件在进行 usingComponents 处理时,会从此表寻找同名组件进行覆盖处理,支持传入覆盖路径或者组件 id |
8585
| behaviors | Array<Behavior> | behavior 的用法和小程序类似 |
8686
| options | Object | 配置对象,支持小程序自定义组件 options 定义段支持的所有字段 |
8787
| options.classPrefix | String | 组件样式的私有化前缀,默认是空串,即没有前缀 |

docs/update.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 更新日志
22

3+
## 1.5.3
4+
5+
* 支持子孙组件的 usingComponents 覆盖
6+
37
## 1.5.0
48

59
* load 方法支持 compilerOptions 配置

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,19 +105,20 @@ function register(componentPath, tagName, cache, hasRegisterCache) {
105105
const rootPath = cache.options.rootPath
106106
const usingComponents = component.json.usingComponents || {}
107107
const overrideUsingComponents = cache.options.usingComponents || {}
108+
Object.assign(usingComponents, overrideUsingComponents)
108109
const usingComponentKeys = Object.keys(usingComponents)
109110
for (let i = 0, len = usingComponentKeys.length; i < len; i++) {
110111
const key = usingComponentKeys[i]
111112

112-
if (Object.prototype.hasOwnProperty.call(overrideUsingComponents, key)) continue // 被 override 的跳过
113-
114113
const value = usingComponents[key]
115114
const usingPath = _.isAbsolute(value) ? path.join(rootPath, value) : path.join(path.dirname(componentPath), value)
116-
const id = register(usingPath, key, cache, hasRegisterCache)
117-
118-
usingComponents[key] = id
115+
116+
if (_.readFile(`${usingPath}.json`)) {
117+
// 文件路径
118+
const id = register(usingPath, key, cache, hasRegisterCache)
119+
usingComponents[key] = id
120+
}
119121
}
120-
Object.assign(usingComponents, overrideUsingComponents)
121122

122123
// 读取自定义组件的静态内容
123124
component.wxml = compile.getWxml(componentPath, cache.options)

test/comp11/comp/comp1.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Component({
2+
properties: {
3+
prop: {
4+
type: String,
5+
value: 'comp1.properties'
6+
},
7+
},
8+
methods: {
9+
getStr() {
10+
return 'comp1'
11+
}
12+
}
13+
})

test/comp11/comp/comp1.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"component": true,
3+
"usingComponents": {
4+
"comp2": "12345"
5+
}
6+
}

test/comp11/comp/comp1.wxml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<view class="index">{{prop}}</view>
2+
<comp2 class="comp2" prop="from comp1"></comp2>

test/comp11/comp/comp1.wxss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.index {
2+
color: yellow;
3+
}

test/comp11/comp/comp2.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Component({
2+
properties: {
3+
prop: {
4+
type: String,
5+
value: 'comp2.properties'
6+
},
7+
},
8+
methods: {
9+
getStr() {
10+
return 'comp2'
11+
}
12+
}
13+
})

test/comp11/comp/comp2.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"component": true,
3+
"usingComponents": {}
4+
}

0 commit comments

Comments
 (0)