1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
pub use crate::{
utils::{analyze, analyzer, State},
visitors::{
display_name_and_id::display_name_and_id, transpile_css_prop::transpile::transpile_css_prop,
},
};
use serde::Deserialize;
use std::{cell::RefCell, rc::Rc, sync::Arc};
use swc_atoms::JsWord;
use swc_common::{chain, SourceFile};
use swc_ecmascript::visit::{Fold, VisitMut};
mod css;
mod utils;
mod visitors;
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct Config {
#[serde(default = "true_by_default")]
pub display_name: bool,
#[serde(default = "true_by_default")]
pub ssr: bool,
#[serde(default = "true_by_default")]
pub file_name: bool,
#[serde(default)]
pub namespace: String,
#[serde(default)]
pub top_level_import_paths: Vec<JsWord>,
#[serde(default)]
pub transpile_template_literals: bool,
#[serde(default)]
pub minify: bool,
#[serde(default)]
pub css_prop: bool,
}
fn true_by_default() -> bool {
true
}
impl Config {
pub(crate) fn use_namespace(&self) -> String {
if self.namespace.is_empty() {
return String::new();
}
format!("{}__", self.namespace)
}
}
pub fn styled_components(file: Arc<SourceFile>, config: Config) -> impl Fold + VisitMut {
let state: Rc<RefCell<State>> = Default::default();
let config = Rc::new(config);
chain!(
analyzer(config.clone(), state.clone()),
display_name_and_id(file, config, state),
transpile_css_prop()
)
}