var _ = require('lodash'),
    combine = require('stream-combiner'),
    loadGulpPlugins = require('gulp-load-plugins'),
    CHECK_PLUGINS,
    PROCESS_PLUGINS;

function getPostCSSPlugins(pluginList, pluginOptions) {
  return _.map(pluginList, function requirePluginWithOptions(plugin) {
    var options = _.get(pluginOptions, plugin);
    return require(plugin)(options);
  });
}

CHECK_PLUGINS = [
  'stylelint',
  'reporter-plus/postcss'
];

PROCESS_PLUGINS = [
  // Sass-style convenience
  'postcss-import',
  'postcss-mixins',
  'postcss-nested',
  'postcss-simple-vars',

  // Reduce calcs, when possible
  'postcss-calc',

  // Grid system
  'lost',

  // Sane loops
  'postcss-each',
  'postcss-for',

  // Additional functions
  'postcss-color-function',

  // New selectors
  'postcss-selector-matches',
  'postcss-selector-not',

  // Some niceties
  'postcss-easings',
  'postcss-clearfix',

  // Browser compat
  'autoprefixer',
  'postcss-pseudoelements',
  'postcss-color-rgba-fallback'
];

module.exports = function buildCSS(options) {
  var gulpPlugins = loadGulpPlugins();

  options = _.defaultsDeep({}, options, {
    doCheck: true,
    doProcessing: true,
    doMinify: false,
    doConcat: false,
    doBanner: false,
    doVersioning: false,
    doSourceMaps: true,

    concatName: 'override_me.css',
    banner: '/* Built: ' + Date.now() + ' */\n',
    mapsDir: '.maps',

    postCSSOptions: {
        parser: require('postcss-scss')
      },
    pluginOptions: {
        stylelint: {
            rules: require('../runcoms/rc.stylelint.json')
          },
        'postcss-import': {
            plugins: [
                require('postcss-discard-comments')
              ],
            transform: function (fileContents) {
                // This allows us to use //-style comments in imported files, since
                // PostCSS-import doesn't allow non-css syntaxes in its parser
                return fileContents.replace(/\/\/\s(.*)\n/g, '/* $1 */\n');
              }
          },
        'postcss-simple-vars': {
            silent: true
          }
      },
    minifyOptions: {
        reduceIdents: false,
        mergeIdents: false,
        discardUnused: false,
        zindex: false
      },
    minifyRenameOptions: {
        extname: '.min.css'
      }
  });

  return combine(_.compact([
      // Checking pipeline
      options.doCheck && gulpPlugins.postcss(
          getPostCSSPlugins(CHECK_PLUGINS, options.pluginOptions),
          options.postCSSOptions
        ),

      // Processing pipeline
      options.doSourceMaps && gulpPlugins.sourcemaps.init(),
      options.doProcessing && gulpPlugins.postcss(
          getPostCSSPlugins(PROCESS_PLUGINS, options.pluginOptions),
          options.postCSSOptions
        ),

      // Productionization pipeline
      options.doMinify && gulpPlugins.cssnano(options.minifyOptions),
      options.doConcat && gulpPlugins.concat(options.concatName),
      options.doBanner && gulpPlugins.header(options.banner),
      options.doVersioning && gulpPlugins.rev(),
      options.doMinify && gulpPlugins.rename(options.minifyRenameOptions),
      options.doSourceMaps && gulpPlugins.sourcemaps.write(options.mapsDir)
  ]));
};

@@ -1,12 +1,68 @@
 var 
+path = require('path'),%0A    glob = require('glob'),%0A    
 _ = requ
@@ -72,24 +72,24 @@
 ('lodash'),%0A
-
     combine 
@@ -1140,16 +1140,38 @@
 lugins()
+,%0A      importIDs = %7B%7D
 ;%0A%0A  opt
@@ -1987,16 +1987,16 @@
  parser%0A
-
         
@@ -2063,16 +2063,1132 @@
 */%5Cn');%0A
+              %7D,%0A            resolve: function (id, baseDir, options) %7B%0A                var files,%0A                    pattern = id,%0A                    globOptions = %7B%0A                        cwd: options.path%5B0%5D,%0A                        nosort: true%0A                      %7D,%0A                    bowerSearch;%0A%0A                if (importIDs%5Bid%5D) %7B%0A                  return importIDs%5Bid%5D;%0A                %7D%0A%0A                // Look for a bower package%0A                bowerSearch = /%5E(.+):(.+)$/.exec(id);%0A%0A                if (!!bowerSearch) %7B%0A                  pattern = bowerSearch%5B2%5D;%0A                  globOptions.cwd = path.join(options.root, 'bower_components', bowerSearch%5B1%5D);%0A                %7D%0A%0A                files = glob.sync(path.join('**', pattern), globOptions);%0A%0A                if (files.length) %7B%0A                  files = _.map(files, function addRoot(filepath) %7B%0A                    return path.join(globOptions.cwd, filepath);%0A                  %7D);%0A%0A                  importIDs%5Bid%5D = files;%0A%0A                  return files;%0A                %7D else %7B%0A                  return %5B%5D;%0A                %7D%0A
         

Use globs and bower deps to resolve css imports