Search code examples
broccolijspostcssangular-cli

How do I integrate PostCSS into Broccoli angular-cli Build?


It seems like this build script is running but the CSS that is output is not minified or auto prefixed. I am attempting to compile SASS, then run the output through Post CSS with Broccoli and the angular-cli. I figure maybe some ember-cli folks could help as well. What am I doing wrong?

The build outputs this in terminal:

Slowest Trees                                 | Total               
----------------------------------------------+---------------------
BroccoliTypeScriptCompiler                    | 1274ms              
vendor                                        | 502ms               
PostcssFilter | 465ms        

but the CSS is the same as if it were output from SASS, not Post CSS.

Here is my angular-cli-build.js:

'use strict';
/* global require, module */


const Angular2App = require('angular-cli/lib/broccoli/angular2-app');
const compileSass = require('broccoli-sass');
const compileCSS = require('broccoli-postcss');
const cssnext = require('postcss-cssnext');
const cssnano = require('cssnano');
const mergeTrees = require('broccoli-merge-trees');
const Funnel = require('broccoli-funnel');
const _ = require('lodash');
const glob = require('glob');


var options =  {
  plugins: [
    {
      module: cssnext,
      options: {
          browsers: ['> 1%'],
          warnForDuplicates: false
      }
    },
    {
      module: cssnano,
      options: {
          safe: true,
          sourcemap: true
      }
    }
  ]
};


module.exports = function(defaults) {

  let sourceDir = 'src';
  let appTree = new Angular2App(defaults, {
      sourceDir: sourceDir,
      sassCompiler: {
        includePaths: [
          'src/style'
        ]
      },
      vendorNpmFiles: [
        'systemjs/dist/system-polyfills.js',
        'systemjs/dist/system.src.js',
        'zone.js/dist/*.js',
        'es6-shim/es6-shim.js',
        'reflect-metadata/*.js',
        'reflect-metadata/*.js.map',
        'rxjs/**/*.js',
        '@angular/**/*.js',
        'rxjs/**/*.js.map',
        '@angular/**/*.js.map',
        'd3/d3.js',
        'three/build/three.js',
        'three/examples/js/postprocessing/*.js',
        'three/examples/js/shaders/*.js'
      ]
    });


    let sass = mergeTrees(_.map(glob.sync('src/**/*.scss'), function(sassFile) {
        sassFile = sassFile.replace('src/', '');
        return compileSass(['src'], sassFile, sassFile.replace(/.scss$/, '.css'));
    }));

    let css = compileCSS(sass, options);

    return mergeTrees([sass, css, appTree], { overwrite: true });
};

Solution

  • It was the order of operations, my trees were not overwriting appTree!

    This works! return mergeTrees([appTree, sass, css], { overwrite: true });