Skip to content

Commit 04a9da2

Browse files
Tim Lindvalltimlindvall
Tim Lindvall
authored andcommitted
feat: Provide ability to override concat settings.
- Useful for apps that need to set a particular order of concatenation for CSS files in the final build artifact (for stuff such as, for instance, reset stylesheets, which are lower priority than CSS Blocks.)
1 parent fec42e4 commit 04a9da2

File tree

2 files changed

+90
-13
lines changed

2 files changed

+90
-13
lines changed

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

+37-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CSSBlocksEmberOptions, ResolvedCSSBlocksEmberOptions, getConfig } from "@css-blocks/ember-utils";
1+
import { BroccoliConcatOptions, CSSBlocksEmberOptions, ResolvedCSSBlocksEmberOptions, getConfig } from "@css-blocks/ember-utils";
22
import broccoliConcat = require("broccoli-concat");
33
import BroccoliDebug = require("broccoli-debug");
44
import funnel = require("broccoli-funnel");
@@ -198,23 +198,29 @@ const EMBER_ADDON: AddonImplementation<CSSBlocksApplicationAddon> = {
198198
let env = this.env!;
199199

200200
if (type === "css") {
201-
if (!env.isApp) {
201+
if (!env.isApp || env.config.broccoliConcat === "SKIP") {
202202
return tree;
203203
}
204-
// TODO: THIS IS WRONG - FIX THESE HARCODED PATHS THIS IS A BLOCKER AAAUGH
204+
205+
// Merge default concat options with the options provided by the app.
206+
let concatOptions: BroccoliConcatOptions;
207+
if (env.config.broccoliConcat) {
208+
concatOptions = Object.assign(
209+
{},
210+
buildDefaultBroccoliConcatOptions(env),
211+
env.config.broccoliConcat,
212+
);
213+
} else {
214+
concatOptions = buildDefaultBroccoliConcatOptions(env);
215+
}
216+
217+
// Create the concatenated file...
205218
const concatTree = broccoliConcat(
206219
tree,
207-
{
208-
inputFiles: ["assets/css-blocks.css", "assets/@css-blocks-fixtures-v2/ember-app.css"],
209-
outputFile: "assets/@css-blocks-fixtures-v2/ember-app.css",
210-
sourceMapConfig: {
211-
enabled: true,
212-
extensions: ["css"],
213-
inline: true,
214-
mapCommentType: "block",
215-
},
216-
},
220+
concatOptions,
217221
);
222+
223+
// Then overwrite the original file with our final build artifact.
218224
const mergedTree = mergeTrees([tree, concatTree], { overwrite: true });
219225
return new BroccoliDebug(mergedTree, "css-blocks:css-postprocess");
220226
}
@@ -223,6 +229,24 @@ const EMBER_ADDON: AddonImplementation<CSSBlocksApplicationAddon> = {
223229
},
224230
};
225231

232+
/**
233+
* Build a default broccoli-concat config, using given enviroment settings.
234+
* @param env - The addon environment.
235+
* @returns - Default broccoli-concat options, accounting for current env settings.
236+
*/
237+
function buildDefaultBroccoliConcatOptions(env: AddonEnvironment): BroccoliConcatOptions {
238+
return {
239+
inputFiles: ["assets/css-blocks.css", `assets/${env.modulePrefix}.css`],
240+
outputFile: `assets/${env.modulePrefix}.css`,
241+
sourceMapConfig: {
242+
enabled: true,
243+
extensions: ["css"],
244+
inline: true,
245+
mapCommentType: "block",
246+
},
247+
};
248+
}
249+
226250
type MaybeCSSBlocksTree = MaybeCSSBlocksTreePlugin | string;
227251
interface MaybeCSSBlocksTreePlugin {
228252
_inputNodes: Array<MaybeCSSBlocksTree> | undefined;

packages/@css-blocks/ember-utils/src/options.ts

+53
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,63 @@ import type { OptiCSSOptions } from "opticss";
55

66
type Writeable<T> = { -readonly [P in keyof T]: T[P] };
77

8+
/**
9+
* Sourcemap options provided by fast-sourcemap-concat, which is used
10+
* by broccoli-concat to build sourcemaps. This is *not* an exhaustive
11+
* list, so a catch-all is provided for undocumented props if needed.
12+
*/
13+
export interface BroccoliConcatSourcemapOptions {
14+
enabled?: boolean;
15+
extensions?: string[];
16+
inline?: boolean;
17+
mapCommentType?: "block" | "line";
18+
[propName: string]: unknown;
19+
}
20+
21+
/**
22+
* Options provided by broccoli-concat, which the ember-app plugin uses
23+
* to combine CSS files together during the post-process step. You can
24+
* use these options to control the order in which files are concatenated,
25+
* or other options related to concatenation.
26+
*/
27+
export interface BroccoliConcatOptions {
28+
outputFile?: string;
29+
header?: string;
30+
headerFiles?: string[];
31+
/**
32+
* The files to contatenate together during the postprocess step. Do keep
33+
* in mind this you'll have access only to the files in the CSS postprocess
34+
* tree for ember-app. You can use globs if preferred.
35+
*
36+
* Any top-level files that are processed by Ember CLI will likely be stored
37+
* at "styles/<name-of-file>.css". You can use a plugin such as broccoli-debug
38+
* to determine the structure of your styles after processing.
39+
*
40+
* Your CSS Blocks file will be available by default
41+
* at "styles/css-blocks.css", but will vary if the "output" property is set
42+
* in your CSS Blocks options.
43+
*/
44+
inputFiles?: string[];
45+
footerFiles?: string[];
46+
footer?: string;
47+
sourceMapConfig?: BroccoliConcatSourcemapOptions;
48+
allowNone?: boolean;
49+
}
50+
851
export interface CSSBlocksEmberOptions {
952
output?: string;
1053
aliases?: ObjectDictionary<string>;
1154
analysisOpts?: AnalysisOptions;
1255
parserOpts?: Writeable<ParserOptions>;
1356
optimization?: Partial<OptiCSSOptions>;
57+
/**
58+
* Options that control the behavior of broccoli-concat, which is used
59+
* to concatenate CSS files together by ember-app during postprocess.
60+
* If this is set to "SKIP", broccoli-concat will *not* run.
61+
* You'll need to add additional processing to add the CSS Blocks
62+
* compiled content to your final CSS build artifact.
63+
*/
64+
broccoliConcat?: BroccoliConcatOptions | "SKIP";
1465
}
1566

1667
export interface ResolvedCSSBlocksEmberOptions {
@@ -19,12 +70,14 @@ export interface ResolvedCSSBlocksEmberOptions {
1970
analysisOpts: AnalysisOptions;
2071
parserOpts: ParserOptions;
2172
optimization: Partial<OptiCSSOptions>;
73+
broccoliConcat: BroccoliConcatOptions | "SKIP";
2274
}
2375

2476
export function getConfig(root: string, isProduction: boolean, options: CSSBlocksEmberOptions): ResolvedCSSBlocksEmberOptions {
2577
if (!options.aliases) options.aliases = {};
2678
if (!options.analysisOpts) options.analysisOpts = {};
2779
if (!options.optimization) options.optimization = {};
80+
if (!options.broccoliConcat) options.broccoliConcat = {};
2881

2982
if (!options.parserOpts) {
3083
options.parserOpts = config.searchSync(root) || {};

0 commit comments

Comments
 (0)