@@ -19,20 +19,19 @@ import {
19
19
} from "@opticss/util" ;
20
20
import { IdentGenerator } from "opticss" ;
21
21
22
- import { Block , Style } from "../Block" ;
23
22
import { BlockFactory } from "../BlockParser" ;
23
+ import { Block , Style } from "../BlockTree" ;
24
24
import { ResolvedConfiguration } from "../configuration" ;
25
25
26
26
import { ElementAnalysis , SerializedElementAnalysis } from "./ElementAnalysis" ;
27
- import { StyleAnalysis } from "./StyleAnalysis" ;
28
27
import { TemplateValidator , TemplateValidatorOptions } from "./validations" ;
29
28
30
29
/**
31
30
* This interface defines a JSON friendly serialization
32
31
* of a {TemplateAnalysis}.
33
32
*/
34
- export interface SerializedTemplateAnalysis < K extends keyof TemplateTypes > {
35
- template : SerializedTemplateInfo < K > ;
33
+ export interface SerializedAnalysis {
34
+ template : SerializedTemplateInfo < keyof TemplateTypes > ;
36
35
blocks : ObjectDictionary < string > ;
37
36
stylesFound : string [ ] ;
38
37
// The numbers stored in each element are an index into a stylesFound;
@@ -48,9 +47,9 @@ export interface SerializedTemplateAnalysis<K extends keyof TemplateTypes> {
48
47
* 2. Call [[addExclusiveStyle addExclusiveStyle(alwaysPresent, ...style)]] for all the styles used that are mutually exclusive on the current html element.
49
48
* 3. Call [[endElement endElement()]] when done adding styles for the current element.
50
49
*/
51
- export class TemplateAnalysis < K extends keyof TemplateTypes > implements StyleAnalysis {
50
+ export class Analysis {
52
51
53
- template : TemplateInfo < K > ;
52
+ template : TemplateInfo < keyof TemplateTypes > ;
54
53
idGenerator : IdentGenerator ;
55
54
56
55
/**
@@ -148,7 +147,7 @@ export class TemplateAnalysis<K extends keyof TemplateTypes> implements StyleAna
148
147
/**
149
148
* @param template The template being analyzed.
150
149
*/
151
- constructor ( template : TemplateInfo < K > , options ?: TemplateValidatorOptions ) {
150
+ constructor ( template : TemplateInfo < keyof TemplateTypes > , options ?: TemplateValidatorOptions ) {
152
151
this . idGenerator = new IdentGenerator ( ) ;
153
152
this . template = template ;
154
153
this . blocks = { } ;
@@ -276,7 +275,7 @@ export class TemplateAnalysis<K extends keyof TemplateTypes> implements StyleAna
276
275
/**
277
276
* Generates a [[SerializedTemplateAnalysis]] for this analysis.
278
277
*/
279
- serialize ( ) : SerializedTemplateAnalysis < K > {
278
+ serialize ( ) : SerializedAnalysis {
280
279
let blocks = { } ;
281
280
let stylesFound : string [ ] = [ ] ;
282
281
let elements : ObjectDictionary < SerializedElementAnalysis > = { } ;
@@ -310,9 +309,8 @@ export class TemplateAnalysis<K extends keyof TemplateTypes> implements StyleAna
310
309
elements [ key ] = el . serialize ( styleIndexes ) ;
311
310
} ) ;
312
311
313
- let t : SerializedTemplateInfo < K > = template ;
314
312
// Return serialized Analysis object.
315
- return { template : t , blocks, stylesFound, elements } ;
313
+ return { template, blocks, stylesFound, elements } ;
316
314
}
317
315
318
316
/**
@@ -321,13 +319,13 @@ export class TemplateAnalysis<K extends keyof TemplateTypes> implements StyleAna
321
319
* @param options The plugin options that are used to parse the blocks.
322
320
* @param postcssImpl The instance of postcss that should be used to parse the block's css.
323
321
*/
324
- static deserialize < K extends keyof TemplateTypes > (
325
- serializedAnalysis : SerializedTemplateAnalysis < K > ,
322
+ static async deserialize (
323
+ serializedAnalysis : SerializedAnalysis ,
326
324
blockFactory : BlockFactory ,
327
- ) : Promise < TemplateAnalysis < K > > {
325
+ ) : Promise < Analysis > {
328
326
let blockNames = Object . keys ( serializedAnalysis . blocks ) ;
329
- let info = TemplateInfoFactory . deserialize < K > ( serializedAnalysis . template ) ;
330
- let analysis = new TemplateAnalysis ( info ) ;
327
+ let info = TemplateInfoFactory . deserialize < keyof TemplateTypes > ( serializedAnalysis . template ) ;
328
+ let analysis = new Analysis ( info ) ;
331
329
let blockPromises = new Array < Promise < { name : string ; block : Block } > > ( ) ;
332
330
blockNames . forEach ( n => {
333
331
let blockIdentifier = serializedAnalysis . blocks [ n ] ;
@@ -336,42 +334,41 @@ export class TemplateAnalysis<K extends keyof TemplateTypes> implements StyleAna
336
334
} ) ;
337
335
blockPromises . push ( promise ) ;
338
336
} ) ;
339
- return Promise . all ( blockPromises ) . then ( values => {
340
-
341
- // Create a temporary block so we can take advantage of `Block.lookup`
342
- // to easily resolve all BlockPaths referenced in the serialized analysis.
343
- // TODO: We may want to abstract this so we're not making a temporary block.
344
- let localScope = new Block ( "analysis-block" , "tmp" ) ;
345
- values . forEach ( o => {
346
- analysis . blocks [ o . name ] = o . block ;
347
- localScope . addBlockReference ( o . name , o . block ) ;
348
- } ) ;
349
- let objects = new Array < Style > ( ) ;
350
- serializedAnalysis . stylesFound . forEach ( s => {
351
- let style = localScope . lookup ( s ) ;
352
- if ( style ) {
353
- objects . push ( style ) ;
354
- } else {
355
- throw new Error ( `Cannot resolve ${ s } to a block style.` ) ;
356
- }
357
- } ) ;
358
-
359
- let elementNames = Object . keys ( serializedAnalysis . elements ) ;
360
- elementNames . forEach ( ( elID ) => {
361
- let data = serializedAnalysis . elements [ elID ] ;
362
- let element = new ElementAnalysis < null , null , null > ( data . sourceLocation || { start : POSITION_UNKNOWN } , undefined , elID ) ;
363
- analysis . elements . set ( elID , element ) ;
364
- } ) ;
337
+ let values = await Promise . all ( blockPromises ) ;
338
+
339
+ // Create a temporary block so we can take advantage of `Block.lookup`
340
+ // to easily resolve all BlockPaths referenced in the serialized analysis.
341
+ // TODO: We may want to abstract this so we're not making a temporary block.
342
+ let localScope = new Block ( "analysis-block" , "tmp" ) ;
343
+ values . forEach ( o => {
344
+ analysis . blocks [ o . name ] = o . block ;
345
+ localScope . addBlockReference ( o . name , o . block ) ;
346
+ } ) ;
347
+ let objects = new Array < Style > ( ) ;
348
+ serializedAnalysis . stylesFound . forEach ( s => {
349
+ let style = localScope . lookup ( s ) ;
350
+ if ( style ) {
351
+ objects . push ( style ) ;
352
+ } else {
353
+ throw new Error ( `Cannot resolve ${ s } to a block style.` ) ;
354
+ }
355
+ } ) ;
365
356
366
- // tslint:disable-next-line:prefer-whatever-to-any
367
- return < TemplateAnalysis < K > > < any > analysis ;
357
+ let elementNames = Object . keys ( serializedAnalysis . elements ) ;
358
+ elementNames . forEach ( ( elID ) => {
359
+ let data = serializedAnalysis . elements [ elID ] ;
360
+ let element = new ElementAnalysis < null , null , null > ( data . sourceLocation || { start : POSITION_UNKNOWN } , undefined , elID ) ;
361
+ analysis . elements . set ( elID , element ) ;
368
362
} ) ;
363
+
364
+ // tslint:disable-next-line:prefer-whatever-to-any
365
+ return analysis ;
369
366
}
370
367
371
- forOptimizer ( config : ResolvedConfiguration ) : OptimizationTemplateAnalysis < K > {
372
- let optAnalysis = new OptimizationTemplateAnalysis < K > ( this . template ) ;
368
+ forOptimizer ( opts : ResolvedConfiguration ) : OptimizationTemplateAnalysis < keyof TemplateTypes > {
369
+ let optAnalysis = new OptimizationTemplateAnalysis < keyof TemplateTypes > ( this . template ) ;
373
370
for ( let element of this . elements . values ( ) ) {
374
- let result = element . forOptimizer ( config ) ;
371
+ let result = element . forOptimizer ( opts ) ;
375
372
optAnalysis . elements . push ( result [ 0 ] ) ;
376
373
}
377
374
return optAnalysis ;
0 commit comments