From 1d45e0bf47ead7da5a8b6a3bc820ccddafbee417 Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Mon, 4 Nov 2024 16:19:08 -0800 Subject: [PATCH] refactor(turbopack-core): Migrate more resolve options structs to ResolvedVc (#72200) A hand refactor of all the remaining struct fields from `Vc` to `ResolvedVc`, to unblock local tasks. This is similar to #71948, as there's a focus on resolve options. Note: "resolve" in this context means two different things. `turbopack-resolve` is about resolving module locations on disk. `ResolvedVc` is about resolving "cells" representing incremental computations. Closes PACK-3369 --- crates/next-api/src/app.rs | 207 ++++++++++-------- crates/next-api/src/instrumentation.rs | 4 +- crates/next-api/src/middleware.rs | 2 +- crates/next-api/src/pages.rs | 104 ++++----- crates/next-api/src/project.rs | 6 +- crates/next-core/src/app_structure.rs | 4 +- crates/next-core/src/next_build.rs | 21 +- crates/next-core/src/next_client/context.rs | 10 +- .../next-core/src/next_client/transforms.rs | 2 +- crates/next-core/src/next_edge/unsupported.rs | 11 +- crates/next-core/src/next_font/google/mod.rs | 37 ++-- crates/next-core/src/next_import_map.rs | 191 +++++++++------- crates/next-core/src/next_server/context.rs | 31 +-- .../next-core/src/next_server/transforms.rs | 2 +- .../crates/turbopack-cli/src/contexts.rs | 15 +- .../crates/turbopack-core/src/resolve/mod.rs | 2 +- .../turbopack-core/src/resolve/options.rs | 117 +++++----- .../crates/turbopack-resolve/src/resolve.rs | 8 +- .../turbopack-resolve/src/typescript.rs | 7 +- .../crates/turbopack-tests/tests/execution.rs | 2 +- .../crates/turbopack/src/evaluate_context.rs | 9 +- .../turbopack/src/module_options/mod.rs | 8 +- 22 files changed, 453 insertions(+), 347 deletions(-) diff --git a/crates/next-api/src/app.rs b/crates/next-api/src/app.rs index 2eae1f5aea93d..d22d732d53d25 100644 --- a/crates/next-api/src/app.rs +++ b/crates/next-api/src/app.rs @@ -85,41 +85,14 @@ use crate::{ #[turbo_tasks::value] pub struct AppProject { - project: Vc, - app_dir: Vc, + project: ResolvedVc, + app_dir: ResolvedVc, } #[turbo_tasks::value(transparent)] pub struct OptionAppProject(Option>); -impl AppProject { - fn client_ty(self: Vc) -> ClientContextType { - ClientContextType::App { - app_dir: self.app_dir(), - } - } - - fn rsc_ty(self: Vc) -> ServerContextType { - ServerContextType::AppRSC { - app_dir: self.app_dir(), - client_transition: Some(Vc::upcast(self.client_transition())), - ecmascript_client_reference_transition_name: Some(self.client_transition_name()), - } - } - - fn route_ty(self: Vc) -> ServerContextType { - ServerContextType::AppRoute { - app_dir: self.app_dir(), - ecmascript_client_reference_transition_name: Some(self.client_transition_name()), - } - } - - fn ssr_ty(self: Vc) -> ServerContextType { - ServerContextType::AppSSR { - app_dir: self.app_dir(), - } - } -} +impl AppProject {} pub(crate) const ECMASCRIPT_CLIENT_TRANSITION_NAME: &str = "next-ecmascript-client-reference"; @@ -134,46 +107,83 @@ fn styles_rule_condition() -> RuleCondition { #[turbo_tasks::value_impl] impl AppProject { #[turbo_tasks::function] - pub fn new(project: Vc, app_dir: Vc) -> Vc { + pub fn new(project: ResolvedVc, app_dir: ResolvedVc) -> Vc { AppProject { project, app_dir }.cell() } #[turbo_tasks::function] fn project(&self) -> Vc { - self.project + *self.project } #[turbo_tasks::function] fn app_dir(&self) -> Vc { - self.app_dir + *self.app_dir + } + + #[turbo_tasks::function] + fn client_ty(&self) -> Vc { + ClientContextType::App { + app_dir: self.app_dir, + } + .cell() + } + + #[turbo_tasks::function] + async fn rsc_ty(self: Vc) -> Result> { + let this = self.await?; + Ok(ServerContextType::AppRSC { + app_dir: this.app_dir, + client_transition: Some(Vc::upcast(self.client_transition())), + ecmascript_client_reference_transition_name: Some(self.client_transition_name()), + } + .cell()) + } + + #[turbo_tasks::function] + async fn route_ty(self: Vc) -> Result> { + let this = self.await?; + Ok(ServerContextType::AppRoute { + app_dir: this.app_dir, + ecmascript_client_reference_transition_name: Some(self.client_transition_name()), + } + .cell()) + } + + #[turbo_tasks::function] + fn ssr_ty(&self) -> Vc { + ServerContextType::AppSSR { + app_dir: self.app_dir, + } + .cell() } #[turbo_tasks::function] fn app_entrypoints(&self) -> Vc { - get_entrypoints(self.app_dir, self.project.next_config().page_extensions()) + get_entrypoints(*self.app_dir, self.project.next_config().page_extensions()) } #[turbo_tasks::function] - fn client_module_options_context(self: Vc) -> Vc { - get_client_module_options_context( + async fn client_module_options_context(self: Vc) -> Result> { + Ok(get_client_module_options_context( self.project().project_path(), self.project().execution_context(), self.project().client_compile_time_info().environment(), - Value::new(self.client_ty()), + Value::new(self.client_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), - ) + )) } #[turbo_tasks::function] - fn client_resolve_options_context(self: Vc) -> Vc { - get_client_resolve_options_context( + async fn client_resolve_options_context(self: Vc) -> Result> { + Ok(get_client_resolve_options_context( self.project().project_path(), - Value::new(self.client_ty()), + Value::new(self.client_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] @@ -188,95 +198,97 @@ impl AppProject { } #[turbo_tasks::function] - fn rsc_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn rsc_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), - Value::new(self.rsc_ty()), + Value::new(self.rsc_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), NextRuntime::NodeJs, - ) + )) } #[turbo_tasks::function] - fn edge_rsc_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn edge_rsc_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), - Value::new(self.rsc_ty()), + Value::new(self.rsc_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), NextRuntime::Edge, - ) + )) } #[turbo_tasks::function] - fn route_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn route_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), - Value::new(self.route_ty()), + Value::new(self.route_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), NextRuntime::NodeJs, - ) + )) } #[turbo_tasks::function] - fn edge_route_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn edge_route_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), - Value::new(self.route_ty()), + Value::new(self.route_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), NextRuntime::Edge, - ) + )) } #[turbo_tasks::function] - fn rsc_resolve_options_context(self: Vc) -> Vc { - get_server_resolve_options_context( + async fn rsc_resolve_options_context(self: Vc) -> Result> { + Ok(get_server_resolve_options_context( self.project().project_path(), - Value::new(self.rsc_ty()), + Value::new(self.rsc_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] - fn edge_rsc_resolve_options_context(self: Vc) -> Vc { - get_edge_resolve_options_context( + async fn edge_rsc_resolve_options_context(self: Vc) -> Result> { + Ok(get_edge_resolve_options_context( self.project().project_path(), - Value::new(self.rsc_ty()), + Value::new(self.rsc_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] - fn route_resolve_options_context(self: Vc) -> Vc { - get_server_resolve_options_context( + async fn route_resolve_options_context(self: Vc) -> Result> { + Ok(get_server_resolve_options_context( self.project().project_path(), - Value::new(self.route_ty()), + Value::new(self.route_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] - fn edge_route_resolve_options_context(self: Vc) -> Vc { - get_edge_resolve_options_context( + async fn edge_route_resolve_options_context( + self: Vc, + ) -> Result> { + Ok(get_edge_resolve_options_context( self.project().project_path(), - Value::new(self.route_ty()), + Value::new(self.route_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] @@ -446,49 +458,49 @@ impl AppProject { } #[turbo_tasks::function] - fn ssr_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn ssr_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), - Value::new(self.ssr_ty()), + Value::new(self.ssr_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), NextRuntime::NodeJs, - ) + )) } #[turbo_tasks::function] - fn edge_ssr_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn edge_ssr_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), - Value::new(self.ssr_ty()), + Value::new(self.ssr_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), NextRuntime::Edge, - ) + )) } #[turbo_tasks::function] - fn ssr_resolve_options_context(self: Vc) -> Vc { - get_server_resolve_options_context( + async fn ssr_resolve_options_context(self: Vc) -> Result> { + Ok(get_server_resolve_options_context( self.project().project_path(), - Value::new(self.ssr_ty()), + Value::new(self.ssr_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] - fn edge_ssr_resolve_options_context(self: Vc) -> Vc { - get_edge_resolve_options_context( + async fn edge_ssr_resolve_options_context(self: Vc) -> Result> { + Ok(get_edge_resolve_options_context( self.project().project_path(), - Value::new(self.ssr_ty()), + Value::new(self.ssr_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] @@ -532,8 +544,11 @@ impl AppProject { } #[turbo_tasks::function] - fn runtime_entries(self: Vc) -> Vc { - get_server_runtime_entries(Value::new(self.rsc_ty()), self.project().next_mode()) + async fn runtime_entries(self: Vc) -> Result> { + Ok(get_server_runtime_entries( + Value::new(self.rsc_ty().await?.clone_value()), + self.project().next_mode(), + )) } #[turbo_tasks::function] @@ -557,15 +572,15 @@ impl AppProject { } #[turbo_tasks::function] - fn client_runtime_entries(self: Vc) -> Vc { - get_client_runtime_entries( + async fn client_runtime_entries(self: Vc) -> Result> { + Ok(get_client_runtime_entries( self.project().project_path(), - Value::new(self.client_ty()), + Value::new(self.client_ty().await?.clone_value()), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), ) - .resolve_entries(Vc::upcast(self.client_module_context())) + .resolve_entries(Vc::upcast(self.client_module_context()))) } #[turbo_tasks::function] diff --git a/crates/next-api/src/instrumentation.rs b/crates/next-api/src/instrumentation.rs index dbe5aed97fd61..1d27843831f2d 100644 --- a/crates/next-api/src/instrumentation.rs +++ b/crates/next-api/src/instrumentation.rs @@ -38,7 +38,7 @@ pub struct InstrumentationEndpoint { source: Vc>, is_edge: bool, - app_dir: Option>, + app_dir: Option>, ecmascript_client_reference_transition_name: Option>, } @@ -50,7 +50,7 @@ impl InstrumentationEndpoint { asset_context: Vc>, source: Vc>, is_edge: bool, - app_dir: Option>, + app_dir: Option>, ecmascript_client_reference_transition_name: Option>, ) -> Vc { Self { diff --git a/crates/next-api/src/middleware.rs b/crates/next-api/src/middleware.rs index be87206bbb1a2..c9bc87ea64eb0 100644 --- a/crates/next-api/src/middleware.rs +++ b/crates/next-api/src/middleware.rs @@ -85,7 +85,7 @@ impl MiddlewareEndpoint { let mut evaluatable_assets = get_server_runtime_entries( Value::new(ServerContextType::Middleware { - app_dir: self.app_dir.as_deref().copied(), + app_dir: self.app_dir, ecmascript_client_reference_transition_name: self .ecmascript_client_reference_transition_name .as_deref() diff --git a/crates/next-api/src/pages.rs b/crates/next-api/src/pages.rs index 262a5982b088a..448cccb9e7a0d 100644 --- a/crates/next-api/src/pages.rs +++ b/crates/next-api/src/pages.rs @@ -281,30 +281,30 @@ impl PagesProject { } #[turbo_tasks::function] - fn client_module_options_context(self: Vc) -> Vc { - get_client_module_options_context( + async fn client_module_options_context(self: Vc) -> Result> { + Ok(get_client_module_options_context( self.project().project_path(), self.project().execution_context(), self.project().client_compile_time_info().environment(), Value::new(ClientContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), - ) + )) } #[turbo_tasks::function] - fn client_resolve_options_context(self: Vc) -> Vc { - get_client_resolve_options_context( + async fn client_resolve_options_context(self: Vc) -> Result> { + Ok(get_client_resolve_options_context( self.project().project_path(), Value::new(ClientContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] @@ -387,153 +387,155 @@ impl PagesProject { } #[turbo_tasks::function] - fn ssr_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn ssr_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), Value::new(ServerContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), NextRuntime::NodeJs, - ) + )) } #[turbo_tasks::function] - fn edge_ssr_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn edge_ssr_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), Value::new(ServerContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), NextRuntime::Edge, - ) + )) } #[turbo_tasks::function] - fn api_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn api_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), Value::new(ServerContextType::PagesApi { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), NextRuntime::NodeJs, - ) + )) } #[turbo_tasks::function] - fn edge_api_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn edge_api_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), Value::new(ServerContextType::PagesApi { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), NextRuntime::Edge, - ) + )) } #[turbo_tasks::function] - fn ssr_data_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn ssr_data_module_options_context(self: Vc) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), Value::new(ServerContextType::PagesData { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), NextRuntime::NodeJs, - ) + )) } #[turbo_tasks::function] - fn edge_ssr_data_module_options_context(self: Vc) -> Vc { - get_server_module_options_context( + async fn edge_ssr_data_module_options_context( + self: Vc, + ) -> Result> { + Ok(get_server_module_options_context( self.project().project_path(), self.project().execution_context(), Value::new(ServerContextType::PagesData { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), NextRuntime::Edge, - ) + )) } #[turbo_tasks::function] - fn ssr_resolve_options_context(self: Vc) -> Vc { - get_server_resolve_options_context( + async fn ssr_resolve_options_context(self: Vc) -> Result> { + Ok(get_server_resolve_options_context( self.project().project_path(), // NOTE(alexkirsz) This could be `PagesData` for the data endpoint, but it doesn't // matter (for now at least) because `get_server_resolve_options_context` doesn't // differentiate between the two. Value::new(ServerContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] - fn edge_ssr_resolve_options_context(self: Vc) -> Vc { - get_edge_resolve_options_context( + async fn edge_ssr_resolve_options_context(self: Vc) -> Result> { + Ok(get_edge_resolve_options_context( self.project().project_path(), // NOTE(alexkirsz) This could be `PagesData` for the data endpoint, but it doesn't // matter (for now at least) because `get_server_resolve_options_context` doesn't // differentiate between the two. Value::new(ServerContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), - ) + )) } #[turbo_tasks::function] - fn client_runtime_entries(self: Vc) -> Vc { + async fn client_runtime_entries(self: Vc) -> Result> { let client_runtime_entries = get_client_runtime_entries( self.project().project_path(), Value::new(ClientContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), self.project().next_config(), self.project().execution_context(), ); - client_runtime_entries.resolve_entries(self.client_module_context()) + Ok(client_runtime_entries.resolve_entries(self.client_module_context())) } #[turbo_tasks::function] - fn runtime_entries(self: Vc) -> Vc { - get_server_runtime_entries( + async fn runtime_entries(self: Vc) -> Result> { + Ok(get_server_runtime_entries( Value::new(ServerContextType::Pages { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), - ) + )) } #[turbo_tasks::function] - fn data_runtime_entries(self: Vc) -> Vc { - get_server_runtime_entries( + async fn data_runtime_entries(self: Vc) -> Result> { + Ok(get_server_runtime_entries( Value::new(ServerContextType::PagesData { - pages_dir: self.pages_dir(), + pages_dir: self.pages_dir().to_resolved().await?, }), self.project().next_mode(), - ) + )) } #[turbo_tasks::function] diff --git a/crates/next-api/src/project.rs b/crates/next-api/src/project.rs index bb0177e987519..0c81a1d820b3b 100644 --- a/crates/next-api/src/project.rs +++ b/crates/next-api/src/project.rs @@ -529,7 +529,7 @@ impl Project { let app_dir = find_app_dir(self.project_path()).await?; Ok(Vc::cell( - app_dir.map(|app_dir| AppProject::new(self, app_dir)), + app_dir.map(|app_dir| AppProject::new(self, *app_dir)), )) } @@ -1001,7 +1001,7 @@ impl Project { self, middleware_asset_context, source, - app_dir, + app_dir.as_deref().copied(), ecmascript_client_reference_transition_name, ))) } @@ -1140,7 +1140,7 @@ impl Project { instrumentation_asset_context, source, is_edge, - app_dir, + app_dir.as_deref().copied(), ecmascript_client_reference_transition_name, ))) } diff --git a/crates/next-core/src/app_structure.rs b/crates/next-core/src/app_structure.rs index 350e5e51d9b91..194d31c182987 100644 --- a/crates/next-core/src/app_structure.rs +++ b/crates/next-core/src/app_structure.rs @@ -230,7 +230,7 @@ impl DirectoryTree { } #[turbo_tasks::value(transparent)] -pub struct OptionAppDir(Option>); +pub struct OptionAppDir(Option>); /// Finds and returns the [DirectoryTree] of the app directory if existing. #[turbo_tasks::function] @@ -244,7 +244,7 @@ pub async fn find_app_dir(project_path: Vc) -> Result) -> Vc { - ImportMapping::Alternatives(vec![ +pub async fn get_postcss_package_mapping( + project_path: ResolvedVc, +) -> Result> { + Ok(ImportMapping::Alternatives(vec![ // Prefer the local installed version over the next.js version - ImportMapping::PrimaryAlternative("postcss".into(), Some(project_path)).cell(), - ImportMapping::PrimaryAlternative("postcss".into(), Some(get_next_package(project_path))) - .cell(), + ImportMapping::PrimaryAlternative("postcss".into(), Some(project_path)).resolved_cell(), + ImportMapping::PrimaryAlternative( + "postcss".into(), + Some(get_next_package(*project_path).to_resolved().await?), + ) + .resolved_cell(), ]) - .cell() + .cell()) } #[turbo_tasks::function] @@ -24,6 +29,6 @@ pub async fn get_external_next_compiled_package_mapping( Some(format!("next/dist/compiled/{}", &*package_name.await?).into()), ExternalType::CommonJs, ) - .into()]) + .resolved_cell()]) .cell()) } diff --git a/crates/next-core/src/next_client/context.rs b/crates/next-core/src/next_client/context.rs index 5c626435a4656..e01a6d4e725da 100644 --- a/crates/next-core/src/next_client/context.rs +++ b/crates/next-core/src/next_client/context.rs @@ -127,11 +127,15 @@ pub fn get_client_compile_time_info( .cell() } -#[turbo_tasks::value(serialization = "auto_for_input")] +#[turbo_tasks::value(shared, serialization = "auto_for_input")] #[derive(Debug, Copy, Clone, Hash)] pub enum ClientContextType { - Pages { pages_dir: Vc }, - App { app_dir: Vc }, + Pages { + pages_dir: ResolvedVc, + }, + App { + app_dir: ResolvedVc, + }, Fallback, Other, } diff --git a/crates/next-core/src/next_client/transforms.rs b/crates/next-core/src/next_client/transforms.rs index 79144264808eb..e85447a0ebdca 100644 --- a/crates/next-core/src/next_client/transforms.rs +++ b/crates/next-core/src/next_client/transforms.rs @@ -50,7 +50,7 @@ pub async fn get_next_client_transforms_rules( if !foreign_code { rules.push( get_next_pages_transforms_rule( - pages_dir, + *pages_dir, ExportFilter::StripDataExports, enable_mdx_rs, ) diff --git a/crates/next-core/src/next_edge/unsupported.rs b/crates/next-core/src/next_edge/unsupported.rs index a91e06618eaa8..48b0f56c831d7 100644 --- a/crates/next-core/src/next_edge/unsupported.rs +++ b/crates/next-core/src/next_edge/unsupported.rs @@ -43,7 +43,7 @@ impl NextEdgeUnsupportedModuleReplacer { impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer { #[turbo_tasks::function] fn replace(&self, _capture: Vc) -> Vc { - ReplacedImportMapping::Ignore.into() + ReplacedImportMapping::Ignore.cell() } #[turbo_tasks::function] @@ -63,11 +63,12 @@ impl ImportMappingReplacement for NextEdgeUnsupportedModuleReplacer { }; let content = AssetContent::file(File::from(code).into()); let source = VirtualSource::new(root_path, content); - return Ok( - ImportMapResult::Result(ResolveResult::source(Vc::upcast(source)).into()).into(), - ); + return Ok(ImportMapResult::Result( + ResolveResult::source(Vc::upcast(source)).resolved_cell(), + ) + .cell()); }; - Ok(ImportMapResult::NoEntry.into()) + Ok(ImportMapResult::NoEntry.cell()) } } diff --git a/crates/next-core/src/next_font/google/mod.rs b/crates/next-core/src/next_font/google/mod.rs index 3ad5503ec9e4a..2997398818724 100644 --- a/crates/next-core/src/next_font/google/mod.rs +++ b/crates/next-core/src/next_font/google/mod.rs @@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result}; use futures::FutureExt; use indoc::formatdoc; use serde::{Deserialize, Serialize}; -use turbo_tasks::{Completion, FxIndexMap, RcStr, Value, Vc}; +use turbo_tasks::{Completion, FxIndexMap, RcStr, ResolvedVc, Value, Vc}; use turbo_tasks_bytes::stream::SingleValue; use turbo_tasks_env::{CommandLineProcessEnv, ProcessEnv}; use turbo_tasks_fetch::{fetch, HttpResponseBody}; @@ -132,9 +132,12 @@ impl NextFontGoogleReplacer { ) .into(), ) - .into()), + .cell()), ); - Ok(ImportMapResult::Result(ResolveResult::source(Vc::upcast(js_asset)).into()).into()) + Ok( + ImportMapResult::Result(ResolveResult::source(Vc::upcast(js_asset)).resolved_cell()) + .cell(), + ) } } @@ -257,13 +260,16 @@ impl NextFontGoogleCssModuleReplacer { .await? .into(), ) - .into(), + .cell(), ), ) - .resolve() + .to_resolved() .await?; - Ok(ImportMapResult::Result(ResolveResult::source(Vc::upcast(css_asset)).into()).into()) + Ok(ImportMapResult::Result( + ResolveResult::source(*ResolvedVc::upcast(css_asset)).resolved_cell(), + ) + .cell()) } } @@ -292,10 +298,10 @@ impl ImportMappingReplacement for NextFontGoogleCssModuleReplacer { fragment: _, } = request else { - return Ok(ImportMapResult::NoEntry.into()); + return Ok(ImportMapResult::NoEntry.cell()); }; - Ok(self.import_map_result(query_vc.await?.to_string().into())) + Ok(self.import_map_result(query_vc.await?.clone_value())) } } @@ -323,7 +329,7 @@ impl NextFontGoogleFontFileReplacer { impl ImportMappingReplacement for NextFontGoogleFontFileReplacer { #[turbo_tasks::function] fn replace(&self, _capture: Vc) -> Vc { - ReplacedImportMapping::Ignore.into() + ReplacedImportMapping::Ignore.cell() } /// Intercepts requests for the font made by the CSS @@ -343,7 +349,7 @@ impl ImportMappingReplacement for NextFontGoogleFontFileReplacer { fragment: _, } = request else { - return Ok(ImportMapResult::NoEntry.into()); + return Ok(ImportMapResult::NoEntry.cell()); }; let NextFontGoogleFontFileOptions { @@ -372,15 +378,20 @@ impl ImportMappingReplacement for NextFontGoogleFontFileReplacer { // really matter either. let Some(font) = fetch_from_google_fonts(Vc::cell(url.into()), font_virtual_path).await? else { - return Ok(ImportMapResult::Result(ResolveResult::unresolvable().into()).into()); + return Ok( + ImportMapResult::Result(ResolveResult::unresolvable().resolved_cell()).cell(), + ); }; let font_source = VirtualSource::new( font_virtual_path, - AssetContent::file(FileContent::Content(font.await?.0.as_slice().into()).into()), + AssetContent::file(FileContent::Content(font.await?.0.as_slice().into()).cell()), ); - Ok(ImportMapResult::Result(ResolveResult::source(Vc::upcast(font_source)).into()).into()) + Ok( + ImportMapResult::Result(ResolveResult::source(Vc::upcast(font_source)).resolved_cell()) + .cell(), + ) } } diff --git a/crates/next-core/src/next_import_map.rs b/crates/next-core/src/next_import_map.rs index 1ac0cf374ee7c..df1c325eaca4d 100644 --- a/crates/next-core/src/next_import_map.rs +++ b/crates/next-core/src/next_import_map.rs @@ -1,7 +1,7 @@ use std::collections::{BTreeMap, HashMap}; use anyhow::{Context, Result}; -use turbo_tasks::{fxindexmap, FxIndexMap, RcStr, Value, Vc}; +use turbo_tasks::{fxindexmap, FxIndexMap, RcStr, ResolvedVc, Value, Vc}; use turbo_tasks_fs::{FileSystem, FileSystemPath}; use turbopack_core::{ reference_type::{CommonJsReferenceSubType, ReferenceType}, @@ -87,7 +87,7 @@ const EDGE_UNSUPPORTED_NODE_INTERNALS: [&str; 44] = [ /// Computes the Next-specific client import map. #[turbo_tasks::function] pub async fn get_next_client_import_map( - project_path: Vc, + project_path: ResolvedVc, ty: Value, next_config: Vc, execution_context: Vc, @@ -167,7 +167,7 @@ pub async fn get_next_client_import_map( "next/dist/compiled/react-dom-experimental/static.browser", ), ); - let react_client_package = get_react_client_package(&next_config).await?; + let react_client_package = get_react_client_package(next_config).await?; import_map.insert_exact_alias( "react-dom/client", request_to_import_mapping( @@ -232,40 +232,43 @@ pub async fn get_next_client_import_map( ClientContextType::Other => {} } - insert_turbopack_dev_alias(&mut import_map); + insert_turbopack_dev_alias(&mut import_map).await?; Ok(import_map.cell()) } /// Computes the Next-specific client import map. #[turbo_tasks::function] -pub fn get_next_build_import_map() -> Vc { +pub async fn get_next_build_import_map() -> Result> { let mut import_map = ImportMap::empty(); insert_package_alias( &mut import_map, &format!("{VIRTUAL_PACKAGE_NAME}/"), - next_js_fs().root(), + next_js_fs().root().to_resolved().await?, ); - let external = ImportMapping::External(None, ExternalType::CommonJs).cell(); + let external = ImportMapping::External(None, ExternalType::CommonJs).resolved_cell(); import_map.insert_exact_alias("next", external); import_map.insert_wildcard_alias("next/", external); import_map.insert_exact_alias("styled-jsx", external); import_map.insert_exact_alias( "styled-jsx/style", - ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs).cell(), + ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs) + .resolved_cell(), ); import_map.insert_wildcard_alias("styled-jsx/", external); - import_map.cell() + Ok(import_map.cell()) } /// Computes the Next-specific client fallback import map, which provides /// polyfills to Node.js externals. #[turbo_tasks::function] -pub fn get_next_client_fallback_import_map(ty: Value) -> Vc { +pub async fn get_next_client_fallback_import_map( + ty: Value, +) -> Result> { let mut import_map = ImportMap::empty(); match ty.into_value() { @@ -284,15 +287,15 @@ pub fn get_next_client_fallback_import_map(ty: Value) -> Vc {} } - insert_turbopack_dev_alias(&mut import_map); + insert_turbopack_dev_alias(&mut import_map).await?; - import_map.cell() + Ok(import_map.cell()) } /// Computes the Next-specific server-side import map. #[turbo_tasks::function] pub async fn get_next_server_import_map( - project_path: Vc, + project_path: ResolvedVc, ty: Value, next_config: Vc, execution_context: Vc, @@ -318,7 +321,7 @@ pub async fn get_next_server_import_map( let ty = ty.into_value(); - let external: Vc = ImportMapping::External(None, ExternalType::CommonJs).cell(); + let external = ImportMapping::External(None, ExternalType::CommonJs).resolved_cell(); import_map.insert_exact_alias("next/dist/server/require-hook", external); match ty { @@ -334,7 +337,7 @@ pub async fn get_next_server_import_map( import_map.insert_exact_alias( "styled-jsx/style", ImportMapping::External(Some("styled-jsx/style.js".into()), ExternalType::CommonJs) - .cell(), + .resolved_cell(), ); import_map.insert_wildcard_alias("styled-jsx/", external); // TODO: we should not bundle next/dist/build/utils in the pages renderer at all @@ -370,7 +373,7 @@ pub async fn get_next_server_import_map( /// Computes the Next-specific edge-side import map. #[turbo_tasks::function] pub async fn get_next_edge_import_map( - project_path: Vc, + project_path: ResolvedVc, ty: Value, next_config: Vc, execution_context: Vc, @@ -480,9 +483,10 @@ pub async fn get_next_edge_import_map( | ServerContextType::PagesApi { .. } => { insert_unsupported_node_internal_aliases( &mut import_map, - project_path, + *project_path, execution_context, - ); + ) + .await?; } } @@ -492,19 +496,22 @@ pub async fn get_next_edge_import_map( /// Insert default aliases for the node.js's internal to raise unsupported /// runtime errors. User may provide polyfills for their own by setting user /// config's alias. -fn insert_unsupported_node_internal_aliases( +async fn insert_unsupported_node_internal_aliases( import_map: &mut ImportMap, project_path: Vc, execution_context: Vc, -) { - let unsupported_replacer = ImportMapping::Dynamic(Vc::upcast( - NextEdgeUnsupportedModuleReplacer::new(project_path, execution_context), +) -> Result<()> { + let unsupported_replacer = ImportMapping::Dynamic(ResolvedVc::upcast( + NextEdgeUnsupportedModuleReplacer::new(project_path, execution_context) + .to_resolved() + .await?, )) - .into(); + .resolved_cell(); EDGE_UNSUPPORTED_NODE_INTERNALS.iter().for_each(|module| { import_map.insert_alias(AliasPattern::exact(*module), unsupported_replacer); }); + Ok(()) } pub fn get_next_client_resolved_map( @@ -547,19 +554,21 @@ static NEXT_ALIASES: [(&str, &str); 23] = [ async fn insert_next_server_special_aliases( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, ty: ServerContextType, runtime: NextRuntime, next_config: Vc, ) -> Result<()> { - let external_cjs_if_node = move |context_dir: Vc, request: &str| match runtime { - NextRuntime::Edge => request_to_import_mapping(context_dir, request), - NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request), - }; - let external_esm_if_node = move |context_dir: Vc, request: &str| match runtime { - NextRuntime::Edge => request_to_import_mapping(context_dir, request), - NextRuntime::NodeJs => external_request_to_esm_import_mapping(request), - }; + let external_cjs_if_node = + move |context_dir: ResolvedVc, request: &str| match runtime { + NextRuntime::Edge => request_to_import_mapping(context_dir, request), + NextRuntime::NodeJs => external_request_to_cjs_import_mapping(request), + }; + let external_esm_if_node = + move |context_dir: ResolvedVc, request: &str| match runtime { + NextRuntime::Edge => request_to_import_mapping(context_dir, request), + NextRuntime::NodeJs => external_request_to_esm_import_mapping(request), + }; import_map.insert_exact_alias( "next/dist/compiled/@vercel/og/index.node.js", @@ -572,7 +581,7 @@ async fn insert_next_server_special_aliases( request_to_import_mapping(project_path, "react-dom/server.edge"), request_to_import_mapping(project_path, "react-dom/server.browser"), ]) - .cell(), + .resolved_cell(), ); import_map.insert_exact_alias( @@ -582,7 +591,7 @@ async fn insert_next_server_special_aliases( external_cjs_if_node(project_path, "@opentelemetry/api"), external_cjs_if_node(project_path, "next/dist/compiled/@opentelemetry/api"), ]) - .cell(), + .resolved_cell(), ); match ty { @@ -592,13 +601,14 @@ async fn insert_next_server_special_aliases( ServerContextType::AppSSR { app_dir } | ServerContextType::AppRSC { app_dir, .. } | ServerContextType::AppRoute { app_dir, .. } => { + let next_package = get_next_package(*app_dir).to_resolved().await?; import_map.insert_exact_alias( "styled-jsx", - request_to_import_mapping(get_next_package(app_dir), "styled-jsx"), + request_to_import_mapping(next_package, "styled-jsx"), ); import_map.insert_wildcard_alias( "styled-jsx/", - request_to_import_mapping(get_next_package(app_dir), "styled-jsx/*"), + request_to_import_mapping(next_package, "styled-jsx/*"), ); rsc_aliases(import_map, project_path, ty, runtime, next_config).await?; @@ -665,7 +675,7 @@ async fn insert_next_server_special_aliases( Ok(()) } -async fn get_react_client_package(&next_config: &Vc) -> Result<&'static str> { +async fn get_react_client_package(next_config: Vc) -> Result<&'static str> { let react_production_profiling = *next_config.enable_react_production_profiling().await?; let react_client_package = if react_production_profiling { "profiling" @@ -678,7 +688,7 @@ async fn get_react_client_package(&next_config: &Vc) -> Result<&'sta async fn rsc_aliases( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, ty: ServerContextType, runtime: NextRuntime, next_config: Vc, @@ -691,7 +701,7 @@ async fn rsc_aliases( } else { "" }; - let react_client_package = get_react_client_package(&next_config).await?; + let react_client_package = get_react_client_package(next_config).await?; let mut alias = FxIndexMap::default(); if matches!( @@ -800,7 +810,7 @@ pub fn mdx_import_source_file() -> RcStr { // Keep in sync with getOptimizedModuleAliases in webpack-config.ts async fn insert_optimized_module_aliases( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, ) -> Result<()> { insert_exact_alias_map( import_map, @@ -824,12 +834,12 @@ async fn insert_optimized_module_aliases( // Make sure to not add any external requests here. async fn insert_next_shared_aliases( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, execution_context: Vc, next_config: Vc, is_runtime_edge: bool, ) -> Result<()> { - let package_root = next_js_fs().root(); + let package_root = next_js_fs().root().to_resolved().await?; insert_alias_to_alternatives( import_map, @@ -852,41 +862,52 @@ async fn insert_next_shared_aliases( // // TODO: Add BeforeResolve plugins for `@next/font/google` + let next_font_google_replacer_mapping = ImportMapping::Dynamic(ResolvedVc::upcast( + NextFontGoogleReplacer::new(*project_path) + .to_resolved() + .await?, + )) + .resolved_cell(); + import_map.insert_alias( // Request path from js via next-font swc transform AliasPattern::exact("next/font/google/target.css"), - ImportMapping::Dynamic(Vc::upcast(NextFontGoogleReplacer::new(project_path))).into(), + next_font_google_replacer_mapping, ); import_map.insert_alias( // Request path from js via next-font swc transform AliasPattern::exact("@next/font/google/target.css"), - ImportMapping::Dynamic(Vc::upcast(NextFontGoogleReplacer::new(project_path))).into(), + next_font_google_replacer_mapping, ); import_map.insert_alias( AliasPattern::exact("@vercel/turbopack-next/internal/font/google/cssmodule.module.css"), - ImportMapping::Dynamic(Vc::upcast(NextFontGoogleCssModuleReplacer::new( - project_path, - execution_context, - ))) - .into(), + ImportMapping::Dynamic(ResolvedVc::upcast( + NextFontGoogleCssModuleReplacer::new(*project_path, execution_context) + .to_resolved() + .await?, + )) + .resolved_cell(), ); import_map.insert_alias( AliasPattern::exact(GOOGLE_FONTS_INTERNAL_PREFIX), - ImportMapping::Dynamic(Vc::upcast(NextFontGoogleFontFileReplacer::new( - project_path, - ))) - .into(), + ImportMapping::Dynamic(ResolvedVc::upcast( + NextFontGoogleFontFileReplacer::new(*project_path) + .to_resolved() + .await?, + )) + .resolved_cell(), ); - import_map.insert_singleton_alias("@swc/helpers", get_next_package(project_path)); - import_map.insert_singleton_alias("styled-jsx", get_next_package(project_path)); + let next_package = get_next_package(*project_path).to_resolved().await?; + import_map.insert_singleton_alias("@swc/helpers", next_package); + import_map.insert_singleton_alias("styled-jsx", next_package); import_map.insert_singleton_alias("next", project_path); import_map.insert_singleton_alias("react", project_path); import_map.insert_singleton_alias("react-dom", project_path); - let react_client_package = get_react_client_package(&next_config).await?; + let react_client_package = get_react_client_package(next_config).await?; import_map.insert_exact_alias( "react-dom/client", request_to_import_mapping(project_path, &format!("react-dom/{react_client_package}")), @@ -896,7 +917,7 @@ async fn insert_next_shared_aliases( // Make sure you can't import custom server as it'll cause all Next.js internals to be // bundled which doesn't work. AliasPattern::exact("next"), - ImportMapping::Empty.into(), + ImportMapping::Empty.resolved_cell(), ); //https://github.com/vercel/next.js/blob/f94d4f93e4802f951063cfa3351dd5a2325724b3/packages/next/src/build/webpack-config.ts#L1196 @@ -938,11 +959,14 @@ async fn insert_next_shared_aliases( ), ); - insert_turbopack_dev_alias(import_map); + insert_turbopack_dev_alias(import_map).await?; insert_package_alias( import_map, "@vercel/turbopack-node/", - turbopack_node::embed_js::embed_fs().root(), + turbopack_node::embed_js::embed_fs() + .root() + .to_resolved() + .await?, ); let image_config = next_config.image_config().await?; @@ -980,7 +1004,7 @@ pub async fn get_next_package(context_directory: Vc) -> Result( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, alias_options: Vc, conditions: [&'static str; N], ) -> Result<()> { @@ -996,8 +1020,8 @@ pub async fn insert_alias_option( fn export_value_to_import_mapping( value: &SubpathValue, conditions: &BTreeMap, - project_path: Vc, -) -> Option> { + project_path: ResolvedVc, +) -> Option> { let mut result = Vec::new(); value.add_results( conditions, @@ -1009,24 +1033,26 @@ fn export_value_to_import_mapping( None } else { Some(if result.len() == 1 { - ImportMapping::PrimaryAlternative(result[0].0.into(), Some(project_path)).cell() + ImportMapping::PrimaryAlternative(result[0].0.into(), Some(project_path)) + .resolved_cell() } else { ImportMapping::Alternatives( result .iter() .map(|(m, _)| { - ImportMapping::PrimaryAlternative((*m).into(), Some(project_path)).cell() + ImportMapping::PrimaryAlternative((*m).into(), Some(project_path)) + .resolved_cell() }) .collect(), ) - .cell() + .resolved_cell() }) } } fn insert_exact_alias_map( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, map: FxIndexMap<&'static str, String>, ) { for (pattern, request) in map { @@ -1036,7 +1062,7 @@ fn insert_exact_alias_map( fn insert_wildcard_alias_map( import_map: &mut ImportMap, - project_path: Vc, + project_path: ResolvedVc, map: FxIndexMap<&'static str, String>, ) { for (pattern, request) in map { @@ -1049,11 +1075,11 @@ fn insert_wildcard_alias_map( fn insert_alias_to_alternatives<'a>( import_map: &mut ImportMap, alias: impl Into + 'a, - alternatives: Vec>, + alternatives: Vec>, ) { import_map.insert_exact_alias( alias.into(), - ImportMapping::Alternatives(alternatives).into(), + ImportMapping::Alternatives(alternatives).resolved_cell(), ); } @@ -1061,37 +1087,44 @@ fn insert_alias_to_alternatives<'a>( fn insert_package_alias( import_map: &mut ImportMap, prefix: &str, - package_root: Vc, + package_root: ResolvedVc, ) { import_map.insert_wildcard_alias( prefix, - ImportMapping::PrimaryAlternative("./*".into(), Some(package_root)).cell(), + ImportMapping::PrimaryAlternative("./*".into(), Some(package_root)).resolved_cell(), ); } /// Inserts an alias to @vercel/turbopack-dev into an import map. -fn insert_turbopack_dev_alias(import_map: &mut ImportMap) { +async fn insert_turbopack_dev_alias(import_map: &mut ImportMap) -> Result<()> { insert_package_alias( import_map, "@vercel/turbopack-ecmascript-runtime/", - turbopack_ecmascript_runtime::embed_fs().root(), + turbopack_ecmascript_runtime::embed_fs() + .root() + .to_resolved() + .await?, ); + Ok(()) } /// Creates a direct import mapping to the result of resolving a request /// in a context. -fn request_to_import_mapping(context_path: Vc, request: &str) -> Vc { - ImportMapping::PrimaryAlternative(request.into(), Some(context_path)).cell() +fn request_to_import_mapping( + context_path: ResolvedVc, + request: &str, +) -> ResolvedVc { + ImportMapping::PrimaryAlternative(request.into(), Some(context_path)).resolved_cell() } /// Creates a direct import mapping to the result of resolving an external /// request. -fn external_request_to_cjs_import_mapping(request: &str) -> Vc { - ImportMapping::External(Some(request.into()), ExternalType::CommonJs).into() +fn external_request_to_cjs_import_mapping(request: &str) -> ResolvedVc { + ImportMapping::External(Some(request.into()), ExternalType::CommonJs).resolved_cell() } /// Creates a direct import mapping to the result of resolving an external /// request. -fn external_request_to_esm_import_mapping(request: &str) -> Vc { - ImportMapping::External(Some(request.into()), ExternalType::EcmaScriptModule).into() +fn external_request_to_esm_import_mapping(request: &str) -> ResolvedVc { + ImportMapping::External(Some(request.into()), ExternalType::EcmaScriptModule).resolved_cell() } diff --git a/crates/next-core/src/next_server/context.rs b/crates/next-core/src/next_server/context.rs index c355f33b69687..d52b3c1492c5d 100644 --- a/crates/next-core/src/next_server/context.rs +++ b/crates/next-core/src/next_server/context.rs @@ -72,36 +72,36 @@ use crate::{ }, }; -#[turbo_tasks::value(serialization = "auto_for_input")] +#[turbo_tasks::value(shared, serialization = "auto_for_input")] #[derive(Debug, Copy, Clone, Hash)] pub enum ServerContextType { Pages { - pages_dir: Vc, + pages_dir: ResolvedVc, }, PagesApi { - pages_dir: Vc, + pages_dir: ResolvedVc, }, PagesData { - pages_dir: Vc, + pages_dir: ResolvedVc, }, AppSSR { - app_dir: Vc, + app_dir: ResolvedVc, }, AppRSC { - app_dir: Vc, + app_dir: ResolvedVc, ecmascript_client_reference_transition_name: Option>, client_transition: Option>>, }, AppRoute { - app_dir: Vc, + app_dir: ResolvedVc, ecmascript_client_reference_transition_name: Option>, }, Middleware { - app_dir: Option>, + app_dir: Option>, ecmascript_client_reference_transition_name: Option>, }, Instrumentation { - app_dir: Option>, + app_dir: Option>, ecmascript_client_reference_transition_name: Option>, }, } @@ -622,7 +622,7 @@ pub async fn get_server_module_options_context( foreign_next_server_rules.extend(internal_custom_rules); custom_source_transform_rules.push( - get_next_react_server_components_transform_rule(next_config, false, Some(app_dir)) + get_next_react_server_components_transform_rule(next_config, false, Some(*app_dir)) .await?, ); @@ -701,7 +701,7 @@ pub async fn get_server_module_options_context( foreign_next_server_rules.extend(internal_custom_rules); custom_source_transform_rules.push( - get_next_react_server_components_transform_rule(next_config, true, Some(app_dir)) + get_next_react_server_components_transform_rule(next_config, true, Some(*app_dir)) .await?, ); @@ -755,7 +755,7 @@ pub async fn get_server_module_options_context( next_server_rules.extend(source_transform_rules); let mut common_next_server_rules = vec![ - get_next_react_server_components_transform_rule(next_config, true, Some(app_dir)) + get_next_react_server_components_transform_rule(next_config, true, Some(*app_dir)) .await?, ]; @@ -856,7 +856,12 @@ pub async fn get_server_module_options_context( } custom_source_transform_rules.push( - get_next_react_server_components_transform_rule(next_config, true, app_dir).await?, + get_next_react_server_components_transform_rule( + next_config, + true, + app_dir.as_deref().copied(), + ) + .await?, ); internal_custom_rules.extend(custom_source_transform_rules.iter().cloned()); diff --git a/crates/next-core/src/next_server/transforms.rs b/crates/next-core/src/next_server/transforms.rs index 9f92f50776725..58a043a13a30e 100644 --- a/crates/next-core/src/next_server/transforms.rs +++ b/crates/next-core/src/next_server/transforms.rs @@ -70,7 +70,7 @@ pub async fn get_next_server_transforms_rules( if !foreign_code { rules.push( get_next_pages_transforms_rule( - pages_dir, + *pages_dir, ExportFilter::StripDefaultExport, mdx_rs, ) diff --git a/turbopack/crates/turbopack-cli/src/contexts.rs b/turbopack/crates/turbopack-cli/src/contexts.rs index c64c8aaeb3413..603594cd1d994 100644 --- a/turbopack/crates/turbopack-cli/src/contexts.rs +++ b/turbopack/crates/turbopack-cli/src/contexts.rs @@ -50,7 +50,9 @@ async fn foreign_code_context_condition() -> Result { } #[turbo_tasks::function] -pub fn get_client_import_map(project_path: Vc) -> Vc { +pub async fn get_client_import_map( + project_path: ResolvedVc, +) -> Result> { let mut import_map = ImportMap::empty(); import_map.insert_singleton_alias("@swc/helpers", project_path); @@ -62,12 +64,17 @@ pub fn get_client_import_map(project_path: Vc) -> Vc "@vercel/turbopack-ecmascript-runtime/", ImportMapping::PrimaryAlternative( "./*".into(), - Some(turbopack_ecmascript_runtime::embed_fs().root()), + Some( + turbopack_ecmascript_runtime::embed_fs() + .root() + .to_resolved() + .await?, + ), ) - .cell(), + .resolved_cell(), ); - import_map.cell() + Ok(import_map.cell()) } #[turbo_tasks::function] diff --git a/turbopack/crates/turbopack-core/src/resolve/mod.rs b/turbopack/crates/turbopack-core/src/resolve/mod.rs index a42b546eceb07..dc80f934d7b78 100644 --- a/turbopack/crates/turbopack-core/src/resolve/mod.rs +++ b/turbopack/crates/turbopack-core/src/resolve/mod.rs @@ -2506,7 +2506,7 @@ async fn resolve_import_map_result( query: Vc, ) -> Result>> { Ok(match result { - ImportMapResult::Result(result) => Some(*result), + ImportMapResult::Result(result) => Some(**result), ImportMapResult::Alias(request, alias_lookup_path) => { let request = *request; let lookup_path = match alias_lookup_path { diff --git a/turbopack/crates/turbopack-core/src/resolve/options.rs b/turbopack/crates/turbopack-core/src/resolve/options.rs index b06f3d10171ff..695cc75812a11 100644 --- a/turbopack/crates/turbopack-core/src/resolve/options.rs +++ b/turbopack/crates/turbopack-core/src/resolve/options.rs @@ -34,8 +34,8 @@ pub enum ResolveModules { Nested(ResolvedVc, Vec), /// look into that directory, unless the request has an excluded extension Path { - dir: Vc, - excluded_extensions: Vc, + dir: ResolvedVc, + excluded_extensions: ResolvedVc, }, } @@ -99,11 +99,11 @@ pub enum ImportMapping { /// A request alias that will be resolved first, and fall back to resolving /// the original request if it fails. Useful for the tsconfig.json /// `compilerOptions.paths` option and Next aliases. - PrimaryAlternative(RcStr, Option>), + PrimaryAlternative(RcStr, Option>), Ignore, Empty, - Alternatives(Vec>), - Dynamic(Vc>), + Alternatives(Vec>), + Dynamic(ResolvedVc>), } /// An `ImportMapping` that was applied to a pattern. See `ImportMapping` for @@ -112,18 +112,18 @@ pub enum ImportMapping { #[derive(Clone)] pub enum ReplacedImportMapping { External(Option, ExternalType), - Direct(Vc), - PrimaryAlternative(Pattern, Option>), + Direct(ResolvedVc), + PrimaryAlternative(Pattern, Option>), Ignore, Empty, - Alternatives(Vec>), + Alternatives(Vec>), Dynamic(ResolvedVc>), } impl ImportMapping { pub fn primary_alternatives( list: Vec, - lookup_path: Option>, + lookup_path: Option>, ) -> ImportMapping { if list.is_empty() { ImportMapping::Ignore @@ -132,15 +132,28 @@ impl ImportMapping { } else { ImportMapping::Alternatives( list.into_iter() - .map(|s| ImportMapping::PrimaryAlternative(s, lookup_path).cell()) + .map(|s| ImportMapping::PrimaryAlternative(s, lookup_path).resolved_cell()) .collect(), ) } } } +impl AliasTemplate for ResolvedVc { + type Output<'a> = as AliasTemplate>::Output<'a>; + + fn convert(&self) -> Self::Output<'_> { + (**self).convert() + } + + fn replace<'a>(&'a self, capture: &Pattern) -> Self::Output<'a> { + (**self).replace(capture) + } +} + impl AliasTemplate for Vc { - type Output<'a> = Pin>> + Send + 'a>>; + type Output<'a> = + Pin>> + Send + 'a>>; fn convert(&self) -> Self::Output<'_> { Box::pin(async move { @@ -152,7 +165,7 @@ impl AliasTemplate for Vc { ImportMapping::PrimaryAlternative(name, context) => { ReplacedImportMapping::PrimaryAlternative((*name).clone().into(), *context) } - ImportMapping::Direct(v) => ReplacedImportMapping::Direct(**v), + ImportMapping::Direct(v) => ReplacedImportMapping::Direct(*v), ImportMapping::Ignore => ReplacedImportMapping::Ignore, ImportMapping::Empty => ReplacedImportMapping::Empty, ImportMapping::Alternatives(alternatives) => ReplacedImportMapping::Alternatives( @@ -166,7 +179,7 @@ impl AliasTemplate for Vc { ReplacedImportMapping::Dynamic(replacement.to_resolved().await?) } } - .cell()) + .resolved_cell()) }) } @@ -191,7 +204,7 @@ impl AliasTemplate for Vc { *context, ) } - ImportMapping::Direct(v) => ReplacedImportMapping::Direct(**v), + ImportMapping::Direct(v) => ReplacedImportMapping::Direct(*v), ImportMapping::Ignore => ReplacedImportMapping::Ignore, ImportMapping::Empty => ReplacedImportMapping::Empty, ImportMapping::Alternatives(alternatives) => ReplacedImportMapping::Alternatives( @@ -205,7 +218,7 @@ impl AliasTemplate for Vc { (*replacement.replace(capture.clone().cell()).await?).clone() } } - .cell()) + .resolved_cell()) }) } } @@ -213,12 +226,12 @@ impl AliasTemplate for Vc { #[turbo_tasks::value(shared)] #[derive(Clone, Default)] pub struct ImportMap { - map: AliasMap>, + map: AliasMap>, } impl ImportMap { /// Creates a new import map. - pub fn new(map: AliasMap>) -> ImportMap { + pub fn new(map: AliasMap>) -> ImportMap { Self { map } } @@ -234,7 +247,7 @@ impl ImportMap { } /// Inserts an alias into the import map. - pub fn insert_alias(&mut self, alias: AliasPattern, mapping: Vc) { + pub fn insert_alias(&mut self, alias: AliasPattern, mapping: ResolvedVc) { self.map.insert(alias, mapping); } @@ -242,7 +255,7 @@ impl ImportMap { pub fn insert_exact_alias<'a>( &mut self, pattern: impl Into + 'a, - mapping: Vc, + mapping: ResolvedVc, ) { self.map.insert(AliasPattern::exact(pattern), mapping); } @@ -251,7 +264,7 @@ impl ImportMap { pub fn insert_wildcard_alias<'a>( &mut self, prefix: impl Into + 'a, - mapping: Vc, + mapping: ResolvedVc, ) { self.map.insert(AliasPattern::wildcard(prefix, ""), mapping); } @@ -261,7 +274,7 @@ impl ImportMap { &mut self, prefix: impl Into + 'p, suffix: impl Into + 's, - mapping: Vc, + mapping: ResolvedVc, ) { self.map .insert(AliasPattern::wildcard(prefix, suffix), mapping); @@ -272,18 +285,18 @@ impl ImportMap { pub fn insert_singleton_alias<'a>( &mut self, prefix: impl Into + 'a, - context_path: Vc, + context_path: ResolvedVc, ) { let prefix: RcStr = prefix.into(); let wildcard_prefix: RcStr = (prefix.to_string() + "/").into(); let wildcard_alias: RcStr = (prefix.to_string() + "/*").into(); self.insert_exact_alias( prefix.clone(), - ImportMapping::PrimaryAlternative(prefix.clone(), Some(context_path)).cell(), + ImportMapping::PrimaryAlternative(prefix.clone(), Some(context_path)).resolved_cell(), ); self.insert_wildcard_alias( wildcard_prefix, - ImportMapping::PrimaryAlternative(wildcard_alias, Some(context_path)).cell(), + ImportMapping::PrimaryAlternative(wildcard_alias, Some(context_path)).resolved_cell(), ); } } @@ -292,7 +305,7 @@ impl ImportMap { impl ImportMap { /// Extends the underlying [ImportMap] with another [ImportMap]. #[turbo_tasks::function] - pub async fn extend(self: Vc, other: Vc) -> Result> { + pub async fn extend(self: Vc, other: ResolvedVc) -> Result> { let mut import_map = self.await?.clone_value(); import_map.extend_ref(&*other.await?); Ok(import_map.cell()) @@ -312,7 +325,7 @@ pub struct ResolvedMap { #[turbo_tasks::value(shared)] #[derive(Clone, Debug)] pub enum ImportMapResult { - Result(Vc), + Result(ResolvedVc), Alias(ResolvedVc, Option>), Alternatives(Vec), NoEntry, @@ -326,7 +339,7 @@ async fn import_mapping_to_result( Ok(match &*mapping.await? { ReplacedImportMapping::Direct(result) => ImportMapResult::Result(*result), ReplacedImportMapping::External(name, ty) => ImportMapResult::Result( - *ResolveResult::primary(if let Some(name) = name { + ResolveResult::primary(if let Some(name) = name { ResolveResultItem::External(name.clone(), *ty) } else if let Some(request) = request.await?.request() { ResolveResultItem::External(request, *ty) @@ -336,28 +349,27 @@ async fn import_mapping_to_result( .resolved_cell(), ), ReplacedImportMapping::Ignore => ImportMapResult::Result( - *ResolveResult::primary(ResolveResultItem::Ignore).resolved_cell(), + ResolveResult::primary(ResolveResultItem::Ignore).resolved_cell(), ), ReplacedImportMapping::Empty => ImportMapResult::Result( - *ResolveResult::primary(ResolveResultItem::Empty).resolved_cell(), + ResolveResult::primary(ResolveResultItem::Empty).resolved_cell(), ), ReplacedImportMapping::PrimaryAlternative(name, context) => { - let request = Request::parse(Value::new(name.clone())); - let context_resolved = match context { - Some(c) => Some((*c).to_resolved().await?), - None => None, - }; - ImportMapResult::Alias(request.to_resolved().await?, context_resolved) + let request = Request::parse(Value::new(name.clone())) + .to_resolved() + .await?; + ImportMapResult::Alias(request, *context) } ReplacedImportMapping::Alternatives(list) => ImportMapResult::Alternatives( list.iter() - .map(|mapping| Box::pin(import_mapping_to_result(*mapping, lookup_path, request))) + .map(|mapping| Box::pin(import_mapping_to_result(**mapping, lookup_path, request))) .try_join() .await?, ), - ReplacedImportMapping::Dynamic(replacement) => { - (*replacement.result(lookup_path, request).await?).clone() - } + ReplacedImportMapping::Dynamic(replacement) => replacement + .result(lookup_path, request) + .await? + .clone_value(), }) } @@ -430,8 +442,8 @@ impl ImportMap { .into_iter() .chain(lookup_rel_parent.into_iter()) .chain(lookup.into_iter()) - .map(async |result: super::AliasMatch>| { - import_mapping_to_result(result.try_join_into_self().await?, lookup_path, request) + .map(async |result| { + import_mapping_to_result(*result.try_join_into_self().await?, lookup_path, request) .await }) .try_join() @@ -460,7 +472,7 @@ impl ResolvedMap { if let Some(path) = root.get_path_to(&resolved) { if glob.await?.execute(path) { return Ok(import_mapping_to_result( - mapping.convert().await?, + *mapping.convert().await?, lookup_path, request, ) @@ -495,7 +507,7 @@ pub struct ResolveOptions { /// An import map to use before resolving a request. pub import_map: Option>, /// An import map to use when a request is otherwise unresolvable. - pub fallback_import_map: Option>, + pub fallback_import_map: Option>, pub resolved_map: Option>, pub before_resolve_plugins: Vec>>, pub plugins: Vec>>, @@ -533,15 +545,20 @@ impl ResolveOptions { #[turbo_tasks::function] pub async fn with_extended_fallback_import_map( self: Vc, - import_map: Vc, + extended_import_map: ResolvedVc, ) -> Result> { let mut resolve_options = self.await?.clone_value(); - resolve_options.fallback_import_map = Some( - resolve_options - .fallback_import_map - .map(|current_import_map| current_import_map.extend(import_map)) - .unwrap_or(import_map), - ); + resolve_options.fallback_import_map = + if let Some(current_fallback) = resolve_options.fallback_import_map { + Some( + current_fallback + .extend(*extended_import_map) + .to_resolved() + .await?, + ) + } else { + Some(extended_import_map) + }; Ok(resolve_options.into()) } diff --git a/turbopack/crates/turbopack-resolve/src/resolve.rs b/turbopack/crates/turbopack-resolve/src/resolve.rs index 03b122b171e45..152fad6d0ab95 100644 --- a/turbopack/crates/turbopack-resolve/src/resolve.rs +++ b/turbopack/crates/turbopack-resolve/src/resolve.rs @@ -106,11 +106,11 @@ async fn base_resolve_options( for req in NODE_EXTERNALS { direct_mappings.insert( AliasPattern::exact(req), - ImportMapping::External(None, ExternalType::CommonJs).into(), + ImportMapping::External(None, ExternalType::CommonJs).resolved_cell(), ); direct_mappings.insert( AliasPattern::exact(format!("node:{req}")), - ImportMapping::External(None, ExternalType::CommonJs).into(), + ImportMapping::External(None, ExternalType::CommonJs).resolved_cell(), ); } } @@ -119,11 +119,11 @@ async fn base_resolve_options( direct_mappings.insert( AliasPattern::exact(req), ImportMapping::External(Some(format!("node:{req}").into()), ExternalType::CommonJs) - .into(), + .resolved_cell(), ); direct_mappings.insert( AliasPattern::exact(format!("node:{req}")), - ImportMapping::External(None, ExternalType::CommonJs).into(), + ImportMapping::External(None, ExternalType::CommonJs).resolved_cell(), ); } } diff --git a/turbopack/crates/turbopack-resolve/src/typescript.rs b/turbopack/crates/turbopack-resolve/src/typescript.rs index 6176314f1cfdb..b0a1f5cb37255 100644 --- a/turbopack/crates/turbopack-resolve/src/typescript.rs +++ b/turbopack/crates/turbopack-resolve/src/typescript.rs @@ -271,6 +271,7 @@ pub async fn tsconfig_resolve_options( context_dir = *new_context; } }; + let context_dir = context_dir.to_resolved().await?; for (key, value) in paths.iter() { if let JsonValue::Array(vec) = value { let entries = vec @@ -319,7 +320,7 @@ pub async fn tsconfig_resolve_options( let import_map = if !all_paths.is_empty() { let mut import_map = ImportMap::empty(); for (key, value) in all_paths { - import_map.insert_alias(AliasPattern::parse(key), value.into()); + import_map.insert_alias(AliasPattern::parse(key), value.resolved_cell()); } Some(import_map.resolved_cell()) } else { @@ -360,9 +361,9 @@ pub async fn apply_tsconfig_resolve_options( resolve_options.modules.insert( 0, ResolveModules::Path { - dir: *base_url, + dir: base_url, // tsconfig basepath doesn't apply to json requests - excluded_extensions: Vc::cell(fxindexset![".json".into()]), + excluded_extensions: ResolvedVc::cell(fxindexset![".json".into()]), }, ); } diff --git a/turbopack/crates/turbopack-tests/tests/execution.rs b/turbopack/crates/turbopack-tests/tests/execution.rs index e79da8497b0ef..2d3b674ab7c39 100644 --- a/turbopack/crates/turbopack-tests/tests/execution.rs +++ b/turbopack/crates/turbopack-tests/tests/execution.rs @@ -274,7 +274,7 @@ async fn run_test(prepared_test: Vc) -> Result> let mut import_map = ImportMap::empty(); import_map.insert_wildcard_alias( "esm-external/", - ImportMapping::External(Some("*".into()), ExternalType::EcmaScriptModule).cell(), + ImportMapping::External(Some("*".into()), ExternalType::EcmaScriptModule).resolved_cell(), ); let asset_context: Vc> = Vc::upcast(ModuleAssetContext::new( diff --git a/turbopack/crates/turbopack/src/evaluate_context.rs b/turbopack/crates/turbopack/src/evaluate_context.rs index 0d784863e1f87..62abc6f7778a2 100644 --- a/turbopack/crates/turbopack/src/evaluate_context.rs +++ b/turbopack/crates/turbopack/src/evaluate_context.rs @@ -44,9 +44,14 @@ pub async fn node_evaluate_asset_context( "@vercel/turbopack-node/", ImportMapping::PrimaryAlternative( "./*".into(), - Some(turbopack_node::embed_js::embed_fs().root()), + Some( + turbopack_node::embed_js::embed_fs() + .root() + .to_resolved() + .await?, + ), ) - .cell(), + .resolved_cell(), ); let import_map = import_map.resolved_cell(); let node_env: RcStr = diff --git a/turbopack/crates/turbopack/src/module_options/mod.rs b/turbopack/crates/turbopack/src/module_options/mod.rs index 96a515ba8abd7..895cd59314762 100644 --- a/turbopack/crates/turbopack/src/module_options/mod.rs +++ b/turbopack/crates/turbopack/src/module_options/mod.rs @@ -10,7 +10,7 @@ pub use custom_module_type::CustomModuleType; pub use module_options_context::*; pub use module_rule::*; pub use rule_condition::*; -use turbo_tasks::{RcStr, Vc}; +use turbo_tasks::{RcStr, ResolvedVc, Vc}; use turbo_tasks_fs::{glob::Glob, FileSystemPath}; use turbopack_core::{ reference_type::{CssReferenceSubType, ReferenceType, UrlReferenceSubType}, @@ -29,7 +29,7 @@ use crate::{ #[turbo_tasks::function] async fn package_import_map_from_import_mapping( package_name: RcStr, - package_mapping: Vc, + package_mapping: ResolvedVc, ) -> Vc { let mut import_map = ImportMap::default(); import_map.insert_exact_alias( @@ -42,12 +42,12 @@ async fn package_import_map_from_import_mapping( #[turbo_tasks::function] async fn package_import_map_from_context( package_name: RcStr, - context_path: Vc, + context_path: ResolvedVc, ) -> Vc { let mut import_map = ImportMap::default(); import_map.insert_exact_alias( format!("@vercel/turbopack/{}", package_name), - ImportMapping::PrimaryAlternative(package_name, Some(context_path)).cell(), + ImportMapping::PrimaryAlternative(package_name, Some(context_path)).resolved_cell(), ); import_map.cell() }