1
- import { Block , BlockCompiler , BlockFactory , Options as CSSBlocksOptions , SerializedSourceAnalysis , resolveConfiguration } from "@css-blocks/core" ;
2
- import { BroccoliTreeImporter , EmberAnalysis , EmberAnalyzer , TEMPLATE_TYPE , pathToIdent } from "@css-blocks/ember-support" ;
1
+ import { Block , BlockCompiler , BlockFactory , SerializedSourceAnalysis , resolveConfiguration } from "@css-blocks/core" ;
2
+ import { BroccoliTreeImporter , EmberAnalysis , EmberAnalyzer , ResolvedCSSBlocksEmberOptions , TEMPLATE_TYPE , pathToIdent } from "@css-blocks/ember-support" ;
3
3
import { unionInto } from "@opticss/util" ;
4
4
import mergeTrees = require( "broccoli-merge-trees" ) ;
5
5
import type { InputNode } from "broccoli-node-api" ;
@@ -8,7 +8,7 @@ import Plugin = require("broccoli-plugin");
8
8
import type { PluginOptions } from "broccoli-plugin/dist/interfaces" ;
9
9
import debugGenerator from "debug" ;
10
10
import * as FSTree from "fs-tree-diff" ;
11
- import { Optimizer , postcss } from "opticss" ;
11
+ import { OptiCSSOptions , Optimizer , postcss } from "opticss" ;
12
12
import * as path from "path" ;
13
13
14
14
import { RuntimeDataGenerator } from "./RuntimeDataGenerator" ;
@@ -18,8 +18,8 @@ const debug = debugGenerator("css-blocks:ember-app");
18
18
export class CSSBlocksApplicationPlugin extends Filter {
19
19
appName : string ;
20
20
previousSourceTree : FSTree ;
21
- cssBlocksOptions : CSSBlocksOptions ;
22
- constructor ( appName : string , inputNodes : InputNode [ ] , cssBlocksOptions : CSSBlocksOptions , options ?: PluginOptions ) {
21
+ cssBlocksOptions : ResolvedCSSBlocksEmberOptions ;
22
+ constructor ( appName : string , inputNodes : InputNode [ ] , cssBlocksOptions : ResolvedCSSBlocksEmberOptions , options ?: PluginOptions ) {
23
23
super ( mergeTrees ( inputNodes ) , options || { } ) ;
24
24
this . appName = appName ;
25
25
this . previousSourceTree = new FSTree ( ) ;
@@ -39,24 +39,13 @@ export class CSSBlocksApplicationPlugin extends Filter {
39
39
} else {
40
40
this . previousSourceTree = currentFSTree ;
41
41
}
42
- let config = resolveConfiguration ( this . cssBlocksOptions ) ;
42
+ let config = resolveConfiguration ( this . cssBlocksOptions . parserOpts ) ;
43
43
let importer = new BroccoliTreeImporter ( this . input , null , config . importer ) ;
44
44
config = resolveConfiguration ( { importer} , config ) ;
45
45
let factory = new BlockFactory ( config , postcss ) ;
46
- let analyzer = new EmberAnalyzer ( factory ) ;
47
- // TODO: Make this configurable from the ember app.
48
- let optimizerOptions = {
49
- enabled : true ,
50
- rewriteIdents : {
51
- id : false ,
52
- class : true ,
53
- omitIdents : {
54
- class : [ ] , // TODO: scan css files for other classes in use.
55
- } ,
56
- } ,
57
- removeUnusedStyles : true ,
58
- mergeDeclarations : true ,
59
- } ;
46
+ let analyzer = new EmberAnalyzer ( factory , this . cssBlocksOptions . analysisOpts ) ;
47
+ let optimizerOptions = this . cssBlocksOptions . optimization ;
48
+ this . reserveClassnames ( optimizerOptions ) ;
60
49
let optimizer = new Optimizer ( optimizerOptions , analyzer . optimizationOptions ) ;
61
50
let blocksUsed = new Set < Block > ( ) ;
62
51
for ( let entry of entries ) {
@@ -114,6 +103,37 @@ export class CSSBlocksApplicationPlugin extends Filter {
114
103
export const data = ${ serializedData } ;
115
104
` ) ;
116
105
}
106
+
107
+ /**
108
+ * Modifies the options passed in to supply the CSS classnames used in the
109
+ * application to the the list of identifiers that should be omitted by the
110
+ * classname generator.
111
+ */
112
+ reserveClassnames ( optimizerOptions : Partial < OptiCSSOptions > ) : void {
113
+ let rewriteIdents = optimizerOptions . rewriteIdents ;
114
+ let rewriteIdentsFlag : boolean ;
115
+ let omitIdents : Array < string > ;
116
+ if ( typeof rewriteIdents === "boolean" ) {
117
+ rewriteIdentsFlag = rewriteIdents ;
118
+ omitIdents = [ ] ;
119
+ } else if ( typeof rewriteIdents === "undefined" ) {
120
+ rewriteIdentsFlag = true ;
121
+ omitIdents = [ ] ;
122
+ } else {
123
+ rewriteIdentsFlag = rewriteIdents . class ;
124
+ omitIdents = rewriteIdents . omitIdents && rewriteIdents . omitIdents . class || [ ] ;
125
+ }
126
+
127
+ // TODO: scan css files for other classes in use and add them to `omitIdents`.
128
+
129
+ optimizerOptions . rewriteIdents = {
130
+ id : false ,
131
+ class : rewriteIdentsFlag ,
132
+ omitIdents : {
133
+ class : omitIdents ,
134
+ } ,
135
+ } ;
136
+ }
117
137
}
118
138
119
139
/**
0 commit comments