Skip to content

Commit

Permalink
BeforeResolvePluginCondition: allow module filtering as a condition f…
Browse files Browse the repository at this point in the history
…or `BeforeResolvePlugin`s

This expands `BeforeResolvePluginCondition` to match on individual modules rather than just the request string.

Needed for vercel/next.js#66622
  • Loading branch information
wbinnssmith committed Jun 12, 2024
1 parent 632953e commit d23d385
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,7 @@ async fn handle_before_resolve_plugins(
) -> Result<Option<Vc<ResolveResult>>> {
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;
}

Expand Down
38 changes: 25 additions & 13 deletions crates/turbopack-core/src/resolve/plugin.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand Down Expand Up @@ -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<Glob>,
pub enum BeforeResolvePluginCondition {
Request(Vc<Glob>),
Modules(Vc<Vec<RcStr>>),
}

#[turbo_tasks::value_impl]
impl BeforeResolvePluginCondition {
#[turbo_tasks::function]
pub fn new(glob: Vc<Glob>) -> Vc<Self> {
BeforeResolvePluginCondition { glob }.cell()
pub fn from_modules(modules: Vc<Vec<RcStr>>) -> Vc<Self> {
BeforeResolvePluginCondition::Modules(modules).cell()
}

#[turbo_tasks::function]
pub async fn matches(self: Vc<Self>, request: Vc<Request>) -> Result<Vc<bool>> {
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<Glob>) -> Vc<Self> {
BeforeResolvePluginCondition::Request(glob).cell()
}
}

impl BeforeResolvePluginCondition {
pub async fn matches(&self, request: Vc<Request>) -> Result<bool> {
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,
}))
})
}
}

Expand Down

0 comments on commit d23d385

Please sign in to comment.