var uglify;
try {
  uglify = require('uglify-js');
} catch (err) { /* NOP */ }

process.on('message', function(msg) {
  var outputFile = msg.output,
      data = msg.data,
      sourceMap = msg.sourceMap,
      warnings = [];

  try {
    var warnFunction;
    try {
      warnFunction = uglify.AST_Node.warn_function;
      uglify.AST_Node.warn_function = function(msg) {
        warnings.push(msg);
      };
      var ast = uglify.parse(data),
          compressor = uglify.Compressor(),
          outputOptions = {};

      if (sourceMap) {
        sourceMap = uglify.SourceMap({
          file: outputFile,
          orig: sourceMap
        });
        outputOptions.source_map = sourceMap;
      }

      ast.figure_out_scope();
      ast = ast.transform(compressor);
      ast.figure_out_scope();
      ast.compute_char_frequency();
      ast.mangle_names();
      data = ast.print_to_string(outputOptions);

      process.send({
        data: {
          data: data,
          warnings: warnings,
          sourceMap: sourceMap && sourceMap.toString()
        }
      });
    } finally {
      uglify.AST_Node.warn_function = warnFunction;
    }
  } catch (err) {
    process.send({err: err.toString()});
  }
});

var uglify;
try {
  uglify = require('uglify-js');
} catch (err) { /* NOP */ }

process.on('message', function(msg) {
  var outputFile = msg.output,
      data = msg.data,
      sourceMap = msg.sourceMap,
      warnings = [];

  try {
    var warnFunction;
    try {
      warnFunction = uglify.AST_Node.warn_function;
      uglify.AST_Node.warn_function = function(msg) {
        warnings.push(msg);
      };
      var ast = uglify.parse(data),
          compressor = uglify.Compressor(),
          outputOptions = {};

      if (sourceMap) {
        sourceMap = uglify.SourceMap({
          file: outputFile,
          orig: sourceMap
        });
        outputOptions.source_map = sourceMap;
      }

      ast.figure_out_scope();
      ast = ast.transform(compressor);
      ast.figure_out_scope();
      ast.compute_char_frequency();
      ast.mangle_names();
      data = ast.print_to_string(outputOptions);

      process.send({
        data: {
          data: data,
          warnings: warnings,
          sourceMap: sourceMap && sourceMap.toString()
        }
      });
    } finally {
      uglify.AST_Node.warn_function = warnFunction;
    }
  } catch (err) {
    process.send({err: err.stack || err.msg || err.toString()});
  }
});

Improve error logging in uglify worker
Improve error logging in uglify worker
