@@ -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,84 @@ 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+ impl ReferenceTypeCondition {
390+ pub fn includes ( & self , other : & ReferenceType ) -> bool {
391+ if matches ! (
392+ self ,
393+ ReferenceTypeCondition :: Css ( Some ( CssReferenceSubType :: AtImport ( _) ) )
394+ ) && matches ! ( other, ReferenceType :: Css ( CssReferenceSubType :: AtImport ( _) ) )
395+ {
396+ // For condition matching, treat any AtImport pair as identical.
397+ return true ;
398+ }
399+
400+ if matches ! ( self , ReferenceTypeCondition :: Custom ( _) ) {
401+ todo ! ( )
402+ }
403+
404+ macro_rules! match_condition_includes {
405+ (
406+ $self: expr, $other: expr,
407+ optional: [ $( $opt: ident) ,* $( , ) ?] ,
408+ unit: [ $( $unit: ident) ,* $( , ) ?] ,
409+ value: [ $( $val: ident) ,* $( , ) ?]
410+ $( , ) ?
411+ ) => {
412+ match $self {
413+ $(
414+ ReferenceTypeCondition :: $opt( sub_type) => {
415+ if let ReferenceType :: $opt( other_sub_type) = $other {
416+ return sub_type. as_ref( ) . is_none_or( |s| s == other_sub_type) ;
417+ }
418+ }
419+ ) *
420+ $(
421+ ReferenceTypeCondition :: $unit => {
422+ return matches!( $other, ReferenceType :: $unit { .. } ) ;
423+ }
424+ ) *
425+ $(
426+ ReferenceTypeCondition :: $val( v) => {
427+ if let ReferenceType :: $val( ov) = $other {
428+ return v == ov;
429+ }
430+ }
431+ ) *
432+ }
433+ } ;
434+ }
435+ match_condition_includes ! (
436+ self , other,
437+ optional: [ CommonJs , EcmaScriptModules , Css , Url , TypeScript , Worker , Entry ] ,
438+ unit: [ Runtime , Loader , Internal ] ,
439+ value: [ Custom ] ,
440+ ) ;
441+
442+ false
443+ }
444+ }
0 commit comments