Skip to content

Commit 318fa24

Browse files
committed
feat: 增加半编译支持
1 parent 8c3a53d commit 318fa24

File tree

7 files changed

+435
-10
lines changed

7 files changed

+435
-10
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,7 @@ export function combineCssVariables(variables: Array<string>): string | null;
259259

260260
## CSS 变量
261261

262-
若使用 CSS 变量,挂载的属性应当挂载`:root`选择器上
263-
264-
⚠️:暂不支持`var传递3个及以上参数`, 如`height: var(--h, --he, 30px);``margin: var(--m, 30px 30px);`
262+
⚠️:暂不支持 **动态修改 Css 变量的值**
265263

266264
```css
267265
:root {

__test__/compileMode.spec.mjs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import * as fs from 'fs'
2+
import * as path from 'path'
3+
import { fileURLToPath } from 'url';
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = path.dirname(__filename);
7+
8+
import test from 'ava'
9+
import { parse } from '../index.js'
10+
11+
const normal = fs.readFileSync(path.resolve(__dirname, 'fixure/normal.jsx'), 'utf8')
12+
13+
test('Harmony compile mode', t => {
14+
const { code } = parse(`
15+
import { View } from '@tarojs/components';
16+
const Index = () => {
17+
return (
18+
<View className='item' compileMode />
19+
)
20+
};
21+
export default Index;
22+
`, [`
23+
.item {
24+
height: 100px;
25+
}
26+
`], {
27+
platformString: 'Harmony'
28+
})
29+
t.snapshot(code)
30+
})
31+
32+
test('Harmony compile mode2', t => {
33+
const { code } = parse(`
34+
import { View } from '@tarojs/components';
35+
const Index = () => {
36+
return (
37+
<View className='item' compileMode />
38+
)
39+
};
40+
export default Index;
41+
`, [`
42+
.item {
43+
display: flex
44+
}
45+
`], {
46+
platformString: 'Harmony'
47+
})
48+
t.snapshot(code)
49+
})
50+
51+
test('Harmony compile mode3', t => {
52+
const { code } = parse(`
53+
import { View } from '@tarojs/components';
54+
const Index = () => {
55+
return (
56+
<View className='item' compileMode />
57+
)
58+
};
59+
export default Index;
60+
`, [`
61+
.item {
62+
display: flex;
63+
flex-direction: row;
64+
}
65+
`], {
66+
platformString: 'Harmony'
67+
})
68+
t.snapshot(code)
69+
})
70+
71+
test('Harmony compile mode4', t => {
72+
const { code } = parse(`
73+
import { View } from '@tarojs/components';
74+
const Index = () => {
75+
return (
76+
<View className='item' compileMode />
77+
)
78+
};
79+
export default Index;
80+
`, [`
81+
.item {
82+
display: flex;
83+
flex-direction: column;
84+
}
85+
`], {
86+
platformString: 'Harmony'
87+
})
88+
t.snapshot(code)
89+
})
90+
91+
92+
test('Harmony compile mode5', t => {
93+
const { code } = parse(`
94+
import { View } from '@tarojs/components';
95+
const Index = () => {
96+
return (
97+
<View className='item' compileMode style={{ flexDirection: 'row' }} />
98+
)
99+
};
100+
export default Index;
101+
`, [`
102+
.item {
103+
display: flex;
104+
flex-direction: column;
105+
}
106+
`], {
107+
platformString: 'Harmony'
108+
})
109+
t.snapshot(code)
110+
})

__test__/compileMode.spec.mjs.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Snapshot report for `__test__/compileMode.spec.mjs`
2+
3+
The actual snapshot is saved in `compileMode.spec.mjs.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## Harmony compile mode
8+
9+
> Snapshot 1
10+
11+
`import { View } from '@tarojs/components';␊
12+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
13+
let __inner_style_data__;␊
14+
function __inner_style__() {␊
15+
if (__inner_style_data__) return __inner_style_data__;␊
16+
__inner_style_data__ = {␊
17+
"item": {␊
18+
height: convertNumber2VP(100)␊
19+
}␊
20+
};␊
21+
return __inner_style_data__;␊
22+
}␊
23+
const Index = ()=>{␊
24+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'item', null)} className='item' compileMode harmonyDirection="column"/>, null);␊
25+
};␊
26+
export default Index;␊
27+
`
28+
29+
## Harmony compile mode2
30+
31+
> Snapshot 1
32+
33+
`import { View } from '@tarojs/components';␊
34+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
35+
let __inner_style_data__;␊
36+
function __inner_style__() {␊
37+
if (__inner_style_data__) return __inner_style_data__;␊
38+
__inner_style_data__ = {␊
39+
"item": {␊
40+
display: "flex"␊
41+
}␊
42+
};␊
43+
return __inner_style_data__;␊
44+
}␊
45+
const Index = ()=>{␊
46+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'item', null)} className='item' compileMode harmonyDirection="row"/>, null);␊
47+
};␊
48+
export default Index;␊
49+
`
50+
51+
## Harmony compile mode3
52+
53+
> Snapshot 1
54+
55+
`import { View } from '@tarojs/components';␊
56+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
57+
let __inner_style_data__;␊
58+
function __inner_style__() {␊
59+
if (__inner_style_data__) return __inner_style_data__;␊
60+
__inner_style_data__ = {␊
61+
"item": {␊
62+
display: "flex",␊
63+
flexDirection: FlexDirection.Row␊
64+
}␊
65+
};␊
66+
return __inner_style_data__;␊
67+
}␊
68+
const Index = ()=>{␊
69+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'item', null)} className='item' compileMode harmonyDirection="row"/>, null);␊
70+
};␊
71+
export default Index;␊
72+
`
73+
74+
## Harmony compile mode4
75+
76+
> Snapshot 1
77+
78+
`import { View } from '@tarojs/components';␊
79+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
80+
let __inner_style_data__;␊
81+
function __inner_style__() {␊
82+
if (__inner_style_data__) return __inner_style_data__;␊
83+
__inner_style_data__ = {␊
84+
"item": {␊
85+
display: "flex",␊
86+
flexDirection: FlexDirection.Column␊
87+
}␊
88+
};␊
89+
return __inner_style_data__;␊
90+
}␊
91+
const Index = ()=>{␊
92+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'item', null)} className='item' compileMode harmonyDirection="column"/>, null);␊
93+
};␊
94+
export default Index;␊
95+
`
96+
97+
## Harmony compile mode5
98+
99+
> Snapshot 1
100+
101+
`import { View } from '@tarojs/components';␊
102+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
103+
let __inner_style_data__;␊
104+
function __inner_style__() {␊
105+
if (__inner_style_data__) return __inner_style_data__;␊
106+
__inner_style_data__ = {␊
107+
"item": {␊
108+
display: "flex",␊
109+
flexDirection: FlexDirection.Column␊
110+
}␊
111+
};␊
112+
return __inner_style_data__;␊
113+
}␊
114+
const Index = ()=>{␊
115+
return __combine_nesting_style__(<View __hmStyle={calcStaticStyle(__inner_style__(), 'item', {␊
116+
flexDirection: FlexDirection.Row␊
117+
})} className='item' compileMode harmonyDirection="row"/>, null);␊
118+
};␊
119+
export default Index;␊
120+
`

__test__/compileMode.spec.mjs.snap

581 Bytes
Binary file not shown.

src/style_propetries/background_image.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,45 @@ pub fn parse_background_image_item(image: &Image) -> Option<BackgroundImageKind>
116116
}
117117
},
118118
Gradient::RepeatingLinear(_) => None,
119-
Gradient::Radial(_) => None,
119+
Gradient::Radial(gradient) => {
120+
// Radial 话啊喂的半径需要
121+
let mut color_stops = vec![];
122+
for item in &gradient.items {
123+
match item {
124+
GradientItem::ColorStop(color_stop) => {
125+
let color_stop_position = color_stop
126+
.position
127+
.clone()
128+
.unwrap_or(DimensionPercentage::Dimension(LengthValue::Px(0.0)));
129+
color_stops.push((
130+
convert_color_keywords_to_hex(color_stop
131+
.color
132+
.to_css_string(PrinterOptions {
133+
minify: false,
134+
targets: Targets {
135+
include: Features::HexAlphaColors,
136+
..Targets::default()
137+
},
138+
..PrinterOptions::default()
139+
})
140+
.unwrap()),
141+
match &color_stop_position {
142+
DimensionPercentage::Dimension(length) => {
143+
length.to_css_string(PrinterOptions::default()).unwrap()
144+
}
145+
DimensionPercentage::Percentage(percentage) => percentage.0.to_string(),
146+
_ => color_stop_position
147+
.to_css_string(PrinterOptions::default())
148+
.unwrap(),
149+
},
150+
));
151+
}
152+
_ => {}
153+
};
154+
}
155+
156+
None
157+
},
120158
Gradient::RepeatingRadial(_) => None,
121159
Gradient::Conic(_) => None,
122160
Gradient::RepeatingConic(_) => None,

src/style_write.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ impl<'i> StyleWrite<'i> {
4242
let mut jsx_mut_visitor =
4343
JSXMutVisitor::new(
4444
self.jsx_record.clone(),
45+
self.all_style.clone(),
4546
self.pesudo_style_record.clone(),
4647
taro_components.clone(),
4748
platform.clone()

0 commit comments

Comments
 (0)