Skip to content

Commit af9efaa

Browse files
committed
feat: Enable optimizer and runtime rewriting of optimized styles.
1 parent 0b51ce2 commit af9efaa

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Diff for: packages/@css-blocks/ember-app/src/RuntimeDataGenerator.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class RuntimeDataGenerator {
3737
}
3838
sourceClassIndex(className: string): number {
3939
if (!this.sourceClassIndices.has(className)) {
40-
this.sourceClassIndices.set(className, this.sourceClassIndices.size);
40+
throw new Error("[internal error] unknown class");
4141
}
4242
return this.sourceClassIndices.get(className)!;
4343
}
@@ -78,6 +78,7 @@ export class RuntimeDataGenerator {
7878
debug(`There are ${stylesInUse.size} styles in use.`);
7979
let styleRequirements: StyleRequirements = {};
8080
for (let style of stylesInUse) {
81+
this.sourceClassIndices.set(this.cssClass(style), this.styleIndex(style));
8182
if (isAttrValue(style)) {
8283
styleRequirements[this.styleIndex(style)] = [Operator.AND, this.styleIndex(style.blockClass)];
8384
}
@@ -105,10 +106,26 @@ export class RuntimeDataGenerator {
105106
getOptimizations(stylesInUse: Set<Style>): Array<OptimizationEntry> {
106107
let optimizations = new Array<OptimizationEntry>();
107108
for (let style of stylesInUse) {
108-
if (this.styleMapping.isStyledAfterOptimization({name: "class", value: this.cssClass(style)})) {
109+
let attr = {name: "class", value: this.cssClass(style)};
110+
if (this.styleMapping.isStyledAfterOptimization(attr)) {
109111
optimizations.push([this.outputClassIndex(style), this.styleIndex(style)]);
110112
continue;
111113
}
114+
115+
if (this.styleMapping.replacedAttributes.containsKey(attr)) {
116+
let replacedWith = this.styleMapping.replacedAttributes.getValue(attr)!;
117+
optimizations.push([this.outputClassIndex(replacedWith.value), this.styleIndex(style)]);
118+
continue;
119+
}
120+
121+
if (this.styleMapping.linkedAttributes.containsKey(attr)) {
122+
let links = this.styleMapping.linkedAttributes.getValue(attr);
123+
for (let link of links) {
124+
let exceptions = link.unless.map(u => this.sourceClassIndex((<SimpleAttribute>u).value));
125+
let expr: AndStyleExpression = [Operator.AND, this.styleIndex(style), [Operator.NOT, [Operator.OR, ...exceptions]]];
126+
optimizations.push([this.outputClassIndex(link.to.value), expr]);
127+
}
128+
}
112129
}
113130
return optimizations;
114131
}

Diff for: packages/@css-blocks/ember-app/src/brocolli-plugin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ export class CSSBlocksApplicationPlugin extends Filter {
4646
let analyzer = new EmberAnalyzer(factory);
4747
// TODO: Make this configurable from the ember app.
4848
let optimizerOptions = {
49-
enabled: false,
49+
enabled: true,
5050
rewriteIdents: {
5151
id: false,
5252
class: true,
5353
omitIdents: {
5454
class: [], // TODO: scan css files for other classes in use.
5555
},
5656
},
57-
removeUnusedStyles: true,
57+
removeUnusedStyles: false,
5858
mergeDeclarations: false,
5959
};
6060
let optimizer = new Optimizer(optimizerOptions, analyzer.optimizationOptions);

0 commit comments

Comments
 (0)