From 601f5c1d71af43404e92e2e1139e880771f3b853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 29 Oct 2024 10:52:02 +0900 Subject: [PATCH] feat(emotion): Make it configurable from Rust code (#354) --- Cargo.lock | 2 +- packages/emotion/CHANGELOG.md | 6 +++++ packages/emotion/README.md | 6 +++++ packages/emotion/package.json | 2 +- packages/emotion/transform/Cargo.toml | 2 +- packages/emotion/transform/src/import_map.rs | 26 ++++++++++++-------- packages/emotion/transform/src/lib.rs | 3 ++- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 47fc26f1e..184600223 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2852,7 +2852,7 @@ dependencies = [ [[package]] name = "swc_emotion" -version = "0.72.24" +version = "0.72.25" dependencies = [ "base64 0.22.1", "byteorder", diff --git a/packages/emotion/CHANGELOG.md b/packages/emotion/CHANGELOG.md index 6f1123b9b..9bd5610ea 100644 --- a/packages/emotion/CHANGELOG.md +++ b/packages/emotion/CHANGELOG.md @@ -1,5 +1,11 @@ # @swc/plugin-emotion +## 5.0.0 + +### Major Changes + +- f5e393d: Make it configurable from Rust code + ## 4.0.3 ### Patch Changes diff --git a/packages/emotion/README.md b/packages/emotion/README.md index 8eeaeaf17..dc331c929 100644 --- a/packages/emotion/README.md +++ b/packages/emotion/README.md @@ -34,6 +34,12 @@ Source code for plugin itself (not transforms) are copied from https://github.co # @swc/plugin-emotion +## 5.0.0 + +### Major Changes + +- f5e393d: Make it configurable from Rust code + ## 4.0.3 ### Patch Changes diff --git a/packages/emotion/package.json b/packages/emotion/package.json index 557d10fba..e25b9e207 100644 --- a/packages/emotion/package.json +++ b/packages/emotion/package.json @@ -1,6 +1,6 @@ { "name": "@swc/plugin-emotion", - "version": "4.0.3", + "version": "5.0.0", "description": "SWC plugin for emotion css-in-js library", "main": "swc_plugin_emotion.wasm", "scripts": { diff --git a/packages/emotion/transform/Cargo.toml b/packages/emotion/transform/Cargo.toml index a62d93a6b..93941ac0f 100644 --- a/packages/emotion/transform/Cargo.toml +++ b/packages/emotion/transform/Cargo.toml @@ -7,7 +7,7 @@ license = { workspace = true } name = "swc_emotion" repository = { workspace = true } rust-version = { workspace = true } -version = "0.72.24" +version = "0.72.25" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/packages/emotion/transform/src/import_map.rs b/packages/emotion/transform/src/import_map.rs index 71c57acf3..f9a21052c 100644 --- a/packages/emotion/transform/src/import_map.rs +++ b/packages/emotion/transform/src/import_map.rs @@ -8,17 +8,18 @@ use crate::{EmotionModuleConfig, ExportItem}; pub type ImportMap = AHashMap; /// key: `localExportName` -pub type ImportMapValue = AHashMap; +pub type ImportMapValue = AHashMap; #[derive(Debug, Clone, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct Config { - canonical_import: ImportItem, +#[serde(rename_all = "camelCase", deny_unknown_fields)] +pub struct ImportItemConfig { + pub canonical_import: ItemSpecifier, + pub styled_base_import: Option, } /// `(packageName, exportName)` #[derive(Debug, Clone, Serialize, Deserialize)] -pub(crate) struct ImportItem(JsWord, JsWord); +pub struct ItemSpecifier(pub JsWord, pub JsWord); pub(crate) fn expand_import_map( map: Option<&ImportMap>, @@ -26,10 +27,14 @@ pub(crate) fn expand_import_map( ) -> Vec { if let Some(map) = map { map.iter().for_each(|(import_source, value)| { - value - .iter() - .for_each(|(local_export_name, Config { canonical_import })| { - let ImportItem(package_name, export_name) = canonical_import; + value.iter().for_each( + |( + local_export_name, + ImportItemConfig { + canonical_import, .. + }, + )| { + let ItemSpecifier(package_name, export_name) = canonical_import; if &**package_name == "@emotion/react" && &**export_name == "jsx" { return; @@ -73,7 +78,8 @@ pub(crate) fn expand_import_map( }], default_export: package_transformers.default_export, }); - }) + }, + ) }); } diff --git a/packages/emotion/transform/src/lib.rs b/packages/emotion/transform/src/lib.rs index d2294d0c9..1d10e8fe8 100644 --- a/packages/emotion/transform/src/lib.rs +++ b/packages/emotion/transform/src/lib.rs @@ -6,7 +6,6 @@ use std::{ use base64::Engine; use fxhash::FxHashMap; -use import_map::ImportMap; use once_cell::sync::Lazy; use regex::{Regex, RegexBuilder}; use serde::{Deserialize, Serialize}; @@ -24,6 +23,8 @@ use swc_ecma_utils::ExprFactory; use swc_ecma_visit::{Fold, FoldWith}; use swc_trace_macro::swc_trace; +pub use crate::import_map::*; + mod import_map; static EMOTION_OFFICIAL_LIBRARIES: Lazy> = Lazy::new(|| {