From 2a67d6b427cc2b3b508111a7ae1f5f88a0243dee Mon Sep 17 00:00:00 2001 From: Alex Kirszenberg Date: Tue, 4 Jul 2023 15:03:10 +0200 Subject: [PATCH] Fix internal JS/TS references not being processed (vercel/turbo#5457) ### Description My recent change in vercel/turbo#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 --- crates/turbopack/src/module_options/mod.rs | 4 ++-- .../src/module_options/module_rule.rs | 20 ++++++++++++++----- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/turbopack/src/module_options/mod.rs b/crates/turbopack/src/module_options/mod.rs index bd9677f8dbcff..84c01cc292011 100644 --- a/crates/turbopack/src/module_options/mod.rs +++ b/crates/turbopack/src/module_options/mod.rs @@ -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()), @@ -255,7 +255,7 @@ impl ModuleOptionsVc { }, })], ), - ModuleRule::new( + ModuleRule::new_all( ModuleRuleCondition::any(vec![ ModuleRuleCondition::ResourcePathEndsWith(".ts".to_string()), ModuleRuleCondition::ResourcePathEndsWith(".tsx".to_string()), diff --git a/crates/turbopack/src/module_options/module_rule.rs b/crates/turbopack/src/module_options/module_rule.rs index 6a170ab1436de..86666aca2b133 100644 --- a/crates/turbopack/src/module_options/module_rule.rs +++ b/crates/turbopack/src/module_options/module_rule.rs @@ -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) ) } } @@ -42,7 +43,7 @@ impl ModuleRule { ModuleRule { condition, effects, - match_mode: Default::default(), + match_mode: MatchMode::NonInternal, } } @@ -55,6 +56,15 @@ impl ModuleRule { } } + /// Creates a new module rule. Will only matches internal references. + pub fn new_all(condition: ModuleRuleCondition, effects: Vec) -> Self { + ModuleRule { + condition, + effects, + match_mode: MatchMode::All, + } + } + pub fn effects(&self) -> impl Iterator { self.effects.iter() }