Skip to content

Commit

Permalink
Merge pull request #31 from ahorek/fix_keep
Browse files Browse the repository at this point in the history
fix keep_classnames and keep_numbers
  • Loading branch information
ahorek authored Jun 28, 2022
2 parents 84c54fc + ef129db commit 5705304
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- { ruby: 2.7, c-o-e: false }
- { ruby: 3.0, c-o-e: false }
- { ruby: 3.1, c-o-e: false }
- { ruby: jruby-9.3.4.0, c-o-e: false }
- { ruby: jruby-9.3.6.0, c-o-e: false }
- { ruby: ruby-head, c-o-e: true }
- { ruby: jruby-head, c-o-e: true }
- { ruby: truffleruby, c-o-e: true }
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## Unreleased

## 1.1.11 (28 June 2022)
- add keep_classnames and keep_numbers available in terser (default is false)

## 1.1.10 (13 June 2022)
- update TerserJS to [5.14.1]

Expand Down
2 changes: 1 addition & 1 deletion benchmark/compare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
gemfile(true) do
source "https://rubygems.org"

gem "terser", git: "https://github.com/ahorek/terser-ruby.git"
gem "terser", path: ".."
gem "uglifier"
end

Expand Down
22 changes: 19 additions & 3 deletions lib/terser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Error < StandardError; end
: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
Expand Down Expand Up @@ -80,6 +81,7 @@ class Error < StandardError; end
: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
Expand All @@ -94,7 +96,8 @@ class Error < StandardError; end
: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_fanems to true.
: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
Expand Down Expand Up @@ -297,7 +300,8 @@ def read_source(source)
def mangle_options
defaults = conditional_option(
DEFAULTS[:mangle],
:keep_fnames => keep_fnames?(:mangle)
:keep_fnames => keep_fnames?(:mangle),
:keep_classnames => keep_classnames?(:mangle)
)

conditional_option(
Expand Down Expand Up @@ -335,7 +339,10 @@ def compressor_options
conditional_option(
@options[:compress],
defaults,
{ :keep_fnames => keep_fnames?(:compress) }.merge(negate_iife_block)
{
:keep_fnames => keep_fnames?(:compress),
:keep_classnames => keep_classnames?(:compress)
}.merge(negate_iife_block)
)
end

Expand Down Expand Up @@ -403,6 +410,15 @@ def keep_fnames?(type)
end
end

def keep_classnames?(type)
if @options[:keep_classnames] || DEFAULTS[:keep_classnames]
true
else
@options[type].respond_to?(:[]) && @options[type][:keep_classnames] ||
DEFAULTS[type].respond_to?(:[]) && DEFAULTS[type][:keep_classnames]
end
end

def source_map_options(input_map, source_map_options)
options = conditional_option(source_map_options[:source_map], SOURCE_MAP_DEFAULTS) || SOURCE_MAP_DEFAULTS

Expand Down
2 changes: 1 addition & 1 deletion lib/terser/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class Terser
# Current version of Terser.
VERSION = "1.1.10"
VERSION = "1.1.11"
end
55 changes: 55 additions & 0 deletions spec/terser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,61 @@
end
end

describe 'keep_classnames' do
let(:code) do
<<-JS
function foo() {
class Bar {}
}
JS
end

it "keeps function names in output when compressor keep_classnames is set" do
out = Terser.compile(code, :compress => false, :keep_classnames => true)
expect(out).to include("Bar")
end

it "keeps function names in output when compressor keep_classnames is set as regex" do
out = Terser.compile(code, :compress => false, :keep_classnames => /Bar$/)
expect(out).to include("Bar")
end

it "keeps function names in output when compressor keep_classnames is set via mangle" do
out = Terser.compile(code, :compress => false, :mangle => { :keep_classnames => true })
expect(out).to include("Bar")
end

it "keeps function names in output when compressor keep_classnames is false" do
out = Terser.compile(code, :compress => false, :keep_classnames => false)
expect(out).not_to include("Bar")
end

it "keeps function names in output when compressor keep_classnames is not set" do
out = Terser.compile(code, :compress => false)
expect(out).not_to include("Bar")
end
end

describe 'keep_numbers' do
let(:code) do
<<-JS
function foo() {
return 1000000000000;
}
JS
end

it "keeps number in the original form" do
out = Terser.compile(code, :compress => false, :output => { :keep_numbers => true })
expect(out).to include("1000000000000")
end

it "uses a short form" do
out = Terser.compile(code, :compress => false, :output => { :keep_numbers => false })
expect(out).to include("1e12")
end
end

describe "Input Formats" do
let(:code) { "function hello() { return 'hello world'; }" }

Expand Down

0 comments on commit 5705304

Please sign in to comment.