From eca0ca5b5469069948c347acc5beacc64b677fed Mon Sep 17 00:00:00 2001 From: Leah Date: Tue, 21 Nov 2023 16:10:48 +0100 Subject: [PATCH] Revert "Extend resolving to allow reference type for plugins and extensions for MainField" (#6527) Reverts vercel/turbo#6510 needs more work according to @sokra and blocks other PRs right now Closes PACK-2009 --- crates/turbopack-core/src/resolve/mod.rs | 27 ++------- crates/turbopack-core/src/resolve/node.rs | 18 ++---- crates/turbopack-core/src/resolve/options.rs | 13 +--- crates/turbopack-core/src/resolve/plugin.rs | 8 +-- crates/turbopack-dev/src/react_refresh.rs | 12 +--- .../src/references/mod.rs | 1 - .../turbopack-ecmascript/src/resolve/mod.rs | 17 +----- .../src/typescript/resolve.rs | 59 +++---------------- .../turbopack-ecmascript/src/webpack/mod.rs | 1 - crates/turbopack/src/lib.rs | 7 +-- crates/turbopack/src/resolve.rs | 56 ++++++++---------- crates/turbopack/src/unsupported_sass.rs | 4 +- 12 files changed, 54 insertions(+), 169 deletions(-) diff --git a/crates/turbopack-core/src/resolve/mod.rs b/crates/turbopack-core/src/resolve/mod.rs index fac3aed34c00b..edf4f2472074a 100644 --- a/crates/turbopack-core/src/resolve/mod.rs +++ b/crates/turbopack-core/src/resolve/mod.rs @@ -1008,7 +1008,6 @@ pub async fn resolve_raw( #[turbo_tasks::function] pub async fn resolve( lookup_path: Vc, - reference_type: Value, request: Vc, options: Vc, ) -> Result> { @@ -1016,14 +1015,12 @@ pub async fn resolve( .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, - reference_type: Value, request: Vc, options: Vc, result: Vc, @@ -1031,17 +1028,13 @@ async fn handle_resolve_plugins( async fn apply_plugins_to_path( path: Vc, lookup_path: Vc, - reference_type: Value, request: Vc, options: Vc, ) -> Result>> { 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)); } } @@ -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; @@ -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?; @@ -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)); diff --git a/crates/turbopack-core/src/resolve/node.rs b/crates/turbopack-core/src/resolve/node.rs index f41a842a419dc..f456767dfc80f 100644 --- a/crates/turbopack-core/src/resolve/node.rs +++ b/crates/turbopack-core/src/resolve/node.rs @@ -13,9 +13,8 @@ pub fn node_cjs_resolve_options(root: Vc) -> Vc ("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()], @@ -25,10 +24,7 @@ pub fn node_cjs_resolve_options(root: Vc) -> Vc 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 { @@ -47,7 +43,6 @@ pub fn node_esm_resolve_options(root: Vc) -> Vc ("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( @@ -59,11 +54,10 @@ pub fn node_esm_resolve_options(root: Vc) -> Vc 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, diff --git a/crates/turbopack-core/src/resolve/options.rs b/crates/turbopack-core/src/resolve/options.rs index 0447ba84cc6f9..1185a1bd531fa 100644 --- a/crates/turbopack-core/src/resolve/options.rs +++ b/crates/turbopack-core/src/resolve/options.rs @@ -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>, - }, + MainField(String), /// Default behavior of using the index.js file at the root of the package. Default(String), } @@ -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, extensions: Vec) -> Result> { - let mut resolve_options = self.await?.clone_value(); - resolve_options.extensions = extensions; - Ok(resolve_options.into()) - } } #[turbo_tasks::value(shared)] diff --git a/crates/turbopack-core/src/resolve/plugin.rs b/crates/turbopack-core/src/resolve/plugin.rs index 3cbca4592ed30..eaea46053db91 100644 --- a/crates/turbopack-core/src/resolve/plugin.rs +++ b/crates/turbopack-core/src/resolve/plugin.rs @@ -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] @@ -51,7 +48,6 @@ pub trait ResolvePlugin { self: Vc, fs_path: Vc, lookup_path: Vc, - reference_type: Value, request: Vc, ) -> Vc; } diff --git a/crates/turbopack-dev/src/react_refresh.rs b/crates/turbopack-dev/src/react_refresh.rs index ef3c97c35044d..da235accd5200 100644 --- a/crates/turbopack-dev/src/react_refresh.rs +++ b/crates/turbopack-dev/src/react_refresh.rs @@ -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; @@ -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()); diff --git a/crates/turbopack-ecmascript/src/references/mod.rs b/crates/turbopack-ecmascript/src/references/mod.rs index 56b91c74a096e..845bbe4d0803a 100644 --- a/crates/turbopack-ecmascript/src/references/mod.rs +++ b/crates/turbopack-ecmascript/src/references/mod.rs @@ -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, ); diff --git a/crates/turbopack-ecmascript/src/resolve/mod.rs b/crates/turbopack-ecmascript/src/resolve/mod.rs index f3910377cd0b5..9433d2a28446b 100644 --- a/crates/turbopack-ecmascript/src/resolve/mod.rs +++ b/crates/turbopack-ecmascript/src/resolve/mod.rs @@ -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 }; diff --git a/crates/turbopack-ecmascript/src/typescript/resolve.rs b/crates/turbopack-ecmascript/src/typescript/resolve.rs index 5b41f8b346e5a..de645580aa50e 100644 --- a/crates/turbopack-ecmascript/src/typescript/resolve.rs +++ b/crates/turbopack-ecmascript/src/typescript/resolve.rs @@ -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) } @@ -158,30 +157,14 @@ async fn resolve_extends_rooted_or_relative( resolve_options: Vc, path: &str, ) -> Result> { - 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) } @@ -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() @@ -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())); diff --git a/crates/turbopack-ecmascript/src/webpack/mod.rs b/crates/turbopack-ecmascript/src/webpack/mod.rs index 4ba6ecbd359f6..eb261540fe31d 100644 --- a/crates/turbopack-ecmascript/src/webpack/mod.rs +++ b/crates/turbopack-ecmascript/src/webpack/mod.rs @@ -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, ); diff --git a/crates/turbopack/src/lib.rs b/crates/turbopack/src/lib.rs index 68452f854d9f3..2f484e5c8bea7 100644 --- a/crates/turbopack/src/lib.rs +++ b/crates/turbopack/src/lib.rs @@ -459,12 +459,7 @@ impl AssetContext for ModuleAssetContext { ) -> Result> { 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? { diff --git a/crates/turbopack/src/resolve.rs b/crates/turbopack/src/resolve.rs index f59e06fc30405..b8e9883f0a149 100644 --- a/crates/turbopack/src/resolve.rs +++ b/crates/turbopack/src/resolve.rs @@ -157,28 +157,27 @@ async fn base_resolve_options( conditions }; - let extensions = if let Some(environment) = emulating { - environment.resolve_extensions().await?.clone_value() - } else { - let mut ext = Vec::new(); - if opt.enable_typescript && opt.enable_react { - ext.push(".tsx".to_string()); - } - if opt.enable_typescript { - ext.push(".ts".to_string()); - } - if opt.enable_react { - ext.push(".jsx".to_string()); - } - ext.push(".js".to_string()); - if opt.enable_node_native_modules { - ext.push(".node".to_string()); - } - ext.push(".json".to_string()); - ext - }; Ok(ResolveOptions { - extensions, + extensions: if let Some(environment) = emulating { + environment.resolve_extensions().await?.clone_value() + } else { + let mut ext = Vec::new(); + if opt.enable_typescript && opt.enable_react { + ext.push(".tsx".to_string()); + } + if opt.enable_typescript { + ext.push(".ts".to_string()); + } + if opt.enable_react { + ext.push(".jsx".to_string()); + } + ext.push(".js".to_string()); + if opt.enable_node_native_modules { + ext.push(".node".to_string()); + } + ext.push(".json".to_string()); + ext + }, modules: if let Some(environment) = emulating { if *environment.resolve_node_modules().await? { vec![ResolveModules::Nested( @@ -204,21 +203,12 @@ async fn base_resolve_options( unspecified_conditions: ConditionValue::Unset, }]; if opt.browser { - resolve_into.push(ResolveIntoPackage::MainField { - field: "browser".to_string(), - extensions: None, - }); + resolve_into.push(ResolveIntoPackage::MainField("browser".to_string())); } if opt.module { - resolve_into.push(ResolveIntoPackage::MainField { - field: "module".to_string(), - extensions: None, - }); + resolve_into.push(ResolveIntoPackage::MainField("module".to_string())); } - resolve_into.push(ResolveIntoPackage::MainField { - field: "main".to_string(), - extensions: None, - }); + resolve_into.push(ResolveIntoPackage::MainField("main".to_string())); resolve_into.push(ResolveIntoPackage::Default("index".to_string())); resolve_into }, diff --git a/crates/turbopack/src/unsupported_sass.rs b/crates/turbopack/src/unsupported_sass.rs index 14e2d6952e942..4a9183202d7ab 100644 --- a/crates/turbopack/src/unsupported_sass.rs +++ b/crates/turbopack/src/unsupported_sass.rs @@ -1,11 +1,10 @@ //! TODO(WEB-741) Remove this file once Sass is supported. use anyhow::Result; -use turbo_tasks::{Value, Vc}; +use turbo_tasks::Vc; use turbo_tasks_fs::{glob::Glob, FileSystemPath}; use turbopack_core::{ issue::{Issue, IssueExt, IssueSeverity, StyledString}, - reference_type::ReferenceType, resolve::{ parse::Request, plugin::{ResolvePlugin, ResolvePluginCondition}, @@ -39,7 +38,6 @@ impl ResolvePlugin for UnsupportedSassResolvePlugin { &self, fs_path: Vc, lookup_path: Vc, - _reference_type: Value, request: Vc, ) -> Result> { let extension = fs_path.extension().await?;