Skip to content

Commit

Permalink
refactor(next-core): move transforms to plugin (#49621)
Browse files Browse the repository at this point in the history
### What? 

relateed to WEB-1048.

Moving emotion to the plugin, deprecate enable_emotion option.
  • Loading branch information
kwonoj committed May 16, 2023
1 parent 05a9e0a commit de08f8b
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 118 deletions.
21 changes: 11 additions & 10 deletions packages/next-swc/crates/next-core/src/next_client/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,16 @@ use crate::{
get_next_client_resolved_map, mdx_import_source_file,
},
next_shared::{
resolve::UnsupportedModulesResolvePluginVc, transforms::get_relay_transform_plugin,
resolve::UnsupportedModulesResolvePluginVc,
transforms::{
emotion::get_emotion_transform_plugin, get_relay_transform_plugin,
styled_components::get_styled_components_transform_plugin,
styled_jsx::get_styled_jsx_transform_plugin,
},
},
transform_options::{
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options,
get_decorators_transform_options, get_jsx_transform_options,
get_typescript_transform_options,
},
util::foreign_code_context_condition,
};
Expand Down Expand Up @@ -201,10 +206,11 @@ pub async fn get_client_module_options_context(
.clone_if()
};

let enable_emotion = *get_emotion_compiler_config(next_config).await?;

let source_transforms = vec![
*get_relay_transform_plugin(next_config).await?,
*get_emotion_transform_plugin(next_config).await?,
*get_styled_components_transform_plugin(next_config).await?,
*get_styled_jsx_transform_plugin().await?,
Some(TransformPluginVc::cell(Box::new(
ServerDirectiveTransformer::new(
// ServerDirective is not implemented yet and always reports an issue.
Expand Down Expand Up @@ -238,16 +244,11 @@ pub async fn get_client_module_options_context(
..Default::default()
};

let enable_styled_components = *get_styled_components_compiler_config(next_config).await?;

let module_options_context = ModuleOptionsContext {
// We don't need to resolve React Refresh for each module. Instead,
// 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,
enable_styled_components,
enable_styled_jsx: true,
enable_postcss_transform: postcss_transform_options,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand Down
93 changes: 66 additions & 27 deletions packages/next-swc/crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,16 @@ use crate::{
next_import_map::{get_next_server_import_map, mdx_import_source_file},
next_server::resolve::ExternalPredicate,
next_shared::{
resolve::UnsupportedModulesResolvePluginVc, transforms::get_relay_transform_plugin,
resolve::UnsupportedModulesResolvePluginVc,
transforms::{
emotion::get_emotion_transform_plugin, get_relay_transform_plugin,
styled_components::get_styled_components_transform_plugin,
styled_jsx::get_styled_jsx_transform_plugin,
},
},
transform_options::{
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options,
get_decorators_transform_options, get_jsx_transform_options,
get_typescript_transform_options,
},
util::foreign_code_context_condition,
};
Expand Down Expand Up @@ -281,6 +286,10 @@ pub async fn get_server_module_options_context(
.clone_if()
};

// EcmascriptTransformPlugins for custom transforms
let styled_components_transform_plugin =
*get_styled_components_transform_plugin(next_config).await?;
let styled_jsx_transform_plugin = *get_styled_jsx_transform_plugin().await?;
let client_directive_transform_plugin = Some(TransformPluginVc::cell(Box::new(
ClientDirectiveTransformer::new(&StringVc::cell("server-to-client".to_string())),
)));
Expand All @@ -292,6 +301,7 @@ pub async fn get_server_module_options_context(
),
)));

// ModuleOptionsContext related options
let tsconfig = get_typescript_transform_options(project_path);
let decorators_options = get_decorators_transform_options(project_path);
let enable_mdx_rs = if *next_config.mdx_rs().await? {
Expand All @@ -305,13 +315,15 @@ pub async fn get_server_module_options_context(
None
};
let jsx_runtime_options = get_jsx_transform_options(project_path, None);
let enable_emotion = *get_emotion_compiler_config(next_config).await?;
let enable_styled_components = *get_styled_components_compiler_config(next_config).await?;

let mut source_transforms = vec![];
if let Some(relay_transform_plugin) = *get_relay_transform_plugin(next_config).await? {
source_transforms.push(relay_transform_plugin);
}
let source_transforms: Vec<TransformPluginVc> = vec![
*get_relay_transform_plugin(next_config).await?,
*get_emotion_transform_plugin(next_config).await?,
]
.into_iter()
.flatten()
.collect();

let output_transforms = vec![];

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
Expand All @@ -323,6 +335,23 @@ pub async fn get_server_module_options_context(

let module_options_context = match ty.into_value() {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> = vec![
styled_components_transform_plugin,
styled_jsx_transform_plugin,
]
.into_iter()
.flatten()
.collect();

base_source_transforms.extend(source_transforms);

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms: base_source_transforms,
output_transforms,
},
));

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
..Default::default()
Expand All @@ -336,9 +365,6 @@ pub async fn get_server_module_options_context(

ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_styled_jsx: true,
enable_emotion,
enable_styled_components,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand All @@ -360,11 +386,14 @@ pub async fn get_server_module_options_context(
}
}
ServerContextType::AppSSR { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> =
vec![server_directive_transform_plugin]
.into_iter()
.flatten()
.collect();
let mut base_source_transforms: Vec<TransformPluginVc> = vec![
styled_components_transform_plugin,
styled_jsx_transform_plugin,
server_directive_transform_plugin,
]
.into_iter()
.flatten()
.collect();

let base_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
Expand All @@ -373,7 +402,7 @@ pub async fn get_server_module_options_context(
},
));

base_source_transforms.extend(source_transforms.clone());
base_source_transforms.extend(source_transforms);

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
Expand All @@ -394,9 +423,6 @@ pub async fn get_server_module_options_context(

ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_styled_jsx: true,
enable_emotion,
enable_styled_components,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand All @@ -419,6 +445,7 @@ pub async fn get_server_module_options_context(
}
ServerContextType::AppRSC { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> = vec![
styled_components_transform_plugin,
client_directive_transform_plugin,
server_directive_transform_plugin,
]
Expand All @@ -433,7 +460,7 @@ pub async fn get_server_module_options_context(
},
));

base_source_transforms.extend(source_transforms.clone());
base_source_transforms.extend(source_transforms);

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
Expand All @@ -453,8 +480,6 @@ pub async fn get_server_module_options_context(
};
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_emotion,
enable_styled_components,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand Down Expand Up @@ -506,6 +531,23 @@ pub async fn get_server_module_options_context(
}
}
ServerContextType::Middleware => {
let mut base_source_transforms: Vec<TransformPluginVc> = vec![
styled_components_transform_plugin,
styled_jsx_transform_plugin,
]
.into_iter()
.flatten()
.collect();

base_source_transforms.extend(source_transforms);

let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins {
source_transforms: base_source_transforms,
output_transforms,
},
));

let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context),
..Default::default()
Expand All @@ -516,9 +558,6 @@ pub async fn get_server_module_options_context(
};
ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options),
enable_emotion,
enable_styled_jsx: true,
enable_styled_components,
enable_postcss_transform,
enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use anyhow::Result;
use turbopack_binding::turbopack::{
ecmascript::{OptionTransformPluginVc, TransformPluginVc},
ecmascript_plugin::transform::emotion::{EmotionTransformConfig, EmotionTransformer},
};

use crate::next_config::{EmotionTransformOptionsOrBoolean, NextConfigVc};

#[turbo_tasks::function]
pub async fn get_emotion_transform_plugin(
next_config: NextConfigVc,
) -> Result<OptionTransformPluginVc> {
let transform_plugin = next_config
.await?
.compiler
.as_ref()
.map(|value| {
value
.emotion
.as_ref()
.map(|value| {
let transformer = match value {
EmotionTransformOptionsOrBoolean::Boolean(true) => {
EmotionTransformer::new(&EmotionTransformConfig {
..Default::default()
})
}
EmotionTransformOptionsOrBoolean::Boolean(false) => None,

EmotionTransformOptionsOrBoolean::Options(value) => {
EmotionTransformer::new(value)
}
};

transformer.map_or_else(
|| OptionTransformPluginVc::cell(None),
|v| {
OptionTransformPluginVc::cell(Some(TransformPluginVc::cell(Box::new(
v,
))))
},
)
})
.unwrap_or_default()
})
.unwrap_or_default();

Ok(transform_plugin)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
pub(crate) mod emotion;
pub(crate) mod modularize_imports;
pub(crate) mod next_dynamic;
pub(crate) mod next_font;
pub(crate) mod next_strip_page_exports;
pub(crate) mod relay;
pub(crate) mod styled_components;
pub(crate) mod styled_jsx;

pub use modularize_imports::{get_next_modularize_imports_rule, ModularizeImportPackageConfig};
pub use next_dynamic::get_next_dynamic_transform_rule;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use anyhow::Result;
use turbopack_binding::turbopack::{
ecmascript::{OptionTransformPluginVc, TransformPluginVc},
ecmascript_plugin::transform::styled_components::{
StyledComponentsTransformConfig, StyledComponentsTransformer,
},
};

use crate::next_config::{NextConfigVc, StyledComponentsTransformOptionsOrBoolean};

#[turbo_tasks::function]
pub async fn get_styled_components_transform_plugin(
next_config: NextConfigVc,
) -> Result<OptionTransformPluginVc> {
let transform_plugin = next_config
.await?
.compiler
.as_ref()
.map(|value| {
value
.styled_components
.as_ref()
.map(|value| {
let transformer = match value {
StyledComponentsTransformOptionsOrBoolean::Boolean(true) => Some(
StyledComponentsTransformer::new(&StyledComponentsTransformConfig {
..Default::default()
}),
),
StyledComponentsTransformOptionsOrBoolean::Boolean(false) => None,
StyledComponentsTransformOptionsOrBoolean::Options(value) => {
Some(StyledComponentsTransformer::new(value))
}
};

transformer.map_or_else(
|| OptionTransformPluginVc::cell(None),
|v| {
OptionTransformPluginVc::cell(Some(TransformPluginVc::cell(Box::new(
v,
))))
},
)
})
.unwrap_or_default()
})
.unwrap_or_default();

Ok(transform_plugin)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use anyhow::Result;
use turbopack_binding::turbopack::{
ecmascript::{OptionTransformPluginVc, TransformPluginVc},
ecmascript_plugin::transform::styled_jsx::StyledJsxTransformer,
};

/// Returns a transform plugin for the relay graphql transform.
#[turbo_tasks::function]
pub async fn get_styled_jsx_transform_plugin() -> Result<OptionTransformPluginVc> {
Ok(OptionTransformPluginVc::cell(Some(
TransformPluginVc::cell(Box::new(StyledJsxTransformer::new())),
)))
}

0 comments on commit de08f8b

Please sign in to comment.