diff --git a/crates/turbopack-core/src/resolve/mod.rs b/crates/turbopack-core/src/resolve/mod.rs index 3b6e98377fbf6..f4390cb252735 100644 --- a/crates/turbopack-core/src/resolve/mod.rs +++ b/crates/turbopack-core/src/resolve/mod.rs @@ -1416,7 +1416,7 @@ async fn handle_before_resolve_plugins( ) -> Result>> { for plugin in &options.await?.before_resolve_plugins { let condition = plugin.before_resolve_condition().resolve().await?; - if !*condition.matches(request).await? { + if !condition.await?.matches(request).await? { continue; } diff --git a/crates/turbopack-core/src/resolve/plugin.rs b/crates/turbopack-core/src/resolve/plugin.rs index 67becbe71576b..4bfa3306b59c9 100644 --- a/crates/turbopack-core/src/resolve/plugin.rs +++ b/crates/turbopack-core/src/resolve/plugin.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use turbo_tasks::{Value, Vc}; +use turbo_tasks::{RcStr, Value, Vc}; use turbo_tasks_fs::{glob::Glob, FileSystemPath}; use crate::{ @@ -41,27 +41,39 @@ impl AfterResolvePluginCondition { /// A condition which determines if the hooks of a resolve plugin gets called. #[turbo_tasks::value] -pub struct BeforeResolvePluginCondition { - glob: Vc, +pub enum BeforeResolvePluginCondition { + Request(Vc), + Modules(Vc>), } #[turbo_tasks::value_impl] impl BeforeResolvePluginCondition { #[turbo_tasks::function] - pub fn new(glob: Vc) -> Vc { - BeforeResolvePluginCondition { glob }.cell() + pub fn from_modules(modules: Vc>) -> Vc { + BeforeResolvePluginCondition::Modules(modules).cell() } #[turbo_tasks::function] - pub async fn matches(self: Vc, request: Vc) -> Result> { - Ok(Vc::cell(match request.await?.request() { - Some(request) => { - let this = self.await?; - let glob = this.glob.await?; - glob.execute(&request) + pub fn from_request_glob(glob: Vc) -> Vc { + BeforeResolvePluginCondition::Request(glob).cell() + } +} + +impl BeforeResolvePluginCondition { + pub async fn matches(&self, request: Vc) -> Result { + Ok(match self { + BeforeResolvePluginCondition::Request(glob) => match request.await?.request() { + Some(request) => glob.await?.execute(request.as_str()), + None => false, + }, + BeforeResolvePluginCondition::Modules(modules) => { + if let Request::Module { module, .. } = &*request.await? { + modules.await?.contains(module) + } else { + false + } } - None => false, - })) + }) } }