Skip to content

Commit

Permalink
feat(emotion): Make it configurable from Rust code (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Oct 29, 2024
1 parent b87b782 commit 601f5c1
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/emotion/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @swc/plugin-emotion

## 5.0.0

### Major Changes

- f5e393d: Make it configurable from Rust code

## 4.0.3

### Patch Changes
Expand Down
6 changes: 6 additions & 0 deletions packages/emotion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion/transform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 16 additions & 10 deletions packages/emotion/transform/src/import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@ use crate::{EmotionModuleConfig, ExportItem};
pub type ImportMap = AHashMap<JsWord, ImportMapValue>;

/// key: `localExportName`
pub type ImportMapValue = AHashMap<JsWord, Config>;
pub type ImportMapValue = AHashMap<JsWord, ImportItemConfig>;

#[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<ItemSpecifier>,
}

/// `(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>,
mut imports: Vec<EmotionModuleConfig>,
) -> Vec<EmotionModuleConfig> {
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;
Expand Down Expand Up @@ -73,7 +78,8 @@ pub(crate) fn expand_import_map(
}],
default_export: package_transformers.default_export,
});
})
},
)
});
}

Expand Down
3 changes: 2 additions & 1 deletion packages/emotion/transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<Vec<EmotionModuleConfig>> = Lazy::new(|| {
Expand Down

0 comments on commit 601f5c1

Please sign in to comment.