@@ -6,7 +6,7 @@ import type { ObjectDictionary } from "@opticss/util";
6
6
7
7
/// @ts -ignore
8
8
import { data as _data } from "./-css-blocks-data" ;
9
- import type { AggregateRewriteData , ConditionalStyle , ImpliedStyles , StyleExpression , StyleRequirements } from "./AggregateRewriteData" ;
9
+ import type { AggregateRewriteData , ConditionalStyle , GlobalBlockIndex , ImpliedStyles , OptimizationEntry , StyleExpression , StyleRequirements } from "./AggregateRewriteData" ;
10
10
11
11
const data : AggregateRewriteData = _data ;
12
12
@@ -95,8 +95,17 @@ const enum FalsySwitchBehavior {
95
95
default = 2 ,
96
96
}
97
97
98
+ interface StyleIdToOptimizationsMap {
99
+ [ styleId : number ] : Array < number > ;
100
+ }
101
+
98
102
// tslint:disable-next-line:no-default-export
99
103
export default class CSSBlocksService extends Service {
104
+ possibleOptimizations ! : StyleIdToOptimizationsMap ;
105
+ constructor ( ) {
106
+ super ( ...arguments ) ;
107
+ this . possibleOptimizations = getOptimizationInverseMap ( data . optimizations ) ;
108
+ }
100
109
classNamesFor ( argv : Array < string | number | boolean | null > ) : string {
101
110
let args = argv . slice ( ) ;
102
111
args . reverse ( ) ; // pop() is faster than shift()
@@ -222,7 +231,7 @@ export default class CSSBlocksService extends Service {
222
231
let classNameIndices = new Set < number > ( ) ;
223
232
// TODO: Only iterate over the subset of optimizations that might match this
224
233
// element's styles.
225
- for ( let [ clsIdx , expr ] of data . optimizations ) {
234
+ for ( let [ clsIdx , expr ] of this . getPossibleOptimizations ( stylesApplied ) ) {
226
235
if ( evaluateExpression ( expr , stylesApplied ) ) {
227
236
classNameIndices . add ( clsIdx ) ;
228
237
}
@@ -234,6 +243,17 @@ export default class CSSBlocksService extends Service {
234
243
let result = classNames . join ( " " ) ;
235
244
return result ;
236
245
}
246
+
247
+ getPossibleOptimizations ( stylesApplied : Set < number > ) : Array < OptimizationEntry > {
248
+ let optimizations : Array < number > = [ ] ;
249
+ for ( let style of stylesApplied ) {
250
+ let possibleOpts = this . possibleOptimizations [ style ] ;
251
+ if ( possibleOpts ) {
252
+ optimizations . push ( ...possibleOpts ) ;
253
+ }
254
+ }
255
+ return [ ...new Set ( optimizations ) ] . map ( i => data . optimizations [ i ] ) ;
256
+ }
237
257
}
238
258
239
259
function evaluateExpression ( expr : StyleExpression , stylesApplied : Set < number > , stylesApplied2 ?: Set < number > ) : boolean {
@@ -315,3 +335,28 @@ function applyImpliedStyles(stylesApplied: Set<number>, impliedStyles: ImpliedSt
315
335
}
316
336
return aliases ;
317
337
}
338
+
339
+ function getOptimizationInverseMap ( optimizations : Array < OptimizationEntry > ) : StyleIdToOptimizationsMap {
340
+ let inverseMap : StyleIdToOptimizationsMap = { } ;
341
+ for ( let i = 0 ; i < optimizations . length ; i ++ ) {
342
+ for ( let styleId of new Set ( extractStyleIds ( optimizations [ i ] [ 1 ] ) ) ) {
343
+ if ( inverseMap [ styleId ] === undefined ) {
344
+ inverseMap [ styleId ] = [ ] ;
345
+ }
346
+ inverseMap [ styleId ] . push ( i ) ;
347
+ }
348
+ }
349
+ return inverseMap ;
350
+ }
351
+
352
+ function extractStyleIds ( expr : StyleExpression ) : Array < GlobalBlockIndex > {
353
+ if ( typeof expr === "number" ) {
354
+ return [ expr ] ;
355
+ } else {
356
+ let result = new Array < GlobalBlockIndex > ( ) ;
357
+ for ( let i = 1 ; i < expr . length ; i ++ ) {
358
+ result . push ( ...extractStyleIds ( expr [ i ] ) ) ;
359
+ }
360
+ return result ;
361
+ }
362
+ }
0 commit comments