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

feat(contextcondition): support InPath contextcondition #4521

Merged
merged 1 commit into from
Apr 18, 2023
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
1 change: 1 addition & 0 deletions crates/turbopack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bench_against_node_nft = []
[dependencies]
anyhow = { workspace = true }
async-recursion = "1.0.2"
futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
lazy_static = { workspace = true }
regex = { workspace = true }
Expand Down
29 changes: 24 additions & 5 deletions crates/turbopack/src/condition.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use async_recursion::async_recursion;
use futures::{stream, StreamExt};
use serde::{Deserialize, Serialize};
use turbo_tasks::trace::TraceRawVcs;
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_fs::{FileSystem, FileSystemPath, FileSystemPathVc};

#[derive(Debug, Clone, Serialize, Deserialize, TraceRawVcs, PartialEq, Eq)]
pub enum ContextCondition {
All(Vec<ContextCondition>),
Any(Vec<ContextCondition>),
Not(Box<ContextCondition>),
InDirectory(String),
InPath(FileSystemPathVc),
}

impl ContextCondition {
Expand All @@ -27,12 +30,28 @@ impl ContextCondition {
ContextCondition::Not(Box::new(condition))
}

#[async_recursion]
/// Returns true if the condition matches the context.
pub fn matches(&self, context: &FileSystemPath) -> bool {
pub async fn matches(&self, context: &FileSystemPath) -> bool {
kwonoj marked this conversation as resolved.
Show resolved Hide resolved
match self {
ContextCondition::All(conditions) => conditions.iter().all(|c| c.matches(context)),
ContextCondition::Any(conditions) => conditions.iter().any(|c| c.matches(context)),
ContextCondition::Not(condition) => !condition.matches(context),
ContextCondition::All(conditions) => {
stream::iter(conditions)
.all(|c| async move { c.matches(context).await })
.await
}
ContextCondition::Any(conditions) => {
stream::iter(conditions)
.any(|c| async move { c.matches(context).await })
.await
}
ContextCondition::Not(condition) => !condition.matches(context).await,
ContextCondition::InPath(path) => {
if let Ok(path) = path.await {
context.is_inside(&path)
} else {
false
}
}
ContextCondition::InDirectory(dir) => {
context.path.starts_with(&format!("{dir}/"))
|| context.path.contains(&format!("/{dir}/"))
Expand Down
5 changes: 3 additions & 2 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use module_options_context::*;
pub use module_rule::*;
pub use rule_condition::*;
use turbo_tasks::primitives::{OptionStringVc, StringsVc};
use turbo_tasks_fs::FileSystemPathVc;
use turbo_tasks_fs::{FileSystem, FileSystemPathVc};
use turbopack_core::{
reference_type::{ReferenceType, UrlReferenceSubType},
resolve::options::{ImportMap, ImportMapVc, ImportMapping, ImportMappingVc},
Expand Down Expand Up @@ -84,8 +84,9 @@ impl ModuleOptionsVc {
} = *context.await?;
if !rules.is_empty() {
let path_value = path.await?;

for (condition, new_context) in rules.iter() {
if condition.matches(&path_value) {
if condition.matches(&path_value).await {
return Ok(ModuleOptionsVc::new(path, *new_context));
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ pub async fn resolve_options(
if !options_context_value.rules.is_empty() {
let context_value = &*context.await?;
for (condition, new_options_context) in options_context_value.rules.iter() {
if condition.matches(context_value) {
if condition.matches(context_value).await {
return Ok(resolve_options(context, *new_options_context));
}
}
Expand Down