Skip to content

Commit 9c5b851

Browse files
committed
feat: 支持env
1 parent 358cbfa commit 9c5b851

File tree

13 files changed

+157
-63
lines changed

13 files changed

+157
-63
lines changed

__test__/compileMode.spec.mjs.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Generated by [AVA](https://avajs.dev).
99
> Snapshot 1
1010
1111
`import { View } from '@tarojs/components';␊
12-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
12+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1313
let __inner_style_data__;␊
1414
function __inner_style__() {␊
1515
if (__inner_style_data__) return __inner_style_data__;␊
@@ -31,7 +31,7 @@ Generated by [AVA](https://avajs.dev).
3131
> Snapshot 1
3232
3333
`import { View } from '@tarojs/components';␊
34-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
34+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
3535
let __inner_style_data__;␊
3636
function __inner_style__() {␊
3737
if (__inner_style_data__) return __inner_style_data__;␊
@@ -53,7 +53,7 @@ Generated by [AVA](https://avajs.dev).
5353
> Snapshot 1
5454
5555
`import { View } from '@tarojs/components';␊
56-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
56+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
5757
let __inner_style_data__;␊
5858
function __inner_style__() {␊
5959
if (__inner_style_data__) return __inner_style_data__;␊
@@ -76,7 +76,7 @@ Generated by [AVA](https://avajs.dev).
7676
> Snapshot 1
7777
7878
`import { View } from '@tarojs/components';␊
79-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
79+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
8080
let __inner_style_data__;␊
8181
function __inner_style__() {␊
8282
if (__inner_style_data__) return __inner_style_data__;␊
@@ -99,7 +99,7 @@ Generated by [AVA](https://avajs.dev).
9999
> Snapshot 1
100100
101101
`import { View } from '@tarojs/components';␊
102-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
102+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
103103
let __inner_style_data__;␊
104104
function __inner_style__() {␊
105105
if (__inner_style_data__) return __inner_style_data__;␊

__test__/compileMode.spec.mjs.snap

9 Bytes
Binary file not shown.

__test__/fixure/pesudo.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.container {
2-
align-items: baseline;
2+
height: env(safe-area-inset-bottom, 30px);
33
}

__test__/index.spec.mjs.md

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

__test__/index.spec.mjs.snap

12 Bytes
Binary file not shown.

__test__/selector.spec.mjs.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Generated by [AVA](https://avajs.dev).
99
> Snapshot 1
1010
1111
`import { View } from '@tarojs/components';␊
12-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
12+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
1313
let __inner_style_data__;␊
1414
function __inner_style__() {␊
1515
if (__inner_style_data__) return __inner_style_data__;␊
@@ -31,7 +31,7 @@ Generated by [AVA](https://avajs.dev).
3131
> Snapshot 1
3232
3333
`import { View } from '@tarojs/components';␊
34-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
34+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
3535
let __inner_style_data__;␊
3636
let __nesting_style_data__;␊
3737
function __nesting_style__() {␊
@@ -82,7 +82,7 @@ Generated by [AVA](https://avajs.dev).
8282
> Snapshot 1
8383
8484
`import { View } from '@tarojs/components';␊
85-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
85+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
8686
let __inner_style_data__;␊
8787
let __nesting_style_data__;␊
8888
function __nesting_style__() {␊
@@ -133,7 +133,7 @@ Generated by [AVA](https://avajs.dev).
133133
> Snapshot 1
134134
135135
`import { View } from '@tarojs/components';␊
136-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
136+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
137137
let __inner_style_data__;␊
138138
let __nesting_style_data__;␊
139139
function __nesting_style__() {␊
@@ -174,7 +174,7 @@ Generated by [AVA](https://avajs.dev).
174174
> Snapshot 1
175175
176176
`import { View } from '@tarojs/components';␊
177-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
177+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
178178
let __inner_style_data__;␊
179179
function __inner_style__() {␊
180180
if (__inner_style_data__) return __inner_style_data__;␊
@@ -199,7 +199,7 @@ Generated by [AVA](https://avajs.dev).
199199
> Snapshot 1
200200
201201
`import { View } from '@tarojs/components';␊
202-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
202+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
203203
let __inner_style_data__;␊
204204
function __inner_style__() {␊
205205
if (__inner_style_data__) return __inner_style_data__;␊
@@ -221,7 +221,7 @@ Generated by [AVA](https://avajs.dev).
221221
> Snapshot 1
222222
223223
`import { View } from '@tarojs/components';␊
224-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
224+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
225225
let __inner_style_data__;␊
226226
function __inner_style__() {␊
227227
if (__inner_style_data__) return __inner_style_data__;␊
@@ -243,7 +243,7 @@ Generated by [AVA](https://avajs.dev).
243243
> Snapshot 1
244244
245245
`import { View } from '@tarojs/components';␊
246-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
246+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
247247
const Index = ()=>{␊
248248
return __combine_nesting_style__(<View style={{␊
249249
height: '100px'␊
@@ -257,7 +257,7 @@ Generated by [AVA](https://avajs.dev).
257257
> Snapshot 1
258258
259259
`import { View } from '@tarojs/components';␊
260-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
260+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
261261
const Index = ()=>{␊
262262
return __combine_nesting_style__(<View style={{␊
263263
height: Math.random() > 0 ? '100px' : '50px'␊
@@ -271,7 +271,7 @@ Generated by [AVA](https://avajs.dev).
271271
> Snapshot 1
272272
273273
`import { View } from '@tarojs/components';␊
274-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
274+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
275275
const Index = ()=>{␊
276276
return __combine_nesting_style__(<View style={{␊
277277
height: Math.random() > 0 ? '100px' : '50px',␊
@@ -286,7 +286,7 @@ Generated by [AVA](https://avajs.dev).
286286
> Snapshot 1
287287
288288
`import { View } from '@tarojs/components';␊
289-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
289+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
290290
let __inner_style_data__;␊
291291
function __inner_style__() {␊
292292
if (__inner_style_data__) return __inner_style_data__;␊
@@ -308,7 +308,7 @@ Generated by [AVA](https://avajs.dev).
308308
> Snapshot 1
309309
310310
`import { View } from '@tarojs/components';␊
311-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
311+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
312312
let __inner_style_data__;␊
313313
function __inner_style__() {␊
314314
if (__inner_style_data__) return __inner_style_data__;␊
@@ -330,7 +330,7 @@ Generated by [AVA](https://avajs.dev).
330330
> Snapshot 1
331331
332332
`import { View } from '@tarojs/components';␊
333-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
333+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
334334
let __inner_style_data__;␊
335335
function __inner_style__() {␊
336336
if (__inner_style_data__) return __inner_style_data__;␊
@@ -360,7 +360,7 @@ Generated by [AVA](https://avajs.dev).
360360
> Snapshot 1
361361
362362
`import { View } from '@tarojs/components';␊
363-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
363+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
364364
let __inner_style_data__;␊
365365
function __inner_style__() {␊
366366
if (__inner_style_data__) return __inner_style_data__;␊
@@ -395,7 +395,7 @@ Generated by [AVA](https://avajs.dev).
395395
396396
`import { View } from '@tarojs/components';␊
397397
import './pesudo.scss';␊
398-
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, globalCss } from "@tarojs/runtime";␊
398+
import { calcStaticStyle, convertNumber2VP, __combine_nesting_style__, __var_fn, __env__, globalCss } from "@tarojs/runtime";␊
399399
let __inner_style_data__;␊
400400
function __inner_style__() {␊
401401
if (__inner_style_data__) return __inner_style_data__;␊

__test__/selector.spec.mjs.snap

2 Bytes
Binary file not shown.

src/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pub const INNER_STYLE_DATA: &'static str = "__inner_style_data__";
66
pub const NESTING_STYLE: &'static str = "__nesting_style__";
77
pub const COMBINE_NESTING_STYLE: &'static str = "__combine_nesting_style__";
88
pub const NESTINT_STYLE_DATA: &'static str = "__nesting_style_data__";
9+
pub const ENV_FUN: &'static str = "__env__";
10+
911
// pub const CALC_DYMAMIC_STYLE: &'static str = "calcDynamicStyle";
1012
pub const CALC_STATIC_STYLE: &'static str = "calcStaticStyle";
1113
pub const CSS_VAR_FN: &'static str = "__var_fn";

src/parse_style_properties.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,61 @@
11
use std::{cell::RefCell, collections::HashMap, rc::Rc};
22

3-
use lightningcss::{properties::Property, stylesheet::PrinterOptions};
3+
use lightningcss::{properties::{custom::TokenOrValue, Property}, stylesheet::PrinterOptions, traits::ToCss};
4+
use swc_core::{common::DUMMY_SP, ecma::{ast::{self}, utils::quote_ident}};
5+
use swc_core::ecma::ast::*;
46

5-
use crate::{style_parser::KeyFrameItem, style_propetries::{animation::Animation, aspect_ratio::AspactRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, color::ColorProperty, display::Display, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, overflow::Overflow, size::SizeProperty, style_value_type::StyleValueType, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, transform::Transform, transform_origin::TransformOrigin, vertical_align::VerticalAlign}};
7+
use crate::{constants::ENV_FUN, style_parser::KeyFrameItem, style_propetries::{animation::Animation, aspect_ratio::AspactRatio, background::Background, background_image::BackgroundImage, background_position::BackgroundPosition, background_repeat::BackgroundRepeat, background_size::BackgroundSize, border::Border, border_color::BorderColor, border_radius::BorderRadius, border_style::BorderStyle, border_width::BorderWidth, color::ColorProperty, display::Display, expr::Expr, flex::Flex, flex_align::FlexAlign, flex_basis::FlexBasis, flex_direction::FlexDirection, flex_wrap::FlexWrap, font_size::FontSize, font_style::FontStyle, font_weight::FontWeight, gap::Gap, item_align::ItemAlign, length_value::LengthValueProperty, letter_spacing::LetterSpacing, line_height::LineHeight, marin_padding::MarginPadding, max_size::MaxSizeProperty, normal::Normal, number::NumberProperty, overflow::Overflow, size::SizeProperty, style_value_type::StyleValueType, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, transform::Transform, transform_origin::TransformOrigin, unit::{generate_expr_by_length_value, generate_expr_with_css_input, Platform}, vertical_align::VerticalAlign}};
68

79
pub fn parse_style_properties(properties: &Vec<(String, Property)>, keyframes_map: Option<Rc<RefCell<HashMap<String, Vec<KeyFrameItem>>>>>) -> Vec<StyleValueType> {
810
let mut final_properties = vec![];
911
for (id, value) in properties.iter() {
12+
13+
let mut is_env: bool = false;
14+
match value {
15+
Property::Unparsed(unparsed) => {
16+
unparsed.value.0.iter().for_each(|item| {
17+
println!("Unparsed item: {:?}", item);
18+
match item {
19+
TokenOrValue::Env(env) => {
20+
is_env = true;
21+
let mut args = vec![
22+
ExprOrSpread {
23+
spread: None,
24+
expr: Box::new(ast::Expr::Lit(Lit::Str(env.name.to_css_string(PrinterOptions::default()).unwrap().into())))
25+
}
26+
];
27+
// env.name.to_css_string(PrinterOptions::default()).unwrap()))
28+
if env.fallback.is_some() {
29+
let fallback = env.fallback.as_ref().unwrap().0.get(0);
30+
if let Some(token) = fallback {
31+
if let TokenOrValue::Length(length) = token {
32+
args.push(
33+
ExprOrSpread {
34+
spread: None,
35+
expr: Box::new(generate_expr_by_length_value(length, Platform::Harmony))
36+
}
37+
)
38+
}
39+
}
40+
}
41+
final_properties.push(StyleValueType::Expr(Expr::new(id.to_string(), ast::Expr::Call(CallExpr {
42+
span: DUMMY_SP,
43+
callee: Callee::Expr(Box::new(ast::Expr::Ident(quote_ident!(ENV_FUN)))),
44+
args: args,
45+
type_args: None
46+
}))));
47+
},
48+
_ => {}
49+
}
50+
});
51+
},
52+
_ => {}
53+
};
54+
if is_env {
55+
continue;
56+
}
57+
58+
1059
let property_name = id.as_str();
1160
match property_name {
1261
// 基础样式

src/style_propetries/expr.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
use swc_core::ecma::ast;
2+
3+
use super::{traits::ToExpr, unit::PropertyTuple};
4+
5+
6+
7+
#[derive(Debug, Clone)]
8+
pub struct Expr(String, ast::Expr);
9+
10+
impl Expr {
11+
pub fn new(id: String, value: ast::Expr) -> Self {
12+
Self(id, value)
13+
}
14+
}
15+
16+
17+
impl ToExpr for Expr {
18+
fn to_expr(&self) -> PropertyTuple {
19+
PropertyTuple::One(
20+
self.0.clone(),
21+
self.1.clone()
22+
)
23+
}
24+
25+
fn to_rn_expr(&self) -> PropertyTuple {
26+
PropertyTuple::One(
27+
self.0.clone(),
28+
self.1.clone()
29+
)
30+
}
31+
}
32+

0 commit comments

Comments
 (0)