From 7cf88a66003c8a81cd210c0c2e280fe21599de2d Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 26 Sep 2024 01:29:55 +0200 Subject: [PATCH] pick next config keys for more granular caching --- .../next-core/src/next_client/transforms.rs | 6 ++--- crates/next-core/src/next_config.rs | 24 +++++++++++++++++-- .../next-core/src/next_server/transforms.rs | 6 ++--- .../src/next_shared/transforms/emotion.rs | 4 ++-- .../transforms/modularize_imports.rs | 4 ++-- .../transforms/react_remove_properties.rs | 4 ++-- .../src/next_shared/transforms/relay.rs | 14 +++++------ .../next_shared/transforms/remove_console.rs | 4 ++-- .../transforms/styled_components.rs | 4 ++-- .../transforms/swc_ecma_transform_plugins.rs | 2 +- crates/next-core/src/transform_options.rs | 7 +----- 11 files changed, 46 insertions(+), 33 deletions(-) diff --git a/crates/next-core/src/next_client/transforms.rs b/crates/next-core/src/next_client/transforms.rs index 72c676e46446a..1582bf6afa3dc 100644 --- a/crates/next-core/src/next_client/transforms.rs +++ b/crates/next-core/src/next_client/transforms.rs @@ -29,11 +29,11 @@ pub async fn get_next_client_transforms_rules( ) -> Result> { let mut rules = vec![]; - let modularize_imports_config = &next_config.await?.modularize_imports; + let modularize_imports_config = &next_config.modularize_imports().await?; let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); - if let Some(modularize_imports_config) = modularize_imports_config { + if !modularize_imports_config.is_empty() { rules.push(get_next_modularize_imports_rule( - modularize_imports_config, + &modularize_imports_config, enable_mdx_rs, )); } diff --git a/crates/next-core/src/next_config.rs b/crates/next-core/src/next_config.rs index f3ca4eb9cbee3..0195e18dda8b2 100644 --- a/crates/next-core/src/next_config.rs +++ b/crates/next-core/src/next_config.rs @@ -36,6 +36,9 @@ struct CustomRoutes { rewrites: Vc, } +#[turbo_tasks::value(transparent)] +pub struct ModularizeImports(IndexMap); + #[turbo_tasks::value(serialization = "custom", eq = "manual")] #[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -481,7 +484,8 @@ pub enum ReactCompilerOptionsOrBoolean { #[turbo_tasks::value(transparent)] pub struct OptionalReactCompilerOptions(Option>); -#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)] +#[turbo_tasks::value(eq = "manual")] +#[derive(Clone, Debug, Default, PartialEq)] #[serde(rename_all = "camelCase")] pub struct ExperimentalConfig { pub allowed_revalidate_header_keys: Option>, @@ -723,7 +727,8 @@ impl StyledComponentsTransformOptionsOrBoolean { } } -#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, TraceRawVcs)] +#[turbo_tasks::value(eq = "manual")] +#[derive(Clone, Debug, PartialEq, Default)] #[serde(rename_all = "camelCase")] pub struct CompilerConfig { pub react_remove_properties: Option, @@ -801,6 +806,11 @@ impl NextConfig { )) } + #[turbo_tasks::function] + pub fn compiler(&self) -> Vc { + self.compiler.clone().unwrap_or_default().cell() + } + #[turbo_tasks::function] pub fn env(&self) -> Result> { // The value expected for env is Record, but config itself @@ -996,6 +1006,16 @@ impl NextConfig { Ok(options.cell()) } + #[turbo_tasks::function] + pub fn modularize_imports(&self) -> Vc { + Vc::cell(self.modularize_imports.clone().unwrap_or_default()) + } + + #[turbo_tasks::function] + pub fn experimental(&self) -> Vc { + self.experimental.clone().cell() + } + #[turbo_tasks::function] pub fn react_compiler(&self) -> Result> { let options = &self.experimental.react_compiler; diff --git a/crates/next-core/src/next_server/transforms.rs b/crates/next-core/src/next_server/transforms.rs index 0e0ae9d671ec9..95c676470c472 100644 --- a/crates/next-core/src/next_server/transforms.rs +++ b/crates/next-core/src/next_server/transforms.rs @@ -32,11 +32,11 @@ pub async fn get_next_server_transforms_rules( ) -> Result> { let mut rules = vec![]; - let modularize_imports_config = &next_config.await?.modularize_imports; + let modularize_imports_config = &next_config.modularize_imports().await?; let mdx_rs = next_config.mdx_rs().await?.is_some(); - if let Some(modularize_imports_config) = modularize_imports_config { + if !modularize_imports_config.is_empty() { rules.push(get_next_modularize_imports_rule( - modularize_imports_config, + &modularize_imports_config, mdx_rs, )); } diff --git a/crates/next-core/src/next_shared/transforms/emotion.rs b/crates/next-core/src/next_shared/transforms/emotion.rs index 1556f5b729680..d8997df8695ee 100644 --- a/crates/next-core/src/next_shared/transforms/emotion.rs +++ b/crates/next-core/src/next_shared/transforms/emotion.rs @@ -9,10 +9,10 @@ use crate::next_config::{EmotionTransformOptionsOrBoolean, NextConfig}; pub async fn get_emotion_transform_rule(next_config: Vc) -> Result> { let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let module_rule = next_config + .compiler() .await? - .compiler + .emotion .as_ref() - .and_then(|value| value.emotion.as_ref()) .and_then(|config| match config { EmotionTransformOptionsOrBoolean::Boolean(true) => { EmotionTransformer::new(&Default::default()) diff --git a/crates/next-core/src/next_shared/transforms/modularize_imports.rs b/crates/next-core/src/next_shared/transforms/modularize_imports.rs index fb93d127f67fc..7d7629cd7fae1 100644 --- a/crates/next-core/src/next_shared/transforms/modularize_imports.rs +++ b/crates/next-core/src/next_shared/transforms/modularize_imports.rs @@ -18,7 +18,7 @@ use turbopack_ecmascript::{CustomTransformer, EcmascriptInputTransform, Transfor use super::module_rule_match_js_no_url; -#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)] #[serde(rename_all = "camelCase")] pub struct ModularizeImportPackageConfig { pub transform: Transform, @@ -28,7 +28,7 @@ pub struct ModularizeImportPackageConfig { pub skip_default_conversion: bool, } -#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize, TraceRawVcs)] +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)] #[serde(untagged)] pub enum Transform { #[default] diff --git a/crates/next-core/src/next_shared/transforms/react_remove_properties.rs b/crates/next-core/src/next_shared/transforms/react_remove_properties.rs index 8798f6b152abe..50c143a552275 100644 --- a/crates/next-core/src/next_shared/transforms/react_remove_properties.rs +++ b/crates/next-core/src/next_shared/transforms/react_remove_properties.rs @@ -21,10 +21,10 @@ pub async fn get_react_remove_properties_transform_rule( let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let module_rule = next_config + .compiler() .await? - .compiler + .react_remove_properties .as_ref() - .and_then(|value| value.react_remove_properties.as_ref()) .and_then(|config| match config { ReactRemoveProperties::Boolean(false) => None, ReactRemoveProperties::Boolean(true) => { diff --git a/crates/next-core/src/next_shared/transforms/relay.rs b/crates/next-core/src/next_shared/transforms/relay.rs index 39b19a364a35b..4f78b10b7facf 100644 --- a/crates/next-core/src/next_shared/transforms/relay.rs +++ b/crates/next-core/src/next_shared/transforms/relay.rs @@ -14,14 +14,12 @@ pub async fn get_relay_transform_rule( ) -> Result> { let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let project_path = &*project_path.await?; - let module_rule = next_config.await?.compiler.as_ref().and_then(|value| { - value.relay.as_ref().map(|config| { - get_ecma_transform_rule( - Box::new(RelayTransformer::new(config, project_path)), - enable_mdx_rs, - true, - ) - }) + let module_rule = next_config.compiler().await?.relay.as_ref().map(|config| { + get_ecma_transform_rule( + Box::new(RelayTransformer::new(config, project_path)), + enable_mdx_rs, + true, + ) }); Ok(module_rule) diff --git a/crates/next-core/src/next_shared/transforms/remove_console.rs b/crates/next-core/src/next_shared/transforms/remove_console.rs index ec690c5f12a56..5b33beb5f851b 100644 --- a/crates/next-core/src/next_shared/transforms/remove_console.rs +++ b/crates/next-core/src/next_shared/transforms/remove_console.rs @@ -21,10 +21,10 @@ pub async fn get_remove_console_transform_rule( let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let module_rule = next_config + .compiler() .await? - .compiler + .remove_console .as_ref() - .and_then(|value| value.remove_console.as_ref()) .and_then(|config| match config { RemoveConsoleConfig::Boolean(false) => None, RemoveConsoleConfig::Boolean(true) => Some(remove_console::Config::All(true)), diff --git a/crates/next-core/src/next_shared/transforms/styled_components.rs b/crates/next-core/src/next_shared/transforms/styled_components.rs index dc8e39300b598..7b81974b1f8cc 100644 --- a/crates/next-core/src/next_shared/transforms/styled_components.rs +++ b/crates/next-core/src/next_shared/transforms/styled_components.rs @@ -14,10 +14,10 @@ pub async fn get_styled_components_transform_rule( let enable_mdx_rs = next_config.mdx_rs().await?.is_some(); let module_rule = next_config + .compiler() .await? - .compiler + .styled_components .as_ref() - .and_then(|value| value.styled_components.as_ref()) .and_then(|config| match config { StyledComponentsTransformOptionsOrBoolean::Boolean(true) => { Some(StyledComponentsTransformer::new(&Default::default())) diff --git a/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs b/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs index ef514c2276343..bd31bf0f175db 100644 --- a/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs +++ b/crates/next-core/src/next_shared/transforms/swc_ecma_transform_plugins.rs @@ -11,7 +11,7 @@ pub async fn get_swc_ecma_transform_plugin_rule( next_config: Vc, project_path: Vc, ) -> Result> { - match next_config.await?.experimental.swc_plugins.as_ref() { + match next_config.experimental().await?.swc_plugins.as_ref() { Some(plugin_configs) if !plugin_configs.is_empty() => { #[cfg(feature = "plugin")] { diff --git a/crates/next-core/src/transform_options.rs b/crates/next-core/src/transform_options.rs index b44adea605e38..5e912862c1346 100644 --- a/crates/next-core/src/transform_options.rs +++ b/crates/next-core/src/transform_options.rs @@ -136,12 +136,7 @@ pub async fn get_jsx_transform_options( false }; - let is_emotion_enabled = next_config - .await? - .compiler - .as_ref() - .map(|c| c.emotion.is_some()) - .unwrap_or_default(); + let is_emotion_enabled = next_config.compiler().await?.emotion.is_some(); // [NOTE]: ref: WEB-901 // next.js does not allow to overriding react runtime config via tsconfig /