Source: tasks/javascript/minify.js

'use strict';

/**
 * @module tasks/javascript/minify
 */

/**
 * taskFactory
 * @param sources {Array} glob
 * @param options {Object}
 * @returns {Function}
 * gulp stream
 */
module.exports = function taskFactory(sources, options) {

    return function task() {

        var gulp = require('gulp');
        var uglify = require('gulp-uglify');
        var rename = require('gulp-rename');
        var combiner = require('stream-combiner2');
        var destinationsHelper = require('../helpers/build-destinations');
        var destination;

        if (!options) {
            options = {};
        }

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

        var minify = combiner.obj([
            gulp.src(sources),
            uglify({
                mangle: options.mangle || false
            }),
            rename({
                suffix: options.suffix || ''
            }),
            gulp.dest(function (file) {
                return destinationsHelper.getDestination(file.base, destination);
            })
        ]);

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

        return minify;
    };
};