Skip to content

Commit cb23721

Browse files
feat(pack): support emotion react transform (#2682)
Co-authored-by: xusd320 <xusd320@gmail.com>
1 parent 7f9a9ae commit cb23721

20 files changed

Lines changed: 303 additions & 8 deletions

File tree

crates/pack-core/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ turbopack-ecmascript-plugins = { workspace = true, default-features = false }
7171

7272
[target.'cfg(not(all(target_family = "wasm", target_os = "unknown")))'.dependencies]
7373
turbo-tasks-fetch = { workspace = true }
74-
turbopack-ecmascript-plugins = { workspace = true, default-features = true }
74+
turbopack-ecmascript-plugins = { workspace = true, default-features = true, features = ["transform_emotion"] }
7575

7676
[features]
7777
default = ["process_pool"]

crates/pack-core/src/client/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use crate::{
4747
transforms::{
4848
css_modules::get_auto_css_modules_rule,
4949
default_export_namer::get_default_export_namer_rule,
50-
remove_console::get_remove_console_transform_rule,
50+
emotion::get_emotion_transform_rule, remove_console::get_remove_console_transform_rule,
5151
styled_components::get_styled_components_transform_rule,
5252
styled_jsx::get_styled_jsx_transform_rule,
5353
swc_ecma_transform_plugins::get_swc_ecma_transform_plugin_rule,
@@ -244,6 +244,7 @@ pub async fn get_client_module_options_context(
244244

245245
let additional_rules: Vec<ModuleRule> = vec![
246246
get_swc_ecma_transform_plugin_rule(config, project_path.clone()).await?,
247+
get_emotion_transform_rule(config).await?,
247248
get_styled_components_transform_rule(config).await?,
248249
get_styled_jsx_transform_rule(config, target_browsers).await?,
249250
get_remove_console_transform_rule(config).await?,

crates/pack-core/src/config.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ pub struct ReactConfig {
271271
#[serde(rename_all = "camelCase")]
272272
pub struct StyleConfig {
273273
pub styled_components: Option<StyledComponentsTransformOptionsOrBoolean>,
274+
pub emotion: Option<bool>,
274275
#[bincode(with = "turbo_bincode::serde_self_describing")]
275276
sass: Option<serde_json::Value>,
276277
#[bincode(with = "turbo_bincode::serde_self_describing")]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use anyhow::Result;
2+
use turbo_tasks::Vc;
3+
use turbopack::module_options::ModuleRule;
4+
use turbopack_ecmascript_plugins::transform::emotion::{
5+
EmotionTransformConfig, EmotionTransformer,
6+
};
7+
8+
use crate::{
9+
config::Config,
10+
shared::transforms::{EcmascriptTransformStage, get_ecma_transform_rule},
11+
};
12+
13+
pub async fn get_emotion_transform_rule(config: Vc<Config>) -> Result<Option<ModuleRule>> {
14+
let styles = config.styles().await?;
15+
let enabled = styles.emotion.unwrap_or(false);
16+
17+
if !enabled {
18+
return Ok(None);
19+
}
20+
21+
Ok(
22+
EmotionTransformer::new(&EmotionTransformConfig::default()).map(|transformer| {
23+
get_ecma_transform_rule(Box::new(transformer), false, EcmascriptTransformStage::Main)
24+
}),
25+
)
26+
}

crates/pack-core/src/shared/transforms/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use wasm::StaticWasmModuleType;
1111
pub mod classic_jsx_react_import;
1212
pub mod css_modules;
1313
pub mod default_export_namer;
14+
pub mod emotion;
1415
pub mod image;
1516
pub mod modularize_imports;
1617
pub mod remove_console;

crates/pack-core/src/transform_options.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,19 @@ pub async fn get_jsx_transform_options(
140140
enable_react_refresh: bool,
141141
) -> Result<Vc<JsxTransformOptions>> {
142142
let react_config = config.react().await?;
143+
let emotion_enabled = config.styles().await?.emotion.unwrap_or(false);
144+
145+
let import_source = react_config.import_source.clone().or_else(|| {
146+
if emotion_enabled {
147+
Some("@emotion/react".into())
148+
} else {
149+
Some("react".into())
150+
}
151+
});
143152

144153
let react_transform_options = JsxTransformOptions {
145154
development: mode.await?.is_react_development(),
146-
import_source: react_config.import_source.clone().or(Some("react".into())),
155+
import_source,
147156
runtime: react_config
148157
.runtime
149158
.as_ref()

crates/pack-schema/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@ pub struct SchemaStyleConfig {
734734
#[schemars(description = "Styled components configuration")]
735735
pub styled_components: Option<serde_json::Value>,
736736

737+
/// Enable @emotion/react transform support
738+
#[serde(skip_serializing_if = "Option::is_none")]
739+
#[schemars(description = "Enable @emotion/react transform support")]
740+
pub emotion: Option<bool>,
741+
737742
/// Sass configuration
738743
#[serde(skip_serializing_if = "Option::is_none")]
739744
#[schemars(description = "Sass configuration")]

crates/pack-tests/tests/snapshot/basic/jsx/output/_project___e95e9ae8.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pack-tests/tests/snapshot/basic/jsx/output/_project___e95e9ae8.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pack-tests/tests/snapshot/node_modules/@emotion/react/index.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)