Sha256: f192331e63c37e6f4bac112236278b006fc0d0eb1644c1721b968fc581ddfe3f

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

# From npm install uglify-js
class Condenser::UglifyMinifier < Condenser::NodeProcessor

  class Error < StandardError
  end
  
  def self.call(environment, input)
    new.call(environment, input)
  end
  
  def initialize(options = {})
    @options = options.merge({
      warnings: true
    }).freeze
  end

  def call(environment, input)
    opts = {
      # 'moduleRoot' => nil,
      # 'filename' => input[:filename],
      # 'moduleId' => input[:filename].sub(/(\..+)+/, ''),
      # 'filenameRelative' => input[:filename],#split_subpath(input[:load_path], input[:filename]),
      # 'sourceFileName' => input[:filename],
      # 'sourceMapTarget' => input[:filename]
      # # 'inputSourceMap'
    }.merge(@options)
    
    result = exec_runtime(<<-JS)
      const UglifyJS = require("#{File.expand_path('../node_modules', __FILE__)}/uglify-js");
      const source = #{JSON.generate(input[:filename] => input[:source])}
      const options = #{JSON.generate(opts)};

      // {
      //     sourceMap: {
      //         content: "content from compiled.js.map",
      //         url: "minified.js.map"
      //     }
      // });
      
      try {
        var result = UglifyJS.minify(source, options);
        console.log(JSON.stringify(result));
      } catch(e) {
        console.log(JSON.stringify({'error': e.name + ": " + e.message}));
        process.exit(1);
      }
    JS

    raise Error, result['error'] if result['error']
    
    if result['warnings']
      result['warnings'].each { |w| environment.logger.warn(w) }
    end
    
    input[:source] = result['code']
    input[:map] = result['map']
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
condenser-0.3 lib/condenser/minifiers/uglify_minifier.rb