Unverified Commit 76824e75 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(turbopack): Apply `import_map` option of `swc_emotion` correctly (#71776)

### What?

Preserve the `import_map` config specified by the user

### Why?

The default mode (webpack mode) supports the `import_map` config, so turbopack should also support it.

### How?

Closes https://github.com/vercel/next.js/issues/71408
parent 2b54a007
'use client' 'use client'
import { styledCss } from 'import-map-test'
const red = styledCss`
color: red;
`
export default function Page() { export default function Page() {
return <h1 css={{ color: 'blue' }}>Blue</h1> return (
<>
<h1 css={{ color: 'blue' }}>Blue</h1>
<p css={red}>Red</p>
</>
)
} }
...@@ -26,5 +26,15 @@ describe('app dir - emotion-js', () => { ...@@ -26,5 +26,15 @@ describe('app dir - emotion-js', () => {
), ),
'rgb(0, 0, 255)' 'rgb(0, 0, 255)'
) )
const el2 = browser.elementByCss('p')
expect(await el2.text()).toBe('Red')
await check(
async () =>
await browser.eval(
`window.getComputedStyle(document.querySelector('p')).color`
),
'rgb(255, 0, 0)'
)
}) })
}) })
...@@ -2,6 +2,14 @@ ...@@ -2,6 +2,14 @@
module.exports = { module.exports = {
reactStrictMode: true, reactStrictMode: true,
compiler: { compiler: {
emotion: true, emotion: {
importMap: {
'import-map-test': {
styledCss: {
canonicalImport: ['@emotion/react', 'css'],
},
},
},
},
}, },
} }
export { css as styledCss } from '@emotion/react'
...@@ -6,15 +6,19 @@ use std::{ ...@@ -6,15 +6,19 @@ use std::{
use anyhow::Result; use anyhow::Result;
use async_trait::async_trait; use async_trait::async_trait;
use rustc_hash::FxHasher; use indexmap::IndexMap;
use rustc_hash::{FxBuildHasher, FxHasher};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use swc_core::{ use swc_core::{
atoms::Atom,
common::util::take::Take, common::util::take::Take,
ecma::{ ecma::{
ast::{Module, Program}, ast::{Module, Program},
visit::FoldWith, visit::FoldWith,
}, },
}; };
use swc_emotion::ImportMap;
use turbo_rcstr::RcStr;
use turbo_tasks::{trace::TraceRawVcs, NonLocalValue, OperationValue, ValueDefault, Vc}; use turbo_tasks::{trace::TraceRawVcs, NonLocalValue, OperationValue, ValueDefault, Vc};
use turbopack_ecmascript::{CustomTransformer, TransformContext}; use turbopack_ecmascript::{CustomTransformer, TransformContext};
...@@ -28,8 +32,37 @@ pub enum EmotionLabelKind { ...@@ -28,8 +32,37 @@ pub enum EmotionLabelKind {
Never, Never,
} }
//[TODO]: need to support importmap, there are type mismatch between #[derive(
//next.config.js to swc's emotion options 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)] #[turbo_tasks::value(shared, operation)]
#[derive(Default, Clone, Debug)] #[derive(Default, Clone, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
...@@ -37,6 +70,7 @@ pub struct EmotionTransformConfig { ...@@ -37,6 +70,7 @@ pub struct EmotionTransformConfig {
pub sourcemap: Option<bool>, pub sourcemap: Option<bool>,
pub label_format: Option<String>, pub label_format: Option<String>,
pub auto_label: Option<EmotionLabelKind>, pub auto_label: Option<EmotionLabelKind>,
pub import_map: Option<IndexMap<RcStr, EmotionImportMapValue, FxBuildHasher>>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -78,7 +112,18 @@ impl EmotionTransformer { ...@@ -78,7 +112,18 @@ impl EmotionTransformer {
} else { } else {
None None
}, },
..Default::default() 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 }) Some(EmotionTransformer { config })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment