Skip to content

Commit

Permalink
refactor external ResolveResult to include source
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon authored and mischnic committed Oct 11, 2024
1 parent 65d9706 commit 5807e93
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 33 deletions.
27 changes: 15 additions & 12 deletions crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
))
}
Expand Down Expand Up @@ -401,25 +402,27 @@ 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(),
))
}
}
(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(),
))
}
Expand Down
9 changes: 5 additions & 4 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)))
}
Expand Down
47 changes: 39 additions & 8 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ use crate::{error::PrettyPrintError, issue::IssueSeverity};
pub enum ModuleResolveResultItem {
Module(Vc<Box<dyn Module>>),
OutputAsset(Vc<Box<dyn OutputAsset>>),
External(RcStr, ExternalType),
External {
/// uri, path, reference, etc.
name: RcStr,
typ: ExternalType,
source: Option<Vc<Box<dyn OutputAsset>>>,
},
Ignore,
Error(Vc<RcStr>),
Empty,
Expand Down Expand Up @@ -407,7 +412,12 @@ impl Display for ExternalType {
#[derive(Clone, Debug)]
pub enum ResolveResultItem {
Source(Vc<Box<dyn Source>>),
External(RcStr, ExternalType),
External {
/// uri, path, reference, etc.
name: RcStr,
typ: ExternalType,
source: Option<Vc<Box<dyn Source>>>,
},
Ignore,
Error(Vc<RcStr>),
Empty,
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}
Expand Down
20 changes: 14 additions & 6 deletions turbopack/crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,11 @@ async fn to_single_pattern_mapping(
) -> Result<SinglePatternMapping> {
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),
Expand Down
7 changes: 6 additions & 1 deletion turbopack/crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,12 @@ pub async fn replace_externals(
import_externals: bool,
) -> Result<ModuleResolveResult> {
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;
};

Expand Down

0 comments on commit 5807e93

Please sign in to comment.