From be928f739e271c6c2b47ac1f4e765fa1bc11a5e0 Mon Sep 17 00:00:00 2001 From: Alexander Lyon Date: Wed, 25 Sep 2024 14:52:43 +0100 Subject: [PATCH] refactor external ResolveResult to include source --- crates/next-core/src/next_server/resolve.rs | 29 +++++++----- crates/next-core/src/next_shared/resolve.rs | 11 +++-- .../crates/turbopack-core/src/resolve/mod.rs | 47 +++++++++++++++---- .../turbopack-core/src/resolve/options.rs | 20 +++++--- .../src/references/esm/base.rs | 6 ++- .../src/references/pattern_mapping.rs | 6 ++- turbopack/crates/turbopack/src/lib.rs | 7 ++- 7 files changed, 91 insertions(+), 35 deletions(-) diff --git a/crates/next-core/src/next_server/resolve.rs b/crates/next-core/src/next_server/resolve.rs index e79fc228f00a5..c008459a8510e 100644 --- a/crates/next-core/src/next_server/resolve.rs +++ b/crates/next-core/src/next_server/resolve.rs @@ -11,7 +11,7 @@ use turbopack_core::{ package_json, parse::Request, plugin::{AfterResolvePlugin, AfterResolvePluginCondition}, - resolve, ExternalType, FindContextFileResult, ResolveResult, ResolveResultItem, + resolve, External, ExternalType, FindContextFileResult, ResolveResult, ResolveResultItem, ResolveResultOption, }, source::Source, @@ -364,10 +364,11 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { (FileType::CommonJs, false) => { // mark as external Ok(ResolveResultOption::some( - ResolveResult::primary(ResolveResultItem::External( - request_str.into(), - ExternalType::CommonJs, - )) + ResolveResult::primary(ResolveResultItem::External { + name: request_str.into(), + typ: ExternalType::CommonJs, + source: None, + }) .cell(), )) } @@ -401,14 +402,15 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { } else { // mark as external Ok(ResolveResultOption::some( - ResolveResult::primary(ResolveResultItem::External( - request_str.into(), - if resolves_equal { + ResolveResult::primary(ResolveResultItem::External { + name: request_str.into(), + typ: if resolves_equal { ExternalType::CommonJs } else { ExternalType::EcmaScriptModule }, - )) + source: None, + }) .cell(), )) } @@ -416,10 +418,11 @@ impl AfterResolvePlugin for ExternalCjsModulesResolvePlugin { (FileType::EcmaScriptModule, true) => { // mark as external Ok(ResolveResultOption::some( - ResolveResult::primary(ResolveResultItem::External( - request_str.into(), - ExternalType::EcmaScriptModule, - )) + ResolveResult::primary(ResolveResultItem::External { + name: request_str.into(), + typ: ExternalType::EcmaScriptModule, + source: None, // TODO(arlyon): source + }) .cell(), )) } diff --git a/crates/next-core/src/next_shared/resolve.rs b/crates/next-core/src/next_shared/resolve.rs index 82d797ad885b3..cbb933430c285 100644 --- a/crates/next-core/src/next_shared/resolve.rs +++ b/crates/next-core/src/next_shared/resolve.rs @@ -15,7 +15,7 @@ use turbopack_core::{ AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin, BeforeResolvePluginCondition, }, - ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption, + External, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption, }, }; @@ -241,10 +241,11 @@ impl AfterResolvePlugin for NextExternalResolvePlugin { // Replace '/esm/' with '/' to match the CJS version of the file. let modified_path = path[starting_index..].replace("/esm/", "/"); Ok(Vc::cell(Some( - ResolveResult::primary(ResolveResultItem::External( - modified_path.into(), - ExternalType::CommonJs, - )) + ResolveResult::primary(ResolveResultItem::External { + name: modified_path.into(), + typ: ExternalType::CommonJs, + source: None, // TODO(arlyon): source + }) .into(), ))) } diff --git a/turbopack/crates/turbopack-core/src/resolve/mod.rs b/turbopack/crates/turbopack-core/src/resolve/mod.rs index 180ab844baef9..7ad5eaa0a91fc 100644 --- a/turbopack/crates/turbopack-core/src/resolve/mod.rs +++ b/turbopack/crates/turbopack-core/src/resolve/mod.rs @@ -63,7 +63,12 @@ use crate::{error::PrettyPrintError, issue::IssueSeverity}; pub enum ModuleResolveResultItem { Module(Vc>), OutputAsset(Vc>), - External(RcStr, ExternalType), + External { + /// uri, path, reference, etc. + name: RcStr, + typ: ExternalType, + source: Option>>, + }, Ignore, Error(Vc), Empty, @@ -413,7 +418,12 @@ impl Display for ExternalType { #[derive(Clone, Debug)] pub enum ResolveResultItem { Source(Vc>), - External(RcStr, ExternalType), + External { + /// uri, path, reference, etc. + name: RcStr, + typ: ExternalType, + source: Option>>, + }, Ignore, Error(Vc), Empty, @@ -489,10 +499,23 @@ impl ValueToString for ResolveResult { ResolveResultItem::Source(a) => { result.push_str(&a.ident().to_string().await?); } - ResolveResultItem::External(s, ty) => { + ResolveResultItem::External { + name: s, + typ: ty, + source: opt_source, + } => { result.push_str("external "); result.push_str(s); - write!(result, " ({})", ty)?; + write!( + result, + " ({}) {}", + ty, + if opt_source.is_some() { + "with source" + } else { + "" + } + )?; } ResolveResultItem::Ignore => { result.push_str("ignore"); @@ -684,9 +707,13 @@ impl ResolveResult { request, match item { ResolveResultItem::Source(source) => asset_fn(source).await?, - ResolveResultItem::External(s, ty) => { - ModuleResolveResultItem::External(s, ty) - } + ResolveResultItem::External { + name: s, typ: ty, .. + } => ModuleResolveResultItem::External { + name: s, + typ: ty, + source: None, + }, ResolveResultItem::Ignore => ModuleResolveResultItem::Ignore, ResolveResultItem::Empty => ModuleResolveResultItem::Empty, ResolveResultItem::Error(e) => ModuleResolveResultItem::Error(e), @@ -1848,7 +1875,11 @@ async fn resolve_internal_inline( let uri: RcStr = format!("{}{}", protocol, remainder).into(); ResolveResult::primary_with_key( RequestKey::new(uri.clone()), - ResolveResultItem::External(uri, ExternalType::Url), + ResolveResultItem::External { + name: uri, + typ: ExternalType::Url, + source: None, + }, ) .into() } diff --git a/turbopack/crates/turbopack-core/src/resolve/options.rs b/turbopack/crates/turbopack-core/src/resolve/options.rs index eac1ac620d64c..097bf094330e3 100644 --- a/turbopack/crates/turbopack-core/src/resolve/options.rs +++ b/turbopack/crates/turbopack-core/src/resolve/options.rs @@ -324,12 +324,20 @@ 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 { - ResolveResultItem::External(name.clone(), *ty) - } else if let Some(request) = request.await?.request() { - ResolveResultItem::External(request, *ty) - } else { - bail!("Cannot resolve external reference without request") + ResolveResult::primary({ + let name = if let Some(name) = name { + name.clone() + } else if let Some(request) = request.await?.request() { + request + } else { + bail!("Cannot resolve external reference without request") + }; + + ResolveResultItem::External { + name, + typ: *ty, + source: None, // TODO(arlyon): source + } }) .cell(), ), diff --git a/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs b/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs index 456c45b48577e..ea2e143fc20cd 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -75,7 +75,11 @@ impl ReferencedAsset { // TODO handle multiple keyed results for (_key, result) in resolve_result.await?.primary.iter() { match result { - ModuleResolveResultItem::External(request, ty) => { + ModuleResolveResultItem::External { + name: request, + typ: ty, + source: _, // TODO(arlyon): handle source + } => { return Ok(ReferencedAsset::External(request.clone(), *ty).cell()); } &ModuleResolveResultItem::Module(module) => { diff --git a/turbopack/crates/turbopack-ecmascript/src/references/pattern_mapping.rs b/turbopack/crates/turbopack-ecmascript/src/references/pattern_mapping.rs index 9cd4fd372ab42..8ede34eda794f 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/pattern_mapping.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/pattern_mapping.rs @@ -311,7 +311,11 @@ async fn to_single_pattern_mapping( ) -> Result { let module = match resolve_item { ModuleResolveResultItem::Module(module) => *module, - ModuleResolveResultItem::External(s, ty) => { + ModuleResolveResultItem::External { + name: s, + typ: ty, + source: _, // TODO(arlyon): handle source + } => { return Ok(SinglePatternMapping::External(s.clone(), *ty)); } ModuleResolveResultItem::Ignore => return Ok(SinglePatternMapping::Ignored), diff --git a/turbopack/crates/turbopack/src/lib.rs b/turbopack/crates/turbopack/src/lib.rs index edab1193284ce..d82de0c267c03 100644 --- a/turbopack/crates/turbopack/src/lib.rs +++ b/turbopack/crates/turbopack/src/lib.rs @@ -906,7 +906,12 @@ pub async fn replace_externals( import_externals: bool, ) -> Result { for item in result.primary.values_mut() { - let ModuleResolveResultItem::External(request, ty) = item else { + let ModuleResolveResultItem::External { + name: request, + typ: ty, + source: None, + } = item + else { continue; };