Class: Terser
- Inherits:
-
Object
- Object
- Terser
- Defined in:
- lib/terser.rb,
lib/terser/railtie.rb,
lib/terser/version.rb,
lib/terser/compressor.rb
Overview
A wrapper around the Terser interface
Defined Under Namespace
Classes: Compressor, Error, Railtie
Constant Summary collapse
- SourcePath =
TerserJS source path
File.("../terser.js", __FILE__)
- SourceMapPath =
Source Map path
File.("../source-map.js", __FILE__)
- ES5FallbackPath =
ES5 shims source path
File.("../es5.js", __FILE__)
- SplitFallbackPath =
String.split shim source path
File.("../split.js", __FILE__)
- TerserJSWrapperPath =
TerserJS wrapper path
File.("../terser_wrapper.js", __FILE__)
- DEFAULTS =
Default options for compilation
{ # rubocop:disable Layout/LineLength :output => { :ascii_only => true, # Escape non-ASCII characterss :comments => :copyright, # Preserve comments (:all, :jsdoc, :copyright, :none) :inline_script => false, # Escape occurrences of </script in strings :keep_numbers => false, # Disables optimizations like converting 1000000 into 1e6 :quote_keys => false, # Quote keys in object literals :max_line_len => 32 * 1024, # Maximum line length in minified code :semicolons => true, # Separate statements with semicolons :beautify => false, # Beautify output :indent_level => 4, # Indent level in spaces :indent_start => 0, # Starting indent level :width => 80, # Specify line width when beautifier is used (only with beautifier) :preamble => nil, # Preamble for the generated JS file. Can be used to insert any code or comment. :wrap_iife => false, # Wrap IIFEs in parenthesis. Note: this disables the negate_iife compression option. :shebang => true, # Preserve shebang (#!) in preamble (shell scripts) :quote_style => 0, # Quote style, possible values :auto (default), :single, :double, :original :keep_quoted_props => false # Keep quotes property names }, :mangle => { :eval => false, # Mangle names when eval of when is used in scope :reserved => ["$super"], # Argument names to be excluded from mangling :properties => false, # Mangle property names :toplevel => false # Mangle names declared in the toplevel scope }, # Mangle variable and function names, set to false to skip mangling :compress => { :sequences => true, # Allow statements to be joined by commas :properties => true, # Rewrite property access using the dot notation :dead_code => true, # Remove unreachable code :drop_debugger => true, # Remove debugger; statements :unsafe => false, # Apply "unsafe" transformations :unsafe_comps => false, # Reverse < and <= to > and >= to allow improved compression. This might be unsafe when an at least one of two operands is an object with computed values due the use of methods like get, or valueOf. This could cause change in execution order after operands in the comparison are switching. Compression only works if both comparisons and unsafe_comps are both set to true. :unsafe_math => false, # Optimize numerical expressions like 2 * x * 3 into 6 * x, which may give imprecise floating point results. :unsafe_proto => false, # Optimize expressions like Array.prototype.slice.call(a) into [].slice.call(a) :conditionals => true, # Optimize for if-s and conditional expressions :comparisons => true, # Apply binary node optimizations for comparisons :evaluate => true, # Attempt to evaluate constant expressions :booleans => true, # Various optimizations to boolean contexts :loops => true, # Optimize loops when condition can be statically determined :unused => true, # Drop unreferenced functions and variables (simple direct variable assignments do not count as references unless set to `"keep_assign"`) :toplevel => false, # Drop unreferenced top-level functions and variables :top_retain => [], # prevent specific toplevel functions and variables from `unused` removal :hoist_funs => true, # Hoist function declarations :hoist_vars => false, # Hoist var declarations :if_return => true, # Optimizations for if/return and if/continue :join_vars => true, # Join consecutive var statements :collapse_vars => true, # Collapse single-use var and const definitions when possible. :inline => true, # Inline single-use functions as function expressions. Depends on reduce_vars. :reduce_vars => false, # Collapse variables assigned with and used as constant values. :negate_iife => true, # Negate immediately invoked function expressions to avoid extra parens :pure_getters => false, # Assume that object property access does not have any side-effects :pure_funcs => nil, # List of functions without side-effects. Can safely discard function calls when the result value is not used :drop_console => false, # Drop calls to console.* functions :keep_fargs => false, # Preserve unused function arguments :keep_classnames => false, # Prevents discarding or mangling of class names. Pass a regular expression to only keep class names matching that regex. :keep_fnames => false, # Do not drop names in function definitions :passes => 1, # Number of times to run compress. Raising the number of passes will increase compress time, but can produce slightly smaller code. :keep_infinity => false, # Prevent compression of Infinity to 1/0 :lhs_constants => true, # Moves constant values to the left-hand side of binary nodes. `foo == 42 → 42 == foo` :side_effects => true, # Pass false to disable potentially dropping functions marked as "pure" using pure comment annotation. See TerserJS documentation for details. :switches => true # de-duplicate and remove unreachable switch branches }, # Apply transformations to code, set to false to skip :parse => { :bare_returns => false, # Allow top-level return statements. :expression => false, # Parse a single expression, rather than a program (for parsing JSON). :html5_comments => true, # Ignore HTML5 comments in input :shebang => true, # support #!command as the first line :strict => false }, :define => {}, # Define values for symbol replacement :keep_fnames => false, # Generate code safe for the poor souls relying on Function.prototype.name at run-time. Sets both compress and mangle keep_fnames to true. :keep_classnames => false, # Prevents discarding or mangling of class names. Sets both compress and mangle keep_classnames to true. :toplevel => false, :source_map => false, # Generate source map :error_context_lines => 8 # How many lines surrounding the error line }
- EXTRA_OPTIONS =
[:comments, :mangle_properties]
- MANGLE_PROPERTIES_DEFAULTS =
{ :debug => false, # Add debug prefix and suffix to mangled properties :regex => nil, # A regular expression to filter property names to be mangled :keep_quoted => false, # Keep quoted property names :reserved => [], # List of properties that should not be mangled :builtins => false # Mangle properties that overlap with standard JS globals }
- SOURCE_MAP_DEFAULTS =
{ :map_url => false, # Url for source mapping to be appended in minified source :url => false, # Url for original source to be appended in minified source :sources_content => false, # Include original source content in map :filename => nil, # The filename of the input file :root => nil, # The URL of the directory which contains :filename :output_filename => nil, # The filename or URL where the minified output can be found :input_source_map => nil # The contents of the source map describing the input }
- VERSION =
Current version of Terser.
"1.1.19"
Class Method Summary collapse
-
.compile(source, options = {}) ⇒ String
Minifies JavaScript code using implicit context.
-
.compile_with_map(source, options = {}) ⇒ Array(String, String)
Minifies JavaScript code and generates a source map using implicit context.
Instance Method Summary collapse
-
#compile(source, source_map_options = @options) ⇒ String
(also: #compress)
Minifies JavaScript code.
-
#compile_with_map(source, source_map_options = @options) ⇒ Array(String, String)
Minifies JavaScript code and generates a source map.
-
#initialize(options = {}) ⇒ Terser
constructor
Initialize new context for Terser with given options.
Constructor Details
#initialize(options = {}) ⇒ Terser
Initialize new context for Terser with given options
150 151 152 153 154 155 |
# File 'lib/terser.rb', line 150 def initialize( = {}) missing = .keys - DEFAULTS.keys - EXTRA_OPTIONS raise ArgumentError, "Invalid option: #{missing.first}" if missing.any? @options = end |
Class Method Details
.compile(source, options = {}) ⇒ String
Minifies JavaScript code using implicit context.
134 135 136 |
# File 'lib/terser.rb', line 134 def self.compile(source, = {}) new().compile(source) end |
.compile_with_map(source, options = {}) ⇒ Array(String, String)
Minifies JavaScript code and generates a source map using implicit context.
143 144 145 |
# File 'lib/terser.rb', line 143 def self.compile_with_map(source, = {}) new().compile_with_map(source) end |
Instance Method Details
#compile(source, source_map_options = @options) ⇒ String Also known as: compress
Minifies JavaScript code
162 163 164 165 166 167 168 169 170 171 |
# File 'lib/terser.rb', line 162 def compile(source, = @options) if [:source_map] compiled, source_map = run_terserjs(source, true, ) source_map_uri = Base64.strict_encode64(source_map) source_map_mime = "application/json;charset=utf-8;base64" compiled + "\n//# sourceMappingURL=data:#{source_map_mime},#{source_map_uri}" else run_terserjs(source, false) end end |
#compile_with_map(source, source_map_options = @options) ⇒ Array(String, String)
Minifies JavaScript code and generates a source map
179 180 181 |
# File 'lib/terser.rb', line 179 def compile_with_map(source, = @options) run_terserjs(source, true, ) end |