Source: tasks/html/minify.js

'use strict';

/**
 * @module tasks/html/minify
 */

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

    return function task() {

        var gulp = require('gulp');
        var minifyHTML = require('gulp-htmlmin');
        var minifyInline = require('gulp-minify-inline');
        var header = require('gulp-header');
        var headerCommentHTML = '<!-- Generated on:' + new Date() + ' -->';
        var destinationsHelper = require('../helpers/build-destinations');
        var destination;
        var minifyHtmlOpts = {};
        var userOptions = {};

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

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

        if (options.minify) {
            userOptions = options.minify;
        }

        var objectAssign = require('object-assign');

        objectAssign(minifyHtmlOpts, {
            collapseWhitespace: true,
            removeComments : true,
            ignoreCustomFragments : options.ignoreCustomFragments || []
        }, userOptions);

        return gulp.src(sources)
            .pipe(minifyHTML(minifyHtmlOpts))
            .pipe(minifyInline({
                jsSelector: 'script.inline-minify',
                mangle: true
            }))
            .pipe(header(headerCommentHTML))
            .pipe(gulp.dest(function (file) {
                return destinationsHelper.getDestination(file.base, destination);
            }));
    };
};