From 22c332769c632fb28418eb864c1933ac00611ea9 Mon Sep 17 00:00:00 2001 From: zhuminghui2 Date: Wed, 19 Feb 2025 17:05:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E8=A7=84?= =?UTF-8?q?=E5=88=99=E5=AF=B9env=E7=9A=84=E6=8F=90=E5=89=8D=E8=AF=86?= =?UTF-8?q?=E5=88=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/json_writer.rs | 13 +++++++++---- src/parse_style_properties.rs | 15 +++++++++++++-- src/style_parser.rs | 7 ++++--- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/json_writer.rs b/src/json_writer.rs index d7ff28c..26176ae 100644 --- a/src/json_writer.rs +++ b/src/json_writer.rs @@ -5,6 +5,7 @@ use swc_core::common::DUMMY_SP; use swc_core::ecma::ast::*; use crate::constants::{Pseudo, SUPPORT_PSEUDO_KEYS}; +use crate::parse_style_properties::DeclsAndVars; use crate::style_propetries::style_value_type::StyleValueType; use crate::style_propetries::unit::Platform; @@ -14,20 +15,20 @@ use crate::style_parser::KeyFrameItem; use crate::style_propetries::style_media::StyleMedia; pub struct JsonWriter { - styles: IndexMap<(u32, String), Vec>, + styles: IndexMap<(u32, String), DeclsAndVars>, keyframes: IndexMap<(u32, String), Vec>, medias: Vec, } impl JsonWriter { - pub fn new(styles: IndexMap<(u32, String), Vec>, keyframes: IndexMap<(u32, String), Vec>, + pub fn new(styles: IndexMap<(u32, String), DeclsAndVars>, keyframes: IndexMap<(u32, String), Vec>, medias: Vec) -> Self { Self { styles ,keyframes,medias } } pub fn to_json(&self) -> String { - let elems: Vec = self.styles.iter().filter_map(|((media_index,selector), prop_or_spreads)| Some({ + let elems: Vec = self.styles.iter().filter_map(|((media_index,selector), item)| Some({ // 识别伪类 let mut new_selector = selector.clone(); let mut pseudo_key = String::new(); @@ -90,8 +91,12 @@ impl JsonWriter { key: PropName::Ident(Ident::new("declarations".into(), DUMMY_SP)), value: Box::new(Expr::Array(ArrayLit { span: DUMMY_SP, - elems: parse_style_values(prop_or_spreads.clone(), Platform::Harmony) + elems: parse_style_values(item.decls.clone(), Platform::Harmony) })), + }))), + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(Ident::new("has_env".into(), DUMMY_SP)), + value: Box::new(Expr::Lit(Lit::Bool(Bool { span: DUMMY_SP, value: item.has_var }))), }))) ]; diff --git a/src/parse_style_properties.rs b/src/parse_style_properties.rs index 97176ae..0461315 100644 --- a/src/parse_style_properties.rs +++ b/src/parse_style_properties.rs @@ -5,8 +5,15 @@ use swc_core::ecma::ast::*; use crate::{generate_expr_lit_str, style_parser::KeyFrameItem, style_propetries::{animation::Animation, animation_multi::AnimationMulti, aspect_ratio::AspectRatio, 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, box_shadow::BoxShadow, 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, opacity::Opacity, overflow::Overflow, position::Position, size::SizeProperty, style_property_type::{string_to_css_property_type, CSSPropertyType}, style_value_type::StyleValueType, text_align::TextAlign, text_decoration::TextDecoration, text_overflow::TextOverflow, text_shadow::TextShadow, text_transform::TextTransform, transform::Transform, transform_origin::TransformOrigin, transition::Transition, unit::{generate_expr_by_length_value, Platform}, vertical_align::VerticalAlign, visibility::Visibility, white_space::WhiteSpace, word_break::WordBreak}, utils::lowercase_first}; -pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec { +#[derive(Debug, Clone)] +pub struct DeclsAndVars { + pub decls:Vec, + pub has_var: bool +} + +pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> DeclsAndVars{ let mut final_properties = vec![]; + let mut has_env = false; for (id, value) in properties.iter() { let mut is_env: bool = false; @@ -28,6 +35,7 @@ pub fn parse_style_properties(properties: &Vec<(String, Property)>) -> Vec