swc_css_modules/
imports.rs1use swc_atoms::Atom;
4use swc_css_ast::{
5 ComponentValue, Declaration, DeclarationName, ImportHref, ImportPrelude, Stylesheet, UrlValue,
6};
7use swc_css_visit::{Visit, VisitWith};
8
9pub fn analyze_imports(ss: &Stylesheet) -> Vec<Atom> {
10 let mut v = Analyzer {
11 imports: Default::default(),
12 };
13 ss.visit_with(&mut v);
14 v.imports.sort();
15 v.imports.dedup();
16 v.imports
17}
18
19struct Analyzer {
20 imports: Vec<Atom>,
21}
22
23impl Visit for Analyzer {
24 fn visit_import_prelude(&mut self, n: &ImportPrelude) {
25 n.visit_children_with(self);
26
27 match &*n.href {
28 ImportHref::Url(u) => {
29 if let Some(s) = &u.value {
30 match &**s {
31 UrlValue::Str(s) => {
32 self.imports.push(s.value.clone());
33 }
34 UrlValue::Raw(v) => {
35 self.imports.push(v.value.clone());
36 }
37 }
38 }
39 }
40 ImportHref::Str(s) => {
41 self.imports.push(s.value.clone());
42 }
43 }
44 }
45
46 fn visit_declaration(&mut self, d: &Declaration) {
47 d.visit_children_with(self);
48
49 if let DeclarationName::Ident(name) = &d.name {
50 if &*name.value == "composes" {
51 if d.value.len() >= 3 {
53 match (&d.value[d.value.len() - 2], &d.value[d.value.len() - 1]) {
54 (ComponentValue::Ident(ident), ComponentValue::Str(s))
55 if ident.value == "from" =>
56 {
57 self.imports.push(s.value.clone());
58 }
59 _ => (),
60 }
61 }
62 }
63 }
64 }
65}