Source: tasks/sass/compile.js

'use strict';

/**
 * @module tasks/sass/compile
 */

/**
 * taskFactory
 * @param sources {Array}
 * @param options {Object}
 * @description {destination:{String}, includePaths:{Array}, autoPrefix:{Object}}
 * @returns task {Function}
 */
module.exports = function taskFactory(sources, options) {

    return function compile() {

        var gulp = require('gulp');
        var sass = require('gulp-sass');
        var sourceMaps = require('gulp-sourcemaps');
        var postCss = require('gulp-postcss');
        var autoPrefixer = require('autoprefixer');
        var combiner = require('stream-combiner2');
        var destinationsHelper = require('../helpers/build-destinations');
        var destination;

        if (options && options.destination) {
            destination = options.destination;
        }

        var sassOptions = {
            includePaths: options.includePaths || [],
            outputStyle: 'expanded',
            sourceComments: true,
            errLogToConsole: true
        };

        var compile = combiner.obj([
            gulp.src(sources),
            sourceMaps.init(),
            sass(sassOptions),
            postCss([autoPrefixer(options.autoPrefix || {})]),
            sourceMaps.write('.'),
            gulp.dest(function (file) {
                return destinationsHelper.getDestination(file.base, destination);
            })
        ]);

        compile.on('error', console.error.bind(console));

        return compile;
    };
};