File size: 2,990 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use anyhow::Result;
use async_trait::async_trait;
use swc_core::{
    common::comments::NoopComments,
    ecma::{ast::Program, atoms::Atom},
};
use turbo_tasks::{ValueDefault, Vc};
use turbopack_ecmascript::{CustomTransformer, TransformContext};

#[turbo_tasks::value(shared, operation)]
#[derive(Clone, Debug)]
#[serde(default, rename_all = "camelCase")]
pub struct StyledComponentsTransformConfig {
    pub display_name: bool,
    pub ssr: bool,
    pub file_name: bool,
    pub top_level_import_paths: Vec<String>,
    pub meaningless_file_names: Vec<String>,
    pub css_prop: bool,
    pub namespace: Option<String>,
}

impl Default for StyledComponentsTransformConfig {
    fn default() -> Self {
        StyledComponentsTransformConfig {
            display_name: true,
            ssr: true,
            file_name: true,
            top_level_import_paths: vec![],
            meaningless_file_names: vec!["index".to_string()],
            css_prop: true,
            namespace: None,
        }
    }
}

#[turbo_tasks::value_impl]
impl StyledComponentsTransformConfig {
    #[turbo_tasks::function]
    fn default_private() -> Vc<Self> {
        Self::cell(Default::default())
    }
}

impl ValueDefault for StyledComponentsTransformConfig {
    fn value_default() -> Vc<Self> {
        StyledComponentsTransformConfig::default_private()
    }
}

#[derive(Debug)]
pub struct StyledComponentsTransformer {
    config: styled_components::Config,
}

impl StyledComponentsTransformer {
    pub fn new(config: &StyledComponentsTransformConfig) -> Self {
        let mut options = styled_components::Config {
            display_name: config.display_name,
            ssr: config.ssr,
            file_name: config.file_name,
            css_prop: config.css_prop,
            ..Default::default()
        };

        if let Some(namespace) = &config.namespace {
            options.namespace.clone_from(namespace);
        }

        let top_level_import_paths = &config.top_level_import_paths;
        if !top_level_import_paths.is_empty() {
            options.top_level_import_paths = top_level_import_paths
                .iter()
                .map(|s| Atom::from(s.clone()))
                .collect();
        }
        let meaningless_file_names = &config.meaningless_file_names;
        if !meaningless_file_names.is_empty() {
            options
                .meaningless_file_names
                .clone_from(meaningless_file_names);
        }

        Self { config: options }
    }
}

#[async_trait]
impl CustomTransformer for StyledComponentsTransformer {
    #[tracing::instrument(level = tracing::Level::TRACE, name = "styled_components", skip_all)]
    async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
        program.mutate(styled_components::styled_components(
            Some(ctx.file_path_str),
            ctx.file_name_hash,
            &self.config,
            NoopComments,
        ));

        Ok(())
    }
}