|
| 1 | +#![allow(unused)] |
| 2 | +use std::{ |
| 3 | + hash::{Hash, Hasher}, |
| 4 | + path::Path, |
| 5 | +}; |
| 6 | + |
| 7 | +use anyhow::Result; |
| 8 | +use serde::{Deserialize, Serialize}; |
| 9 | +use swc_core::{ |
| 10 | + common::util::take::Take, |
| 11 | + ecma::{ |
| 12 | + ast::{Module, Program}, |
| 13 | + visit::FoldWith, |
| 14 | + }, |
| 15 | +}; |
| 16 | +use turbo_tasks::trace::TraceRawVcs; |
| 17 | +use turbopack_ecmascript::{CustomTransformer, TransformContext}; |
| 18 | + |
| 19 | +#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize)] |
| 20 | +#[serde(rename_all = "kebab-case")] |
| 21 | +pub enum EmotionLabelKind { |
| 22 | + DevOnly, |
| 23 | + Always, |
| 24 | + Never, |
| 25 | +} |
| 26 | + |
| 27 | +#[turbo_tasks::value(transparent)] |
| 28 | +pub struct OptionEmotionTransformConfig(Option<EmotionTransformConfigVc>); |
| 29 | + |
| 30 | +//[TODO]: need to support importmap, there are type mismatch between |
| 31 | +//next.config.js to swc's emotion options |
| 32 | +#[turbo_tasks::value(shared)] |
| 33 | +#[derive(Default, Clone, Debug)] |
| 34 | +#[serde(rename_all = "camelCase")] |
| 35 | +pub struct EmotionTransformConfig { |
| 36 | + pub sourcemap: Option<bool>, |
| 37 | + pub label_format: Option<String>, |
| 38 | + pub auto_label: Option<EmotionLabelKind>, |
| 39 | +} |
| 40 | + |
| 41 | +#[turbo_tasks::value_impl] |
| 42 | +impl EmotionTransformConfigVc { |
| 43 | + #[turbo_tasks::function] |
| 44 | + pub fn default() -> Self { |
| 45 | + Self::cell(Default::default()) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl Default for EmotionTransformConfigVc { |
| 50 | + fn default() -> Self { |
| 51 | + Self::default() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#[derive(Debug)] |
| 56 | +pub struct EmotionTransformer { |
| 57 | + #[cfg(feature = "transform_emotion")] |
| 58 | + config: swc_emotion::EmotionOptions, |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(feature = "transform_emotion")] |
| 62 | +impl EmotionTransformer { |
| 63 | + pub fn new(config: &EmotionTransformConfig) -> Option<Self> { |
| 64 | + let config = swc_emotion::EmotionOptions { |
| 65 | + // When you create a transformer structure, it is assumed that you are performing an |
| 66 | + // emotion transform. |
| 67 | + enabled: Some(true), |
| 68 | + sourcemap: config.sourcemap, |
| 69 | + label_format: config.label_format.clone(), |
| 70 | + auto_label: if let Some(auto_label) = config.auto_label.as_ref() { |
| 71 | + match auto_label { |
| 72 | + EmotionLabelKind::Always => Some(true), |
| 73 | + EmotionLabelKind::Never => Some(false), |
| 74 | + // [TODO]: this is not correct coerece, need to be fixed |
| 75 | + EmotionLabelKind::DevOnly => None, |
| 76 | + } |
| 77 | + } else { |
| 78 | + None |
| 79 | + }, |
| 80 | + ..Default::default() |
| 81 | + }; |
| 82 | + |
| 83 | + Some(EmotionTransformer { config }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +#[cfg(not(feature = "transform_emotion"))] |
| 88 | +impl EmotionTransformer { |
| 89 | + pub fn new(_config: &EmotionTransformConfig) -> Option<Self> { |
| 90 | + None |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl CustomTransformer for EmotionTransformer { |
| 95 | + fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Option<Program> { |
| 96 | + #[cfg(feature = "transform_emotion")] |
| 97 | + { |
| 98 | + let p = std::mem::replace(program, Program::Module(Module::dummy())); |
| 99 | + let hash = { |
| 100 | + #[allow(clippy::disallowed_types)] |
| 101 | + let mut hasher = std::collections::hash_map::DefaultHasher::new(); |
| 102 | + p.hash(&mut hasher); |
| 103 | + hasher.finish() |
| 104 | + }; |
| 105 | + *program = p.fold_with(&mut swc_emotion::emotion( |
| 106 | + self.config.clone(), |
| 107 | + Path::new(ctx.file_name_str), |
| 108 | + hash as u32, |
| 109 | + ctx.source_map.clone(), |
| 110 | + ctx.comments.clone(), |
| 111 | + )); |
| 112 | + } |
| 113 | + |
| 114 | + None |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +pub async fn build_emotion_transformer( |
| 119 | + config: &Option<EmotionTransformConfigVc>, |
| 120 | +) -> Result<Option<Box<EmotionTransformer>>> { |
| 121 | + Ok(if let Some(config) = config { |
| 122 | + EmotionTransformer::new(&*config.await?).map(Box::new) |
| 123 | + } else { |
| 124 | + None |
| 125 | + }) |
| 126 | +} |
0 commit comments