Skip to content

Commit 183dc2f

Browse files
committed
feat: Add BlockCompiler method for compiling a definition file.
1 parent 423071b commit 183dc2f

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { postcss } from "opticss";
2+
3+
import { Block } from "../BlockTree";
4+
import { ResolvedConfiguration } from "../configuration";
5+
6+
export const INLINE_DEFINITION_FILE = Symbol("Inline Definition");
7+
export class BlockDefinitionCompiler {
8+
postcss: typeof postcss;
9+
config: ResolvedConfiguration;
10+
constructor(postcssImpl: typeof postcss, config: ResolvedConfiguration) {
11+
this.postcss = postcssImpl;
12+
this.config = config;
13+
}
14+
15+
compile(_block: Block): postcss.Root {
16+
throw new Error("Method not implemented.");
17+
// return postcss.root();
18+
}
19+
20+
insertReference(_css: postcss.Root, _definitionPath: string) {
21+
throw new Error("Method not implemented.");
22+
}
23+
insertInlineReference(_css: postcss.Root, _definition: postcss.Root) {
24+
throw new Error("Method not implemented.");
25+
}
26+
}

packages/@css-blocks/core/src/BlockCompiler/index.ts

+50
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,68 @@ import {
1515
resolveConfiguration,
1616
} from "../configuration";
1717

18+
import { BlockDefinitionCompiler, INLINE_DEFINITION_FILE } from "./BlockDefinitionCompiler";
1819
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+
1934
/**
2035
* Compiler that, given a Block will return a transformed AST
2136
* interface is `BlockParser.parse`.
2237
*/
2338
export class BlockCompiler {
2439
private config: ResolvedConfiguration;
2540
private postcss: typeof postcss;
41+
private definitionCompiler: BlockDefinitionCompiler;
2642

2743
constructor(postcssImpl: typeof postcss, opts?: Options) {
2844
this.config = resolveConfiguration(opts);
2945
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;
3080
}
3181

3282
compile(block: Block, root: postcss.Root, reservedClassNames: Set<string>): postcss.Root {

0 commit comments

Comments
 (0)