1
1
'use strict' ;
2
+ const fs = require ( "fs" ) ;
3
+ const path = require ( "path" ) ;
2
4
3
5
const { CSSBlocksAggregate, CSSBlocksAnalyze, Transport } = require ( "@css-blocks/broccoli" ) ;
4
6
const { GlimmerAnalyzer, GlimmerRewriter } = require ( "@css-blocks/glimmer" ) ;
7
+
8
+ const BroccoliStew = require ( "broccoli-stew" ) ;
9
+ const BroccoliConcat = require ( "broccoli-concat" ) ;
10
+ const BroccoliMerge = require ( "broccoli-merge-trees" ) ;
11
+
5
12
const debugGenerator = require ( "debug" ) ;
6
- const path = require ( "path" ) ;
7
13
8
14
const DEBUG = debugGenerator ( "css-blocks:ember-cli" ) ;
9
15
@@ -21,6 +27,8 @@ const NOOP_PLUGIN = { name: 'css-blocks-noop', visitors: {}, visitor: {} };
21
27
22
28
module . exports = {
23
29
name : '@css-blocks/ember-cli' ,
30
+ outputFile : 'app.css' ,
31
+ aggregateFile : 'css-blocks.css' ,
24
32
isDevelopingAddon ( ) { return true ; } ,
25
33
transports : new Map ( ) ,
26
34
_owners : new Set ( ) ,
@@ -123,19 +131,25 @@ module.exports = {
123
131
this . _owners . add ( parent ) ;
124
132
125
133
// Fetch information about the environment we're running in.
126
- let env = this . getEnv ( parent ) ;
134
+ let env = this . env = this . getEnv ( parent ) ;
127
135
128
136
// Fetch and validate user-provided options.
129
137
let options = this . _options = this . getOptions ( env ) ;
130
138
131
139
// If the consuming app has explicitly disabled CSS Blocks, exit.
132
140
if ( options . disabled ) { return ; }
133
141
142
+ // Determine the aggregate file that we'll be storing Block styles in
143
+ // during the build.
144
+ this . aggregateFile = options . output || ( env . isEmber ? `css-blocks.css` : "src/ui/styles/css-blocks.css" ) ;
145
+
134
146
// In Ember, we need to inject the CSS Blocks runtime helpers. Only do this in
135
147
// the top level addon. `app.import` is not a thing in Glimmer.
136
148
// TODO: Pull in as CJS so we don't need to build @css-blocks/glimmer to CJS *and* AMD.
137
149
// Blocked by: https://github.com/rwjblue/ember-cli-cjs-transform/issues/72
138
150
if ( env . isEmber && env . app === parent ) {
151
+ this . outputFile = env . app . options . outputPaths . app . css . app . slice ( 1 ) ;
152
+
139
153
env . app . import ( 'node_modules/@css-blocks/glimmer/dist/amd/src/helpers/classnames.js' , {
140
154
using : [ { transformation : 'amd' , as : '@css-blocks/helpers/classnames' } ] ,
141
155
resolveFrom : __dirname ,
@@ -172,6 +186,27 @@ module.exports = {
172
186
173
187
} ,
174
188
189
+ // At the very end of the build, append our CSS Blocks aggregate file to
190
+ // the main `app.css` file. Un-link the destination file first to make sure
191
+ // we don't modify the source file if broccoli sym-linked it all the way back.
192
+ postprocessTree ( name , tree ) {
193
+
194
+ const aggregatorTree = this . env . app . trees . cssblocks ;
195
+
196
+ // If this is not the root app, or the css tree, no-op.
197
+ if ( this . env . isAddon || name !== 'css' || ! aggregatorTree ) { return tree ; }
198
+
199
+ DEBUG ( `Writing all CSS Blocks output to "${ this . outputFile } ".` ) ;
200
+ let merged = new BroccoliMerge ( [ tree , aggregatorTree ] , { overwrite : true } )
201
+ merged = new BroccoliConcat ( merged , {
202
+ outputFile : this . outputFile ,
203
+ inputFiles : [ this . outputFile , this . aggregateFile ] ,
204
+ allowNone : true ,
205
+ } ) ;
206
+
207
+ return new BroccoliMerge ( [ tree , merged ] , { overwrite : true } ) ;
208
+ } ,
209
+
175
210
getEnv ( parent ) {
176
211
177
212
// Fetch a reference to the parent app
@@ -251,8 +286,6 @@ module.exports = {
251
286
genTreeWrapper ( env , options , prev = NOOP ) {
252
287
const { isEmber, app, parent, rootDir, moduleConfig, modulePrefix } = env ;
253
288
254
- const outputPath = options . output || ( isEmber ? `app.css` : "src/ui/styles/app.css" ) ;
255
-
256
289
// In Ember, we treat every template as an entry point. `BroccoliCSSBlocks` will
257
290
// automatically discover all template files if an empty entry array is passed.
258
291
const entry = isEmber ? [ ] : ( Array . isArray ( options . entry ) ? options . entry : [ options . entry ] ) ;
@@ -276,7 +309,7 @@ module.exports = {
276
309
return ( tree ) => {
277
310
if ( ! tree ) { return prev . call ( parent , tree ) ; }
278
311
tree = new CSSBlocksAnalyze ( tree , transport , broccoliOptions ) ;
279
- app . trees . styles = new CSSBlocksAggregate ( [ app . trees . styles , tree ] , transport , outputPath ) ;
312
+ app . trees . cssblocks = new CSSBlocksAggregate ( [ app . trees . cssblocks || app . trees . styles , tree ] , transport , this . aggregateFile ) ;
280
313
281
314
// Mad hax for Engines <=0.5.20 support 💩 Right now, engines will throw away the
282
315
// tree passed to `treeForAddon` and re-generate it. In order for template rewriting
0 commit comments