|
| 1 | +import { Block, Configuration, Style, isAttrValue, isBlockClass, isStyle } from "@css-blocks/core"; |
| 2 | +import { StyleMapping } from "@opticss/template-api"; |
| 3 | +import { ObjectDictionary } from "@opticss/util"; |
| 4 | +import * as debugGenerator from "debug"; |
| 5 | + |
| 6 | +import { AggregateRewriteData, BlockInfo, ConditionalStyle, ConditionalStyleExpression, GlobalBlockIndex, GlobalStyleIndex, ImpliedStyles, LocalStyleIndex, Operator, OptimizationEntry, StyleRequirements } from "./AggregateRewriteData"; |
| 7 | +import { EmberAnalyzer } from "./EmberAnalyzer"; |
| 8 | + |
| 9 | +const debug = debugGenerator("css-blocks:ember-app:runtime-data-generator"); |
| 10 | + |
| 11 | +export class RuntimeDataGenerator { |
| 12 | + blocks: Array<Block>; |
| 13 | + styleIndices: Map<Style, number>; |
| 14 | + blockIndices: Map<Block, number>; |
| 15 | + sourceClassIndices: Map<string, number>; |
| 16 | + outputClassIndices: Map<string, number>; |
| 17 | + styleMapping: StyleMapping; |
| 18 | + analyzer: EmberAnalyzer; |
| 19 | + config: Configuration; |
| 20 | + reservedClassNames: Set<string>; |
| 21 | + constructor(blocks: Array<Block>, styleMapping: StyleMapping, analyzer: EmberAnalyzer, config: Configuration, reservedClassNames: Set<string>) { |
| 22 | + this.blocks = blocks; |
| 23 | + this.styleIndices = new Map<Style, number>(); |
| 24 | + this.blockIndices = new Map<Block, number>(); |
| 25 | + this.sourceClassIndices = new Map<string, number>(); |
| 26 | + this.outputClassIndices = new Map<string, number>(); |
| 27 | + this.styleMapping = styleMapping; |
| 28 | + this.analyzer = analyzer; |
| 29 | + this.config = config; |
| 30 | + this.reservedClassNames = reservedClassNames; |
| 31 | + } |
| 32 | + blockIndex(block: Block): number { |
| 33 | + if (!this.blockIndices.has(block)) { |
| 34 | + this.blockIndices.set(block, this.blockIndices.size); |
| 35 | + } |
| 36 | + return this.blockIndices.get(block)!; |
| 37 | + } |
| 38 | + sourceClassIndex(className: string): number { |
| 39 | + if (!this.sourceClassIndices.has(className)) { |
| 40 | + this.sourceClassIndices.set(className, this.sourceClassIndices.size); |
| 41 | + } |
| 42 | + return this.sourceClassIndices.get(className)!; |
| 43 | + } |
| 44 | + cssClass(style: Style): string { |
| 45 | + return style.cssClass(this.config, this.reservedClassNames); |
| 46 | + } |
| 47 | + outputClassIndex(classNameOrStyle: string | Style): number { |
| 48 | + let className: string; |
| 49 | + if (isStyle(classNameOrStyle)) { |
| 50 | + className = this.cssClass(classNameOrStyle); |
| 51 | + } else { |
| 52 | + className = classNameOrStyle; |
| 53 | + } |
| 54 | + if (!this.outputClassIndices.has(className)) { |
| 55 | + this.outputClassIndices.set(className, this.outputClassIndices.size); |
| 56 | + } |
| 57 | + return this.outputClassIndices.get(className)!; |
| 58 | + } |
| 59 | + styleIndex(style: undefined): null; |
| 60 | + styleIndex(style: Style): number; |
| 61 | + styleIndex(style: Style | undefined): number | null { |
| 62 | + if (!style) return null; |
| 63 | + if (!this.styleIndices.has(style)) { |
| 64 | + this.styleIndices.set(style, this.styleIndices.size); |
| 65 | + } |
| 66 | + return this.styleIndices.get(style)!; |
| 67 | + } |
| 68 | + generate(): AggregateRewriteData { |
| 69 | + let blockIds: ObjectDictionary<GlobalBlockIndex> = {}; |
| 70 | + let blocks = new Array<BlockInfo>(); |
| 71 | + for (let block of this.blocks) { |
| 72 | + blockIds[block.guid] = this.blockIndex(block); |
| 73 | + } |
| 74 | + for (let block of this.blocks) { |
| 75 | + blocks[this.blockIndex(block)] = this.generateBlockInfo(block); |
| 76 | + } |
| 77 | + let stylesInUse = this.findStylesInUse(); |
| 78 | + debug(`There are ${stylesInUse.size} styles in use.`); |
| 79 | + let styleRequirements: StyleRequirements = {}; |
| 80 | + for (let style of stylesInUse) { |
| 81 | + if (isAttrValue(style)) { |
| 82 | + styleRequirements[this.styleIndex(style)] = [Operator.AND, this.styleIndex(style.blockClass)]; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + let impliedStyles = this.getImpliedStyles(stylesInUse); |
| 87 | + |
| 88 | + let optimizations = this.getOptimizations(stylesInUse); |
| 89 | + |
| 90 | + let outputClassnames = new Array<string>(this.outputClassIndices.size); |
| 91 | + for (let outputClass of this.outputClassIndices.keys()) { |
| 92 | + outputClassnames[this.outputClassIndices.get(outputClass)!] = outputClass; |
| 93 | + } |
| 94 | + |
| 95 | + return { |
| 96 | + blockIds, |
| 97 | + blocks, |
| 98 | + outputClassnames, |
| 99 | + styleRequirements, |
| 100 | + impliedStyles, |
| 101 | + optimizations, |
| 102 | + }; |
| 103 | + } |
| 104 | + |
| 105 | + getOptimizations(stylesInUse: Set<Style>): Array<OptimizationEntry> { |
| 106 | + let optimizations = new Array<OptimizationEntry>(); |
| 107 | + for (let style of stylesInUse) { |
| 108 | + if (this.styleMapping.isStyledAfterOptimization({name: "class", value: this.cssClass(style)})) { |
| 109 | + optimizations.push([this.outputClassIndex(style), this.styleIndex(style)]); |
| 110 | + continue; |
| 111 | + } |
| 112 | + } |
| 113 | + return optimizations; |
| 114 | + } |
| 115 | + |
| 116 | + getImpliedStyles(stylesInUse: Set<Style>): ImpliedStyles { |
| 117 | + let impliedStyles: ImpliedStyles = {}; |
| 118 | + for (let style of stylesInUse) { |
| 119 | + let implied = new Array<GlobalStyleIndex | string | ConditionalStyle>(); |
| 120 | + // implied by inheritance |
| 121 | + let baseStyle = style.base; |
| 122 | + if (baseStyle) { |
| 123 | + implied.push(this.styleIndex(baseStyle)); |
| 124 | + } |
| 125 | + |
| 126 | + // implied by aliasing |
| 127 | + implied.push(...style.getStyleAliases()); |
| 128 | + |
| 129 | + // implied by composition |
| 130 | + if (isBlockClass(style)) { |
| 131 | + for (let composition of style.composedStyles()) { |
| 132 | + if (composition.conditions.length > 0) { |
| 133 | + let style = this.styleIndex(composition.style); |
| 134 | + let conditions: ConditionalStyleExpression = [Operator.AND, ...composition.conditions.map(s => this.styleIndex(s))]; |
| 135 | + implied.push({style, conditions}); |
| 136 | + } else { |
| 137 | + implied.push(this.styleIndex(composition.style)); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + if (implied.length > 0) { |
| 142 | + impliedStyles[this.styleIndex(style)] = implied; |
| 143 | + } |
| 144 | + } |
| 145 | + return impliedStyles; |
| 146 | + } |
| 147 | + |
| 148 | + findStylesInUse(): Set<Style> { |
| 149 | + let stylesInUse = new Set<Style>(); |
| 150 | + for (let style of this.analyzer.stylesFound) { |
| 151 | + for (let s of style.resolveStyles()) { |
| 152 | + stylesInUse.add(s); |
| 153 | + } |
| 154 | + if (isBlockClass(style)) { |
| 155 | + for (let c of style.resolveComposedStyles()) { |
| 156 | + stylesInUse.add(c.style); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + return stylesInUse; |
| 161 | + } |
| 162 | + generateBlockInfo(block: Block): BlockInfo { |
| 163 | + let blockInterfaceStyles: ObjectDictionary<LocalStyleIndex> = {}; |
| 164 | + let resolvedInterface = block.resolvedStyleInterface(); |
| 165 | + let styleNames = Object.keys(resolvedInterface).sort(); |
| 166 | + let i = 0; |
| 167 | + for (let styleName of styleNames) { |
| 168 | + blockInterfaceStyles[styleName] = i++; |
| 169 | + } |
| 170 | + let implementingBlocks = this.blocks.filter(b => b.isImplementationOf(block)); |
| 171 | + let implementations: ObjectDictionary<Array<GlobalBlockIndex | null>> = {}; |
| 172 | + for (let impl of implementingBlocks) { |
| 173 | + let implInterface = impl.resolvedStyleInterface(); |
| 174 | + implementations[this.blockIndex(impl)] = styleNames.map(n => this.styleIndex(implInterface[n])); |
| 175 | + } |
| 176 | + return { |
| 177 | + blockInterfaceStyles, |
| 178 | + implementations, |
| 179 | + }; |
| 180 | + } |
| 181 | +} |
0 commit comments