|
| 1 | +use crate::{ |
| 2 | + ExtractStyleProp, |
| 3 | + extract_style::{extract_import::ExtractImport, extract_style_value::ExtractStyleValue}, |
| 4 | + extractor::{ |
| 5 | + ExtractResult, GlobalExtractResult, |
| 6 | + extract_style_from_expression::extract_style_from_expression, |
| 7 | + }, |
| 8 | +}; |
| 9 | +use css::style_selector::StyleSelector; |
| 10 | +use oxc_ast::{ |
| 11 | + AstBuilder, |
| 12 | + ast::{ArrayExpressionElement, Expression, ObjectPropertyKind, PropertyKey}, |
| 13 | +}; |
| 14 | + |
| 15 | +pub fn extract_global_style_from_expression<'a>( |
| 16 | + ast_builder: &AstBuilder<'a>, |
| 17 | + expression: &mut Expression<'a>, |
| 18 | + file: &str, |
| 19 | +) -> GlobalExtractResult<'a> { |
| 20 | + let mut styles = vec![]; |
| 21 | + |
| 22 | + if let Expression::ObjectExpression(obj) = expression { |
| 23 | + for p in obj.properties.iter_mut() { |
| 24 | + if let ObjectPropertyKind::ObjectProperty(o) = p { |
| 25 | + let name = if let PropertyKey::StaticIdentifier(ident) = &o.key { |
| 26 | + ident.name.to_string() |
| 27 | + } else if let PropertyKey::StringLiteral(s) = &o.key { |
| 28 | + s.value.to_string() |
| 29 | + } else if let PropertyKey::TemplateLiteral(t) = &o.key { |
| 30 | + t.quasis |
| 31 | + .iter() |
| 32 | + .map(|q| q.value.raw.as_str()) |
| 33 | + .collect::<Vec<_>>() |
| 34 | + .join("") |
| 35 | + } else { |
| 36 | + continue; |
| 37 | + }; |
| 38 | + |
| 39 | + if name == "imports" { |
| 40 | + if let Expression::ArrayExpression(arr) = &o.value { |
| 41 | + for p in arr.elements.iter() { |
| 42 | + styles.push(ExtractStyleProp::Static(ExtractStyleValue::Import( |
| 43 | + ExtractImport { |
| 44 | + url: if let ArrayExpressionElement::StringLiteral(s) = p { |
| 45 | + s.value.trim().to_string() |
| 46 | + } else if let ArrayExpressionElement::TemplateLiteral(t) = p { |
| 47 | + t.quasis |
| 48 | + .iter() |
| 49 | + .map(|q| q.value.raw.as_str()) |
| 50 | + .collect::<Vec<_>>() |
| 51 | + .join("") |
| 52 | + .trim() |
| 53 | + .to_string() |
| 54 | + } else { |
| 55 | + continue; |
| 56 | + }, |
| 57 | + file: file.to_string(), |
| 58 | + }, |
| 59 | + ))); |
| 60 | + } |
| 61 | + } |
| 62 | + continue; |
| 63 | + } |
| 64 | + styles.extend( |
| 65 | + extract_style_from_expression( |
| 66 | + ast_builder, |
| 67 | + None, |
| 68 | + &mut o.value, |
| 69 | + 0, |
| 70 | + Some(&StyleSelector::Global(name.clone(), file.to_string())), |
| 71 | + ) |
| 72 | + .styles, |
| 73 | + ); |
| 74 | + |
| 75 | + // if let ExtractResult::Extract { |
| 76 | + // styles: Some(_styles), |
| 77 | + // .. |
| 78 | + // } = extract_style_from_expression( |
| 79 | + // ast_builder, |
| 80 | + // None, |
| 81 | + // &mut o.value, |
| 82 | + // 0, |
| 83 | + // Some(&StyleSelector::Global(name.clone(), file.to_string())), |
| 84 | + // ) { |
| 85 | + // styles.extend( |
| 86 | + // extract_style_from_expression( |
| 87 | + // ast_builder, |
| 88 | + // None, |
| 89 | + // &mut o.value, |
| 90 | + // 0, |
| 91 | + // Some(&StyleSelector::Global(name.clone(), file.to_string())), |
| 92 | + // ) |
| 93 | + // .styles |
| 94 | + // .iter() |
| 95 | + // ); |
| 96 | + // } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + GlobalExtractResult { |
| 101 | + styles, |
| 102 | + style_order: None, |
| 103 | + } |
| 104 | +} |
0 commit comments