Skip to content

Commit

Permalink
pick next config keys for more granular caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 26, 2024
1 parent 8efe29a commit 7cf88a6
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 33 deletions.
6 changes: 3 additions & 3 deletions crates/next-core/src/next_client/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ pub async fn get_next_client_transforms_rules(
) -> Result<Vec<ModuleRule>> {
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,
));
}
Expand Down
24 changes: 22 additions & 2 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ struct CustomRoutes {
rewrites: Vc<Rewrites>,
}

#[turbo_tasks::value(transparent)]
pub struct ModularizeImports(IndexMap<String, ModularizeImportPackageConfig>);

#[turbo_tasks::value(serialization = "custom", eq = "manual")]
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -481,7 +484,8 @@ pub enum ReactCompilerOptionsOrBoolean {
#[turbo_tasks::value(transparent)]
pub struct OptionalReactCompilerOptions(Option<Vc<ReactCompilerOptions>>);

#[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<Vec<RcStr>>,
Expand Down Expand Up @@ -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<ReactRemoveProperties>,
Expand Down Expand Up @@ -801,6 +806,11 @@ impl NextConfig {
))
}

#[turbo_tasks::function]
pub fn compiler(&self) -> Vc<CompilerConfig> {
self.compiler.clone().unwrap_or_default().cell()
}

#[turbo_tasks::function]
pub fn env(&self) -> Result<Vc<EnvMap>> {
// The value expected for env is Record<String, String>, but config itself
Expand Down Expand Up @@ -996,6 +1006,16 @@ impl NextConfig {
Ok(options.cell())
}

#[turbo_tasks::function]
pub fn modularize_imports(&self) -> Vc<ModularizeImports> {
Vc::cell(self.modularize_imports.clone().unwrap_or_default())
}

#[turbo_tasks::function]
pub fn experimental(&self) -> Vc<ExperimentalConfig> {
self.experimental.clone().cell()
}

#[turbo_tasks::function]
pub fn react_compiler(&self) -> Result<Vc<OptionalReactCompilerOptions>> {
let options = &self.experimental.react_compiler;
Expand Down
6 changes: 3 additions & 3 deletions crates/next-core/src/next_server/transforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ pub async fn get_next_server_transforms_rules(
) -> Result<Vec<ModuleRule>> {
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,
));
}
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/next_shared/transforms/emotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::next_config::{EmotionTransformOptionsOrBoolean, NextConfig};
pub async fn get_emotion_transform_rule(next_config: Vc<NextConfig>) -> Result<Option<ModuleRule>> {
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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
14 changes: 6 additions & 8 deletions crates/next-core/src/next_shared/transforms/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ pub async fn get_relay_transform_rule(
) -> Result<Option<ModuleRule>> {
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)
Expand Down
4 changes: 2 additions & 2 deletions crates/next-core/src/next_shared/transforms/remove_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub async fn get_swc_ecma_transform_plugin_rule(
next_config: Vc<NextConfig>,
project_path: Vc<FileSystemPath>,
) -> Result<Option<ModuleRule>> {
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")]
{
Expand Down
7 changes: 1 addition & 6 deletions crates/next-core/src/transform_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 /
Expand Down

0 comments on commit 7cf88a6

Please sign in to comment.