Terser
Ruby wrapper for Terser JavaScript
compressor.
a fork based on https://github.com/lautis/uglifier and https://github.com/mishoo/UglifyJS2
ES6 support
Rails
When used in Rails, replace
Sprockets.register_compressor 'application/javascript', :terser, Terser::Compressor
Rails.application.configure do
config.assets.js_compressor = :terser
end
in config/environments/production.rb
.
Installation
Terser is available as a ruby gem.
$ gem install terser
Ensure that your environment has a JavaScript interpreter supported by
ExecJS. Using miniracer
gem or NodeJS
is recommended.
Usage
require 'terser'
Terser.new.compile(File.read("source.js"))
Terser.compile(File.read("source.js"))
Terser also supports generating source maps:
uglified, source_map = Terser.new.compile_with_map(source)
When initializing Terser, you can tune the behavior of Terser by passing options. For example, if you want disable variable name mangling:
Terser.new(:mangle => false).compile(source)
Terser.compile(source, :mangle => false)
Available options and their defaults are
{
:output => {
:ascii_only => true,
:comments => :copyright,
:inline_script => false,
:quote_keys => false,
:max_line_len => 32 * 1024,
:bracketize => false,
:semicolons => true,
:preserve_line => false,
:beautify => false,
:indent_level => 4,
:indent_start => 0,
:width => 80,
:preamble => nil,
:wrap_iife => false
:shebang => true
:quote_style => 0,
:keep_quoted_props => false
},
:mangle => {
:eval => false,
:reserved => ["$super"],
:sort => false,
:toplevel => false,
:properties => false,
:keep_fnames => false
},
:mangle_properties => {
:regex => nil,
:ignore_quoted => false,
:debug => false,
}
:compress => {
:sequences => true,
:properties => true,
:dead_code => true,
:drop_debugger => true,
:unsafe => false,
:unsafe_comps => false,
:unsafe_math => false,
:unsafe_proto => false,
:conditionals => true,
:comparisons => true,
:evaluate => true,
:booleans => true,
:loops => true,
:unused => true,
:toplevel => false,
:top_retain => [],
:hoist_funs => true,
:hoist_vars => false,
:if_return => true,
:join_vars => true,
:collapse_vars => false,
:reduce_funcs => false,
:reduce_vars => false,
:negate_iife => true,
:pure_getters => false,
:pure_funcs => nil,
:drop_console => false,
:keep_fargs => false,
:keep_fnames => false,
:passes => 1,
:keep_infinity => false,
:side_effects => true,
:switches => true,
},
:parse => {
:bare_returns => false,
:expression => false,
:html5_comments => true,
:shebang => true,
:strict => false
},
:define => {},
:enclose => false,
:keep_fnames => false,
:source_map => {
:map_url => false,
:url => false,
:sources_content => false,
:filename => nil,
:root => nil,
:output_filename => nil,
:input_source_map => nil
},
:error_context_lines => 8,
}
When passing a regular expression to the output => comments option, be sure to pass a valid Ruby Regexp.
The beginning and ending of comments are removed and cannot be matched (/*, */, //). For example:
When matching
/*!
* comment
*/
use Terser.new(output: {comments: /^!/})
.
Development
Tests are run using
bundle exec rake
See CONTRIBUTING for details about working on and contributing to Terser.
Copyright
Released under MIT license, see LICENSE for details.