Class: Condenser::UglifyMinifier

Inherits:
NodeProcessor show all
Defined in:
lib/condenser/minifiers/uglify_minifier.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary

Attributes inherited from NodeProcessor

#npm_path

Instance Method Summary collapse

Methods inherited from NodeProcessor

#binary, call, #exec_runtime, #exec_runtime_error, #exec_syntax_error, #name, #npm_install, #npm_module_path, setup

Constructor Details

#initialize(dir, options = {}) ⇒ UglifyMinifier

Returns a new instance of UglifyMinifier.



6
7
8
9
10
11
12
13
# File 'lib/condenser/minifiers/uglify_minifier.rb', line 6

def initialize(dir, options = {})
  super(dir)
  npm_install('uglify-js')
  
  @options = options.merge({
    warnings: true
  }).freeze
end

Instance Method Details

#call(environment, input) ⇒ Object

Raises:



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
# File 'lib/condenser/minifiers/uglify_minifier.rb', line 15

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("    const UglifyJS = require(\"\#{npm_module_path('uglify-js')}\");\n    const source = \#{JSON.generate(input[:filename] => input[:source])}\n    const options = \#{JSON.generate(opts)};\n\n    // {\n    //     sourceMap: {\n    //         content: \"content from compiled.js.map\",\n    //         url: \"minified.js.map\"\n    //     }\n    // });\n    \n    try {\n      var result = UglifyJS.minify(source, options);\n      console.log(JSON.stringify(result));\n    } catch(e) {\n      console.log(JSON.stringify({'error': e.name + \": \" + e.message}));\n      process.exit(1);\n    }\n  JS\n\n  raise Error, result['error'] if result['error']\n  \n  if result['warnings']\n    result['warnings'].each { |w| environment.logger.warn(w) }\n  end\n  \n  input[:source] = result['code']\n  input[:map] = result['map']\nend\n")