@@ -15,18 +15,68 @@ import {
15
15
resolveConfiguration ,
16
16
} from "../configuration" ;
17
17
18
+ import { BlockDefinitionCompiler , INLINE_DEFINITION_FILE } from "./BlockDefinitionCompiler" ;
18
19
import { ConflictResolver } from "./ConflictResolver" ;
20
+
21
+ export { INLINE_DEFINITION_FILE } from "./BlockDefinitionCompiler" ;
22
+
23
+ export interface CompiledBlockAndDefinition {
24
+ definitionPath : string ;
25
+ css : postcss . Root ;
26
+ definition : postcss . Root ;
27
+ }
28
+
29
+ export interface CompiledBlockAndInlineDefinition {
30
+ definitionPath : typeof INLINE_DEFINITION_FILE ;
31
+ css : postcss . Root ;
32
+ }
33
+
19
34
/**
20
35
* Compiler that, given a Block will return a transformed AST
21
36
* interface is `BlockParser.parse`.
22
37
*/
23
38
export class BlockCompiler {
24
39
private config : ResolvedConfiguration ;
25
40
private postcss : typeof postcss ;
41
+ private definitionCompiler : BlockDefinitionCompiler ;
26
42
27
43
constructor ( postcssImpl : typeof postcss , opts ?: Options ) {
28
44
this . config = resolveConfiguration ( opts ) ;
29
45
this . postcss = postcssImpl ;
46
+ this . definitionCompiler = new BlockDefinitionCompiler ( postcssImpl , this . config ) ;
47
+ }
48
+
49
+ compileWithDefinition ( block : Block , root : postcss . Root , reservedClassNames : Set < string > , definitionPath : typeof INLINE_DEFINITION_FILE ) : CompiledBlockAndInlineDefinition ;
50
+ compileWithDefinition ( block : Block , root : postcss . Root , reservedClassNames : Set < string > , definitionPath : string ) : CompiledBlockAndDefinition ;
51
+ compileWithDefinition ( block : Block , root : postcss . Root , reservedClassNames : Set < string > , definitionPath : string | typeof INLINE_DEFINITION_FILE ) : CompiledBlockAndDefinition | CompiledBlockAndInlineDefinition {
52
+ let css = this . compile ( block , root , reservedClassNames ) ;
53
+ let definition = this . definitionCompiler . compile ( block ) ;
54
+ let result : CompiledBlockAndDefinition | CompiledBlockAndInlineDefinition ;
55
+
56
+ if ( definitionPath === INLINE_DEFINITION_FILE ) {
57
+ this . definitionCompiler . insertInlineReference ( css , definition ) ;
58
+ result = {
59
+ definitionPath,
60
+ css,
61
+ } ;
62
+ } else {
63
+ this . definitionCompiler . insertReference ( css , definitionPath ) ;
64
+ result = {
65
+ definitionPath,
66
+ css,
67
+ definition,
68
+ } ;
69
+ }
70
+
71
+ let startComment = postcss . comment ( { text : `#css-blocks ${ block . guid } ` } ) ;
72
+ startComment . raws . after = "\n" ;
73
+ css . prepend ( startComment ) ;
74
+
75
+ let endComment = postcss . comment ( { text : `#css-blocks end` } ) ;
76
+ endComment . raws . after = "\n" ;
77
+ css . append ( endComment ) ;
78
+
79
+ return result ;
30
80
}
31
81
32
82
compile ( block : Block , root : postcss . Root , reservedClassNames : Set < string > ) : postcss . Root {
0 commit comments