Skip to content

Commit 527f753

Browse files
committed
support wx unit.
1 parent f965cda commit 527f753

File tree

8 files changed

+117
-3
lines changed

8 files changed

+117
-3
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "weex-vue-precompiler",
3-
"version": "0.1.16",
3+
"version": "0.1.17",
44
"description": "a precompiler for weex-vue-render.",
55
"main": "src/index.js",
66
"scripts": {

src/hooks/style-binding.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ const bindingStyleNamesForPx2Rem = require('../config').bindingStyleNamesForPx2R
99
const { parseAst } = require('../util')
1010
const { getCompiler, getTransformer } = require('wxv-transformer')
1111

12+
function alreadyTransformed (node) {
13+
if (node
14+
&& node.type === 'CallExpression'
15+
&& node.callee
16+
&& (node.callee.name + ''.match(/_processExclusiveStyle|_px2rem/)))
17+
{
18+
return true
19+
}
20+
return false
21+
}
22+
1223
/**
1324
* transform :style="{width:w}" => :style="{width:_px2rem(w, rootValue)}"
1425
* This kind of style binding with object literal is a good practice.
@@ -26,6 +37,9 @@ function transformObject (obj, origTagName, rootValue) {
2637
const keyType = keyNode.type
2738
const key = keyType === 'Literal' ? keyNode.value : keyNode.name
2839
const valNode = prop.value
40+
if (alreadyTransformed(valNode)) {
41+
continue
42+
}
2943
if (bindingStyleNamesForPx2Rem.indexOf(key) > -1) {
3044
prop.value = {
3145
type: 'CallExpression',
@@ -47,6 +61,10 @@ function transformObject (obj, origTagName, rootValue) {
4761
* @param {string} tagName
4862
*/
4963
function transformVariable (node, tagName, rootValue) {
64+
if (alreadyTransformed(node)) {
65+
return node
66+
}
67+
5068
let callName = '_px2rem'
5169
const args = [node, { type: 'Literal', value: rootValue }]
5270
const transformer = getTransformer(tagName)

src/hooks/style.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ module.exports = function styleHook (
7474
.split(';')
7575
.map(statement => statement
7676
.split(':')
77-
.map(part => `"${part}"`)
77+
.map(part => {
78+
return '"' + part.replace(
79+
/([+-]?\d+(?:.\d*)?)(wx)/g,
80+
function ($0, $1, $2) {
81+
return $1 + 'px'
82+
}
83+
) + '"'
84+
})
7885
.join(':'))
7986
.join(",")
8087
// console.log('res=>', after)

test/input/hooks/events.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
<some-cmp @someEvent="someHandler" @appear.native="appear"></some-cmp>
44
<slider @change="change" @appear="appear"></slider>
55
<div @click="click" @appear="appear"></div>
6+
<input @return="onReturn" />
67
</div>
78
</template>

test/input/misc/units.vue

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<template>
2+
<div>
3+
<text class="txt" style="font-size:32wx">static style 32wx</text>
4+
<text class="txt" :style="{ fontSize: '32wx' }">binding style 32wx</text>
5+
<text class="txt wx">style tag 32wx</text>
6+
</div>
7+
</template>

test/output/hooks/events.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,27 @@ module.exports = [
110110
],
111111
staticClass: '" weex-ct weex-div"',
112112
_origTag: 'div'
113+
}, {
114+
type: 1,
115+
tag: 'input',
116+
_weexRegistered: true,
117+
plain: false,
118+
hasBindings: true,
119+
_hasBubbleParent: false,
120+
nativeEvents: {
121+
return: {
122+
value: 'onReturn',
123+
modifiers: {
124+
stop: true
125+
}
126+
}
127+
},
128+
attrs: [
129+
{
130+
name: 'data-evt-return',
131+
value: '""'
132+
}
133+
]
113134
}, {
114135
type: 1,
115136
tag: 'div',

test/output/misc/units.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module.exports = [
2+
{
3+
type: 1,
4+
tag: 'p',
5+
_origTag: 'text',
6+
_weexBuiltIn: true,
7+
plain: false,
8+
attrs: [
9+
{
10+
name: 'weex-type',
11+
value: '"text"'
12+
}
13+
],
14+
staticClass: '"txt weex-el weex-text"',
15+
staticStyle: JSON.stringify({
16+
'font-size': '32px'
17+
})
18+
}, {
19+
type: 1,
20+
tag: 'p',
21+
_origTag: 'text',
22+
_weexBuiltIn: true,
23+
plain: false,
24+
attrs: [
25+
{
26+
name: 'weex-type',
27+
value: '"text"'
28+
}
29+
],
30+
staticClass: '"txt weex-el weex-text"',
31+
styleBinding: "{ fontSize: _px2rem('32wx', 75) }"
32+
}, {
33+
type: 1,
34+
tag: 'p',
35+
_origTag: 'text',
36+
_weexBuiltIn: true,
37+
plain: false,
38+
attrs: [
39+
{
40+
name: 'weex-type',
41+
value: '"text"'
42+
}
43+
],
44+
staticClass: '"txt wx weex-el weex-text"',
45+
}, {
46+
type: 1,
47+
tag: 'div',
48+
_origTag: 'div',
49+
_weexBuiltIn: true,
50+
plain: false,
51+
attrs: [
52+
{
53+
name: 'weex-type',
54+
value: '"div"'
55+
}
56+
],
57+
staticClass: '" weex-ct weex-div"',
58+
static: false
59+
}
60+
]

0 commit comments

Comments
 (0)