@@ -106,6 +106,10 @@ interface DesignSystem extends BaseDesignSystem {
106106 SignatureOptions ,
107107 DefaultMap < string , DefaultMap < string , Set < string > > >
108108 >
109+ [ DYNAMIC_UTILITIES_KEY ] : DefaultMap <
110+ SignatureOptions ,
111+ DefaultMap < string , DefaultMap < string , DefaultMap < string , Set < string > > > >
112+ >
109113 [ PRE_COMPUTED_UTILITIES_KEY ] : DefaultMap < SignatureOptions , DefaultMap < string , string [ ] > >
110114 [ VARIANT_SIGNATURE_KEY ] : DefaultMap < RemValue , DefaultMap < string , string | symbol > >
111115 [ PRE_COMPUTED_VARIANTS_KEY ] : DefaultMap < RemValue , DefaultMap < string , string [ ] > >
@@ -130,6 +134,7 @@ export function prepareDesignSystemStorage(
130134 createUtilityThemeVariablesCache ( designSystem )
131135 designSystem . storage [ STATIC_UTILITIES_KEY ] ??= createStaticUtilitiesCache ( )
132136 designSystem . storage [ UTILITY_PROPERTIES_KEY ] ??= createUtilityPropertiesCache ( designSystem )
137+ designSystem . storage [ DYNAMIC_UTILITIES_KEY ] ??= createDynamicUtilitiesCache ( designSystem )
133138 designSystem . storage [ PRE_COMPUTED_UTILITIES_KEY ] ??= createPreComputedUtilitiesCache ( designSystem )
134139 designSystem . storage [ VARIANT_SIGNATURE_KEY ] ??= createVariantSignatureCache ( designSystem )
135140 designSystem . storage [ PRE_COMPUTED_VARIANTS_KEY ] ??= createPreComputedVariantsCache ( designSystem )
@@ -276,6 +281,7 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st
276281 designSystem . storage [ UTILITY_PROPERTIES_KEY ] . get ( signatureOptions )
277282 let staticUtilities = designSystem . storage [ STATIC_UTILITIES_KEY ] . get ( signatureOptions )
278283 let themeVariables = designSystem . storage [ UTILITY_THEME_VARIABLES_KEY ] . get ( signatureOptions )
284+ let dynamicUtilities = designSystem . storage [ DYNAMIC_UTILITIES_KEY ] . get ( signatureOptions )
279285
280286 // For each candidate, compute the used properties and values. E.g.: `mt-1` → `margin-top` → `0.25rem`
281287 //
@@ -339,43 +345,6 @@ function collapseCandidates(options: InternalCanonicalizeOptions, candidates: st
339345 }
340346 }
341347
342- let dynamicUtilities = new DefaultMap ( ( candidate : string ) => {
343- let result = new DefaultMap (
344- ( _property : string ) => new DefaultMap ( ( _value : string ) => new Set < string > ( ) ) ,
345- )
346-
347- let relevantProperties = new Set ( computeUtilitiesPropertiesLookup . get ( candidate ) . keys ( ) )
348- if ( relevantProperties . size === 0 ) return result
349-
350- for ( let parsedCandidate of parseCandidate ( designSystem , candidate ) ) {
351- if ( parsedCandidate . kind !== 'functional' || parsedCandidate . value === null ) {
352- continue
353- }
354-
355- for ( let root of designSystem . utilities . keys ( 'functional' ) ) {
356- if ( root === parsedCandidate . root ) continue // Skip self
357-
358- let replacement = printUnprefixedCandidate ( designSystem , {
359- ...cloneCandidate ( parsedCandidate ) ,
360- root,
361- } )
362-
363- let propertyValues = computeUtilitiesPropertiesLookup . get ( replacement )
364- for ( let [ property , values ] of propertyValues ) {
365- if ( ! relevantProperties . has ( property ) ) continue // Skip properties that are not relevant for the current candidate
366-
367- for ( let value of values ) {
368- result . get ( property ) . get ( value ) . add ( replacement )
369- }
370- }
371- }
372-
373- return result
374- }
375-
376- return result
377- } )
378-
379348 // For each property, lookup other utilities that also set this property and
380349 // this exact value. If multiple properties are used, use the intersection of
381350 // each property.
@@ -2951,6 +2920,53 @@ function createUtilityPropertiesCache(
29512920 } )
29522921}
29532922
2923+ const DYNAMIC_UTILITIES_KEY = Symbol ( )
2924+ function createDynamicUtilitiesCache (
2925+ designSystem : DesignSystem ,
2926+ ) : DesignSystem [ 'storage' ] [ typeof DYNAMIC_UTILITIES_KEY ] {
2927+ return new DefaultMap ( ( signatureOptions : SignatureOptions ) => {
2928+ let computeUtilitiesPropertiesLookup =
2929+ designSystem . storage [ UTILITY_PROPERTIES_KEY ] . get ( signatureOptions )
2930+
2931+ return new DefaultMap ( ( candidate : string ) => {
2932+ let result = new DefaultMap (
2933+ ( _property : string ) => new DefaultMap ( ( _value : string ) => new Set < string > ( ) ) ,
2934+ )
2935+
2936+ let relevantProperties = new Set ( computeUtilitiesPropertiesLookup . get ( candidate ) . keys ( ) )
2937+ if ( relevantProperties . size === 0 ) return result
2938+
2939+ for ( let parsedCandidate of parseCandidate ( designSystem , candidate ) ) {
2940+ if ( parsedCandidate . kind !== 'functional' || parsedCandidate . value === null ) {
2941+ continue
2942+ }
2943+
2944+ for ( let root of designSystem . utilities . keys ( 'functional' ) ) {
2945+ if ( root === parsedCandidate . root ) continue // Skip self
2946+
2947+ let replacement = printUnprefixedCandidate ( designSystem , {
2948+ ...cloneCandidate ( parsedCandidate ) ,
2949+ root,
2950+ } )
2951+
2952+ let propertyValues = computeUtilitiesPropertiesLookup . get ( replacement )
2953+ for ( let [ property , values ] of propertyValues ) {
2954+ if ( ! relevantProperties . has ( property ) ) continue // Skip properties that are not relevant for the current candidate
2955+
2956+ for ( let value of values ) {
2957+ result . get ( property ) . get ( value ) . add ( replacement )
2958+ }
2959+ }
2960+ }
2961+
2962+ return result
2963+ }
2964+
2965+ return result
2966+ } )
2967+ } )
2968+ }
2969+
29542970// For all static utilities in the system, compute a lookup table that maps the
29552971// utility signature to the utility name. This is used to find the utility name
29562972// for a given utility signature.
0 commit comments