Sha256: 8e4ac8dcdfb0977f1efae59bf70f94c674e280bb6227e4c65bf955686ce23907

Contents?: true

Size: 1.73 KB

Versions: 3

Compression:

Stored size: 1.73 KB

Contents

require File.expand_path('../../processors/node_processor', __FILE__)

class Condenser::UglifyMinifier < Condenser::NodeProcessor

  class Error < StandardError
  end
  
  # From npm install uglify-js
  UGLIFY_SOURCE = File.expand_path('../node_modules', __FILE__)
  
  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)
      module.paths.push("#{UGLIFY_SOURCE}")
      const UglifyJS = require("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

3 entries across 3 versions & 1 rubygems

Version Path
condenser-0.0.9 lib/condenser/minifiers/uglify_minifier.rb
condenser-0.0.8 lib/condenser/minifiers/uglify_minifier.rb
condenser-0.0.7 lib/condenser/minifiers/uglify_minifier.rb