@@ -5,6 +5,7 @@ const { CSSBlocksAggregate, CSSBlocksAnalyze, Transport } = require("@css-blocks
5
5
const { GlimmerAnalyzer, GlimmerRewriter } = require ( "@css-blocks/glimmer" ) ;
6
6
const { NodeJsImporter, BlockFactory } = require ( "@css-blocks/core" ) ;
7
7
const config = require ( '@css-blocks/config' ) ;
8
+ const merge = require ( "lodash.merge" ) ;
8
9
9
10
const BroccoliConcat = require ( "broccoli-concat" ) ;
10
11
const BroccoliFunnel = require ( "broccoli-funnel" ) ;
@@ -31,6 +32,30 @@ const NOOP_PLUGIN = {
31
32
cacheKey : ( ) => { return 1 ; }
32
33
} ;
33
34
35
+ class IDAllocator {
36
+ constructor ( startValue , defaultMaxCount ) {
37
+ this . modules = new Map ( ) ;
38
+ this . startValue = startValue ;
39
+ this . defaultMaxCount = defaultMaxCount ;
40
+ }
41
+ allocateRange ( maxCount = undefined ) {
42
+ maxCount = maxCount || this . defaultMaxCount ;
43
+ let startValue = this . startValue ;
44
+ this . startValue += maxCount ;
45
+ return { startValue, maxCount} ;
46
+ }
47
+ getRangeForModuleAndType ( mod , type ) {
48
+ if ( ! this . modules . has ( mod ) ) {
49
+ this . modules . set ( mod , new Map ( ) ) ;
50
+ }
51
+ let ranges = this . modules . get ( mod ) ;
52
+ if ( ! ranges . has ( type ) ) {
53
+ ranges . set ( type , this . allocateRange ( ) ) ;
54
+ }
55
+ return ranges . get ( type ) ;
56
+ }
57
+ }
58
+
34
59
module . exports = {
35
60
name : '@css-blocks/ember-cli' ,
36
61
outputFile : 'app.css' ,
@@ -187,10 +212,10 @@ module.exports = {
187
212
parent . preprocessTree = ( type , tree ) => origPreprocessTree ( type , type === "css" ? withoutCssBlockFiles ( tree ) : tree ) ;
188
213
189
214
// Analyze all templates and block files from `/app` in addons.
190
- parent . treeForApp = this . genTreeWrapper ( env , options , parent . treeForApp ) ;
215
+ parent . treeForApp = this . genTreeWrapper ( env , options , 'addonApp' , parent . treeForApp ) ;
191
216
192
217
// Analyze all templates and block files from `/addon` in addons.
193
- parent . treeForAddon = this . genTreeWrapper ( env , options , parent . treeForAddon ) ;
218
+ parent . treeForAddon = this . genTreeWrapper ( env , options , 'addon' , parent . treeForAddon ) ;
194
219
195
220
// Analyze all templates and block files from `/app` in Ember apps.
196
221
// Analyze all templates and block files from `/src` in Glimmer apps.
@@ -204,7 +229,7 @@ module.exports = {
204
229
let packageJsonTree = BroccoliFunnel ( env . rootDir , { include : [ "package.json" ] } ) ;
205
230
tree = BroccoliMerge ( [ tree , packageJsonTree ] ) ;
206
231
}
207
- parent . trees [ treeName ] = this . genTreeWrapper ( env , options ) ( tree ) ;
232
+ parent . trees [ treeName ] = this . genTreeWrapper ( env , options , 'app' ) ( tree ) ;
208
233
}
209
234
210
235
} ,
@@ -271,12 +296,14 @@ module.exports = {
271
296
272
297
getOptions ( env ) {
273
298
274
- let { isEmber, app, rootDir } = env ;
299
+ let { isEmber, app, rootDir, modulePrefix } = env ;
300
+
301
+ if ( ! app . options [ "css-blocks" ] ) {
302
+ app . options [ "css-blocks" ] = { } ;
303
+ }
275
304
276
305
// Get CSS Blocks options provided by the application, if present.
277
- const options = app . options [ "css-blocks" ]
278
- ? app . options [ "css-blocks" ] // Do not clone! Contains non-json-safe data.
279
- : { } ;
306
+ const options = app . options [ "css-blocks" ] // Do not clone! Contains non-json-safe data.
280
307
options . aliases || ( options . aliases = { } ) ;
281
308
options . analysisOpts || ( options . analysisOpts = { } ) ;
282
309
options . optimization || ( options . optimization = { } ) ;
@@ -289,7 +316,9 @@ module.exports = {
289
316
options . parserOpts . importer = options . parserOpts . importer || new NodeJsImporter ( options . aliases ) ;
290
317
291
318
// Optimization is always disabled for now, until we get project-wide analysis working.
292
- options . optimization . enabled = false ;
319
+ if ( typeof options . optimization . enabled === "undefined" ) {
320
+ options . optimization . enabled = app . isProduction ;
321
+ }
293
322
294
323
// Update parserOpts to include the absolute path to our application code directory.
295
324
if ( ! options . parserOpts . rootDir ) {
@@ -332,7 +361,7 @@ module.exports = {
332
361
} ;
333
362
} ,
334
363
335
- genTreeWrapper ( env , options , prev = NOOP ) {
364
+ genTreeWrapper ( env , options , type , prev = NOOP ) {
336
365
const { isEmber, app, parent, rootDir, moduleConfig, modulePrefix } = env ;
337
366
338
367
// In Ember, we treat every template as an entry point. `BroccoliCSSBlocks` will
@@ -347,11 +376,18 @@ module.exports = {
347
376
let analyzer = new GlimmerAnalyzer ( new BlockFactory ( options . parserOpts ) , options . analysisOpts , moduleConfig ) ;
348
377
analyzer . transport = transport ;
349
378
379
+ if ( ! this . idAllocator ) {
380
+ let identifiers = options . optimization . identifiers || { } ;
381
+ this . idAllocator = new IDAllocator ( identifiers . startValue || 1 , identifiers . maxCount || 500 ) ;
382
+ }
383
+ let identifiers = this . idAllocator . getRangeForModuleAndType ( this . parent , type ) ;
384
+ let optimizationOptions = merge ( { } , options . optimization , { identifiers} ) ;
385
+ DEBUG ( `Optimization is ${ optimizationOptions . enabled ? 'enabled' : 'disabled' } . Identifier allocation for ${ modulePrefix } /${ type } is ${ identifiers . startValue } - ${ identifiers . startValue + identifiers . maxCount - 1 } ` ) ;
350
386
const broccoliOptions = {
351
387
analyzer,
352
388
entry,
353
389
output : options . output ,
354
- optimization : options . optimization ,
390
+ optimization : optimizationOptions ,
355
391
root : rootDir ,
356
392
} ;
357
393
0 commit comments