这是indexloc提供的服务,不要输入任何密码
Skip to content

feat(ecmascript): support configurable emotion options #4482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions crates/turbopack-ecmascript/src/transform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ pub enum EcmascriptInputTransform {
ServerDirective(StringVc),
CommonJs,
Custom(CustomTransformVc),
Emotion,
Emotion {
#[serde(default)]
sourcemap: bool,
label_format: OptionStringVc,
auto_label: Option<bool>,
},
PresetEnv(EnvironmentVc),
React {
#[serde(default)]
Expand Down Expand Up @@ -185,10 +190,24 @@ impl EcmascriptInputTransform {
Some(comments.clone()),
));
}
EcmascriptInputTransform::Emotion => {
EcmascriptInputTransform::Emotion {
sourcemap,
label_format,
auto_label,
} => {
let options = swc_emotion::EmotionOptions {
// this should be always enabled if match arrives here:
// since moduleoptions expect to push emotion transform only if
// there are valid, enabled config values.
enabled: Some(true),
sourcemap: Some(*sourcemap),
label_format: label_format.await?.clone_value(),
auto_label: *auto_label,
..Default::default()
};
let p = std::mem::replace(program, Program::Module(Module::dummy()));
*program = p.fold_with(&mut swc_emotion::emotion(
Default::default(),
options,
Path::new(file_name_str),
source_map.clone(),
comments.clone(),
Expand Down
9 changes: 7 additions & 2 deletions crates/turbopack-tests/tests/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ use turbo_tasks_memory::MemoryBackend;
use turbopack::{
condition::ContextCondition,
ecmascript::EcmascriptModuleAssetVc,
module_options::{JsxTransformOptions, JsxTransformOptionsVc, ModuleOptionsContext},
module_options::{
EmotionTransformConfig, JsxTransformOptions, JsxTransformOptionsVc, ModuleOptionsContext,
},
resolve_options_context::ResolveOptionsContext,
transition::TransitionsByNameVc,
ModuleAssetContextVc,
Expand Down Expand Up @@ -193,7 +195,10 @@ async fn run_test(resource: &str) -> Result<FileSystemPathVc> {
enable_jsx: Some(JsxTransformOptionsVc::cell(JsxTransformOptions {
..Default::default()
})),
enable_emotion: true,
enable_emotion: Some(EmotionTransformConfig::cell(EmotionTransformConfig {
sourcemap: Some(false),
..Default::default()
})),
enable_styled_components: true,
preset_env_versions: Some(env),
rules: vec![(
Expand Down

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

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

20 changes: 17 additions & 3 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl ModuleOptionsVc {
) -> Result<ModuleOptionsVc> {
let ModuleOptionsContext {
enable_jsx,
enable_emotion,
ref enable_emotion,
enable_react_refresh,
enable_styled_jsx,
enable_styled_components,
Expand Down Expand Up @@ -96,8 +96,22 @@ impl ModuleOptionsVc {
if enable_styled_jsx {
transforms.push(EcmascriptInputTransform::StyledJsx);
}
if enable_emotion {
transforms.push(EcmascriptInputTransform::Emotion);
if let Some(enable_emotion) = enable_emotion {
let emotion_transform = enable_emotion.await?;
transforms.push(EcmascriptInputTransform::Emotion {
sourcemap: emotion_transform.sourcemap.unwrap_or(false),
label_format: OptionStringVc::cell(emotion_transform.label_format.clone()),
auto_label: if let Some(auto_label) = emotion_transform.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
},
});
}
if enable_styled_components {
transforms.push(EcmascriptInputTransform::StyledComponents);
Expand Down
38 changes: 37 additions & 1 deletion crates/turbopack/src/module_options/module_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ impl Default for TypescriptTransformOptionsVc {
}
}

#[derive(Clone, PartialEq, Eq, Debug, TraceRawVcs, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum EmotionLabelKind {
DevOnly,
Always,
Never,
}

#[turbo_tasks::value(transparent)]
pub struct OptionEmotionTransformConfig(Option<EmotionTransformConfigVc>);

//[TODO]: need to support importmap, there are type mismatch between
//[TODO]: next.config.js to swc's emotion options
#[turbo_tasks::value(shared)]
#[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>,
}

#[turbo_tasks::value_impl]
impl EmotionTransformConfigVc {
#[turbo_tasks::function]
pub fn default() -> Self {
Self::cell(Default::default())
}
}

impl Default for EmotionTransformConfigVc {
fn default() -> Self {
Self::default()
}
}

impl WebpackLoadersOptions {
pub fn is_empty(&self) -> bool {
self.extension_to_loaders.is_empty()
Expand Down Expand Up @@ -117,7 +153,7 @@ pub struct ModuleOptionsContext {
#[serde(default)]
pub enable_jsx: Option<JsxTransformOptionsVc>,
#[serde(default)]
pub enable_emotion: bool,
pub enable_emotion: Option<EmotionTransformConfigVc>,
#[serde(default)]
pub enable_react_refresh: bool,
#[serde(default)]
Expand Down