@@ -351,53 +351,6 @@ impl Display for ReferenceType {
351351}
352352
353353impl ReferenceType {
354- pub fn includes ( & self , other : & Self ) -> bool {
355- if self == other {
356- return true ;
357- }
358- match self {
359- ReferenceType :: CommonJs ( sub_type) => {
360- matches ! ( other, ReferenceType :: CommonJs ( _) )
361- && matches ! ( sub_type, CommonJsReferenceSubType :: Undefined )
362- }
363- ReferenceType :: EcmaScriptModules ( sub_type) => {
364- matches ! ( other, ReferenceType :: EcmaScriptModules ( _) )
365- && matches ! ( sub_type, EcmaScriptModulesReferenceSubType :: Undefined )
366- }
367- ReferenceType :: Css ( CssReferenceSubType :: AtImport ( _) ) => {
368- // For condition matching, treat any AtImport pair as identical.
369- matches ! ( other, ReferenceType :: Css ( CssReferenceSubType :: AtImport ( _) ) )
370- }
371- ReferenceType :: Css ( sub_type) => {
372- matches ! ( other, ReferenceType :: Css ( _) )
373- && matches ! ( sub_type, CssReferenceSubType :: Undefined )
374- }
375- ReferenceType :: Url ( sub_type) => {
376- matches ! ( other, ReferenceType :: Url ( _) )
377- && matches ! ( sub_type, UrlReferenceSubType :: Undefined )
378- }
379- ReferenceType :: TypeScript ( sub_type) => {
380- matches ! ( other, ReferenceType :: TypeScript ( _) )
381- && matches ! ( sub_type, TypeScriptReferenceSubType :: Undefined )
382- }
383- ReferenceType :: Worker ( sub_type) => {
384- matches ! ( other, ReferenceType :: Worker ( _) )
385- && matches ! ( sub_type, WorkerReferenceSubType :: Undefined )
386- }
387- ReferenceType :: Entry ( sub_type) => {
388- matches ! ( other, ReferenceType :: Entry ( _) )
389- && matches ! ( sub_type, EntryReferenceSubType :: Undefined )
390- }
391- ReferenceType :: Runtime => matches ! ( other, ReferenceType :: Runtime ) ,
392- ReferenceType :: Internal ( _) => matches ! ( other, ReferenceType :: Internal ( _) ) ,
393- ReferenceType :: Loader => matches ! ( other, ReferenceType :: Loader ) ,
394- ReferenceType :: Custom ( _) => {
395- todo ! ( )
396- }
397- ReferenceType :: Undefined => true ,
398- }
399- }
400-
401354 /// Returns true if this reference type is internal. This will be used in
402355 /// combination with [`ModuleRuleCondition::Internal`] to determine if a
403356 /// rule should be applied to an internal asset/reference.
@@ -408,3 +361,85 @@ impl ReferenceType {
408361 )
409362 }
410363}
364+
365+ /// A type to match [`ReferenceType`] against. This is used in conditions to determine if a rule
366+ /// should be applied to a reference of a given type. It allows
367+ /// - to match against a ReferenceType, e.g. with `ReferenceTypeCondition::Url(None)` matching any
368+ /// `ReferenceType::Url(_)`, or
369+ /// - to match against a specific subtype, e.g. with
370+ /// `ReferenceTypeCondition::Url(Some(UrlReferenceSubType::EcmaScriptNewUrl))` matching
371+ /// `ReferenceType::Url(UrlReferenceSubType::EcmaScriptNewUrl)`
372+ #[ derive(
373+ PartialEq , Eq , TraceRawVcs , NonLocalValue , Debug , Clone , Hash , TaskInput , Encode , Decode ,
374+ ) ]
375+ pub enum ReferenceTypeCondition {
376+ CommonJs ( Option < CommonJsReferenceSubType > ) ,
377+ EcmaScriptModules ( Option < EcmaScriptModulesReferenceSubType > ) ,
378+ Css ( Option < CssReferenceSubType > ) ,
379+ Url ( Option < UrlReferenceSubType > ) ,
380+ TypeScript ( Option < TypeScriptReferenceSubType > ) ,
381+ Worker ( Option < WorkerReferenceSubType > ) ,
382+ Entry ( Option < EntryReferenceSubType > ) ,
383+ Runtime ,
384+ Internal ,
385+ Loader ,
386+ Custom ( u8 ) ,
387+ }
388+
389+ macro_rules! match_condition_includes {
390+ (
391+ $self: expr, $other: expr,
392+ optional: [ $( $opt: ident) ,* $( , ) ?] ,
393+ unit: [ $( $unit: ident) ,* $( , ) ?] ,
394+ value: [ $( $val: ident) ,* $( , ) ?]
395+ $( , ) ?
396+ ) => {
397+ match $self {
398+ $(
399+ ReferenceTypeCondition :: $opt( sub_type) => {
400+ if let ReferenceType :: $opt( other_sub_type) = $other {
401+ return sub_type. as_ref( ) . is_none_or( |s| s == other_sub_type) ;
402+ }
403+ }
404+ ) *
405+ $(
406+ ReferenceTypeCondition :: $unit => {
407+ return matches!( $other, ReferenceType :: $unit { .. } ) ;
408+ }
409+ ) *
410+ $(
411+ ReferenceTypeCondition :: $val( v) => {
412+ if let ReferenceType :: $val( ov) = $other {
413+ return v == ov;
414+ }
415+ }
416+ ) *
417+ }
418+ } ;
419+ }
420+
421+ impl ReferenceTypeCondition {
422+ pub fn includes ( & self , other : & ReferenceType ) -> bool {
423+ if matches ! (
424+ self ,
425+ ReferenceTypeCondition :: Css ( Some ( CssReferenceSubType :: AtImport ( _) ) )
426+ ) && matches ! ( other, ReferenceType :: Css ( CssReferenceSubType :: AtImport ( _) ) )
427+ {
428+ // For condition matching, treat any AtImport pair as identical.
429+ return true ;
430+ }
431+
432+ if matches ! ( self , ReferenceTypeCondition :: Custom ( _) ) {
433+ todo ! ( )
434+ }
435+
436+ match_condition_includes ! (
437+ self , other,
438+ optional: [ CommonJs , EcmaScriptModules , Css , Url , TypeScript , Worker , Entry ] ,
439+ unit: [ Runtime , Loader , Internal ] ,
440+ value: [ Custom ] ,
441+ ) ;
442+
443+ false
444+ }
445+ }
0 commit comments