Skip to content

Commit

Permalink
ImportMapping::PrimaryAlternativeExternal
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Nov 18, 2024
1 parent e8eac67 commit 97e3e87
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 94 deletions.
1 change: 0 additions & 1 deletion crates/next-core/src/next_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub async fn get_external_next_compiled_package_mapping(
Some(format!("next/dist/compiled/{}", &*package_name.await?).into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
None,
)
.resolved_cell()])
.cell())
Expand Down
16 changes: 6 additions & 10 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ pub async fn get_next_build_import_map() -> Result<Vc<ImportMap>> {
next_js_fs().root().to_resolved().await?,
);

let external =
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Traced, None)
.resolved_cell();
let external = ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Traced)
.resolved_cell();

import_map.insert_exact_alias("next", external);
import_map.insert_wildcard_alias("next/", external);
Expand All @@ -262,7 +261,6 @@ pub async fn get_next_build_import_map() -> Result<Vc<ImportMap>> {
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
None,
)
.resolved_cell(),
);
Expand Down Expand Up @@ -334,7 +332,6 @@ pub async fn get_next_server_import_map(
ExternalType::CommonJs,
// TODO(arlyon): wiring up in a follow up PR
ExternalTraced::Untraced,
None,
)
.resolved_cell();

Expand All @@ -355,7 +352,6 @@ pub async fn get_next_server_import_map(
Some("styled-jsx/style.js".into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
None,
)
.resolved_cell(),
);
Expand Down Expand Up @@ -1143,11 +1139,11 @@ fn external_request_to_cjs_import_mapping(
context_dir: ResolvedVc<FileSystemPath>,
request: &str,
) -> ResolvedVc<ImportMapping> {
ImportMapping::External(
ImportMapping::PrimaryAlternativeExternal(
Some(request.into()),
ExternalType::CommonJs,
ExternalTraced::Traced,
Some(context_dir),
context_dir,
)
.resolved_cell()
}
Expand All @@ -1158,11 +1154,11 @@ fn external_request_to_esm_import_mapping(
context_dir: ResolvedVc<FileSystemPath>,
request: &str,
) -> ResolvedVc<ImportMapping> {
ImportMapping::External(
ImportMapping::PrimaryAlternativeExternal(
Some(request.into()),
ExternalType::EcmaScriptModule,
ExternalTraced::Traced,
Some(context_dir),
context_dir,
)
.resolved_cell()
}
1 change: 0 additions & 1 deletion crates/next-core/src/next_shared/webpack_rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ async fn loader_runner_package_mapping() -> Result<Vc<ImportMapping>> {
Some("next/dist/compiled/loader-runner".into()),
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.resolved_cell()])
.cell())
Expand Down
75 changes: 38 additions & 37 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2622,46 +2622,47 @@ async fn resolve_import_map_result(
))
}
}
ImportMapResult::External(name, ty, traced, primary_alt) => {
let result = Some(
ResolveResult::primary(ResolveResultItem::External {
name: name.clone(),
ty: *ty,
traced: *traced,
})
.cell(),
);

if let Some(context_dir) = primary_alt {
let request = Request::parse_string(name.clone());

// We must avoid cycles during resolving
if request.resolve().await? == *original_request
&& context_dir.to_resolved().await? == original_lookup_path
{
None
} else {
let resolve_internal = resolve_internal(
**context_dir,
request,
match ty {
ExternalType::Url => options,
// TODO is that root correct?
ExternalType::CommonJs => node_cjs_resolve_options(context_dir.root()),
ExternalType::EcmaScriptModule => {
node_esm_resolve_options(context_dir.root())
}
},
);
ImportMapResult::External(name, ty, traced) => Some(
ResolveResult::primary(ResolveResultItem::External {
name: name.clone(),
ty: *ty,
traced: *traced,
})
.cell(),
),
ImportMapResult::AliasExternal(name, ty, traced, alias_lookup_path) => {
let request = Request::parse_string(name.clone());

if *resolve_internal.is_unresolvable().await? {
None
} else {
result
// We must avoid cycles during resolving
if request.resolve().await? == *original_request
&& alias_lookup_path.to_resolved().await? == original_lookup_path
{
None
} else if !(resolve_internal(
**alias_lookup_path,
request,
match ty {
ExternalType::Url => options,
// TODO is that root correct?
ExternalType::CommonJs => node_cjs_resolve_options(alias_lookup_path.root()),
ExternalType::EcmaScriptModule => {
node_esm_resolve_options(alias_lookup_path.root())
}
}
},
)
.await?
.is_unresolvable_ref())
{
Some(
ResolveResult::primary(ResolveResultItem::External {
name: name.clone(),
ty: *ty,
traced: *traced,
})
.cell(),
)
} else {
result
None
}
}
ImportMapResult::Alternatives(list) => {
Expand Down
84 changes: 62 additions & 22 deletions turbopack/crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,16 @@ pub enum ResolveInPackage {
#[turbo_tasks::value(shared)]
#[derive(Clone)]
pub enum ImportMapping {
// If specified, the optional name overrides the request, importing that external instead
// If the last option is a path, this behaves like PrimaryAlternative, only making it external
// if the request is resolvable from the directory.
External(
/// If specified, the optional name overrides the request, importing that external instead.
External(Option<RcStr>, ExternalType, ExternalTraced),
/// Only make the request external if the request is resolvable from the specified directory.
///
/// If specified, the optional name overrides the request, importing that external instead.
PrimaryAlternativeExternal(
Option<RcStr>,
ExternalType,
ExternalTraced,
Option<ResolvedVc<FileSystemPath>>,
ResolvedVc<FileSystemPath>,
),
/// An already resolved result that will be returned directly.
Direct(ResolvedVc<ResolveResult>),
Expand All @@ -120,11 +122,12 @@ pub enum ImportMapping {
#[turbo_tasks::value(shared)]
#[derive(Clone)]
pub enum ReplacedImportMapping {
External(
External(Option<RcStr>, ExternalType, ExternalTraced),
PrimaryAlternativeExternal(
Option<RcStr>,
ExternalType,
ExternalTraced,
Option<ResolvedVc<FileSystemPath>>,
ResolvedVc<FileSystemPath>,
),
Direct(ResolvedVc<ResolveResult>),
PrimaryAlternative(Pattern, Option<ResolvedVc<FileSystemPath>>),
Expand Down Expand Up @@ -173,11 +176,19 @@ impl AliasTemplate for Vc<ImportMapping> {
Box::pin(async move {
let this = &*self.await?;
Ok(match this {
ImportMapping::External(name, ty, traced, primary_alt) => {
ReplacedImportMapping::External(name.clone(), *ty, *traced, *primary_alt)
ImportMapping::External(name, ty, traced) => {
ReplacedImportMapping::External(name.clone(), *ty, *traced)
}
ImportMapping::PrimaryAlternativeExternal(name, ty, traced, lookup_dir) => {
ReplacedImportMapping::PrimaryAlternativeExternal(
name.clone(),
*ty,
*traced,
*lookup_dir,
)
}
ImportMapping::PrimaryAlternative(name, context) => {
ReplacedImportMapping::PrimaryAlternative((*name).clone().into(), *context)
ImportMapping::PrimaryAlternative(name, lookup_dir) => {
ReplacedImportMapping::PrimaryAlternative((*name).clone().into(), *lookup_dir)
}
ImportMapping::Direct(v) => ReplacedImportMapping::Direct(*v),
ImportMapping::Ignore => ReplacedImportMapping::Ignore,
Expand All @@ -202,22 +213,38 @@ impl AliasTemplate for Vc<ImportMapping> {
Box::pin(async move {
let this = &*self.await?;
Ok(match this {
ImportMapping::External(name, ty, traced, primary_alt) => {
ImportMapping::External(name, ty, traced) => {
if let Some(name) = name {
ReplacedImportMapping::External(
capture.spread_into_star(name).as_string().map(|s| s.into()),
*ty,
*traced,
*primary_alt,
)
} else {
ReplacedImportMapping::External(None, *ty, *traced, *primary_alt)
ReplacedImportMapping::External(None, *ty, *traced)
}
}
ImportMapping::PrimaryAlternative(name, context) => {
ImportMapping::PrimaryAlternativeExternal(name, ty, traced, lookup_dir) => {
if let Some(name) = name {
ReplacedImportMapping::PrimaryAlternativeExternal(
capture.spread_into_star(name).as_string().map(|s| s.into()),
*ty,
*traced,
*lookup_dir,
)
} else {
ReplacedImportMapping::PrimaryAlternativeExternal(
None,
*ty,
*traced,
*lookup_dir,
)
}
}
ImportMapping::PrimaryAlternative(name, lookup_dir) => {
ReplacedImportMapping::PrimaryAlternative(
capture.spread_into_star(name),
*context,
*lookup_dir,
)
}
ImportMapping::Direct(v) => ReplacedImportMapping::Direct(*v),
Expand Down Expand Up @@ -342,11 +369,12 @@ pub struct ResolvedMap {
#[derive(Clone)]
pub enum ImportMapResult {
Result(ResolvedVc<ResolveResult>),
External(
External(RcStr, ExternalType, ExternalTraced),
AliasExternal(
RcStr,
ExternalType,
ExternalTraced,
Option<ResolvedVc<FileSystemPath>>,
ResolvedVc<FileSystemPath>,
),
Alias(ResolvedVc<Request>, Option<ResolvedVc<FileSystemPath>>),
Alternatives(Vec<ImportMapResult>),
Expand All @@ -360,8 +388,19 @@ async fn import_mapping_to_result(
) -> Result<ImportMapResult> {
Ok(match &*mapping.await? {
ReplacedImportMapping::Direct(result) => ImportMapResult::Result(*result),
ReplacedImportMapping::External(name, ty, traced, primary_alt) => {
ImportMapResult::External(
ReplacedImportMapping::External(name, ty, traced) => ImportMapResult::External(
if let Some(name) = name {
name.clone()
} else if let Some(request) = request.await?.request() {
request
} else {
bail!("Cannot resolve external reference without request")
},
*ty,
*traced,
),
ReplacedImportMapping::PrimaryAlternativeExternal(name, ty, traced, lookup_dir) => {
ImportMapResult::AliasExternal(
if let Some(name) = name {
name.clone()
} else if let Some(request) = request.await?.request() {
Expand All @@ -371,7 +410,7 @@ async fn import_mapping_to_result(
},
*ty,
*traced,
*primary_alt,
*lookup_dir,
)
}
ReplacedImportMapping::Ignore => ImportMapResult::Result(
Expand Down Expand Up @@ -405,7 +444,8 @@ impl ValueToString for ImportMapResult {
async fn to_string(&self) -> Result<Vc<RcStr>> {
match self {
ImportMapResult::Result(_) => Ok(Vc::cell("Resolved by import map".into())),
ImportMapResult::External(_, _, _, _) => Ok(Vc::cell("TODO external".into())),
ImportMapResult::External(_, _, _) => Ok(Vc::cell("TODO external".into())),
ImportMapResult::AliasExternal(_, _, _, _) => Ok(Vc::cell("TODO external".into())),
ImportMapResult::Alias(request, context) => {
let s = if let Some(path) = context {
let path = path.to_string().await?;
Expand Down
28 changes: 6 additions & 22 deletions turbopack/crates/turbopack-resolve/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,13 @@ async fn base_resolve_options(
for req in NODE_EXTERNALS {
direct_mappings.insert(
AliasPattern::exact(req),
ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.resolved_cell(),
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Untraced)
.resolved_cell(),
);
direct_mappings.insert(
AliasPattern::exact(format!("node:{req}")),
ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.resolved_cell(),
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Untraced)
.resolved_cell(),
);
}
}
Expand All @@ -134,19 +124,13 @@ async fn base_resolve_options(
Some(format!("node:{req}").into()),
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.resolved_cell(),
);
direct_mappings.insert(
AliasPattern::exact(format!("node:{req}")),
ImportMapping::External(
None,
ExternalType::CommonJs,
ExternalTraced::Untraced,
None,
)
.resolved_cell(),
ImportMapping::External(None, ExternalType::CommonJs, ExternalTraced::Untraced)
.resolved_cell(),
);
}
}
Expand Down
1 change: 0 additions & 1 deletion turbopack/crates/turbopack-tests/tests/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ async fn run_test(prepared_test: Vc<PreparedTest>) -> Result<Vc<RunTestResult>>
Some("*".into()),
ExternalType::EcmaScriptModule,
ExternalTraced::Untraced,
None,
)
.resolved_cell(),
);
Expand Down

0 comments on commit 97e3e87

Please sign in to comment.