<commit_before>var _ = require('lodash'),
    os = require('os'),
    uglifyJs = require('uglify-js'),
    passError = require('passerror'),
    errors = require('../errors'),
    compressorByName = {},
    async = require('async');

compressorByName.uglifyJs = function (assetGraph, javaScript, compressorOptions, cb) {
    compressorOptions = _.extend({}, compressorOptions);
    var mangleOptions = _.defaults({}, _.pick(javaScript.serializationOptions, ['screw_ie8']), this.assetGraph && _.pick(this.assetGraph.javaScriptSerializationOptions, ['screw_ie8']), compressorOptions.mangleOptions);
    delete compressorOptions.mangleOptions;
    _.defaults(compressorOptions, _.pick(javaScript.serializationOptions, ['side_effects', 'screw_ie8']), this.assetGraph && _.pick(this.assetGraph.javaScriptSerializationOptions, ['side_effects', 'screw_ie8']));

    var textAndSourceMap = javaScript.textAndSourceMap;

    var uglifyAst = uglifyJs.parse(textAndSourceMap.text);
    uglifyAst.figure_out_scope();

    var compressor = new uglifyJs.Compressor(compressorOptions);

    compressor.warn = function (message, propertyByName) {
        var warning = new errors.UglifyJsWarning(_.defaults({
            asset: javaScript,
            message: message.replace(/\{([^\}]+)\}/g, function ($0, propertyName) {
                if (propertyName === 'file') {
                    return javaScript.urlOrDescription;
                } else {
                    return propertyByName[propertyName] || 'unknown';
                }
            })
        }, propertyByName));
        assetGraph.emit('warning', warning);
    };

    uglifyAst.transform(compressor);
    uglifyAst.figure_out_scope();
    uglifyAst.compute_char_frequency(mangleOptions);
    uglifyAst.mangle_names(mangleOptions);
    var outSourceMap = uglifyJs.SourceMap({ orig: textAndSourceMap.sourceMap });
    var stream = uglifyJs.OutputStream({ comments: true, source_map: outSourceMap });
    uglifyAst.print(stream);

    var outText = stream.toString();

    var compressedJavaScript = new assetGraph.JavaScript({
        lastKnownByteLength: javaScript.lastKnownByteLength, // I know, I know
        copyrightNoticeComments: javaScript.copyrightNoticeComments,
        text: outText,
        isDirty: true,
        sourceMap: outSourceMap.get().toJSON()
    });

    setImmediate(function () {
        cb(null, compressedJavaScript);
    });
};

compressorByName.yuicompressor = function (assetGraph, javaScript, compressorOptions, cb) {
    var yuicompressor;
    try {
        yuicompressor = require('yui-compressor');
    } catch (e) {
        return cb(new Error('transforms.compressJavaScript: node-yui-compressor not found. Please run \'npm install yui-compressor\' and try again (tested with version 0.1.3).'));
    }
    compressorOptions = compressorOptions || {};
    yuicompressor.compile(javaScript.text, compressorOptions, passError(cb, function (compressedText) {<commit_msg>compressJavaScript: Add sourceMaps option, and only do the work related to loading existing source maps and serializing them afterwards if it's on.<commit_after>var _ = require('lodash'),
    os = require('os'),
    uglifyJs = require('uglify-js'),
    passError = require('passerror'),
    errors = require('../errors'),
    compressorByName = {},
    async = require('async');

compressorByName.uglifyJs = function (assetGraph, javaScript, compressorOptions, cb) {
    var sourceMaps = compressorOptions.sourceMaps;
    compressorOptions = _.extend({}, _.omit(compressorOptions, 'sourceMaps'));
    var mangleOptions = _.defaults({}, _.pick(javaScript.serializationOptions, ['screw_ie8']), this.assetGraph && _.pick(this.assetGraph.javaScriptSerializationOptions, ['screw_ie8']), compressorOptions.mangleOptions);
    delete compressorOptions.mangleOptions;
    _.defaults(compressorOptions, _.pick(javaScript.serializationOptions, ['side_effects', 'screw_ie8']), this.assetGraph && _.pick(this.assetGraph.javaScriptSerializationOptions, ['side_effects', 'screw_ie8']));

    var text,
        sourceMap;
    if (sourceMaps) {
        var textAndSourceMap = javaScript.textAndSourceMap;
        text = textAndSourceMap.text;
        sourceMap = textAndSourceMap.sourceMap;
    } else {
        text = javaScript.text;
    }

    var uglifyAst = uglifyJs.parse(text);
    uglifyAst.figure_out_scope();

    var compressor = new uglifyJs.Compressor(compressorOptions);

    compressor.warn = function (message, propertyByName) {
        var warning = new errors.UglifyJsWarning(_.defaults({
            asset: javaScript,
            message: message.replace(/\{([^\}]+)\}/g, function ($0, propertyName) {
                if (propertyName === 'file') {
                    return javaScript.urlOrDescription;
                } else {
                    return propertyByName[propertyName] || 'unknown';
                }
            })
        }, propertyByName));
        assetGraph.emit('warning', warning);
    };

    uglifyAst.transform(compressor);
    uglifyAst.figure_out_scope();
    uglifyAst.compute_char_frequency(mangleOptions);
    uglifyAst.mangle_names(mangleOptions);
    var outSourceMap = sourceMap && uglifyJs.SourceMap({ orig: sourceMap });
    var stream = uglifyJs.OutputStream({ comments: true, source_map: outSourceMap });
    uglifyAst.print(stream);

    var outText = stream.toString();

    var compressedJavaScript = new assetGraph.JavaScript({
        lastKnownByteLength: javaScript.lastKnownByteLength, // I know, I know
        copyrightNoticeComments: javaScript.copyrightNoticeComments,
        text: outText,
        isDirty: true,
        sourceMap: outSourceMap && outSourceMap.get().toJSON()
    });

    setImmediate(function () {
        cb(null, compressedJavaScript);
    });
};

compressorByName.yuicompressor = function (assetGraph, javaScript, compressorOptions, cb) {
    var yuicompressor;
    try {
        yuicompressor = require('yui-compressor');
    } catch (e) {
        return cb(new Error('transforms.compressJavaScript: node-yui-compressor not found. Please run \'npm install yui-compressor\' and try again (tested with version 0.1.3).'));
    }
    compressorOptions = compressorOptions || {};
    yuicompressor.compile(javaScript.text, compressorOptions, passError(cb, function (compressedText) {
{'fraction_non_alphanumeric': 0.12127929069031032, 'fraction_numerical': 0.00253324889170361, 'mean_word_length': 3.564306358381503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1064470', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1784, 'n_words': 355}