react-code-dataset
/
next.js
/turbopack
/crates
/turbopack-ecmascript-plugins
/src
/transform
/emotion.rs
| use std::{ | |
| hash::{Hash, Hasher}, | |
| path::Path, | |
| }; | |
| use anyhow::Result; | |
| use async_trait::async_trait; | |
| use indexmap::IndexMap; | |
| use rustc_hash::{FxBuildHasher, FxHasher}; | |
| use serde::{Deserialize, Serialize}; | |
| use swc_core::{ | |
| atoms::Atom, | |
| common::util::take::Take, | |
| ecma::{ | |
| ast::{Module, Program}, | |
| visit::FoldWith, | |
| }, | |
| }; | |
| use swc_emotion::ImportMap; | |
| use turbo_rcstr::RcStr; | |
| use turbo_tasks::{NonLocalValue, OperationValue, ValueDefault, Vc, trace::TraceRawVcs}; | |
| use turbopack_ecmascript::{CustomTransformer, TransformContext}; | |
| pub enum EmotionLabelKind { | |
| DevOnly, | |
| Always, | |
| Never, | |
| } | |
| pub struct EmotionImportItemConfig { | |
| pub canonical_import: EmotionItemSpecifier, | |
| pub styled_base_import: Option<EmotionItemSpecifier>, | |
| } | |
| impl From<&EmotionImportItemConfig> for swc_emotion::ImportItemConfig { | |
| fn from(value: &EmotionImportItemConfig) -> Self { | |
| swc_emotion::ImportItemConfig { | |
| canonical_import: From::from(&value.canonical_import), | |
| styled_base_import: value.styled_base_import.as_ref().map(From::from), | |
| } | |
| } | |
| } | |
| pub struct EmotionItemSpecifier(pub RcStr, pub RcStr); | |
| impl From<&EmotionItemSpecifier> for swc_emotion::ItemSpecifier { | |
| fn from(value: &EmotionItemSpecifier) -> Self { | |
| swc_emotion::ItemSpecifier(value.0.as_str().into(), value.1.as_str().into()) | |
| } | |
| } | |
| pub type EmotionImportMapValue = IndexMap<RcStr, EmotionImportItemConfig, FxBuildHasher>; | |
| pub struct EmotionTransformConfig { | |
| pub sourcemap: Option<bool>, | |
| pub label_format: Option<String>, | |
| pub auto_label: Option<EmotionLabelKind>, | |
| pub import_map: Option<IndexMap<RcStr, EmotionImportMapValue, FxBuildHasher>>, | |
| } | |
| impl EmotionTransformConfig { | |
| pub fn default_private() -> Vc<Self> { | |
| Self::cell(Default::default()) | |
| } | |
| } | |
| impl ValueDefault for EmotionTransformConfig { | |
| fn value_default() -> Vc<Self> { | |
| EmotionTransformConfig::default_private() | |
| } | |
| } | |
| pub struct EmotionTransformer { | |
| config: swc_emotion::EmotionOptions, | |
| } | |
| impl EmotionTransformer { | |
| pub fn new(config: &EmotionTransformConfig) -> Option<Self> { | |
| let config = swc_emotion::EmotionOptions { | |
| // When you create a transformer structure, it is assumed that you are performing an | |
| // emotion transform. | |
| enabled: Some(true), | |
| sourcemap: config.sourcemap, | |
| label_format: config.label_format.as_deref().map(From::from), | |
| auto_label: if let Some(auto_label) = config.auto_label.as_ref() { | |
| match auto_label { | |
| EmotionLabelKind::Always => Some(true), | |
| EmotionLabelKind::Never => Some(false), | |
| // [TODO]: this is not correct coerece, need to be fixed | |
| EmotionLabelKind::DevOnly => None, | |
| } | |
| } else { | |
| None | |
| }, | |
| import_map: config.import_map.as_ref().map(|map| { | |
| map.iter() | |
| .map(|(k, v)| { | |
| ( | |
| k.as_str().into(), | |
| swc_emotion::ImportMapValue::from_iter(v.iter().map(|(k, v)| { | |
| (k.as_str().into(), swc_emotion::ImportItemConfig::from(v)) | |
| })), | |
| ) | |
| }) | |
| .collect() | |
| }), | |
| }; | |
| Some(EmotionTransformer { config }) | |
| } | |
| } | |
| impl EmotionTransformer { | |
| pub fn new(_config: &EmotionTransformConfig) -> Option<Self> { | |
| None | |
| } | |
| } | |
| impl CustomTransformer for EmotionTransformer { | |
| async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> { | |
| { | |
| let hash = { | |
| let mut hasher = FxHasher::default(); | |
| program.hash(&mut hasher); | |
| hasher.finish() | |
| }; | |
| program.mutate(swc_emotion::emotion( | |
| &self.config, | |
| Path::new(ctx.file_name_str), | |
| hash as u32, | |
| ctx.source_map.clone(), | |
| ctx.comments.clone(), | |
| )); | |
| } | |
| Ok(()) | |
| } | |
| } | |