Skip to content

Commit b7062d0

Browse files
committed
feat(turbopack): optimize resolve plugin conditions with Option
1 parent f694d5b commit b7062d0

File tree

2 files changed

+62
-15
lines changed

2 files changed

+62
-15
lines changed

turbopack/crates/turbopack-core/src/resolve/mod.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,9 +1685,11 @@ async fn handle_before_resolve_plugins(
16851685
options: Vc<ResolveOptions>,
16861686
) -> Result<Option<Vc<ResolveResult>>> {
16871687
for plugin in &options.await?.before_resolve_plugins {
1688-
let condition = plugin.before_resolve_condition().resolve().await?;
1689-
if !*condition.matches(request).await? {
1690-
continue;
1688+
let condition_option = plugin.before_resolve_condition().await?;
1689+
if let Some(condition) = *condition_option {
1690+
if !*condition.matches(request).await? {
1691+
continue;
1692+
}
16911693
}
16921694

16931695
if let Some(result) = *plugin
@@ -1716,17 +1718,30 @@ async fn handle_after_resolve_plugins(
17161718
options: Vc<ResolveOptions>,
17171719
) -> Result<Option<Vc<ResolveResult>>> {
17181720
for plugin in &options.await?.after_resolve_plugins {
1719-
let after_resolve_condition = plugin.after_resolve_condition().resolve().await?;
1720-
if *after_resolve_condition.matches(path.clone()).await?
1721-
&& let Some(result) = *plugin
1722-
.after_resolve(
1723-
path.clone(),
1724-
lookup_path.clone(),
1725-
reference_type.clone(),
1726-
request,
1727-
)
1728-
.await?
1721+
let after_resolve_condition_option = plugin.after_resolve_condition().await?;
1722+
if let Some(condition) = *after_resolve_condition_option {
1723+
if *condition.matches(path.clone()).await?
1724+
&& let Some(result) = *plugin
1725+
.after_resolve(
1726+
path.clone(),
1727+
lookup_path.clone(),
1728+
reference_type.clone(),
1729+
request,
1730+
)
1731+
.await?
1732+
{
1733+
return Ok(Some(*result));
1734+
}
1735+
} else if let Some(result) = *plugin
1736+
.after_resolve(
1737+
path.clone(),
1738+
lookup_path.clone(),
1739+
reference_type.clone(),
1740+
request,
1741+
)
1742+
.await?
17291743
{
1744+
// If there is no condition, the plugin applies to all paths.
17301745
return Ok(Some(*result));
17311746
}
17321747
}

turbopack/crates/turbopack-core/src/resolve/plugin.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ impl AfterResolvePluginCondition {
4040
}
4141
}
4242

43+
#[turbo_tasks::value(transparent)]
44+
pub struct OptionAfterResolvePluginCondition(Option<ResolvedVc<AfterResolvePluginCondition>>);
45+
46+
#[turbo_tasks::value_impl]
47+
impl OptionAfterResolvePluginCondition {
48+
#[turbo_tasks::function]
49+
pub fn none() -> Vc<Self> {
50+
Vc::cell(None)
51+
}
52+
53+
#[turbo_tasks::function]
54+
pub fn some(condition: ResolvedVc<AfterResolvePluginCondition>) -> Vc<Self> {
55+
Vc::cell(Some(condition))
56+
}
57+
}
58+
4359
/// A condition which determines if the hooks of a resolve plugin gets called.
4460
#[turbo_tasks::value]
4561
pub enum BeforeResolvePluginCondition {
@@ -80,10 +96,26 @@ impl BeforeResolvePluginCondition {
8096
}
8197
}
8298

99+
#[turbo_tasks::value(transparent)]
100+
pub struct OptionBeforeResolvePluginCondition(Option<ResolvedVc<BeforeResolvePluginCondition>>);
101+
102+
#[turbo_tasks::value_impl]
103+
impl OptionBeforeResolvePluginCondition {
104+
#[turbo_tasks::function]
105+
pub fn none() -> Vc<Self> {
106+
Vc::cell(None)
107+
}
108+
109+
#[turbo_tasks::function]
110+
pub fn some(condition: ResolvedVc<BeforeResolvePluginCondition>) -> Vc<Self> {
111+
Vc::cell(Some(condition))
112+
}
113+
}
114+
83115
#[turbo_tasks::value_trait]
84116
pub trait BeforeResolvePlugin {
85117
#[turbo_tasks::function]
86-
fn before_resolve_condition(self: Vc<Self>) -> Vc<BeforeResolvePluginCondition>;
118+
fn before_resolve_condition(self: Vc<Self>) -> Vc<OptionBeforeResolvePluginCondition>;
87119

88120
#[turbo_tasks::function]
89121
fn before_resolve(
@@ -98,7 +130,7 @@ pub trait BeforeResolvePlugin {
98130
pub trait AfterResolvePlugin {
99131
/// A condition which determines if the hooks gets called.
100132
#[turbo_tasks::function]
101-
fn after_resolve_condition(self: Vc<Self>) -> Vc<AfterResolvePluginCondition>;
133+
fn after_resolve_condition(self: Vc<Self>) -> Vc<OptionAfterResolvePluginCondition>;
102134

103135
/// This hook gets called when a full filepath has been resolved and the
104136
/// condition matches. If a value is returned it replaces the resolve

0 commit comments

Comments
 (0)