Skip to content

Commit a2572ed

Browse files
committed
fix: Only merge with app.css if it exists.
If the block filename is specified explicitly or if the app.css file isn't present, then write the block output into the app's styles tree. Then the application code can import it, etc.
1 parent e680ef6 commit a2572ed

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

packages/@css-blocks/ember-app/src/brocolli-plugin.ts

+32-13
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class CSSBlocksApplicationPlugin extends Filter {
8181
}
8282
debug(`Loaded ${blocksUsed.size} blocks.`);
8383
debug(`Loaded ${optimizer.analyses.length} analyses.`);
84-
let cssFileName = cssBlocksOutputFilename(this.appName, this.cssBlocksOptions);
84+
let cssFileName = cssBlocksOutputFilename(this.cssBlocksOptions);
8585
let sourceMapFileName = `${cssFileName}.map`;
8686
let optLogFileName = `${cssFileName}.optimization.log`;
8787
let optimizationResult = await optimizer.optimize(cssFileName);
@@ -153,20 +153,32 @@ export class CSSBlocksStylesProcessorPlugin extends Plugin {
153153
previousSourceTree: FSTree;
154154
cssBlocksOptions: ResolvedCSSBlocksEmberOptions;
155155
constructor(appName: string, cssBlocksOptions: ResolvedCSSBlocksEmberOptions, inputNodes: InputNode[]) {
156-
super(inputNodes);
156+
super(inputNodes, {persistentOutput: true});
157157
this.appName = appName;
158158
this.previousSourceTree = new FSTree();
159159
this.cssBlocksOptions = cssBlocksOptions;
160160
}
161161
async build() {
162+
let mergeIntoAppStyles = true;
162163
if (this.cssBlocksOptions.output) {
163164
// if the output filename is explicitly declared, we don't merge it with
164165
// the application styles.
165-
return;
166+
mergeIntoAppStyles = false;
166167
}
167168
// Read the optimized CSS Blocks styles file, generated previously by the CSSBlocksApplicationPlugin.
168-
let stylesheetPath = cssBlocksOutputFilename(this.appName, this.cssBlocksOptions);
169-
let entries = this.input.entries(".", {globs: [stylesheetPath]});
169+
let stylesheetPath = cssBlocksOutputFilename(this.cssBlocksOptions);
170+
let applicationStylesheetPath = `app/styles/app.css`;
171+
if (!this.input.existsSync(applicationStylesheetPath)) {
172+
mergeIntoAppStyles = false;
173+
debug(`No app/styles/app.css file found. Blocks content is in ${stylesheetPath}. The application should handle it.`);
174+
}
175+
let globs: Array<string>;
176+
if (mergeIntoAppStyles) {
177+
globs = [stylesheetPath, applicationStylesheetPath];
178+
} else {
179+
globs = [stylesheetPath];
180+
}
181+
let entries = this.input.entries(".", {globs});
170182
let currentFSTree = FSTree.fromEntries(entries);
171183
let patch = this.previousSourceTree.calculatePatch(currentFSTree);
172184
if (patch.length === 0) {
@@ -175,19 +187,26 @@ export class CSSBlocksStylesProcessorPlugin extends Plugin {
175187
this.previousSourceTree = currentFSTree;
176188
}
177189

178-
const blocksFileEntry = entries[0];
179-
180190
// And read the application CSS that was previously built by Ember and ignored by CSS Blocks.
181-
const blocksFileContents = this.input.readFileSync(blocksFileEntry.relativePath, { encoding: "utf8" });
182-
const appCssFileContents = this.input.readFileSync("app/styles/app.css", { encoding: "utf8" });
191+
const blocksFileContents = this.input.readFileSync(stylesheetPath, { encoding: "utf8" });
192+
let outputContents: string;
193+
let outputPath: string;
194+
if (mergeIntoAppStyles) {
195+
const appCssFileContents = this.input.readFileSync(applicationStylesheetPath, { encoding: "utf8" });
196+
outputContents = `${appCssFileContents}\n${blocksFileContents}`;
197+
outputPath = applicationStylesheetPath;
198+
} else {
199+
outputContents = blocksFileContents;
200+
outputPath = stylesheetPath;
201+
}
183202

184203
// Now, write out the combined result of the application CSS and CSS Blocks contents.
185-
this.output.mkdirSync("app/styles", { recursive: true });
186-
this.output.writeFileSync("app/styles/app.css", `${appCssFileContents}\n${blocksFileContents}`);
204+
this.output.mkdirSync(path.dirname(outputPath), { recursive: true });
205+
this.output.writeFileSync(outputPath, outputContents);
187206
}
188207
}
189208

190-
function cssBlocksOutputFilename(appName, options: ResolvedCSSBlocksEmberOptions) {
209+
function cssBlocksOutputFilename(options: ResolvedCSSBlocksEmberOptions) {
191210
let outputName = options.output || "css-blocks.css";
192-
return `${appName}/styles/${outputName}`;
211+
return `app/styles/${outputName}`;
193212
}

0 commit comments

Comments
 (0)