Skip to content

Commit

Permalink
feat(turbopack): support initial compiler.emotion flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 5, 2023
1 parent 3f75387 commit c218808
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::{
},
react_refresh::assert_can_resolve_react_refresh,
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_typescript_transform_options,
},
util::foreign_code_context_condition,
Expand Down Expand Up @@ -174,6 +174,8 @@ pub async fn get_client_module_options_context(
.clone_if()
};

let emotion_compiler_options = get_emotion_compiler_config(next_config);

let module_options_context = ModuleOptionsContext {
preset_env_versions: Some(env),
execution_context: Some(execution_context),
Expand All @@ -185,7 +187,7 @@ pub async fn get_client_module_options_context(
// we try resolve it once at the root and pass down a context to all
// the modules.
enable_jsx: Some(jsx_runtime_options),
enable_emotion: true,
enable_emotion: Some(emotion_compiler_options),
enable_react_refresh,
enable_styled_components: true,
enable_styled_jsx: true,
Expand Down
6 changes: 5 additions & 1 deletion packages/next-swc/crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ pub struct NextConfig {
pub rewrites: Rewrites,
pub transpile_packages: Option<Vec<String>>,

// Partially supported
pub compiler: Option<CompilerConfig>,

// unsupported
cross_origin: Option<String>,
compiler: Option<CompilerConfig>,
amp: AmpConfig,
analytics_id: String,
asset_prefix: String,
Expand Down Expand Up @@ -353,6 +355,7 @@ pub struct ExperimentalConfig {
pub app_dir: Option<bool>,
pub server_components_external_packages: Option<Vec<String>>,
pub turbo: Option<ExperimentalTurboConfig>,
pub compiler: Option<CompilerConfig>,

// unsupported
adjust_font_fallbacks: Option<bool>,
Expand Down Expand Up @@ -420,6 +423,7 @@ enum MiddlewarePrefetchType {
pub struct CompilerConfig {
pub react_remove_properties: Option<bool>,
pub relay: Option<RelayConfig>,
pub emotion: Option<serde_json::Value>,
pub remove_console: Option<RemoveConsoleConfig>,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
next_config::NextConfigVc,
next_import_map::get_next_server_import_map,
transform_options::{
get_decorators_transform_options, get_jsx_transform_options,
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_typescript_transform_options,
},
util::foreign_code_context_condition,
Expand Down Expand Up @@ -235,6 +235,7 @@ pub async fn get_server_module_options_context(
let tsconfig = get_typescript_transform_options(project_path);
let decorators_options = get_decorators_transform_options(project_path);
let jsx_runtime_options = get_jsx_transform_options(project_path);
let emotion_compiler_options = get_emotion_compiler_config(next_config);

let module_options_context = match ty.into_value() {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
Expand All @@ -245,6 +246,7 @@ pub async fn get_server_module_options_context(
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_styled_jsx: true,
enable_emotion: Some(emotion_compiler_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand All @@ -265,6 +267,7 @@ pub async fn get_server_module_options_context(
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_styled_jsx: true,
enable_emotion: Some(emotion_compiler_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand All @@ -287,6 +290,7 @@ pub async fn get_server_module_options_context(
};
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_emotion: Some(emotion_compiler_options),
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand Down Expand Up @@ -324,6 +328,7 @@ pub async fn get_server_module_options_context(
};
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_emotion: Some(emotion_compiler_options),
enable_styled_jsx: true,
enable_postcss_transform,
enable_webpack_loaders,
Expand Down
41 changes: 40 additions & 1 deletion packages/next-swc/crates/next-core/src/transform_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ use turbo_binding::turbopack::{
},
ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig},
turbopack::module_options::{
DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, JsxTransformOptions,
DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, EmotionLabelKind,
EmotionTransformOptions, EmotionTransformOptionsVc, JsxTransformOptions,
JsxTransformOptionsVc, TypescriptTransformOptions, TypescriptTransformOptionsVc,
},
};
Expand Down Expand Up @@ -152,3 +153,41 @@ pub async fn get_jsx_transform_options(

Ok(react_transform_options.cell())
}

#[turbo_tasks::function]
pub async fn get_emotion_compiler_config(
next_config: NextConfigVc,
) -> Result<EmotionTransformOptionsVc> {
let emotion_compiler_config = (*next_config.await?)
.compiler
.as_ref()
.map(|value| {
value
.emotion
.as_ref()
.map(|value| {
if value.is_boolean() {
EmotionTransformOptionsVc::cell(EmotionTransformOptions {
enabled: value.as_bool().unwrap_or(false),
..Default::default()
})
} else {
EmotionTransformOptionsVc::cell(EmotionTransformOptions {
enabled: true,
sourcemap: value["sourcemap"].as_bool(),
auto_label: match value["auto_label"].to_string().as_str() {
"dev-only" => Some(EmotionLabelKind::DevOnly),
"always" => Some(EmotionLabelKind::Always),
"never" => Some(EmotionLabelKind::Never),
_ => None,
},
label_format: value["labelFormat"].as_str().map(|s| s.to_string()),
})
}
})
.unwrap_or_default()
})
.unwrap_or_default();

Ok(emotion_compiler_config)
}
1 change: 1 addition & 0 deletions packages/next/src/lib/turbopack-warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const supportedTurbopackNextConfigOptions = [
'configFileName',
'env',
'experimental.appDir',
'compiler.emotion',
'experimental.serverComponentsExternalPackages',
'experimental.turbo',
'images',
Expand Down

0 comments on commit c218808

Please sign in to comment.