Skip to content

Commit

Permalink
ModuleRule::new_internal
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed Jul 3, 2023
1 parent 41230e8 commit a3871d6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
10 changes: 10 additions & 0 deletions crates/turbopack-core/src/reference_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,14 @@ impl ReferenceType {
ReferenceType::Undefined => true,
}
}

/// Returns true if this reference type is internal. This will be used in
/// combination with [`ModuleRuleCondition::Internal`] to determine if a
/// rule should be applied to an internal asset/reference.
pub fn is_internal(&self) -> bool {
matches!(
self,
ReferenceType::Internal(_) | ReferenceType::Css(CssReferenceSubType::Internal)
)
}
}
40 changes: 14 additions & 26 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,9 @@ impl ModuleOptionsVc {
vec![ModuleRuleEffect::ModuleType(ModuleType::Json)],
),
ModuleRule::new(
ModuleRuleCondition::all(vec![
ModuleRuleCondition::ResourcePathEndsWith(".css".to_string()),
ModuleRuleCondition::not(ModuleRuleCondition::ReferenceType(
ReferenceType::Css(CssReferenceSubType::Internal),
)),
]),
ModuleRuleCondition::all(vec![ModuleRuleCondition::ResourcePathEndsWith(
".css".to_string(),
)]),
[
if let Some(options) = enable_postcss_transform {
let execution_context = execution_context
Expand Down Expand Up @@ -263,33 +260,24 @@ impl ModuleOptionsVc {
.collect(),
),
ModuleRule::new(
ModuleRuleCondition::all(vec![
ModuleRuleCondition::ResourcePathEndsWith(".module.css".to_string()),
ModuleRuleCondition::not(ModuleRuleCondition::ReferenceType(
ReferenceType::Css(CssReferenceSubType::Internal),
)),
]),
ModuleRuleCondition::all(vec![ModuleRuleCondition::ResourcePathEndsWith(
".module.css".to_string(),
)]),
vec![ModuleRuleEffect::ModuleType(ModuleType::CssModule)],
),
ModuleRule::new(
ModuleRuleCondition::all(vec![
ModuleRuleCondition::ResourcePathEndsWith(".css".to_string()),
ModuleRuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::Internal,
)),
]),
ModuleRule::new_internal(
ModuleRuleCondition::all(vec![ModuleRuleCondition::ResourcePathEndsWith(
".css".to_string(),
)]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Default,
transforms: css_transforms,
})],
),
ModuleRule::new(
ModuleRuleCondition::all(vec![
ModuleRuleCondition::ResourcePathEndsWith(".module.css".to_string()),
ModuleRuleCondition::ReferenceType(ReferenceType::Css(
CssReferenceSubType::Internal,
)),
]),
ModuleRule::new_internal(
ModuleRuleCondition::all(vec![ModuleRuleCondition::ResourcePathEndsWith(
".module.css".to_string(),
)]),
vec![ModuleRuleEffect::ModuleType(ModuleType::Css {
ty: CssModuleAssetType::Module,
transforms: css_transforms,
Expand Down
38 changes: 36 additions & 2 deletions crates/turbopack/src/module_options/module_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,44 @@ use super::{CustomModuleTypeVc, ModuleRuleCondition};
pub struct ModuleRule {
condition: ModuleRuleCondition,
effects: Vec<ModuleRuleEffect>,
match_mode: MatchMode,
}

#[derive(Default, Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, TraceRawVcs)]
enum MatchMode {
// Match all but internal references.
#[default]
Default,
// Only match internal references.
Internal,
}

impl MatchMode {
fn matches(&self, reference_type: &ReferenceType) -> bool {
matches!(
(self, reference_type.is_internal()),
(MatchMode::Default, false) | (MatchMode::Internal, true)
)
}
}

impl ModuleRule {
/// Creates a new module rule. Will not match internal references.
pub fn new(condition: ModuleRuleCondition, effects: Vec<ModuleRuleEffect>) -> Self {
ModuleRule { condition, effects }
ModuleRule {
condition,
effects,
match_mode: Default::default(),
}
}

/// Creates a new module rule. Will only matches internal references.
pub fn new_internal(condition: ModuleRuleCondition, effects: Vec<ModuleRuleEffect>) -> Self {
ModuleRule {
condition,
effects,
match_mode: MatchMode::Internal,
}
}

pub fn effects(&self) -> impl Iterator<Item = &ModuleRuleEffect> {
Expand All @@ -32,7 +65,8 @@ impl ModuleRule {
path: &FileSystemPath,
reference_type: &ReferenceType,
) -> Result<bool> {
self.condition.matches(source, path, reference_type).await
Ok(self.match_mode.matches(reference_type)
&& self.condition.matches(source, path, reference_type).await?)
}
}

Expand Down

0 comments on commit a3871d6

Please sign in to comment.