Unverified Commit de08f8b3 authored by OJ Kwon's avatar OJ Kwon Committed by GitHub

refactor(next-core): move transforms to plugin (#49621)



### What? 

relateed to WEB-1048.

Moving emotion to the plugin, deprecate enable_emotion option.
parent 05a9e0ac
...@@ -53,11 +53,16 @@ use crate::{ ...@@ -53,11 +53,16 @@ use crate::{
get_next_client_resolved_map, mdx_import_source_file, get_next_client_resolved_map, mdx_import_source_file,
}, },
next_shared::{ 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::{ transform_options::{
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options, get_decorators_transform_options, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options, get_typescript_transform_options,
}, },
util::foreign_code_context_condition, util::foreign_code_context_condition,
}; };
...@@ -201,10 +206,11 @@ pub async fn get_client_module_options_context( ...@@ -201,10 +206,11 @@ pub async fn get_client_module_options_context(
.clone_if() .clone_if()
}; };
let enable_emotion = *get_emotion_compiler_config(next_config).await?;
let source_transforms = vec![ let source_transforms = vec![
*get_relay_transform_plugin(next_config).await?, *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( Some(TransformPluginVc::cell(Box::new(
ServerDirectiveTransformer::new( ServerDirectiveTransformer::new(
// ServerDirective is not implemented yet and always reports an issue. // ServerDirective is not implemented yet and always reports an issue.
...@@ -238,16 +244,11 @@ pub async fn get_client_module_options_context( ...@@ -238,16 +244,11 @@ pub async fn get_client_module_options_context(
..Default::default() ..Default::default()
}; };
let enable_styled_components = *get_styled_components_compiler_config(next_config).await?;
let module_options_context = ModuleOptionsContext { let module_options_context = ModuleOptionsContext {
// We don't need to resolve React Refresh for each module. Instead, // 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 // we try resolve it once at the root and pass down a context to all
// the modules. // the modules.
enable_jsx: Some(jsx_runtime_options), enable_jsx: Some(jsx_runtime_options),
enable_emotion,
enable_styled_components,
enable_styled_jsx: true,
enable_postcss_transform: postcss_transform_options, enable_postcss_transform: postcss_transform_options,
enable_webpack_loaders, enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
......
...@@ -45,11 +45,16 @@ use crate::{ ...@@ -45,11 +45,16 @@ use crate::{
next_import_map::{get_next_server_import_map, mdx_import_source_file}, next_import_map::{get_next_server_import_map, mdx_import_source_file},
next_server::resolve::ExternalPredicate, next_server::resolve::ExternalPredicate,
next_shared::{ 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::{ transform_options::{
get_decorators_transform_options, get_emotion_compiler_config, get_jsx_transform_options, get_decorators_transform_options, get_jsx_transform_options,
get_styled_components_compiler_config, get_typescript_transform_options, get_typescript_transform_options,
}, },
util::foreign_code_context_condition, util::foreign_code_context_condition,
}; };
...@@ -281,6 +286,10 @@ pub async fn get_server_module_options_context( ...@@ -281,6 +286,10 @@ pub async fn get_server_module_options_context(
.clone_if() .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( let client_directive_transform_plugin = Some(TransformPluginVc::cell(Box::new(
ClientDirectiveTransformer::new(&StringVc::cell("server-to-client".to_string())), ClientDirectiveTransformer::new(&StringVc::cell("server-to-client".to_string())),
))); )));
...@@ -292,6 +301,7 @@ pub async fn get_server_module_options_context( ...@@ -292,6 +301,7 @@ pub async fn get_server_module_options_context(
), ),
))); )));
// ModuleOptionsContext related options
let tsconfig = get_typescript_transform_options(project_path); let tsconfig = get_typescript_transform_options(project_path);
let decorators_options = get_decorators_transform_options(project_path); let decorators_options = get_decorators_transform_options(project_path);
let enable_mdx_rs = if *next_config.mdx_rs().await? { let enable_mdx_rs = if *next_config.mdx_rs().await? {
...@@ -305,13 +315,15 @@ pub async fn get_server_module_options_context( ...@@ -305,13 +315,15 @@ pub async fn get_server_module_options_context(
None None
}; };
let jsx_runtime_options = get_jsx_transform_options(project_path, 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![]; let source_transforms: Vec<TransformPluginVc> = vec![
if let Some(relay_transform_plugin) = *get_relay_transform_plugin(next_config).await? { *get_relay_transform_plugin(next_config).await?,
source_transforms.push(relay_transform_plugin); *get_emotion_transform_plugin(next_config).await?,
} ]
.into_iter()
.flatten()
.collect();
let output_transforms = vec![]; let output_transforms = vec![];
let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell( let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
...@@ -323,6 +335,23 @@ pub async fn get_server_module_options_context( ...@@ -323,6 +335,23 @@ pub async fn get_server_module_options_context(
let module_options_context = match ty.into_value() { let module_options_context = match ty.into_value() {
ServerContextType::Pages { .. } | ServerContextType::PagesData { .. } => { 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 { let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context), execution_context: Some(execution_context),
..Default::default() ..Default::default()
...@@ -336,9 +365,6 @@ pub async fn get_server_module_options_context( ...@@ -336,9 +365,6 @@ pub async fn get_server_module_options_context(
ModuleOptionsContext { ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options), enable_jsx: Some(jsx_runtime_options),
enable_styled_jsx: true,
enable_emotion,
enable_styled_components,
enable_postcss_transform, enable_postcss_transform,
enable_webpack_loaders, enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
...@@ -360,11 +386,14 @@ pub async fn get_server_module_options_context( ...@@ -360,11 +386,14 @@ pub async fn get_server_module_options_context(
} }
} }
ServerContextType::AppSSR { .. } => { ServerContextType::AppSSR { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> = let mut base_source_transforms: Vec<TransformPluginVc> = vec![
vec![server_directive_transform_plugin] styled_components_transform_plugin,
.into_iter() styled_jsx_transform_plugin,
.flatten() server_directive_transform_plugin,
.collect(); ]
.into_iter()
.flatten()
.collect();
let base_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell( let base_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins { CustomEcmascriptTransformPlugins {
...@@ -373,7 +402,7 @@ pub async fn get_server_module_options_context( ...@@ -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( let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins { CustomEcmascriptTransformPlugins {
...@@ -394,9 +423,6 @@ pub async fn get_server_module_options_context( ...@@ -394,9 +423,6 @@ pub async fn get_server_module_options_context(
ModuleOptionsContext { ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options), enable_jsx: Some(jsx_runtime_options),
enable_styled_jsx: true,
enable_emotion,
enable_styled_components,
enable_postcss_transform, enable_postcss_transform,
enable_webpack_loaders, enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
...@@ -419,6 +445,7 @@ pub async fn get_server_module_options_context( ...@@ -419,6 +445,7 @@ pub async fn get_server_module_options_context(
} }
ServerContextType::AppRSC { .. } => { ServerContextType::AppRSC { .. } => {
let mut base_source_transforms: Vec<TransformPluginVc> = vec![ let mut base_source_transforms: Vec<TransformPluginVc> = vec![
styled_components_transform_plugin,
client_directive_transform_plugin, client_directive_transform_plugin,
server_directive_transform_plugin, server_directive_transform_plugin,
] ]
...@@ -433,7 +460,7 @@ pub async fn get_server_module_options_context( ...@@ -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( let custom_ecma_transform_plugins = Some(CustomEcmascriptTransformPluginsVc::cell(
CustomEcmascriptTransformPlugins { CustomEcmascriptTransformPlugins {
...@@ -453,8 +480,6 @@ pub async fn get_server_module_options_context( ...@@ -453,8 +480,6 @@ pub async fn get_server_module_options_context(
}; };
ModuleOptionsContext { ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options), enable_jsx: Some(jsx_runtime_options),
enable_emotion,
enable_styled_components,
enable_postcss_transform, enable_postcss_transform,
enable_webpack_loaders, enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
...@@ -506,6 +531,23 @@ pub async fn get_server_module_options_context( ...@@ -506,6 +531,23 @@ pub async fn get_server_module_options_context(
} }
} }
ServerContextType::Middleware => { 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 { let module_options_context = ModuleOptionsContext {
execution_context: Some(execution_context), execution_context: Some(execution_context),
..Default::default() ..Default::default()
...@@ -516,9 +558,6 @@ pub async fn get_server_module_options_context( ...@@ -516,9 +558,6 @@ pub async fn get_server_module_options_context(
}; };
ModuleOptionsContext { ModuleOptionsContext {
enable_jsx: Some(jsx_runtime_options), enable_jsx: Some(jsx_runtime_options),
enable_emotion,
enable_styled_jsx: true,
enable_styled_components,
enable_postcss_transform, enable_postcss_transform,
enable_webpack_loaders, enable_webpack_loaders,
enable_typescript_transform: Some(tsconfig), enable_typescript_transform: Some(tsconfig),
......
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)
}
pub(crate) mod emotion;
pub(crate) mod modularize_imports; pub(crate) mod modularize_imports;
pub(crate) mod next_dynamic; pub(crate) mod next_dynamic;
pub(crate) mod next_font; pub(crate) mod next_font;
pub(crate) mod next_strip_page_exports; pub(crate) mod next_strip_page_exports;
pub(crate) mod relay; 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 modularize_imports::{get_next_modularize_imports_rule, ModularizeImportPackageConfig};
pub use next_dynamic::get_next_dynamic_transform_rule; pub use next_dynamic::get_next_dynamic_transform_rule;
......
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)
}
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())),
)))
}
...@@ -9,14 +9,6 @@ use turbopack_binding::{ ...@@ -9,14 +9,6 @@ use turbopack_binding::{
}, },
dev::react_refresh::assert_can_resolve_react_refresh, dev::react_refresh::assert_can_resolve_react_refresh,
ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig}, ecmascript::typescript::resolve::{read_from_tsconfigs, read_tsconfigs, tsconfig},
ecmascript_plugin::transform::{
emotion::{
EmotionTransformConfig, EmotionTransformConfigVc, OptionEmotionTransformConfigVc,
},
styled_components::{
OptionStyledComponentsTransformConfigVc, StyledComponentsTransformConfig,
},
},
turbopack::{ turbopack::{
module_options::{ module_options::{
DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, JsxTransformOptions, DecoratorsKind, DecoratorsOptions, DecoratorsOptionsVc, JsxTransformOptions,
...@@ -27,10 +19,6 @@ use turbopack_binding::{ ...@@ -27,10 +19,6 @@ use turbopack_binding::{
}, },
}; };
use crate::next_config::{
EmotionTransformOptionsOrBoolean, NextConfigVc, StyledComponentsTransformOptionsOrBoolean,
};
async fn get_typescript_options( async fn get_typescript_options(
project_path: FileSystemPathVc, project_path: FileSystemPathVc,
) -> Option<Vec<(FileJsonContentVc, AssetVc)>> { ) -> Option<Vec<(FileJsonContentVc, AssetVc)>> {
...@@ -176,72 +164,3 @@ pub async fn get_jsx_transform_options( ...@@ -176,72 +164,3 @@ pub async fn get_jsx_transform_options(
Ok(react_transform_options.cell()) Ok(react_transform_options.cell())
} }
#[turbo_tasks::function]
pub async fn get_emotion_compiler_config(
next_config: NextConfigVc,
) -> Result<OptionEmotionTransformConfigVc> {
let emotion_compiler_config = next_config
.await?
.compiler
.as_ref()
.map(|value| {
value
.emotion
.as_ref()
.map(|value| {
let options = match value {
EmotionTransformOptionsOrBoolean::Boolean(true) => {
Some(EmotionTransformConfigVc::cell(EmotionTransformConfig {
..Default::default()
}))
}
EmotionTransformOptionsOrBoolean::Boolean(false) => None,
EmotionTransformOptionsOrBoolean::Options(value) => {
Some(EmotionTransformConfigVc::cell(value.clone()))
}
};
OptionEmotionTransformConfigVc::cell(options)
})
.unwrap_or_else(|| OptionEmotionTransformConfigVc::cell(None))
})
.unwrap_or_else(|| OptionEmotionTransformConfigVc::cell(None));
Ok(emotion_compiler_config)
}
#[turbo_tasks::function]
pub async fn get_styled_components_compiler_config(
next_config: NextConfigVc,
) -> Result<OptionStyledComponentsTransformConfigVc> {
let styled_components_compiler_config = next_config
.await?
.compiler
.as_ref()
.map(|value| {
value
.styled_components
.as_ref()
.map(|value| {
let options = match value {
StyledComponentsTransformOptionsOrBoolean::Boolean(true) => Some(
StyledComponentsTransformConfig {
..Default::default()
}
.cell(),
),
StyledComponentsTransformOptionsOrBoolean::Boolean(false) => None,
StyledComponentsTransformOptionsOrBoolean::Options(value) => {
Some(value.clone().cell())
}
};
OptionStyledComponentsTransformConfigVc::cell(options)
})
.unwrap_or_else(|| OptionStyledComponentsTransformConfigVc::cell(None))
})
.unwrap_or_else(|| OptionStyledComponentsTransformConfigVc::cell(None));
Ok(styled_components_compiler_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