Skip to content

Commit

Permalink
feat(contextcondition): support in filesystempath contextcondition
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Apr 13, 2023
1 parent 13ff78b commit 0c06c8f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
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
32 changes: 27 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,31 @@ 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 {
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 {
path.fs
.root()
.await
.map_or(false, |root| context.is_inside(&root))
} 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

0 comments on commit 0c06c8f

Please sign in to comment.