From 521cdaef70ac5d85cf5e1803baa2aecbdeaa68d8 Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Mon, 13 Jan 2025 12:28:22 -0800 Subject: [PATCH] refactor(turbopack-core): Use ResolvedVc in ContextCondition (#74711) --- crates/next-core/src/next_client/context.rs | 16 ++------- crates/next-core/src/next_server/context.rs | 33 +++++-------------- crates/next-core/src/util.rs | 33 +++++++++++++++++-- .../crates/turbopack-core/src/condition.rs | 6 ++-- .../src/resolve_options_context.rs | 2 +- 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/crates/next-core/src/next_client/context.rs b/crates/next-core/src/next_client/context.rs index 77e35284c3d36..af930ae07c064 100644 --- a/crates/next-core/src/next_client/context.rs +++ b/crates/next-core/src/next_client/context.rs @@ -4,7 +4,7 @@ use anyhow::Result; use turbo_rcstr::RcStr; use turbo_tasks::{FxIndexMap, ResolvedVc, Value, Vc}; use turbo_tasks_env::EnvMap; -use turbo_tasks_fs::{FileSystem, FileSystemPath}; +use turbo_tasks_fs::FileSystemPath; use turbopack::{ module_options::{ module_options_context::ModuleOptionsContext, CssOptionsContext, EcmascriptOptionsContext, @@ -19,7 +19,6 @@ use turbopack_core::{ CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, FreeVarReference, FreeVarReferences, }, - condition::ContextCondition, environment::{BrowserEnvironment, Environment, ExecutionEnvironment}, free_var_references, resolve::{parse::Request, pattern::Pattern}, @@ -31,7 +30,6 @@ use turbopack_node::{ use super::transforms::get_next_client_transforms_rules; use crate::{ - embed_js::next_js_fs, mode::NextMode, next_build::get_postcss_package_mapping, next_client::runtime_entry::{RuntimeEntries, RuntimeEntry}, @@ -60,7 +58,7 @@ use crate::{ get_decorators_transform_options, get_jsx_transform_options, get_typescript_transform_options, }, - util::foreign_code_context_condition, + util::{foreign_code_context_condition, internal_assets_conditions}, }; fn defines(define_env: &FxIndexMap) -> CompileTimeDefines { @@ -212,14 +210,6 @@ pub async fn get_client_resolve_options_context( .cell()) } -fn internal_assets_conditions() -> ContextCondition { - ContextCondition::any(vec![ - ContextCondition::InPath(next_js_fs().root()), - ContextCondition::InPath(turbopack_ecmascript_runtime::embed_fs().root()), - ContextCondition::InPath(turbopack_node::embed_js::embed_fs().root()), - ]) -} - #[turbo_tasks::function] pub async fn get_client_module_options_context( project_path: ResolvedVc, @@ -387,7 +377,7 @@ pub async fn get_client_module_options_context( foreign_codes_options_context.resolved_cell(), ), ( - internal_assets_conditions(), + internal_assets_conditions().await?, internal_context.resolved_cell(), ), ], diff --git a/crates/next-core/src/next_server/context.rs b/crates/next-core/src/next_server/context.rs index 0273af182d3c6..9e23f49f07ae7 100644 --- a/crates/next-core/src/next_server/context.rs +++ b/crates/next-core/src/next_server/context.rs @@ -4,7 +4,7 @@ use anyhow::{bail, Result}; use turbo_rcstr::RcStr; use turbo_tasks::{FxIndexMap, ResolvedVc, Value, Vc}; use turbo_tasks_env::{EnvMap, ProcessEnv}; -use turbo_tasks_fs::{FileSystem, FileSystemPath}; +use turbo_tasks_fs::FileSystemPath; use turbopack::{ module_options::{ CssOptionsContext, EcmascriptOptionsContext, JsxTransformOptions, ModuleOptionsContext, @@ -19,7 +19,6 @@ use turbopack_core::{ CompileTimeDefineValue, CompileTimeDefines, CompileTimeInfo, DefineableNameSegment, FreeVarReferences, }, - condition::ContextCondition, environment::{ Environment, ExecutionEnvironment, NodeJsEnvironment, NodeJsVersion, RuntimeVersions, }, @@ -41,7 +40,6 @@ use super::{ transforms::{get_next_server_internal_transforms_rules, get_next_server_transforms_rules}, }; use crate::{ - embed_js::next_js_fs, mode::NextMode, next_build::get_postcss_package_mapping, next_client::RuntimeEntries, @@ -71,8 +69,8 @@ use crate::{ get_typescript_transform_options, }, util::{ - foreign_code_context_condition, get_transpiled_packages, load_next_js_templateon, - NextRuntime, + foreign_code_context_condition, get_transpiled_packages, internal_assets_conditions, + load_next_js_templateon, NextRuntime, }, }; @@ -396,21 +394,6 @@ pub async fn get_server_compile_time_info( .await } -/// Determins if the module is an internal asset (i.e overlay, fallback) coming -/// from the embedded FS, don't apply user defined transforms. -/// -/// [TODO] turbopack specific embed fs should be handled by internals of -/// turbopack itself and user config should not try to leak this. However, -/// currently we apply few transform options subject to next.js's configuration -/// even if it's embedded assets. -fn internal_assets_conditions() -> ContextCondition { - ContextCondition::any(vec![ - ContextCondition::InPath(next_js_fs().root()), - ContextCondition::InPath(turbopack_ecmascript_runtime::embed_fs().root()), - ContextCondition::InPath(turbopack_node::embed_js::embed_fs().root()), - ]) -} - #[turbo_tasks::function] pub async fn get_server_module_options_context( project_path: ResolvedVc, @@ -648,7 +631,7 @@ pub async fn get_server_module_options_context( foreign_code_module_options_context.resolved_cell(), ), ( - internal_assets_conditions(), + internal_assets_conditions().await?, internal_module_options_context.resolved_cell(), ), ], @@ -713,7 +696,7 @@ pub async fn get_server_module_options_context( foreign_code_module_options_context.resolved_cell(), ), ( - internal_assets_conditions(), + internal_assets_conditions().await?, internal_module_options_context.resolved_cell(), ), ], @@ -789,7 +772,7 @@ pub async fn get_server_module_options_context( foreign_code_module_options_context.resolved_cell(), ), ( - internal_assets_conditions(), + internal_assets_conditions().await?, internal_module_options_context.resolved_cell(), ), ], @@ -864,7 +847,7 @@ pub async fn get_server_module_options_context( foreign_code_module_options_context.resolved_cell(), ), ( - internal_assets_conditions(), + internal_assets_conditions().await?, internal_module_options_context.resolved_cell(), ), ], @@ -961,7 +944,7 @@ pub async fn get_server_module_options_context( foreign_code_module_options_context.resolved_cell(), ), ( - internal_assets_conditions(), + internal_assets_conditions().await?, internal_module_options_context.resolved_cell(), ), ], diff --git a/crates/next-core/src/util.rs b/crates/next-core/src/util.rs index b1191f04625cb..3d944b8e4028e 100644 --- a/crates/next-core/src/util.rs +++ b/crates/next-core/src/util.rs @@ -13,7 +13,7 @@ use turbo_tasks::{ }; use turbo_tasks_fs::{ self, json::parse_json_rope_with_source_context, rope::Rope, util::join_path, File, - FileContent, FileSystemPath, + FileContent, FileSystem, FileSystemPath, }; use turbopack_core::{ asset::AssetContent, @@ -32,6 +32,7 @@ use turbopack_ecmascript::{ }; use crate::{ + embed_js::next_js_fs, next_config::{NextConfig, RouteHas}, next_import_map::get_next_package, next_manifests::MiddlewareMatcher, @@ -121,7 +122,10 @@ pub async fn foreign_code_context_condition( // of the `node_modules` specific resolve options (the template files are // technically node module files). let not_next_template_dir = ContextCondition::not(ContextCondition::InPath( - get_next_package(*project_path).join(NEXT_TEMPLATE_PATH.into()), + get_next_package(*project_path) + .join(NEXT_TEMPLATE_PATH.into()) + .to_resolved() + .await?, )); let result = ContextCondition::all(vec![ @@ -137,6 +141,31 @@ pub async fn foreign_code_context_condition( Ok(result) } +/// Determines if the module is an internal asset (i.e overlay, fallback) coming +/// from the embedded FS, don't apply user defined transforms. +/// +/// [TODO] turbopack specific embed fs should be handled by internals of +/// turbopack itself and user config should not try to leak this. However, +/// currently we apply few transform options subject to next.js's configuration +/// even if it's embedded assets. +pub async fn internal_assets_conditions() -> Result { + Ok(ContextCondition::any(vec![ + ContextCondition::InPath(next_js_fs().root().to_resolved().await?), + ContextCondition::InPath( + turbopack_ecmascript_runtime::embed_fs() + .root() + .to_resolved() + .await?, + ), + ContextCondition::InPath( + turbopack_node::embed_js::embed_fs() + .root() + .to_resolved() + .await?, + ), + ])) +} + #[derive( Default, PartialEq, diff --git a/turbopack/crates/turbopack-core/src/condition.rs b/turbopack/crates/turbopack-core/src/condition.rs index bd0060de9e6e7..9085d4c9ede08 100644 --- a/turbopack/crates/turbopack-core/src/condition.rs +++ b/turbopack/crates/turbopack-core/src/condition.rs @@ -1,16 +1,16 @@ use anyhow::Result; use futures::{stream, StreamExt}; use serde::{Deserialize, Serialize}; -use turbo_tasks::{trace::TraceRawVcs, Vc}; +use turbo_tasks::{trace::TraceRawVcs, NonLocalValue, ResolvedVc}; use turbo_tasks_fs::FileSystemPath; -#[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, NonLocalValue)] pub enum ContextCondition { All(Vec), Any(Vec), Not(Box), InDirectory(String), - InPath(Vc), + InPath(ResolvedVc), } impl ContextCondition { diff --git a/turbopack/crates/turbopack-resolve/src/resolve_options_context.rs b/turbopack/crates/turbopack-resolve/src/resolve_options_context.rs index 4607b1ff479a8..b7e009fab215f 100644 --- a/turbopack/crates/turbopack-resolve/src/resolve_options_context.rs +++ b/turbopack/crates/turbopack-resolve/src/resolve_options_context.rs @@ -11,7 +11,7 @@ use turbopack_core::{ }, }; -#[turbo_tasks::value(shared, local)] +#[turbo_tasks::value(shared)] #[derive(Default, Clone)] pub struct ResolveOptionsContext { #[serde(default)]