Skip to content

Commit 5189ba2

Browse files
committed
feat(alita wx-react): 减少JSX是文本的时候,生成的小程序的结构
1 parent d87ccf9 commit 5189ba2

File tree

7 files changed

+94
-27
lines changed

7 files changed

+94
-27
lines changed

mptemp/commonwxs.wxs

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ var getFinalStyle = function (d) {
1717
.replace('_5_', DEFAULTCONTAINERSTYLE)
1818
}
1919

20+
var lite = function(v) {
21+
return v.constructor === 'Number' || v.constructor === 'String'
22+
}
23+
2024
module.exports = {
21-
getFinalStyle: getFinalStyle
25+
getFinalStyle: getFinalStyle,
26+
lite: lite
2227
};

packages/wx-react/miniprogram_dist/render.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,14 @@ export default function render(vnode, parentInst, parentContext, data, oldData,
147147
|| tempVnode === null
148148
) {
149149
// do nothing
150-
return
150+
data[datakey] = ''
151151
} else if (typeof tempVnode === 'string'
152152
|| typeof tempVnode === 'number'
153153
) {
154154
// 不是jsx的情况
155-
data[datakey] = {
156-
isLiteral: true,
157-
v: tempVnode
158-
}
155+
data[datakey] = tempVnode
159156
} else if (Array.isArray(tempVnode)) {
160-
// key必须明确指定,对于不知道key的情况, React和小程序处理可能存在差异,照成两个平台的行为差异
157+
// key必须明确指定,对于不知道key的情况, React和小程序处理可能存在差异,造成两个平台的行为差异
161158

162159
let oldSubDataKeyMap = {}
163160
if (oldData && oldData[datakey] && oldData[datakey].isArray) {
@@ -180,10 +177,7 @@ export default function render(vnode, parentInst, parentContext, data, oldData,
180177
if (typeof subVnode === 'string'
181178
|| typeof subVnode === 'number'
182179
) {
183-
data[datakey].v.push({
184-
isLiteral: true,
185-
v: subVnode
186-
})
180+
data[datakey].v.push(subVnode)
187181
continue
188182
}
189183

src/tran/addWXPrefixHandler.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export default function addWXPrefixHandler (ast) {
6262
}
6363

6464
function addWXPrefix(path) {
65+
const {textComp} = global.execArgs
6566

6667
// Picker.Item
6768
if (path.node.name.type === 'JSXMemberExpression') {
@@ -95,13 +96,13 @@ function addWXPrefix(path) {
9596
}
9697

9798
// Text 特殊需要处理
98-
if (name === 'Text' && isInText(path)) {
99+
if (textComp.has(name) && isInText(path)) {
99100
path.node.name.name = `view`
100101
addViewOriginalAttri(path, "InnerText")
101102
return
102103
}
103104

104-
if (name === 'Text' && !isInText(path)) {
105+
if (textComp.has(name) && !isInText(path)) {
105106
path.node.name.name = `view`
106107
addViewOriginalAttri(path, "OuterText")
107108
return
@@ -113,6 +114,7 @@ function addWXPrefix(path) {
113114
}
114115

115116
function isInText(path) {
117+
const {textComp} = global.execArgs
116118
if (path.parentPath.parentPath.node.openingElement) {
117119
const op = path.parentPath.parentPath.node.openingElement
118120

@@ -124,10 +126,7 @@ function isInText(path) {
124126
})
125127
}
126128

127-
128-
return (
129-
op.name.name === 'Text'
130-
)
129+
return textComp.has(op.name.name)
131130
}
132131
return false
133132
}

src/tran/geneWxml.js

+3-9
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,12 @@ export default function(info) {
6969
<template name="${name}">
7070
<block wx:if="{{isArray}}">
7171
<block wx:for="{{v}}" wx:key="key">
72-
<template wx:if="{{item.tempName}}" is="{{item.tempName}}" data="{{...item}}"></template>
73-
<block wx:if="{{item.isLiteral}}">
74-
{{item.v}}
75-
</block>
72+
<block wx:if="{{tools.lite(item)}}">{{item}}</block>
73+
<template wx:else is="{{item.tempName}}" data="{{...item}}"></template>
7674
</block>
7775
</block>
78-
<block wx:elif="{{isJSX}}">
79-
<template is="{{v.tempName}}" data="{{...v}}"></template>
80-
</block>
81-
<block wx:elif="{{isLiteral}}">{{v}}</block>
76+
<template wx:elif="{{isJSX}}" is="{{v.tempName}}" data="{{...v}}"></template>
8277
</template>
83-
8478
`;
8579

8680
templateWxml = subT + templateWxml;

src/tran/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import compOutElementToBlock from './compOutElementToBlock'
2323
import addEventHandler from './addEventHandler'
2424
import addWXPrefixHandler from './addWXPrefixHandler'
2525
import cptCompHandler from './cptCompHandler'
26+
import literalTemplate from './literalTemplate'
2627

2728
export default function (ast, filepath, isFuncComp, entryFilePath, isPageComp, isStatelessComp) {
2829
const info = {
@@ -65,6 +66,8 @@ export default function (ast, filepath, isFuncComp, entryFilePath, isPageComp, i
6566

6667
geneReactJS(ast, info)
6768

69+
ast = literalTemplate(ast, info)
70+
6871
ast = addEventHandler(ast, info)
6972

7073
ast = geneAllTemplate(ast, info)

src/tran/literalTemplate.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) Areslabs.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
*/
8+
9+
import traverse from "@babel/traverse";
10+
import * as t from "@babel/types"
11+
12+
/**
13+
* 如果 由childrenToTemplate生成的template 最终运行的结果是literal,那直接展示
14+
* @param ast
15+
* @param info
16+
* @returns {*}
17+
*/
18+
export default function literalTemplate (ast,info) {
19+
20+
traverse(ast, {
21+
exit: path => {
22+
if (path.type === 'JSXElement') {
23+
24+
const newChildren = []
25+
path.node.children.forEach(item => {
26+
if (
27+
item.type === 'JSXElement'
28+
&& item.openingElement.name.name === 'template'
29+
) {
30+
31+
let datakey = null
32+
item.openingElement.attributes.forEach(attr => {
33+
if (attr.type === 'JSXAttribute' && attr.name.name === 'wx:if') {
34+
attr.name.name = 'wx:else'
35+
attr.value = null
36+
}
37+
38+
if (attr.type === 'JSXAttribute' && attr.name.name === 'datakey') {
39+
datakey = attr.value.value
40+
}
41+
})
42+
43+
newChildren.push(
44+
t.jsxText(`<block wx:if="{{tools.lite(${datakey})}}">{{${datakey}}}</block>`)
45+
)
46+
}
47+
48+
newChildren.push(item)
49+
})
50+
51+
path.node.children = newChildren
52+
}
53+
54+
}
55+
56+
})
57+
58+
return ast
59+
}

src/util/getExtCompPathMaps.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* jsxPropsMap: 属性是JSX片段,包括方法返回JSX片段等,Alita需要特殊处理这些属性
1515
* extChildComp: 需要处理children 为childrencpt的集合
1616
* extReactComp:一般来说对齐的组件 需要继承于 RNBaseComponent,但是有些复杂的组件需要继承于Component/PureComponent,比如FlatList
17+
* textComp: Text节点,一般来说只要官方的Text组件
1718
* allExtComp:所有组件,包括RN官方组件,配置在extCompLibs里的组件,这些组件在Alita转化的时候,会当做基本组件,基本组件的属性处理和自定义组件有一些区别
1819
* @param extCompLibs
1920
* @returns {{extCompPathMaps, extChildComp: Set, extReactComp: Set, allExtComp: Set, jsxPropsMap}}
@@ -23,6 +24,7 @@ export default function getExtCompPathMaps(extCompLibs) {
2324
const jsxPropsMap = {}
2425
const extChildComp = new Set([])
2526
const extReactComp = new Set([])
27+
const textComp = new Set(['Text'])
2628
const allExtComp = new Set([])
2729
for(let i = 0; i < extCompLibs.length; i ++) {
2830
const extLib = extCompLibs[i]
@@ -44,7 +46,13 @@ export default function getExtCompPathMaps(extCompLibs) {
4446
compPathMap[compName] = `${wxLibName}${compDir}${compName}/index`
4547
allExtComp.add(compName)
4648
} else {
47-
const {name, extendsComponent, needOperateChildren, jsxProps} = compName
49+
const {
50+
name,
51+
extendsComponent,
52+
needOperateChildren,
53+
jsxProps,
54+
textChildren
55+
} = compName
4856
compPathMap[name] = `${wxLibName}${compDir}${name}/index`
4957

5058
if (needOperateChildren === true) {
@@ -59,6 +67,10 @@ export default function getExtCompPathMaps(extCompLibs) {
5967
jsxPropsMap[name] = jsxProps
6068
}
6169

70+
if (textChildren === true) {
71+
textComp.add(name)
72+
}
73+
6274
allExtComp.add(name)
6375
}
6476

@@ -70,6 +82,7 @@ export default function getExtCompPathMaps(extCompLibs) {
7082
extChildComp,
7183
extReactComp,
7284
allExtComp,
85+
textComp,
7386
jsxPropsMap
7487
}
7588
}

0 commit comments

Comments
 (0)