Skip to content

Commit d81b406

Browse files
committed
perf(alita wx-react): 基本组件view/text的默认样式改为class实现
1 parent 7b83782 commit d81b406

File tree

5 files changed

+168
-11
lines changed

5 files changed

+168
-11
lines changed

mptemp/commonwxs.wxs

+24-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,30 @@ var DEFAULTCONTAINERSTYLE =
1010

1111

1212
var getFinalStyle = function (d) {
13-
return d.replace('_1_', DEFAULTVIEWSTYLE)
14-
.replace('_2_', DEFAULTTEXTWSTYLE)
15-
.replace('_3_', DEFAULTINNERTEXTWSTYLE)
16-
.replace('_4_', DEFAULTSCROLLVIEWSTYLE)
17-
.replace('_5_', DEFAULTCONTAINERSTYLE)
13+
if (!d) return ''
14+
15+
if (d === '_5_') return DEFAULTCONTAINERSTYLE
16+
17+
if (d.charAt(0) !== '_') {
18+
return d
19+
}
20+
21+
22+
if (d.charAt(1) === '1') {
23+
return d.replace('_1_', DEFAULTVIEWSTYLE)
24+
}
25+
26+
if (d.charAt(1) === '2') {
27+
return d.replace('_2_', DEFAULTTEXTWSTYLE)
28+
}
29+
30+
if (d.charAt(1) === '3') {
31+
return d.replace('_3_', DEFAULTINNERTEXTWSTYLE)
32+
}
33+
34+
if (d.charAt(1) === '4') {
35+
return d.replace('_4_', DEFAULTSCROLLVIEWSTYLE)
36+
}
1837
}
1938

2039
var lite = function(v) {

mptemp/compCommon.wxss

+64
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,67 @@
1+
.view {
2+
display: flex;
3+
flex-grow: 0;
4+
flex-shrink: 0;
5+
position: relative;
6+
flex-direction: column;
7+
box-sizing: border-box;
8+
border-width: 0;
9+
border-style: solid;
10+
min-width: 0;
11+
min-height: 0;
12+
}
13+
14+
.text {
15+
display: inline-block;
16+
flex-grow: 0;
17+
flex-shrink: 0;
18+
overflow: hidden;
19+
position: relative;
20+
flex-direction: column;
21+
box-sizing: border-box ;
22+
border-width: 0;
23+
border-style: solid;
24+
min-width: 0;
25+
min-height: 0;
26+
}
27+
28+
.textInner {
29+
display: inline;
30+
min-width: 0;
31+
min-height: 0;
32+
}
33+
34+
.scroll {
35+
display: flex;
36+
position: relative;
37+
flex-direction: column;
38+
box-sizing: border-box;
39+
flex: 1 ;
40+
border-width: 0;
41+
border-style: solid;
42+
min-width: 0;
43+
min-height: 0;
44+
}
45+
46+
.container {
47+
display: flex;
48+
flex: 1;
49+
flex-basis: 100%;
50+
width: 100%;
51+
height: 100%;
52+
justify-content: inherit;
53+
align-items: inherit;
54+
flex-wrap: inherit;
55+
box-sizing: border-box;
56+
border:none;
57+
flex-direction: inherit;
58+
border-radius: inherit;
59+
position: static;
60+
min-width: 0;
61+
min-height: 0;
62+
}
63+
64+
165
/*TouchableOpacity TouchableHighlight 使用*/
266
.thFabricate {
367
display: none;

packages/wx-react/miniprogram_dist/render.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default function render(vnode, parentInst, parentContext, data, oldData,
7171
} else if (k === 'mode') {
7272
data[`${vnodeDiuu}${k}`] = resizeMode(v)
7373
} else if (k === 'style' && finalNodeType !== 'TouchableWithoutFeedback') {
74-
data[`${vnodeDiuu}${k}`] = tackleWithStyleObj(v, finalNodeType)
74+
data[`${vnodeDiuu}${k}`] = tackleWithStyleObj(v, isFirstEle ? finalNodeType : null)
7575
} else if (k === 'activeOpacity') {
7676
data[`${vnodeDiuu}hoverClass`] = activeOpacityHandler(v)
7777
} else {
@@ -93,11 +93,6 @@ export default function render(vnode, parentInst, parentContext, data, oldData,
9393
})
9494
}
9595

96-
97-
if (!props.style && finalNodeType !== 'TouchableWithoutFeedback') {
98-
data[`${vnodeDiuu}style`] = tackleWithStyleObj('', finalNodeType)
99-
}
100-
10196
if (props.activeOpacity === undefined && finalNodeType === 'TouchableOpacity') {
10297
data[`${vnodeDiuu}hoverClass`] = activeOpacityHandler(0.2)
10398
}
@@ -124,6 +119,10 @@ export default function render(vnode, parentInst, parentContext, data, oldData,
124119
}
125120
}
126121

122+
if (props.original === 'TouchableWithoutFeedback') {
123+
// isFirstEle 需要往外上报样式
124+
children[0].isFirstEle = true
125+
}
127126

128127
for (let i = 0; i < children.length; i++) {
129128
const childVnode = children[i]

src/tran/classNameHandler.js

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
* 统一处理节点的类名添加
14+
* @param ast
15+
* @param info
16+
* @returns {*}
17+
*/
18+
export default function classNameHandler (ast,info) {
19+
20+
traverse(ast, {
21+
exit: path => {
22+
if (path.type === 'JSXOpeningElement'
23+
&& path.node.name.name === 'view'
24+
) {
25+
const attris = path.node.attributes
26+
const original = (attris.filter(item => item.type === 'JSXAttribute' && item.name.name === 'original'))[0].value.value
27+
28+
if (original === 'View'
29+
|| original === 'AnimatedView'
30+
|| original === 'AnimatedText'
31+
|| original === 'TouchableOpacity'
32+
|| original === 'TouchableHighlight'
33+
) {
34+
attris.push(
35+
t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('view'))
36+
)
37+
} else if (
38+
original === 'OuterText'
39+
) {
40+
attris.push(
41+
t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('text'))
42+
)
43+
} else if (
44+
original === 'InnerText'
45+
) {
46+
attris.push(
47+
t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('textInner'))
48+
)
49+
} else if (
50+
original === 'TouchableWithoutFeedback'
51+
) {
52+
// do nothing
53+
} else {
54+
attris.push(
55+
t.jsxAttribute(t.jsxIdentifier('class'), t.stringLiteral('view'))
56+
)
57+
}
58+
}
59+
60+
if (path.type === 'JSXOpeningElement'
61+
&& path.node.name.name === 'image'
62+
) {
63+
// image 不支持包裹元素了,so, do nothing!
64+
}
65+
66+
}
67+
68+
})
69+
70+
return ast
71+
}
72+

src/tran/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import addEventHandler from './addEventHandler'
2424
import addWXPrefixHandler from './addWXPrefixHandler'
2525
import cptCompHandler from './cptCompHandler'
2626
import literalTemplate from './literalTemplate'
27+
import classNameHandler from './classNameHandler'
2728

2829
export default function (ast, filepath, isFuncComp, entryFilePath, isPageComp, isStatelessComp) {
2930
const info = {
@@ -70,6 +71,8 @@ export default function (ast, filepath, isFuncComp, entryFilePath, isPageComp, i
7071

7172
ast = addEventHandler(ast, info)
7273

74+
ast = classNameHandler(ast, info)
75+
7376
ast = geneAllTemplate(ast, info)
7477

7578
geneWxml(info)

0 commit comments

Comments
 (0)