Skip to content

Commit 4ac10b9

Browse files
committed
build(): build a -rtl.css file along the css, and compile _every_ sass
files under src/components. It's simpler.
1 parent bc6d2bc commit 4ac10b9

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

ember-cli-build.js

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
'use strict';
12
var fs = require('fs');
23
var path = require('path');
34

45
var mergeTrees = require('broccoli-merge-trees');
56
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
67
var BroccoliSass = require('broccoli-sass');
78
var broccoliAutoprefixer = require('broccoli-autoprefixer');
9+
var cssjanus = require('cssjanus');
810

911
var autoprefixerOptions = require('./build/autoprefixer-options');
1012

1113
module.exports = function(defaults) {
1214
var demoAppCssTree = new BroccoliSass(['src/demo-app'], './demo-app.scss', 'demo-app/demo-app.css');
13-
var componentCssTree = getComponentsCssTree();
15+
var componentCssTree = getCssTree('src/components/', 'components/');
1416
var angularAppTree = new Angular2App(defaults);
1517

1618
return mergeTrees([
@@ -21,27 +23,53 @@ module.exports = function(defaults) {
2123
};
2224

2325

26+
/** Generate RTL CSS files along side regular CSS. */
27+
class BroccoliSassWithRtl extends BroccoliSass {
28+
build() {
29+
// This will output the regular CSS.
30+
super.build();
31+
32+
// We then read that file and output the RTL CSS.
33+
const cssOutputPath = path.join(this.outputPath, this.outputFile);
34+
const cssRtlOutputPath = path.join(this.outputPath,
35+
path.dirname(this.outputFile),
36+
path.basename(this.outputFile, '.css') + '-rtl.css');
37+
console.log(cssOutputPath, cssRtlOutputPath);
38+
fs.writeFileSync(cssRtlOutputPath, cssjanus.transform(fs.readFileSync(cssOutputPath).toString()));
39+
}
40+
}
41+
42+
43+
2444
/** Gets the tree for all of the components' CSS. */
25-
function getComponentsCssTree() {
26-
// Assume that the list of components is all directories in `src/components/`
27-
var componentsSrc = 'src/components/';
28-
var components = fs.readdirSync(componentsSrc)
29-
.filter(file => fs.statSync(path.join(componentsSrc, file)).isDirectory());
30-
31-
var componentCssTrees = components.reduce((trees, component) => {
32-
// We want each individual scss file to be compiled into a corresponding css file
33-
// so that they can be individually included in styleUrls.
34-
var scssFiles = fs.readdirSync(path.join(componentsSrc, component))
35-
.filter(file => path.extname(file) === '.scss')
36-
.map(file => path.basename(file, '.scss'));
37-
38-
return scssFiles.map(fileName => {
39-
return BroccoliSass(
40-
[`src/components/${component}`, 'src/core/style'], // Directories w/ scss sources
41-
`./${fileName}.scss`, // Root scss input file
42-
`components/${component}/${fileName}.css`); // Css output file
43-
}).concat(trees);
44-
}, []);
45-
46-
return broccoliAutoprefixer(mergeTrees(componentCssTrees), autoprefixerOptions);
45+
function getCssTree(source, destination) {
46+
function walk(dir) {
47+
const dirs = fs.readdirSync(dir)
48+
.filter(file => fs.statSync(path.join(dir, file)).isDirectory());
49+
if (!dirs.length) {
50+
return [dir];
51+
}
52+
53+
return dirs.reduce((previous, current) => {
54+
return previous.concat(walk(path.join(dir, current)));
55+
}, [dir]);
56+
}
57+
58+
const trees = walk(source)
59+
.reduce((previous, current) => {
60+
const dir = path.join(destination, current.substr(source.length));
61+
const scssFiles = fs.readdirSync(current)
62+
.filter(file => path.extname(file) === '.scss')
63+
.map(file => path.basename(file, '.scss'));
64+
65+
return scssFiles.map(filename => {
66+
return new BroccoliSassWithRtl(
67+
[current, 'src/core/style'],
68+
path.join('.', filename + '.scss'),
69+
path.join(dir, filename + '.css')
70+
);
71+
}).concat(previous);
72+
}, []);
73+
74+
return broccoliAutoprefixer(mergeTrees(trees), autoprefixerOptions);
4775
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
},
2020
"dependencies": {
2121
"angular2": "2.0.0-beta.0",
22+
"cssjanus": "^1.1.2",
2223
"es6-promise": "^3.0.2",
2324
"es6-shim": "^0.33.3",
2425
"reflect-metadata": "0.1.2",

0 commit comments

Comments
 (0)