From 5807e930e181242e47c172c1411b4093aa67f46b 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 | 27 ++++++----- crates/next-core/src/next_shared/resolve.rs | 9 ++-- .../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, 89 insertions(+), 33 deletions(-) diff --git a/crates/next-core/src/next_server/resolve.rs b/crates/next-core/src/next_server/resolve.rs index ad0de304dd1e4..bf5d00a02ccdf 100644 --- a/crates/next-core/src/next_server/resolve.rs +++ b/crates/next-core/src/next_server/resolve.rs @@ -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 21c650675587c..a0b89a1e526a8 100644 --- a/crates/next-core/src/next_shared/resolve.rs +++ b/crates/next-core/src/next_shared/resolve.rs @@ -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 19d36b4153b6d..16f7cc4327f81 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, @@ -407,7 +412,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, @@ -485,10 +495,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"); @@ -680,9 +703,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), @@ -1839,7 +1866,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 844168168ced0..120a9e5545edf 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 0391d400cb892..2b893855b7f4c 100644 --- a/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs +++ b/turbopack/crates/turbopack-ecmascript/src/references/esm/base.rs @@ -74,7 +74,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 c0ae2ec8c7bd9..c3010ed4e0d1c 100644 --- a/turbopack/crates/turbopack/src/lib.rs +++ b/turbopack/crates/turbopack/src/lib.rs @@ -910,7 +910,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; };