Skip to content

Commit

Permalink
Revert "Extend resolving to allow reference type for plugins and exte…
Browse files Browse the repository at this point in the history
…nsions for MainField" (#6527)

Reverts #6510

needs more work according to @sokra and blocks other PRs right now

Closes PACK-2009
  • Loading branch information
ForsakenHarmony authored Nov 21, 2023
1 parent 0295873 commit eca0ca5
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 169 deletions.
27 changes: 5 additions & 22 deletions crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1008,40 +1008,33 @@ pub async fn resolve_raw(
#[turbo_tasks::function]
pub async fn resolve(
lookup_path: Vc<FileSystemPath>,
reference_type: Value<ReferenceType>,
request: Vc<Request>,
options: Vc<ResolveOptions>,
) -> Result<Vc<ResolveResult>> {
let raw_result = resolve_internal_inline(lookup_path, request, options)
.await?
.resolve()
.await?;
let result =
handle_resolve_plugins(lookup_path, reference_type, request, options, raw_result).await?;
let result = handle_resolve_plugins(lookup_path, request, options, raw_result).await?;
Ok(result)
}

async fn handle_resolve_plugins(
lookup_path: Vc<FileSystemPath>,
reference_type: Value<ReferenceType>,
request: Vc<Request>,
options: Vc<ResolveOptions>,
result: Vc<ResolveResult>,
) -> Result<Vc<ResolveResult>> {
async fn apply_plugins_to_path(
path: Vc<FileSystemPath>,
lookup_path: Vc<FileSystemPath>,
reference_type: Value<ReferenceType>,
request: Vc<Request>,
options: Vc<ResolveOptions>,
) -> Result<Option<Vc<ResolveResult>>> {
for plugin in &options.await?.plugins {
let after_resolve_condition = plugin.after_resolve_condition().resolve().await?;
if *after_resolve_condition.matches(path).await? {
if let Some(result) = *plugin
.after_resolve(path, lookup_path, reference_type.clone(), request)
.await?
{
if let Some(result) = *plugin.after_resolve(path, lookup_path, request).await? {
return Ok(Some(result));
}
}
Expand All @@ -1059,8 +1052,7 @@ async fn handle_resolve_plugins(
if let &ResolveResultItem::Source(source) = primary {
let path = source.ident().path().resolve().await?;
if let Some(new_result) =
apply_plugins_to_path(path, lookup_path, reference_type.clone(), request, options)
.await?
apply_plugins_to_path(path, lookup_path, request, options).await?
{
let new_result = new_result.await?;
changed = true;
Expand Down Expand Up @@ -1341,21 +1333,12 @@ async fn resolve_into_folder(
return resolve_internal_inline(package_path, request.resolve().await?, options)
.await;
}
ResolveIntoPackage::MainField {
field: name,
extensions,
} => {
ResolveIntoPackage::MainField(name) => {
if let Some(package_json) = &*read_package_json(package_json_path).await? {
if let Some(field_value) = package_json[name].as_str() {
let request =
Request::parse(Value::new(normalize_request(field_value).into()));

let options = if let Some(extensions) = extensions {
options.with_extensions(extensions.clone())
} else {
options
};

let result = &*resolve_internal_inline(package_path, request, options)
.await?
.await?;
Expand Down Expand Up @@ -1559,7 +1542,7 @@ async fn resolve_into_package(
if could_match_others {
for resolve_into_package in options_value.into_package.iter() {
match resolve_into_package {
ResolveIntoPackage::Default(_) | ResolveIntoPackage::MainField { .. } => {
ResolveIntoPackage::Default(_) | ResolveIntoPackage::MainField(_) => {
// doesn't affect packages with subpath
if path.is_match("/") {
results.push(resolve_into_folder(package_path, options, query));
Expand Down
18 changes: 6 additions & 12 deletions crates/turbopack-core/src/resolve/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ pub fn node_cjs_resolve_options(root: Vc<FileSystemPath>) -> Vc<ResolveOptions>
("require".to_string(), ConditionValue::Set),
]
.into();
let extensions = vec![".js".to_string(), ".json".to_string(), ".node".to_string()];
ResolveOptions {
extensions,
extensions: vec![".js".to_string(), ".json".to_string(), ".node".to_string()],
modules: vec![ResolveModules::Nested(
root,
vec!["node_modules".to_string()],
Expand All @@ -25,10 +24,7 @@ pub fn node_cjs_resolve_options(root: Vc<FileSystemPath>) -> Vc<ResolveOptions>
conditions: conditions.clone(),
unspecified_conditions: ConditionValue::Unset,
},
ResolveIntoPackage::MainField {
field: "main".to_string(),
extensions: None,
},
ResolveIntoPackage::MainField("main".to_string()),
ResolveIntoPackage::Default("index".to_string()),
],
in_package: vec![ResolveInPackage::ImportsField {
Expand All @@ -47,7 +43,6 @@ pub fn node_esm_resolve_options(root: Vc<FileSystemPath>) -> Vc<ResolveOptions>
("import".to_string(), ConditionValue::Set),
]
.into();
let extensions = vec![".js".to_string(), ".json".to_string(), ".node".to_string()];
ResolveOptions {
extensions: vec![],
modules: vec![ResolveModules::Nested(
Expand All @@ -59,11 +54,10 @@ pub fn node_esm_resolve_options(root: Vc<FileSystemPath>) -> Vc<ResolveOptions>
conditions: conditions.clone(),
unspecified_conditions: ConditionValue::Unset,
},
ResolveIntoPackage::MainField {
field: "main".to_string(),
extensions: Some(extensions),
},
ResolveIntoPackage::Default("index".to_string()),
ResolveIntoPackage::MainField("main".to_string()),
ResolveIntoPackage::Default("index.js".to_string()),
ResolveIntoPackage::Default("index.json".to_string()),
ResolveIntoPackage::Default("index.node".to_string()),
],
in_package: vec![ResolveInPackage::ImportsField {
conditions,
Expand Down
13 changes: 1 addition & 12 deletions crates/turbopack-core/src/resolve/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ pub enum ResolveIntoPackage {
/// [main]: https://nodejs.org/api/packages.html#main
/// [module]: https://esbuild.github.io/api/#main-fields
/// [browser]: https://esbuild.github.io/api/#main-fields
MainField {
field: String,
extensions: Option<Vec<String>>,
},
MainField(String),
/// Default behavior of using the index.js file at the root of the package.
Default(String),
}
Expand Down Expand Up @@ -458,14 +455,6 @@ impl ResolveOptions {
);
Ok(resolve_options.into())
}

/// Overrides the extensions used for resolving
#[turbo_tasks::function]
pub async fn with_extensions(self: Vc<Self>, extensions: Vec<String>) -> Result<Vc<Self>> {
let mut resolve_options = self.await?.clone_value();
resolve_options.extensions = extensions;
Ok(resolve_options.into())
}
}

#[turbo_tasks::value(shared)]
Expand Down
8 changes: 2 additions & 6 deletions crates/turbopack-core/src/resolve/plugin.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use anyhow::Result;
use turbo_tasks::{Value, Vc};
use turbo_tasks::Vc;
use turbo_tasks_fs::{glob::Glob, FileSystemPath};

use crate::{
reference_type::ReferenceType,
resolve::{parse::Request, ResolveResultOption},
};
use crate::resolve::{parse::Request, ResolveResultOption};

/// A condition which determines if the hooks of a resolve plugin gets called.
#[turbo_tasks::value]
Expand Down Expand Up @@ -51,7 +48,6 @@ pub trait ResolvePlugin {
self: Vc<Self>,
fs_path: Vc<FileSystemPath>,
lookup_path: Vc<FileSystemPath>,
reference_type: Value<ReferenceType>,
request: Vc<Request>,
) -> Vc<ResolveResultOption>;
}
12 changes: 3 additions & 9 deletions crates/turbopack-dev/src/react_refresh.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;
use turbo_tasks::{Value, Vc};
use turbo_tasks::Vc;
use turbo_tasks_fs::FileSystemPath;
use turbopack::resolve_options_context::ResolveOptionsContext;
use turbopack_core::{
issue::{Issue, IssueExt, IssueSeverity, StyledString},
reference_type::{CommonJsReferenceSubType, ReferenceType},
resolve::parse::Request,
};
use turbopack_ecmascript::resolve::apply_cjs_specific_options;
Expand Down Expand Up @@ -50,13 +49,8 @@ pub async fn assert_can_resolve_react_refresh(
let resolve_options =
apply_cjs_specific_options(turbopack::resolve_options(path, resolve_options_context));
for request in [react_refresh_request_in_next(), react_refresh_request()] {
let result = turbopack_core::resolve::resolve(
path,
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
request,
resolve_options,
)
.first_source();
let result =
turbopack_core::resolve::resolve(path, request, resolve_options).first_source();

if result.await?.is_some() {
return Ok(ResolveReactRefreshResult::Found(request).cell());
Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2585,7 +2585,6 @@ async fn resolve_as_webpack_runtime(

let resolved = resolve(
origin.origin_path().parent().resolve().await?,
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
request,
options,
);
Expand Down
17 changes: 3 additions & 14 deletions crates/turbopack-ecmascript/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,22 +100,11 @@ pub async fn url_resolve(
let ty = Value::new(ReferenceType::Url(ty.into_value()));
let resolve_options = origin.resolve_options(ty.clone());
let rel_request = request.as_relative();
let reference_type = Value::new(ReferenceType::Url(UrlReferenceSubType::EcmaScriptNewUrl));
let rel_result = resolve(
origin.origin_path().parent(),
reference_type.clone(),
rel_request,
resolve_options,
);
let rel_result = resolve(origin.origin_path().parent(), rel_request, resolve_options);
let result = if *rel_result.is_unresolveable().await? && rel_request.resolve().await? != request
{
resolve(
origin.origin_path().parent(),
reference_type,
request,
resolve_options,
)
.with_affecting_sources(rel_result.await?.get_affecting_sources().clone())
resolve(origin.origin_path().parent(), request, resolve_options)
.with_affecting_sources(rel_result.await?.get_affecting_sources().clone())
} else {
rel_result
};
Expand Down
59 changes: 9 additions & 50 deletions crates/turbopack-ecmascript/src/typescript/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,16 @@ async fn resolve_extends(
// An empty extends is treated as "./tsconfig"
Request::Empty => {
let request = Request::parse_string("./tsconfig".to_string());
Ok(resolve(parent_dir,
Value::new(ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)), request, resolve_options).first_source())
Ok(resolve(parent_dir, request, resolve_options).first_source())
}

// All other types are treated as module imports, and potentially joined with
// "tsconfig.json". This includes "relative" imports like '.' and '..'.
_ => {
let mut result = resolve(parent_dir, Value::new(ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)), request, resolve_options).first_source();
let mut result = resolve(parent_dir, request, resolve_options).first_source();
if result.await?.is_none() {
let request = Request::parse_string(format!("{extends}/tsconfig"));
result = resolve(parent_dir, Value::new(ReferenceType::TypeScript(TypeScriptReferenceSubType::Undefined)), request, resolve_options).first_source();
result = resolve(parent_dir, request, resolve_options).first_source();
}
Ok(result)
}
Expand All @@ -158,30 +157,14 @@ async fn resolve_extends_rooted_or_relative(
resolve_options: Vc<ResolveOptions>,
path: &str,
) -> Result<Vc<OptionSource>> {
let mut result = resolve(
lookup_path,
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
request,
resolve_options,
)
.first_source();
let mut result = resolve(lookup_path, request, resolve_options).first_source();

// If the file doesn't end with ".json" and we can't find the file, then we have
// to try again with it.
// https://github.com/microsoft/TypeScript/blob/611a912d/src/compiler/commandLineParser.ts#L3305
if !path.ends_with(".json") && result.await?.is_none() {
let request = Request::parse_string(format!("{path}.json"));
result = resolve(
lookup_path,
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
request,
resolve_options,
)
.first_source();
result = resolve(lookup_path, request, resolve_options).first_source();
}
Ok(result)
}
Expand Down Expand Up @@ -371,35 +354,14 @@ pub async fn type_resolve(
};
let context_path = context_path.resolve().await?;
let result = if let Some(types_request) = types_request {
let result1 = resolve(
context_path,
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
request,
options,
);
let result1 = resolve(context_path, request, options);
if !*result1.is_unresolveable().await? {
result1
} else {
resolve(
context_path,
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
types_request,
options,
)
resolve(context_path, types_request, options)
}
} else {
resolve(
context_path,
Value::new(ReferenceType::TypeScript(
TypeScriptReferenceSubType::Undefined,
)),
request,
options,
)
resolve(context_path, request, options)
};
let result = origin
.asset_context()
Expand Down Expand Up @@ -474,10 +436,7 @@ async fn apply_typescript_types_options(
.collect();
resolve_options
.into_package
.push(ResolveIntoPackage::MainField {
field: "types".to_string(),
extensions: Some(vec![".d.ts".to_string(), ".ts".to_string()]),
});
.push(ResolveIntoPackage::MainField("types".to_string()));
resolve_options
.into_package
.push(ResolveIntoPackage::Default("index".to_string()));
Expand Down
1 change: 0 additions & 1 deletion crates/turbopack-ecmascript/src/webpack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ impl ModuleReference for WebpackRuntimeAssetReference {

let resolved = resolve(
self.origin.origin_path().parent().resolve().await?,
Value::new(ReferenceType::CommonJs(CommonJsReferenceSubType::Undefined)),
self.request,
options,
);
Expand Down
7 changes: 1 addition & 6 deletions crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,7 @@ impl AssetContext for ModuleAssetContext {
) -> Result<Vc<ModuleResolveResult>> {
let context_path = origin_path.parent().resolve().await?;

let result = resolve(
context_path,
reference_type.clone(),
request,
resolve_options,
);
let result = resolve(context_path, request, resolve_options);
let mut result = self.process_resolve_result(result.resolve().await?, reference_type);

if *self.is_types_resolving_enabled().await? {
Expand Down
Loading

0 comments on commit eca0ca5

Please sign in to comment.