uglifier 4.1.20 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e80ba00a3806f9848b926823cd7cee1d7f6fd72a050c8b17f5dd084f45a1ed39
4
- data.tar.gz: 32f396008cd005a2725b698b55a9167872c13b5334e637801ca725ba7049fcc5
3
+ metadata.gz: d93953baa3d4cf3d30b44e0a0f90dc3b57162aa62283d53213e1647d3de236f3
4
+ data.tar.gz: 647b93d713f352702df61de8a4bcec00dc3a659d81f5a0ff5a6bac6ecfa39895
5
5
  SHA512:
6
- metadata.gz: 464ea32ccd16245306fcc71c575580890d0590dd61e913b1c8b10496dd80c1f6d44d1f3716a06fa4a9d145bca75243e880f466b63e9f07719c02962b8667f9c4
7
- data.tar.gz: 3b5a4997bcf52bc6fa0670fab41a187eeff8e716a579309a2949b1b2677334e4298cb6358e8359c3597f34126a3c13b2e1929cf40db99d798cfc5c0357af6522
6
+ metadata.gz: e5a9c7445f6f385a4ed0117badd20011e75da0aaf2df4ca8102b2d5c33ad9c14651d2d33dc14b7b5bc9be9b4400623a851176ac993fa54663c0edb68a723f0e9
7
+ data.tar.gz: 5762685023380924279b268fab8c6e2de4c91c18ee45a613b344a937b3743e4b50cf73d8f259b73d99aaee71911e2e3be0608aa165e016b5a03061fa9f9ffd00
@@ -0,0 +1,52 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ name: >-
8
+ Test (${{ matrix.ruby }})
9
+ runs-on: ubuntu-latest
10
+ strategy:
11
+ fail-fast: false
12
+ matrix:
13
+ gemfile: [Gemfile]
14
+ ruby:
15
+ - "2.4"
16
+ - "2.5"
17
+ - "2.6"
18
+ - "2.7"
19
+ - "3.0"
20
+ - "3.1"
21
+ - "3.2"
22
+ - "3.3"
23
+ - jruby
24
+ - truffleruby
25
+ include:
26
+ - ruby: 2.5
27
+ gemfile: gemfiles/rubyracer
28
+ - ruby: jruby
29
+ gemfile: gemfiles/rubyrhino
30
+ - ruby: 2.5
31
+ gemfile: gemfiles/alaska
32
+ alaska: 1
33
+ env:
34
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
35
+ ALASKA: ${{ matrix.alaska}}
36
+ steps:
37
+ - uses: actions/checkout@v2
38
+ - uses: ruby/setup-ruby@v1
39
+ with:
40
+ ruby-version: ${{ matrix.ruby }}
41
+ bundler-cache: true
42
+ - run: bundle exec rake spec
43
+ lint:
44
+ runs-on: ubuntu-latest
45
+
46
+ steps:
47
+ - uses: actions/checkout@v2
48
+ - uses: ruby/setup-ruby@v1
49
+ with:
50
+ ruby-version: "3.0"
51
+ bundler-cache: true
52
+ - run: bundle exec rubocop --format github
data/.rubocop.yml CHANGED
@@ -1,11 +1,17 @@
1
1
  AllCops:
2
2
  DisplayCopNames: true
3
+ SuggestExtensions: false
4
+ NewCops: disable
5
+ TargetRubyVersion: 2.4
3
6
  Exclude:
4
7
  - uglifier.gemspec
5
8
  - lib/uglifier/version.rb
6
9
  - "vendor/**/*"
7
10
  - "gemfiles/vendor/**/*"
8
11
 
12
+ Metrics/AbcSize:
13
+ Max: 20
14
+
9
15
  Metrics/BlockLength:
10
16
  Enabled: true
11
17
  Exclude:
@@ -14,7 +20,7 @@ Metrics/BlockLength:
14
20
  Metrics/ClassLength:
15
21
  Enabled: false
16
22
 
17
- Metrics/LineLength:
23
+ Layout/LineLength:
18
24
  Max: 100
19
25
 
20
26
  Metrics/MethodLength:
@@ -49,3 +55,9 @@ Style/MutableConstant:
49
55
 
50
56
  Style/ExpandPathArguments:
51
57
  Enabled: false
58
+
59
+ Style/FrozenStringLiteralComment:
60
+ Enabled: false
61
+
62
+ Style/StringConcatenation:
63
+ Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 4.2.1 (22 September 2024)
2
+
3
+ - fix Ruby 3.4 compatiblity, drop Ruby 2.3 and older support (#195)
4
+ - fix typos (#194)
5
+
6
+ ## 4.2.0 (25 September 2019)
7
+
8
+ - show code context for error messages (#167)
9
+ - defer initialisation of ExecJS context to fix #165
10
+
1
11
  ## 4.1.20 (17 November 2018)
2
12
 
3
13
  - update UglifyJS to [3.4.8](https://github.com/mishoo/UglifyJS2/compare/v3.4.9...v3.4.8)
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rubocop', '~> 0.57.0', :group => [:development] if RUBY_VERSION >= '2.1'
5
+ gem 'rubocop', '~> 1.7.0', :group => [:development] if RUBY_VERSION >= '2.5'
data/README.md CHANGED
@@ -3,27 +3,7 @@
3
3
  Ruby wrapper for [UglifyJS](https://github.com/mishoo/UglifyJS2) JavaScript
4
4
  compressor.
5
5
 
6
- UglifyJS currently is extensively tested with ES5, but also includes experimental ES6/ES2015+/Harmony support.
7
-
8
- More stable alternatives for working with ES6 code is to first transpile to ES5 with e.g. [babel-transpiler](https://github.com/babel/ruby-babel-transpiler) or using [Closure Compiler](https://github.com/documentcloud/closure-compiler) to directly minify ES6 code.
9
-
10
- [![Build Status](https://travis-ci.org/lautis/uglifier.svg?branch=master)](https://travis-ci.org/lautis/uglifier)
11
-
12
- ## ES6 / ES2015+ / Harmony mode
13
-
14
- When using Uglifier with ES6 syntax without any options, an error will be
15
- thrown.
16
-
17
- ```
18
- Uglifier::Error: Unexpected token: punc ((). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).
19
- ```
20
-
21
- The experimental ES6 syntax support can be enabled by passing `:harmony => true`
22
- option to Uglifier.
23
-
24
- ```ruby
25
- Uglifier.compile(js, harmony: true)
26
- ```
6
+ UglifyJS only works with ES5. If you need to compress ES6, [ruby-terser](https://github.com/ahorek/terser-ruby) is a better option.
27
7
 
28
8
  ### Rails
29
9
 
@@ -48,7 +28,7 @@ Uglifier is available as a ruby gem.
48
28
  $ gem install uglifier
49
29
 
50
30
  Ensure that your environment has a JavaScript interpreter supported by
51
- [ExecJS](https://github.com/sstephenson/execjs). Using `therubyracer` gem
31
+ [ExecJS](https://github.com/rails/execjs). Using `therubyracer` gem
52
32
  is a safe choice if a runtime isn't already present. Note that while JScript built-in Windows 7 and older works, it is extremely slow.
53
33
 
54
34
  ## Usage
@@ -96,8 +76,8 @@ Available options and their defaults are
96
76
  :indent_start => 0, # Starting indent level
97
77
  :width => 80, # Specify line width when beautifier is used (only with beautifier)
98
78
  :preamble => nil, # Preamble for the generated JS file. Can be used to insert any code or comment.
99
- :wrap_iife => false # Wrap IIFEs in parenthesis. Note: this disables the negate_iife compression option.
100
- :shebang => true # Preserve shebang (#!) in preamble (shell scripts)
79
+ :wrap_iife => false, # Wrap IIFEs in parenthesis. Note: this disables the negate_iife compression option.
80
+ :shebang => true, # Preserve shebang (#!) in preamble (shell scripts)
101
81
  :quote_style => 0, # Quote style, possible values :auto (default), :single, :double, :original
102
82
  :keep_quoted_props => false # Keep quotes property names
103
83
  },
@@ -113,7 +93,7 @@ Available options and their defaults are
113
93
  :regex => nil, # A regular expression to filter property names to be mangled
114
94
  :ignore_quoted => false, # Only mangle unquoted property names
115
95
  :debug => false, # Mangle names with the original name still present
116
- } # Mangle property names, disabled by default
96
+ }, # Mangle property names, disabled by default
117
97
  :compress => {
118
98
  :sequences => true, # Allow statements to be joined by commas
119
99
  :properties => true, # Rewrite property access using the dot notation
@@ -168,6 +148,7 @@ Available options and their defaults are
168
148
  :output_filename => nil, # The filename or URL where the minified output can be found
169
149
  :input_source_map => nil # The contents of the source map describing the input
170
150
  },
151
+ :error_context_lines => 8, # How many context lines surrounding the error line. Env var ERROR_CONTEXT_LINES overrides this option
171
152
  :harmony => false # Enable ES6/Harmony mode (experimental). Disabling mangling and compressing is recommended with Harmony mode.
172
153
  }
173
154
  ```
@@ -1,4 +1,4 @@
1
1
  class Uglifier
2
2
  # Current version of Uglifier.
3
- VERSION = "4.1.20"
3
+ VERSION = "4.2.1"
4
4
  end
data/lib/uglifier.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  require "json"
4
- require "base64"
5
4
  require "execjs"
6
5
  require "uglifier/version"
7
6
 
@@ -25,9 +24,9 @@ class Uglifier
25
24
 
26
25
  # Default options for compilation
27
26
  DEFAULTS = {
28
- # rubocop:disable LineLength
27
+ # rubocop:disable Layout/LineLength
29
28
  :output => {
30
- :ascii_only => true, # Escape non-ASCII characterss
29
+ :ascii_only => true, # Escape non-ASCII characters
31
30
  :comments => :copyright, # Preserve comments (:all, :jsdoc, :copyright, :none)
32
31
  :inline_script => false, # Escape occurrences of </script in strings
33
32
  :quote_keys => false, # Quote keys in object literals
@@ -49,7 +48,7 @@ class Uglifier
49
48
  :eval => false, # Mangle names when eval of when is used in scope
50
49
  :reserved => ["$super"], # Argument names to be excluded from mangling
51
50
  :properties => false, # Mangle property names
52
- :toplevel => false, # Mangle names declared in the toplevel scope
51
+ :toplevel => false # Mangle names declared in the toplevel scope
53
52
  }, # Mangle variable and function names, set to false to skip mangling
54
53
  :compress => {
55
54
  :sequences => true, # Allow statements to be joined by commas
@@ -84,7 +83,7 @@ class Uglifier
84
83
  :passes => 1, # Number of times to run compress. Raising the number of passes will increase compress time, but can produce slightly smaller code.
85
84
  :keep_infinity => false, # Prevent compression of Infinity to 1/0
86
85
  :side_effects => true, # Pass false to disable potentially dropping functions marked as "pure" using pure comment annotation. See UglifyJS documentation for details.
87
- :switches => true, # de-duplicate and remove unreachable switch branches
86
+ :switches => true # de-duplicate and remove unreachable switch branches
88
87
  }, # Apply transformations to code, set to false to skip
89
88
  :parse => {
90
89
  :bare_returns => false, # Allow top-level return statements.
@@ -98,8 +97,10 @@ class Uglifier
98
97
  :toplevel => false,
99
98
  :ie8 => true, # Generate safe code for IE8
100
99
  :source_map => false, # Generate source map
100
+ :error_context_lines => 8, # How many lines surrounding the error line
101
101
  :harmony => false # Enable ES6/Harmony mode (experimental). Disabling mangling and compressing is recommended with Harmony mode.
102
102
  }
103
+ # rubocop:enable Layout/LineLength
103
104
 
104
105
  EXTRA_OPTIONS = [:comments, :mangle_properties]
105
106
 
@@ -108,7 +109,7 @@ class Uglifier
108
109
  :regex => nil, # A regular expression to filter property names to be mangled
109
110
  :keep_quoted => false, # Keep quoted property names
110
111
  :reserved => [], # List of properties that should not be mangled
111
- :builtins => false, # Mangle properties that overlap with standard JS globals
112
+ :builtins => false # Mangle properties that overlap with standard JS globals
112
113
  }
113
114
 
114
115
  SOURCE_MAP_DEFAULTS = {
@@ -121,8 +122,6 @@ class Uglifier
121
122
  :input_source_map => nil # The contents of the source map describing the input
122
123
  }
123
124
 
124
- # rubocop:enable LineLength
125
-
126
125
  # Minifies JavaScript code using implicit context.
127
126
  #
128
127
  # @param source [IO, String] valid JS source code.
@@ -145,13 +144,12 @@ class Uglifier
145
144
  #
146
145
  # @param options [Hash] optional overrides to +Uglifier::DEFAULTS+
147
146
  def initialize(options = {})
147
+ # rubocop:disable Lint/UnreachableLoop
148
148
  (options.keys - DEFAULTS.keys - EXTRA_OPTIONS)[0..1].each do |missing|
149
149
  raise ArgumentError, "Invalid option: #{missing}"
150
150
  end
151
+ # rubocop:enable Lint/UnreachableLoop
151
152
  @options = options
152
-
153
- source = harmony? ? source_with(HarmonySourcePath) : source_with(SourcePath)
154
- @context = ExecJS.compile(source)
155
153
  end
156
154
 
157
155
  # Minifies JavaScript code
@@ -161,7 +159,7 @@ class Uglifier
161
159
  def compile(source)
162
160
  if @options[:source_map]
163
161
  compiled, source_map = run_uglifyjs(source, true)
164
- source_map_uri = Base64.strict_encode64(source_map)
162
+ source_map_uri = [source_map].pack('m0')
165
163
  source_map_mime = "application/json;charset=utf-8;base64"
166
164
  compiled + "\n//# sourceMappingURL=data:#{source_map_mime},#{source_map_uri}"
167
165
  else
@@ -180,6 +178,13 @@ class Uglifier
180
178
 
181
179
  private
182
180
 
181
+ def context
182
+ @context ||= begin
183
+ source = harmony? ? source_with(HarmonySourcePath) : source_with(SourcePath)
184
+ ExecJS.compile(source)
185
+ end
186
+ end
187
+
183
188
  def source_map_comments
184
189
  return '' unless @options[:source_map].respond_to?(:[])
185
190
 
@@ -213,25 +218,77 @@ class Uglifier
213
218
  :ie8 => ie8?
214
219
  }
215
220
 
216
- parse_result(@context.call("uglifier", options), generate_map)
221
+ parse_result(context.call("uglifier", options), generate_map, options)
217
222
  end
218
223
 
219
224
  def harmony?
220
225
  @options[:harmony]
221
226
  end
222
227
 
223
- def error_message(result)
224
- result['error']['message'] +
225
- if result['error']['message'].start_with?("Unexpected token") && !harmony?
226
- ". To use ES6 syntax, harmony mode must be enabled with " \
227
- "Uglifier.new(:harmony => true)."
228
+ def harmony_error_message(message)
229
+ if message.start_with?("Unexpected token")
230
+ ". To use ES6 syntax, harmony mode must be enabled with " \
231
+ "Uglifier.new(:harmony => true)."
232
+ else
233
+ ""
234
+ end
235
+ end
236
+
237
+ def error_context_lines
238
+ @options.fetch(:error_context_lines, DEFAULTS[:error_context_lines]).to_i
239
+ end
240
+
241
+ def error_context_format_options(low, high, line_index, column)
242
+ line_width = high.to_s.size
243
+ {
244
+ :line_index => line_index,
245
+ :base_index => low,
246
+ :line_width => line_width,
247
+ :line_format => "\e[36m%#{line_width + 1}d\e[0m ", # cyan
248
+ :col => column
249
+ }
250
+ end
251
+
252
+ def format_error_line(line, options)
253
+ # light red
254
+ indicator = ' => '.rjust(options[:line_width] + 2)
255
+ colored_line = "#{line[0...options[:col]]}\e[91m#{line[options[:col]..-1]}"
256
+ "\e[91m#{indicator}\e[0m#{colored_line}\e[0m"
257
+ end
258
+
259
+ def format_lines(lines, options)
260
+ lines.map.with_index do |line, index|
261
+ if options[:base_index] + index == options[:line_index]
262
+ format_error_line(line, options)
228
263
  else
229
- ""
264
+ "#{options[:line_format] % (options[:base_index] + index + 1)}#{line}"
230
265
  end
266
+ end
231
267
  end
232
268
 
233
- def parse_result(result, generate_map)
234
- raise Error, error_message(result) if result.has_key?('error')
269
+ def context_lines_message(source, line_number, column)
270
+ return if line_number.nil?
271
+
272
+ line_index = line_number - 1
273
+ lines = source.split("\n")
274
+
275
+ first_line = [line_index - error_context_lines, 0].max
276
+ last_line = [line_number + error_context_lines, lines.size].min
277
+ options = error_context_format_options(first_line, last_line, line_index, column)
278
+ context_lines = lines[first_line...last_line]
279
+
280
+ "--\n#{format_lines(context_lines, options).join("\n")}\n=="
281
+ end
282
+
283
+ def error_message(result, options)
284
+ err = result['error']
285
+ harmony_msg = harmony? ? '' : harmony_error_message(err['message'].to_s)
286
+ src_ctx = context_lines_message(options[:source], err['line'], err['col'])
287
+ "#{err['message']}#{harmony_msg}\n#{src_ctx}"
288
+ end
289
+
290
+ def parse_result(result, generate_map, options)
291
+ raise Error, error_message(result, options) if result.has_key?('error')
235
292
 
236
293
  if generate_map
237
294
  [result['code'] + source_map_comments, result['map']]
@@ -450,10 +507,11 @@ class Uglifier
450
507
 
451
508
  def input_source_map(source, generate_map)
452
509
  return nil unless generate_map
510
+
453
511
  source_map_options = @options[:source_map].is_a?(Hash) ? @options[:source_map] : {}
454
512
  sanitize_map_root(source_map_options.fetch(:input_source_map) do
455
513
  url = extract_source_mapping_url(source)
456
- Base64.strict_decode64(url.split(",", 2)[-1]) if url && url.start_with?("data:")
514
+ url.split(",", 2)[-1].unpack1('m0') if url && url.start_with?("data:")
457
515
  end)
458
516
  rescue ArgumentError, JSON::ParserError
459
517
  nil
data/uglifier.gemspec CHANGED
@@ -8,12 +8,12 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Uglifier::VERSION
9
9
  spec.authors = ["Ville Lautanala"]
10
10
  spec.email = ["lautis@gmail.com"]
11
- spec.homepage = "http://github.com/lautis/uglifier"
11
+ spec.homepage = "https://github.com/lautis/uglifier"
12
12
  spec.summary = "Ruby wrapper for UglifyJS JavaScript compressor"
13
13
  spec.description = "Uglifier minifies JavaScript files by wrapping UglifyJS to be accessible in Ruby"
14
14
  spec.license = "MIT"
15
15
 
16
- spec.required_ruby_version = '>= 1.9.3'
16
+ spec.required_ruby_version = '>= 2.4.0'
17
17
 
18
18
  spec.extra_rdoc_files = [
19
19
  "LICENSE.txt",
@@ -30,6 +30,6 @@ Gem::Specification.new do |spec|
30
30
  spec.add_runtime_dependency "execjs", [">= 0.3.0", "< 3"]
31
31
  spec.add_development_dependency "rspec", "~> 3.0"
32
32
  spec.add_development_dependency "rake", "~> 12.0"
33
- spec.add_development_dependency "bundler", "~> 1.3"
33
+ spec.add_development_dependency "bundler", ">= 1.3"
34
34
  spec.add_development_dependency "sourcemap", "~> 0.1.1"
35
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uglifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.20
4
+ version: 4.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ville Lautanala
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-17 00:00:00.000000000 Z
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs
@@ -62,14 +62,14 @@ dependencies:
62
62
  name: bundler
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - "~>"
65
+ - - ">="
66
66
  - !ruby/object:Gem::Version
67
67
  version: '1.3'
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
- - - "~>"
72
+ - - ">="
73
73
  - !ruby/object:Gem::Version
74
74
  version: '1.3'
75
75
  - !ruby/object:Gem::Dependency
@@ -99,11 +99,11 @@ extra_rdoc_files:
99
99
  - CONTRIBUTING.md
100
100
  files:
101
101
  - ".document"
102
+ - ".github/workflows/ruby.yml"
102
103
  - ".gitignore"
103
104
  - ".gitmodules"
104
105
  - ".rspec"
105
106
  - ".rubocop.yml"
106
- - ".travis.yml"
107
107
  - ".yardopts"
108
108
  - CHANGELOG.md
109
109
  - CONTRIBUTING.md
@@ -120,11 +120,11 @@ files:
120
120
  - lib/uglify-harmony.js
121
121
  - lib/uglify.js
122
122
  - uglifier.gemspec
123
- homepage: http://github.com/lautis/uglifier
123
+ homepage: https://github.com/lautis/uglifier
124
124
  licenses:
125
125
  - MIT
126
126
  metadata: {}
127
- post_install_message:
127
+ post_install_message:
128
128
  rdoc_options: []
129
129
  require_paths:
130
130
  - lib
@@ -132,16 +132,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
132
  requirements:
133
133
  - - ">="
134
134
  - !ruby/object:Gem::Version
135
- version: 1.9.3
135
+ version: 2.4.0
136
136
  required_rubygems_version: !ruby/object:Gem::Requirement
137
137
  requirements:
138
138
  - - ">="
139
139
  - !ruby/object:Gem::Version
140
140
  version: '0'
141
141
  requirements: []
142
- rubyforge_project:
143
- rubygems_version: 2.7.6
144
- signing_key:
142
+ rubygems_version: 3.2.22
143
+ signing_key:
145
144
  specification_version: 4
146
145
  summary: Ruby wrapper for UglifyJS JavaScript compressor
147
146
  test_files: []
data/.travis.yml DELETED
@@ -1,34 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 1.9.3
6
- - 2.0.0
7
- - 2.1.10
8
- - 2.2.10
9
- - 2.3.7
10
- - 2.4.4
11
- - 2.5.1
12
- - ruby-head
13
- - jruby-9.2.0.0
14
- before_install:
15
- - gem install bundler -v 1.16.1
16
- git:
17
- submodules: false
18
- gemfile:
19
- - Gemfile
20
- matrix:
21
- include:
22
- - rvm: 2.5.1
23
- gemfile: gemfiles/rubyracer
24
- - rvm: jruby-9.2.0.0
25
- gemfile: gemfiles/rubyrhino
26
- - rvm: 2.5.1
27
- gemfile: gemfiles/alaska
28
- env: ALASKA=1
29
- allow_failures:
30
- - rvm: 2.5.1
31
- gemfile: gemfiles/alaska
32
- env: ALASKA=1
33
- - rvm: ruby-head
34
- fast_finish: true