Code coverage report for lib/tasks/jsminify.js

Statements: 65.38% (17 / 26)      Branches: 64.29% (9 / 14)      Functions: 33.33% (1 / 3)      Lines: 65.38% (17 / 26)     

All files » lib/tasks/ » jsminify.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58          1     1 94 94   94               94 94         94   94     94       94   94       94 92   94 94                   94    
/*
 * Copyright (c) 2011-2012, Yahoo! Inc.  All rights reserved.
 * Copyrights licensed under the New BSD License.
 * See the accompanying LICENSE file for terms.
 */
var parser = require('uglify-js').parser,
    uglify = require('uglify-js').uglify;
 
exports.task = function (resource, options, done) {
    'use strict';
    options = options || {};
 
    var config = options.config || {},
        comments = [],
        token = '"jsminify task: preserved comment block"',
        reMultiComments = /\/\*![\s\S]*?\*\//g,
        reTokens = new RegExp(token, 'g'),
        source = resource.content,
        ast;
 
    try {
        source = source.replace(reMultiComments, function (comment) {
            comments.push(comment);
            return ';' + token + ';';
        });
 
        ast = parser.parse(source, config.semicolon || false);
 
        Iif (config.mangle) {
            ast = uglify.ast_mangle(ast, config);
        }
        Iif (config.squeeze) {
            ast = uglify.ast_squeeze(ast, config);
        }
 
        source = uglify.gen_code(ast, config);
 
        source = source.replace(reTokens, function () {
            return '\n' + comments.shift() + '\n';
        });
 
        if (source.substr(source.length - 1) === ')') {
            source += ';';
        }
        source += '\n';
        resource.content = source;
    } catch (e) {
        if (options.callback) {
            options.callback(e);
        }
 
        done('Minify failed, file is unparseable.\nException:\n' + JSON.stringify(e));
        return;
    }
 
    done(null);
};