Unverified Commit f1908dad authored by xusd320's avatar xusd320 Committed by GitHub

perf(turbopack): optimize resolve plugin handling (#88639)

parent 06421057
......@@ -45,7 +45,7 @@ use crate::{
origin::ResolveOrigin,
parse::{Request, stringify_data_uri},
pattern::{Pattern, PatternMatch, read_matches},
plugin::{AfterResolvePlugin, BeforeResolvePlugin},
plugin::{AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin},
remap::{ExportsField, ImportsField, ReplacedSubpathValueResult},
},
source::{OptionSource, Source, Sources},
......@@ -67,6 +67,12 @@ pub use remap::{ResolveAliasMap, SubpathValue};
use crate::{error::PrettyPrintError, issue::IssueSeverity};
/// Type alias for a resolved after-resolve plugin paired with its condition.
type AfterResolvePluginWithCondition = (
ResolvedVc<Box<dyn AfterResolvePlugin>>,
Vc<AfterResolvePluginCondition>,
);
#[turbo_tasks::value(shared)]
#[derive(Clone, Debug)]
pub enum ModuleResolveResultItem {
......@@ -1180,7 +1186,8 @@ enum ImportsFieldResult {
#[turbo_tasks::function]
async fn imports_field(lookup_path: FileSystemPath) -> Result<Vc<ImportsFieldResult>> {
// We don't need to collect affecting sources here because we don't use them
let package_json_context = find_context_file(lookup_path, package_json(), false).await?;
let package_json_context =
find_context_file(lookup_path, package_json().resolve().await?, false).await?;
let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context else {
return Ok(ImportsFieldResult::None.cell());
};
......@@ -1598,13 +1605,24 @@ pub async fn resolve_inline(
}
async {
let before_plugins_result = handle_before_resolve_plugins(
lookup_path.clone(),
reference_type.clone(),
request,
options,
)
.await?;
// Pre-fetch options once to avoid repeated await calls
let options_value = options.await?;
// Fast path: skip plugin handling if no plugins are configured
let has_before_plugins = !options_value.before_resolve_plugins.is_empty();
let has_after_plugins = !options_value.after_resolve_plugins.is_empty();
let before_plugins_result = if has_before_plugins {
handle_before_resolve_plugins(
lookup_path.clone(),
reference_type.clone(),
request,
options,
)
.await?
} else {
None
};
let raw_result = match before_plugins_result {
Some(result) => result,
......@@ -1615,9 +1633,13 @@ pub async fn resolve_inline(
}
};
let result =
let result = if has_after_plugins {
handle_after_resolve_plugins(lookup_path, reference_type, request, options, raw_result)
.await?;
.await?
} else {
raw_result
};
Ok(result)
}
.instrument(span)
......@@ -1685,7 +1707,9 @@ async fn handle_before_resolve_plugins(
request: Vc<Request>,
options: Vc<ResolveOptions>,
) -> Result<Option<Vc<ResolveResult>>> {
for plugin in &options.await?.before_resolve_plugins {
let options_value = options.await?;
for plugin in &options_value.before_resolve_plugins {
let condition = plugin.before_resolve_condition().resolve().await?;
if !*condition.matches(request).await? {
continue;
......@@ -1709,15 +1733,25 @@ async fn handle_after_resolve_plugins(
options: Vc<ResolveOptions>,
result: Vc<ResolveResult>,
) -> Result<Vc<ResolveResult>> {
// Pre-fetch options to avoid repeated await calls in the inner loop
let options_value = options.await?;
// Pre-resolve all plugin conditions once to avoid repeated resolve calls in the loop
let resolved_conditions = options_value
.after_resolve_plugins
.iter()
.map(async |p| Ok((*p, p.after_resolve_condition().resolve().await?)))
.try_join()
.await?;
async fn apply_plugins_to_path(
path: FileSystemPath,
lookup_path: FileSystemPath,
reference_type: ReferenceType,
request: Vc<Request>,
options: Vc<ResolveOptions>,
plugins_with_conditions: &[AfterResolvePluginWithCondition],
) -> Result<Option<Vc<ResolveResult>>> {
for plugin in &options.await?.after_resolve_plugins {
let after_resolve_condition = plugin.after_resolve_condition().resolve().await?;
for (plugin, after_resolve_condition) in plugins_with_conditions {
if *after_resolve_condition.matches(path.clone()).await?
&& let Some(result) = *plugin
.after_resolve(
......@@ -1748,7 +1782,7 @@ async fn handle_after_resolve_plugins(
lookup_path.clone(),
reference_type.clone(),
request,
options,
&resolved_conditions,
)
.await?
{
......@@ -2646,7 +2680,8 @@ enum FindSelfReferencePackageResult {
async fn find_self_reference(
lookup_path: FileSystemPath,
) -> Result<Vc<FindSelfReferencePackageResult>> {
let package_json_context = find_context_file(lookup_path, package_json(), false).await?;
let package_json_context =
find_context_file(lookup_path, package_json().resolve().await?, false).await?;
if let FindContextFileResult::Found(package_json_path, _refs) = &*package_json_context {
let read =
read_package_json(Vc::upcast(FileSource::new(package_json_path.clone()))).await?;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment