1
- import { Analyzer , Block , BlockFactory , Options as CSSBlocksOptions , SerializedSourceAnalysis , resolveConfiguration } from "@css-blocks/core" ;
1
+ import { Analyzer , Block , BlockFactory , Options as CSSBlocksOptions , SerializedSourceAnalysis , resolveConfiguration , BlockCompiler } from "@css-blocks/core" ;
2
2
import { BroccoliTreeImporter , EmberAnalysis , TEMPLATE_TYPE , pathToIdent } from "@css-blocks/ember-support" ;
3
3
import { TemplateIntegrationOptions } from "@opticss/template-api" ;
4
4
import mergeTrees = require( "broccoli-merge-trees" ) ;
@@ -8,6 +8,8 @@ import type { PluginOptions } from "broccoli-plugin/dist/interfaces";
8
8
import debugGenerator from "debug" ;
9
9
import * as FSTree from "fs-tree-diff" ;
10
10
import { Optimizer , postcss } from "opticss" ;
11
+ import * as path from "path" ;
12
+ import { unionInto } from "@opticss/util" ;
11
13
12
14
const debug = debugGenerator ( "css-blocks:ember-app" ) ;
13
15
@@ -71,19 +73,8 @@ export class CSSBlocksApplicationPlugin extends Filter {
71
73
mergeDeclarations : false ,
72
74
} ;
73
75
let optimizer = new Optimizer ( optimizerOptions , analyzer . optimizationOptions ) ;
76
+ let blocksUsed = new Set < Block > ( ) ;
74
77
for ( let entry of entries ) {
75
- let ident = pathToIdent ( entry . relativePath ) ;
76
- if ( entry . relativePath . endsWith ( ".compiledblock.css" ) ) {
77
- debug ( `Parsing precompiled block: ${ entry . relativePath } ` ) ;
78
- let block : Block ;
79
- block = await factory . getBlock ( ident ) ;
80
- debug ( `Got block: ${ block . identifier } ` ) ;
81
- optimizer . addSource ( {
82
- filename : entry . relativePath ,
83
- content : block . stylesheet ! . toResult ( { to : entry . relativePath } ) ,
84
- } ) ;
85
- blocks . push ( block ) ;
86
- }
87
78
if ( entry . relativePath . endsWith ( ".block-analysis.json" ) ) {
88
79
debug ( `Processing analysis: ${ entry . relativePath } ` ) ;
89
80
let serializedAnalysis : SerializedSourceAnalysis < TEMPLATE_TYPE > = JSON . parse ( this . input . readFileSync ( entry . relativePath , "utf8" ) ) ;
@@ -92,11 +83,39 @@ export class CSSBlocksApplicationPlugin extends Filter {
92
83
serializedAnalysis . blocks [ blockId ] = pathToIdent ( serializedAnalysis . blocks [ blockId ] ) ;
93
84
}
94
85
let analysis = await EmberAnalysis . deserializeSource ( serializedAnalysis , factory , analyzer ) ;
86
+ unionInto ( blocksUsed , analysis . transitiveBlockDependencies ( ) ) ;
95
87
optimizer . addAnalysis ( analysis . forOptimizer ( config ) ) ;
96
88
}
97
89
}
90
+ let compiler = new BlockCompiler ( postcss , config ) ;
91
+ for ( let block of blocksUsed ) {
92
+ let content : postcss . Result ;
93
+ let filename = importer . debugIdentifier ( block . identifier , config ) ;
94
+ if ( block . precompiledStylesheet ) {
95
+ debug ( `Optimizing precompiled stylesheet for ${ filename } ` ) ;
96
+ content = block . precompiledStylesheet . toResult ( ) ;
97
+ } else {
98
+ debug ( `Compiling stylesheet for optimization of ${ filename } ` ) ;
99
+ // XXX Do we need to worry about reservedClassnames here?
100
+ content = compiler . compile ( block , block . stylesheet ! , new Set ( ) ) . toResult ( ) ;
101
+ }
102
+ optimizer . addSource ( {
103
+ content,
104
+ filename,
105
+ } ) ;
106
+ }
98
107
debug ( `Loaded ${ blocks . length } blocks.` ) ;
99
108
debug ( `Loaded ${ optimizer . analyses . length } analyses.` ) ;
109
+ let cssFileName = `${ this . appName } /styles/css-blocks.css` ;
110
+ let sourceMapFileName = `${ this . appName } /styles/css-blocks.css.map` ;
111
+ let optLogFileName = `${ this . appName } /styles/css-blocks.optimization.log` ;
112
+ let optimizationResult = await optimizer . optimize ( cssFileName ) ;
113
+ debug ( `Optimized CSS. There were ${ optimizationResult . actions . performed . length } optimizations performed.` ) ;
114
+ this . output . mkdirSync ( path . dirname ( cssFileName ) , { recursive : true } ) ;
115
+ this . output . writeFileSync ( cssFileName , optimizationResult . output . content . toString ( ) , "utf8" ) ;
116
+ this . output . writeFileSync ( sourceMapFileName , optimizationResult . output . sourceMap ?. toString ( ) , "utf8" ) ;
117
+ this . output . writeFileSync ( optLogFileName , optimizationResult . actions . logStrings ( ) . join ( "\n" ) , "utf8" ) ;
118
+ debug ( "Wrote css, sourcemap, and optimization log." ) ;
100
119
101
120
this . output . writeFileSync (
102
121
`${ this . appName } /services/-css-blocks-data.js` ,
0 commit comments