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 committed Sep 26, 2024
1 parent 178f8a0 commit be928f7
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 35 deletions.
29 changes: 16 additions & 13 deletions crates/next-core/src/next_server/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / build / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / build-native / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu - node@16

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / test unit (20) / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / test unit (18) / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / test cargo benches / Test

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 14 in crates/next-core/src/next_server/resolve.rs

View workflow job for this annotation

GitHub Actions / test next-swc wasm / build

unresolved import `turbopack_core::resolve::External`
ResolveResultOption,
},
source::Source,
Expand Down 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
11 changes: 6 additions & 5 deletions crates/next-core/src/next_shared/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use turbopack_core::{
AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin,
BeforeResolvePluginCondition,
},
ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,
External, ExternalType, ResolveResult, ResolveResultItem, ResolveResultOption,

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / build / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / build-native / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / stable - x86_64-unknown-linux-gnu - node@16

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / test unit (20) / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / test unit (18) / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / rust check / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / test cargo benches / Test

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

unresolved import `turbopack_core::resolve::External`

Check failure on line 18 in crates/next-core/src/next_shared/resolve.rs

View workflow job for this annotation

GitHub Actions / test next-swc wasm / build

unresolved import `turbopack_core::resolve::External`
},
};

Expand Down 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 @@ -413,7 +418,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 @@ -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");
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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()
}
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 @@ -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) => {
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 @@ -906,7 +906,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 be928f7

Please sign in to comment.