@@ -4,7 +4,7 @@ import { postcss } from "opticss";
4
4
5
5
import * as cssBlocks from "../src" ;
6
6
import { Block , BlockCompiler , BlockFactory } from "../src" ;
7
- import { BlockDefinitionCompiler } from "../src/BlockCompiler/BlockDefinitionCompiler" ;
7
+ import { BlockDefinitionCompiler , INLINE_DEFINITION_FILE } from "../src/BlockCompiler/BlockDefinitionCompiler" ;
8
8
9
9
import { BEMProcessor } from "./util/BEMProcessor" ;
10
10
import { setupImporting } from "./util/setupImporting" ;
@@ -17,19 +17,30 @@ function clean(text: string): string {
17
17
return lines ( text ) . join ( " " ) . replace ( / \s + / g, " " ) ;
18
18
}
19
19
20
- async function compileBlockWithDefinition ( factory : BlockFactory , blockFilename : string ) : Promise < { block : Block ; cssResult : postcss . Result ; definitionResult : postcss . Result } > {
20
+ async function compileBlockWithDefinition ( factory : BlockFactory , blockFilename : string , definitionFilename ?: string ) : Promise < { block : Block ; cssResult : postcss . Result ; definitionResult : postcss . Result } > ;
21
+ async function compileBlockWithDefinition ( factory : BlockFactory , blockFilename : string , definitionFilename : typeof INLINE_DEFINITION_FILE ) : Promise < { block : Block ; cssResult : postcss . Result ; definitionResult : undefined } > ;
22
+ async function compileBlockWithDefinition ( factory : BlockFactory , blockFilename : string , definitionFilename ?: string | typeof INLINE_DEFINITION_FILE ) : Promise < { block : Block ; cssResult : postcss . Result ; definitionResult : postcss . Result | undefined } > {
21
23
let config = factory . configuration ;
22
24
let importer = config . importer ;
23
25
let block = await factory . getBlock ( importer . identifier ( null , blockFilename , config ) ) ;
24
26
let definitionCompiler = new BlockDefinitionCompiler ( postcss , ( _b , p ) => p . replace ( ".block" , "" ) , config ) ;
25
27
let compiler = new BlockCompiler ( postcss , config ) ;
26
28
compiler . setDefinitionCompiler ( definitionCompiler ) ;
27
- let definitionFilename = blockFilename . replace ( ".block" , ".block.d" ) ;
28
- let { css, definition} = compiler . compileWithDefinition ( block , block . stylesheet ! , new Set ( ) , definitionFilename ) ;
29
- let cssFilename = blockFilename . replace ( ".block" , "" ) ;
30
- let cssResult = css . toResult ( { to : cssFilename } ) ;
31
- let definitionResult = definition . toResult ( { to : definitionFilename } ) ;
32
- return { block, cssResult, definitionResult} ;
29
+ if ( typeof definitionFilename === "undefined" ) {
30
+ definitionFilename = blockFilename . replace ( ".block" , ".block.d" ) ;
31
+ }
32
+ if ( typeof definitionFilename === "symbol" ) {
33
+ let { css} = compiler . compileWithDefinition ( block , block . stylesheet ! , new Set ( ) , definitionFilename ) ;
34
+ let cssFilename = blockFilename . replace ( ".block" , "" ) ;
35
+ let cssResult = css . toResult ( { to : cssFilename } ) ;
36
+ return { block, cssResult, definitionResult : undefined } ;
37
+ } else {
38
+ let { css, definition} = compiler . compileWithDefinition ( block , block . stylesheet ! , new Set ( ) , definitionFilename ) ;
39
+ let cssFilename = blockFilename . replace ( ".block" , "" ) ;
40
+ let cssResult = css . toResult ( { to : cssFilename } ) ;
41
+ let definitionResult = definition . toResult ( { to : definitionFilename } ) ;
42
+ return { block, cssResult, definitionResult} ;
43
+ }
33
44
}
34
45
35
46
@suite ( "Block Factory" )
@@ -75,6 +86,38 @@ export class BlockFactoryTests extends BEMProcessor {
75
86
.foo[size="small"] { block-class: test-block__foo--size-small; block-interface-index: 5 }
76
87
` ) ) ;
77
88
}
89
+ @test async "can generate an inline definition" ( ) {
90
+ let { imports, factory } = setupImporting ( ) ;
91
+ let filename = "test-block.block.css" ;
92
+ imports . registerSource (
93
+ filename ,
94
+ `:scope { color: purple; }
95
+ :scope[large] { font-size: 20px; }
96
+ .foo { float: left; block-alias: foo "another-foo"; }
97
+ .foo[size=small] { font-size: 5px; }` ,
98
+ ) ;
99
+ let { block, cssResult} = await compileBlockWithDefinition ( factory , filename , INLINE_DEFINITION_FILE ) ;
100
+ let css = cssResult . css . replace ( / d a t a : t e x t \/ c s s ; b a s e 6 4 , ( .* ) = = / , "SNIP" ) ;
101
+ let urlData = RegExp . $1 ;
102
+ assert . deepEqual (
103
+ clean ( css ) ,
104
+ clean ( `/*#css-blocks ${ block . guid } */
105
+ .test-block { color: purple; }
106
+ .test-block--large { font-size: 20px; }
107
+ .test-block__foo { float: left; }
108
+ .test-block__foo--size-small { font-size: 5px; }
109
+ /*#blockDefinitionURL=SNIP*/
110
+ /*#css-blocks end*/` ) ,
111
+ ) ;
112
+ assert . deepEqual (
113
+ clean ( Buffer . from ( urlData , "base64" ) . toString ( "utf8" ) ) ,
114
+ clean ( `@block-syntax-version 1;
115
+ :scope { block-id: "${ block . guid } "; block-name: "test-block"; block-class: test-block; block-interface-index: 0 }
116
+ :scope[large] { block-class: test-block--large; block-interface-index: 2 }
117
+ .foo { block-class: test-block__foo; block-interface-index: 3; block-alias: foo another-foo }
118
+ .foo[size="small"] { block-class: test-block__foo--size-small; block-interface-index: 5 }
119
+ ` ) ) ;
120
+ }
78
121
79
122
@test async "can generate a definition with block-global declarations." ( ) {
80
123
let { imports, factory } = setupImporting ( ) ;
0 commit comments