Skip to content

Commit

Permalink
Fix internal JS/TS references not being processed (vercel/turborepo#5457
Browse files Browse the repository at this point in the history
)

### Description

My recent change in vercel/turborepo#5397 added a way to create rules that only apply to
internal references. In the process, I also made it so the default rules
don't apply to internal references as well. However, we still need them
to apply to TS and JS files, as we use that with ReferenceType::Internal
in Next.js.

### Testing Instructions

Next.js CI
  • Loading branch information
alexkirsz authored Jul 4, 2023
1 parent 0a9335b commit 2a67d6b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions crates/turbopack/src/module_options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl ModuleOptionsVc {
ModuleRuleCondition::ResourcePathEndsWith(".json".to_string()),
vec![ModuleRuleEffect::ModuleType(ModuleType::Json)],
),
ModuleRule::new(
ModuleRule::new_all(
ModuleRuleCondition::any(vec![
ModuleRuleCondition::ResourcePathEndsWith(".js".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".jsx".to_string()),
Expand Down Expand Up @@ -255,7 +255,7 @@ impl ModuleOptionsVc {
},
})],
),
ModuleRule::new(
ModuleRule::new_all(
ModuleRuleCondition::any(vec![
ModuleRuleCondition::ResourcePathEndsWith(".ts".to_string()),
ModuleRuleCondition::ResourcePathEndsWith(".tsx".to_string()),
Expand Down
20 changes: 15 additions & 5 deletions crates/turbopack/src/module_options/module_rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@ pub struct ModuleRule {
match_mode: MatchMode,
}

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

impl MatchMode {
fn matches(&self, reference_type: &ReferenceType) -> bool {
matches!(
(self, reference_type.is_internal()),
(MatchMode::Default, false) | (MatchMode::Internal, true)
(MatchMode::All, _) | (MatchMode::NonInternal, false) | (MatchMode::Internal, true)
)
}
}
Expand All @@ -42,7 +43,7 @@ impl ModuleRule {
ModuleRule {
condition,
effects,
match_mode: Default::default(),
match_mode: MatchMode::NonInternal,
}
}

Expand All @@ -55,6 +56,15 @@ impl ModuleRule {
}
}

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

pub fn effects(&self) -> impl Iterator<Item = &ModuleRuleEffect> {
self.effects.iter()
}
Expand Down

0 comments on commit 2a67d6b

Please sign in to comment.