Skip to content

Commit 2356846

Browse files
Timothy Lindvalltimlindvall
Timothy Lindvall
authored andcommitted
feat: Ember wip integration with dfn compiler.
- Add definition compiler to @css-blocks/ember. - Skip compiling files from node_modules that happen to be imported. - Tweak existing fixtures to validate a couple of importing cases.
1 parent 624cb38 commit 2356846

File tree

8 files changed

+21
-2222
lines changed

8 files changed

+21
-2222
lines changed

packages/@css-blocks/ember/src/index.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as config from "@css-blocks/config";
2-
import { AnalysisOptions, Block, BlockCompiler, BlockFactory, Configuration, NodeJsImporter, Options as ParserOptions, OutputMode, resolveConfiguration } from "@css-blocks/core";
2+
import { AnalysisOptions, Block, BlockCompiler, BlockDefinitionCompiler, BlockFactory, Configuration, INLINE_DEFINITION_FILE, NodeJsImporter, Options as ParserOptions, OutputMode, resolveConfiguration } from "@css-blocks/core";
33
import type { ASTPlugin, ASTPluginEnvironment } from "@glimmer/syntax";
44
import { ObjectDictionary } from "@opticss/util";
55
import BroccoliDebug = require("broccoli-debug");
@@ -112,17 +112,23 @@ class CSSBlocksTemplateCompilerPlugin extends TemplateCompilerPlugin {
112112
}
113113
}
114114
let compiler = new BlockCompiler(postcss, this.parserOpts);
115+
compiler.setDefinitionCompiler(new BlockDefinitionCompiler(postcss, (_b, p) => { return p.replace(".block.css", ".css"); }, this.parserOpts));
115116
for (let block of blocks) {
116117
let outputPath = getOutputPath(block);
118+
// Skip processing if we don't get an output path. This happens for files that
119+
// get referenced in @block from node_modules.
120+
if (outputPath === null) {
121+
continue;
122+
}
117123
blockOutputPaths.set(block, outputPath);
118124
if (!block.stylesheet) {
119125
throw new Error("[internal error] block stylesheet expected.");
120126
}
121-
// TODO generate definition file too1
122-
let compiledAST = compiler.compile(block, block.stylesheet, this.analyzingRewriter.reservedClassNames());
127+
// TODO - allow for inline definitions or files, by user option
128+
let { css: compiledAST } = compiler.compileWithDefinition(block, block.stylesheet, this.analyzingRewriter.reservedClassNames(), INLINE_DEFINITION_FILE);
123129
// TODO disable source maps in production?
124130
let result = compiledAST.toResult({ to: outputPath, map: { inline: true } });
125-
this.output.writeFileSync(outputPath, wrapCSSWithDelimiterComments(block.guid, result.css), "utf8");
131+
this.output.writeFileSync(outputPath, result.css, "utf8");
126132
}
127133
for (let analysis of analyses) {
128134
let analysisOutputPath = analysisPath(analysis.template.relativePath);
@@ -136,23 +142,18 @@ class CSSBlocksTemplateCompilerPlugin extends TemplateCompilerPlugin {
136142
}
137143
}
138144

139-
// This is a placeholder, eventually the block compiler should add this.
140-
function wrapCSSWithDelimiterComments(guid: string, css: string) {
141-
return `/*#css-blocks ${guid}*/\n${css}\n/*#css-blocks end*/\n`;
142-
}
143-
144145
function analysisPath(templatePath: string): string {
145146
let analysisPath = path.parse(templatePath);
146147
delete analysisPath.base;
147148
analysisPath.ext = ".block-analysis.json";
148149
return path.format(analysisPath);
149150
}
150151

151-
function getOutputPath(block: Block): string {
152+
function getOutputPath(block: Block): string | null {
152153
if (isBroccoliTreeIdentifier(block.identifier)) {
153154
return identToPath(block.identifier).replace(".block", "");
154155
} else {
155-
throw new Error("Implement me!");
156+
return null;
156157
}
157158
}
158159

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
:scope {
2+
font-weight: bold;
3+
}

private-packages/fixtures-ember-v2/ember-addon/addon/styles/components/addon-component.block.css

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
@block bold from "../bold.block.css";
2+
13
:scope {
4+
extends: bold;
25
--addon-component-block-scope: applied;
36
color: red;
47
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
/* @block-reference other from "in-repo-engine/styles/components/addon-component.block.css"; */
1+
@block other from "@css-blocks-fixtures-v2/ember-addon/addon/styles/components/addon-component.block.css";
22

33
:scope {
4-
/* extends: other; */
5-
}
4+
extends: other;
5+
}

0 commit comments

Comments
 (0)