Skip to content

Commit 40a0022

Browse files
authored
fix(ember-cli): Use separate file for CSS staging, merge app.css at end. (#230)
* fix(ember-cli): Use separate file for CSS staging, merge app.css at end. * fix(ember-cli): Use new Broccoli tree branch to store Block styles.
1 parent fd8bb0b commit 40a0022

File tree

4 files changed

+89
-7
lines changed

4 files changed

+89
-7
lines changed

packages/@css-blocks/ember-cli/index.js

+38-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
'use strict';
2+
const fs = require("fs");
3+
const path = require("path");
24

35
const { CSSBlocksAggregate, CSSBlocksAnalyze, Transport } = require("@css-blocks/broccoli");
46
const { GlimmerAnalyzer, GlimmerRewriter } = require("@css-blocks/glimmer");
7+
8+
const BroccoliStew = require("broccoli-stew");
9+
const BroccoliConcat = require("broccoli-concat");
10+
const BroccoliMerge = require("broccoli-merge-trees");
11+
512
const debugGenerator = require("debug");
6-
const path = require("path");
713

814
const DEBUG = debugGenerator("css-blocks:ember-cli");
915

@@ -21,6 +27,8 @@ const NOOP_PLUGIN = { name: 'css-blocks-noop', visitors: {}, visitor: {} };
2127

2228
module.exports = {
2329
name: '@css-blocks/ember-cli',
30+
outputFile: 'app.css',
31+
aggregateFile: 'css-blocks.css',
2432
isDevelopingAddon() { return true; },
2533
transports: new Map(),
2634
_owners: new Set(),
@@ -123,19 +131,25 @@ module.exports = {
123131
this._owners.add(parent);
124132

125133
// Fetch information about the environment we're running in.
126-
let env = this.getEnv(parent);
134+
let env = this.env = this.getEnv(parent);
127135

128136
// Fetch and validate user-provided options.
129137
let options = this._options = this.getOptions(env);
130138

131139
// If the consuming app has explicitly disabled CSS Blocks, exit.
132140
if (options.disabled) { return; }
133141

142+
// Determine the aggregate file that we'll be storing Block styles in
143+
// during the build.
144+
this.aggregateFile = options.output || (env.isEmber ? `css-blocks.css` : "src/ui/styles/css-blocks.css");
145+
134146
// In Ember, we need to inject the CSS Blocks runtime helpers. Only do this in
135147
// the top level addon. `app.import` is not a thing in Glimmer.
136148
// TODO: Pull in as CJS so we don't need to build @css-blocks/glimmer to CJS *and* AMD.
137149
// Blocked by: https://github.com/rwjblue/ember-cli-cjs-transform/issues/72
138150
if (env.isEmber && env.app === parent) {
151+
this.outputFile = env.app.options.outputPaths.app.css.app.slice(1);
152+
139153
env.app.import('node_modules/@css-blocks/glimmer/dist/amd/src/helpers/classnames.js', {
140154
using: [{ transformation: 'amd', as: '@css-blocks/helpers/classnames' }],
141155
resolveFrom: __dirname,
@@ -172,6 +186,27 @@ module.exports = {
172186

173187
},
174188

189+
// At the very end of the build, append our CSS Blocks aggregate file to
190+
// the main `app.css` file. Un-link the destination file first to make sure
191+
// we don't modify the source file if broccoli sym-linked it all the way back.
192+
postprocessTree(name, tree) {
193+
194+
const aggregatorTree = this.env.app.trees.cssblocks;
195+
196+
// If this is not the root app, or the css tree, no-op.
197+
if (this.env.isAddon || name !== 'css' || !aggregatorTree) { return tree; }
198+
199+
DEBUG(`Writing all CSS Blocks output to "${this.outputFile}".`);
200+
let merged = new BroccoliMerge([tree, aggregatorTree], { overwrite: true })
201+
merged = new BroccoliConcat(merged, {
202+
outputFile: this.outputFile,
203+
inputFiles: [ this.outputFile, this.aggregateFile ],
204+
allowNone: true,
205+
});
206+
207+
return new BroccoliMerge([tree, merged], { overwrite: true });
208+
},
209+
175210
getEnv(parent){
176211

177212
// Fetch a reference to the parent app
@@ -251,8 +286,6 @@ module.exports = {
251286
genTreeWrapper(env, options, prev = NOOP) {
252287
const { isEmber, app, parent, rootDir, moduleConfig, modulePrefix } = env;
253288

254-
const outputPath = options.output || (isEmber ? `app.css` : "src/ui/styles/app.css");
255-
256289
// In Ember, we treat every template as an entry point. `BroccoliCSSBlocks` will
257290
// automatically discover all template files if an empty entry array is passed.
258291
const entry = isEmber ? [] : (Array.isArray(options.entry) ? options.entry : [options.entry]);
@@ -276,7 +309,7 @@ module.exports = {
276309
return (tree) => {
277310
if (!tree) { return prev.call(parent, tree); }
278311
tree = new CSSBlocksAnalyze(tree, transport, broccoliOptions);
279-
app.trees.styles = new CSSBlocksAggregate([app.trees.styles, tree], transport, outputPath);
312+
app.trees.cssblocks = new CSSBlocksAggregate([app.trees.cssblocks || app.trees.styles, tree], transport, this.aggregateFile);
280313

281314
// Mad hax for Engines <=0.5.20 support 💩 Right now, engines will throw away the
282315
// tree passed to `treeForAddon` and re-generate it. In order for template rewriting

packages/@css-blocks/ember-cli/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@
2424
"@css-blocks/broccoli": "^0.20.0-beta.7",
2525
"@css-blocks/glimmer": "^0.20.0-beta.7",
2626
"@glimmer/application-pipeline": "^0.11.1",
27+
"broccoli-concat": "^3.7.3",
2728
"broccoli-funnel": "^2.0.1",
28-
"broccoli-merge-trees": "^3.0.0",
29+
"broccoli-merge-trees": "^3.0.2",
2930
"debug": "^3.1.0",
3031
"ember-cli-babel": "^6.16.0",
3132
"ember-cli-htmlbars": "^3.0.0",

packages/@css-blocks/ember-cli/tests/dummy/app/styles/app.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ body {
66
#reset-stylesheet-selector {
77
--reset-stylesheet-selector: applied;
88
color: rgb(102, 51, 153);
9-
}
9+
}

yarn.lock

+48
Original file line numberDiff line numberDiff line change
@@ -2876,6 +2876,23 @@ broccoli-concat@^3.0.1, broccoli-concat@^3.2.2:
28762876
lodash.uniq "^4.2.0"
28772877
walk-sync "^0.3.2"
28782878

2879+
broccoli-concat@^3.7.3:
2880+
version "3.7.3"
2881+
resolved "https://registry.npmjs.org/broccoli-concat/-/broccoli-concat-3.7.3.tgz#0dca01311567ffb13180e6b4eb111824628e4885"
2882+
dependencies:
2883+
broccoli-debug "^0.6.5"
2884+
broccoli-kitchen-sink-helpers "^0.3.1"
2885+
broccoli-plugin "^1.3.0"
2886+
ensure-posix-path "^1.0.2"
2887+
fast-sourcemap-concat "^1.4.0"
2888+
find-index "^1.1.0"
2889+
fs-extra "^4.0.3"
2890+
fs-tree-diff "^0.5.7"
2891+
lodash.merge "^4.3.1"
2892+
lodash.omit "^4.1.0"
2893+
lodash.uniq "^4.2.0"
2894+
walk-sync "^0.3.2"
2895+
28792896
broccoli-config-loader@^1.0.0:
28802897
version "1.0.1"
28812898
resolved "https://registry.npmjs.org/broccoli-config-loader/-/broccoli-config-loader-1.0.1.tgz#d10aaf8ebc0cb45c1da5baa82720e1d88d28c80a"
@@ -2902,6 +2919,17 @@ broccoli-debug@^0.6.1, broccoli-debug@^0.6.2, broccoli-debug@^0.6.3, broccoli-de
29022919
symlink-or-copy "^1.1.8"
29032920
tree-sync "^1.2.2"
29042921

2922+
broccoli-debug@^0.6.5:
2923+
version "0.6.5"
2924+
resolved "https://registry.npmjs.org/broccoli-debug/-/broccoli-debug-0.6.5.tgz#164a5cdafd8936e525e702bf8f91f39d758e2e78"
2925+
dependencies:
2926+
broccoli-plugin "^1.2.1"
2927+
fs-tree-diff "^0.5.2"
2928+
heimdalljs "^0.2.1"
2929+
heimdalljs-logger "^0.1.7"
2930+
symlink-or-copy "^1.1.8"
2931+
tree-sync "^1.2.2"
2932+
29052933
broccoli-dependency-funnel@^2.0.1:
29062934
version "2.1.0"
29072935
resolved "https://registry.npmjs.org/broccoli-dependency-funnel/-/broccoli-dependency-funnel-2.1.0.tgz#c06c55549c5e92e0bfde4fc8eedc45a8112e5335"
@@ -3031,6 +3059,13 @@ broccoli-merge-trees@^3.0.0:
30313059
broccoli-plugin "^1.3.0"
30323060
merge-trees "^2.0.0"
30333061

3062+
broccoli-merge-trees@^3.0.2:
3063+
version "3.0.2"
3064+
resolved "https://registry.npmjs.org/broccoli-merge-trees/-/broccoli-merge-trees-3.0.2.tgz#f33b451994225522b5c9bcf27d59decfd8ba537d"
3065+
dependencies:
3066+
broccoli-plugin "^1.3.0"
3067+
merge-trees "^2.0.0"
3068+
30343069
broccoli-middleware@^1.2.1:
30353070
version "1.2.1"
30363071
resolved "https://registry.npmjs.org/broccoli-middleware/-/broccoli-middleware-1.2.1.tgz#a21f255f8bfe5a21c2f0fbf2417addd9d24c9436"
@@ -6130,6 +6165,19 @@ fast-sourcemap-concat@^1.3.1:
61306165
source-map-url "^0.3.0"
61316166
sourcemap-validator "^1.1.0"
61326167

6168+
fast-sourcemap-concat@^1.4.0:
6169+
version "1.4.0"
6170+
resolved "https://registry.npmjs.org/fast-sourcemap-concat/-/fast-sourcemap-concat-1.4.0.tgz#122c330d4a2afaff16ad143bc9674b87cd76c8ad"
6171+
dependencies:
6172+
chalk "^2.0.0"
6173+
fs-extra "^5.0.0"
6174+
heimdalljs-logger "^0.1.9"
6175+
memory-streams "^0.1.3"
6176+
mkdirp "^0.5.0"
6177+
source-map "^0.4.2"
6178+
source-map-url "^0.3.0"
6179+
sourcemap-validator "^1.1.0"
6180+
61336181
fastparse@^1.1.1:
61346182
version "1.1.1"
61356183
resolved "https://registry.npmjs.org/fastparse/-/fastparse-1.1.1.tgz#d1e2643b38a94d7583b479060e6c4affc94071f8"

0 commit comments

Comments
 (0)