File size: 5,089 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
#![allow(unused)]
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};
#[derive(
Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue, OperationValue,
)]
#[serde(rename_all = "kebab-case")]
pub enum EmotionLabelKind {
DevOnly,
Always,
Never,
}
#[derive(
Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue, OperationValue,
)]
#[serde(rename_all = "camelCase")]
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),
}
}
}
#[derive(
Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize, NonLocalValue, OperationValue,
)]
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>;
#[turbo_tasks::value(shared, operation)]
#[derive(Default, Clone, Debug)]
#[serde(rename_all = "camelCase")]
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>>,
}
#[turbo_tasks::value_impl]
impl EmotionTransformConfig {
#[turbo_tasks::function]
pub fn default_private() -> Vc<Self> {
Self::cell(Default::default())
}
}
impl ValueDefault for EmotionTransformConfig {
fn value_default() -> Vc<Self> {
EmotionTransformConfig::default_private()
}
}
#[derive(Debug)]
pub struct EmotionTransformer {
#[cfg(feature = "transform_emotion")]
config: swc_emotion::EmotionOptions,
}
#[cfg(feature = "transform_emotion")]
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 })
}
}
#[cfg(not(feature = "transform_emotion"))]
impl EmotionTransformer {
pub fn new(_config: &EmotionTransformConfig) -> Option<Self> {
None
}
}
#[async_trait]
impl CustomTransformer for EmotionTransformer {
#[tracing::instrument(level = tracing::Level::TRACE, name = "emotion", skip_all)]
async fn transform(&self, program: &mut Program, ctx: &TransformContext<'_>) -> Result<()> {
#[cfg(feature = "transform_emotion")]
{
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(())
}
}
|