Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BeforeResolvePluginCondition: allow module filtering as a condition for BeforeResolvePlugins #8407

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading