terser 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 756cc27e5aae90c30bfde06fddd7b21ac958b83fd03566e196b8134ba3fe9763
4
- data.tar.gz: 03b10dead50a434af48ed4c11676bcd0ccf24ccce7b4c8f129836fee3abae491
3
+ metadata.gz: 0df4df7d398c0f92a7764fe0dbc8727d57c81df483ce0971d70fcbab762d4ebe
4
+ data.tar.gz: a11aba6fb280518f7aedf86cc8b66ea2745679fb4e019c8911e4af053fa569b4
5
5
  SHA512:
6
- metadata.gz: 7d0997164194491cf31a0dbef18e0421ca4c728844d48ae2e2fb5d09d02769454f6148c3692673c706c552b9f7695d0421fdb736384d7fb6896cd17c0ded7d5c
7
- data.tar.gz: fdae9057fa77d5d0485e61fd836dd65a45a406a897bf542e73bff44c5b39be73c896a97e3cd3145bff10cb6a36b2875c02b6e95442a5bd107e53f721c788144e
6
+ metadata.gz: 60aca4d535adb3a1d86396e06013657f1db73a1074849ee1a6ac0caf2bc093846abe8f49575a2ce4a2c694e05011814914099e655b702e51a988dcd21b983f78
7
+ data.tar.gz: 41ba3d478eacb5128bf45d86975567570209369e609016ecc8b2e8ead21488ce826ef3ab89f3f53916cd6325512935ad78fd0a1b64ec42579de400419efeb7a5
@@ -1,5 +1,8 @@
1
1
  ## Unreleased
2
2
 
3
+ ## 1.0.1 (02 August 2020)
4
+ - share the context in order to speedup sprockets compilation
5
+
3
6
  ## 1.0.0 (13 July 2020)
4
7
  - add sprockets wrapper
5
8
  - drop Ruby < 2.3.0 support
data/README.md CHANGED
@@ -7,7 +7,7 @@ a fork based on https://github.com/lautis/uglifier and https://github.com/mishoo
7
7
 
8
8
  ES6 support
9
9
 
10
- [![Build Status](https://travis-ci.org/ahorek/terser-ruby.svg?branch=master)](https://travis-ci.org/ahorek/terser-ruby)
10
+ [![Build Status](https://api.travis-ci.com/ahorek/terser-ruby.svg?branch=master)](https://travis-ci.org/ahorek/terser-ruby)
11
11
 
12
12
  ### Rails
13
13
 
@@ -152,10 +152,11 @@ class Terser
152
152
  # Minifies JavaScript code
153
153
  #
154
154
  # @param source [IO, String] valid JS source code.
155
+ # @param source_map_options [Hash] optional
155
156
  # @return [String] minified code.
156
- def compile(source)
157
- if @options[:source_map]
158
- compiled, source_map = run_terserjs(source, true)
157
+ def compile(source, source_map_options = @options)
158
+ if source_map_options[:source_map]
159
+ compiled, source_map = run_terserjs(source, true, source_map_options)
159
160
  source_map_uri = Base64.strict_encode64(source_map)
160
161
  source_map_mime = "application/json;charset=utf-8;base64"
161
162
  compiled + "\n//# sourceMappingURL=data:#{source_map_mime},#{source_map_uri}"
@@ -168,9 +169,10 @@ class Terser
168
169
  # Minifies JavaScript code and generates a source map
169
170
  #
170
171
  # @param source [IO, String] valid JS source code.
172
+ # @param source_map_options [Hash] optional
171
173
  # @return [Array(String, String)] minified code and source map.
172
- def compile_with_map(source)
173
- run_terserjs(source, true)
174
+ def compile_with_map(source, source_map_options = @options)
175
+ run_terserjs(source, true, source_map_options)
174
176
  end
175
177
 
176
178
  private
@@ -189,12 +191,12 @@ class Terser
189
191
  raise e
190
192
  end
191
193
 
192
- def source_map_comments
193
- return '' unless @options[:source_map].respond_to?(:[])
194
+ def source_map_comments(source_map_options)
195
+ return '' unless source_map_options[:source_map].respond_to?(:[])
194
196
 
195
197
  suffix = ''
196
- suffix += "\n//# sourceMappingURL=" + @options[:source_map][:map_url] if @options[:source_map][:map_url]
197
- suffix += "\n//# sourceURL=" + @options[:source_map][:url] if @options[:source_map][:url]
198
+ suffix += "\n//# sourceMappingURL=" + source_map_options[:source_map][:map_url] if source_map_options[:source_map][:map_url]
199
+ suffix += "\n//# sourceURL=" + source_map_options[:source_map][:url] if source_map_options[:source_map][:url]
198
200
  suffix
199
201
  end
200
202
 
@@ -206,19 +208,19 @@ class Terser
206
208
  end
207
209
 
208
210
  # Run TerserJS for given source code
209
- def run_terserjs(input, generate_map)
211
+ def run_terserjs(input, generate_map, source_map_options = {})
210
212
  source = read_source(input)
211
- input_map = input_source_map(source, generate_map)
213
+ input_map = input_source_map(source, generate_map, source_map_options)
212
214
  options = {
213
215
  :source => source,
214
216
  :output => output_options,
215
217
  :compress => compressor_options,
216
218
  :mangle => mangle_options,
217
- :parse => parse_options,
218
- :sourceMap => source_map_options(input_map)
219
+ :parse => parse_options(source_map_options),
220
+ :sourceMap => source_map_options(input_map, source_map_options)
219
221
  }
220
222
 
221
- parse_result(context.call("terser_wrapper", options), generate_map, options)
223
+ parse_result(context.call("terser_wrapper", options), generate_map, options, source_map_options)
222
224
  end
223
225
 
224
226
  def error_context_lines
@@ -273,13 +275,13 @@ class Terser
273
275
  "#{err['message']}\n#{src_ctx}"
274
276
  end
275
277
 
276
- def parse_result(result, generate_map, options)
278
+ def parse_result(result, generate_map, options, source_map_options = {})
277
279
  raise Error, error_message(result, options) if result.has_key?('error')
278
280
 
279
281
  if generate_map
280
- [result['code'] + source_map_comments, result['map']]
282
+ [result['code'] + source_map_comments(source_map_options), result['map']]
281
283
  else
282
- result['code'] + source_map_comments
284
+ result['code'] + source_map_comments(source_map_options)
283
285
  end
284
286
  end
285
287
 
@@ -400,8 +402,8 @@ class Terser
400
402
  end
401
403
  end
402
404
 
403
- def source_map_options(input_map)
404
- options = conditional_option(@options[:source_map], SOURCE_MAP_DEFAULTS) || SOURCE_MAP_DEFAULTS
405
+ def source_map_options(input_map, source_map_options)
406
+ options = conditional_option(source_map_options[:source_map], SOURCE_MAP_DEFAULTS) || SOURCE_MAP_DEFAULTS
405
407
 
406
408
  {
407
409
  :input => options[:filename],
@@ -414,14 +416,14 @@ class Terser
414
416
  }
415
417
  end
416
418
 
417
- def parse_options
419
+ def parse_options(source_map_options)
418
420
  conditional_option(@options[:parse], DEFAULTS[:parse])
419
- .merge(parse_source_map_options)
421
+ .merge(parse_source_map_options(source_map_options))
420
422
  end
421
423
 
422
- def parse_source_map_options
423
- if @options[:source_map].respond_to?(:[])
424
- { :filename => @options[:source_map][:filename] }
424
+ def parse_source_map_options(source_map_options)
425
+ if source_map_options[:source_map].respond_to?(:[])
426
+ { :filename => source_map_options[:source_map][:filename] }
425
427
  else
426
428
  {}
427
429
  end
@@ -479,10 +481,10 @@ class Terser
479
481
  match && match[1]
480
482
  end
481
483
 
482
- def input_source_map(source, generate_map)
484
+ def input_source_map(source, generate_map, options)
483
485
  return nil unless generate_map
484
486
 
485
- source_map_options = @options[:source_map].is_a?(Hash) ? @options[:source_map] : {}
487
+ source_map_options = options[:source_map].is_a?(Hash) ? options[:source_map] : {}
486
488
  sanitize_map_root(source_map_options.fetch(:input_source_map) do
487
489
  url = extract_source_mapping_url(source)
488
490
  Base64.strict_decode64(url.split(",", 2)[-1]) if url && url.start_with?("data:")
@@ -12,6 +12,7 @@ class Terser
12
12
  options[:comments] ||= :none
13
13
  @options = options
14
14
  @cache_key = -"Terser:#{::Terser::VERSION}:#{VERSION}:#{::Sprockets::DigestUtils.digest(options)}"
15
+ @terser = ::Terser.new(@options)
15
16
  end
16
17
 
17
18
  def self.instance
@@ -31,9 +32,8 @@ class Terser
31
32
  if Gem::Version.new(::Sprockets::VERSION) >= Gem::Version.new('4.x')
32
33
  def call(input)
33
34
  input_options = { :source_map => { :filename => input[:filename] } }
34
- terser = ::Terser.new(@options.merge(input_options))
35
35
 
36
- js, map = terser.compile_with_map(input[:data])
36
+ js, map = @terser.compile_with_map(input[:data], input_options)
37
37
 
38
38
  map = ::Sprockets::SourceMapUtils.format_source_map(JSON.parse(map), input)
39
39
  map = ::Sprockets::SourceMapUtils.combine_source_maps(input[:metadata][:map], map)
@@ -42,8 +42,7 @@ class Terser
42
42
  end
43
43
  else
44
44
  def call(input)
45
- terser = ::Terser.new(@options)
46
- terser.compile(input[:data])
45
+ @terser.compile(input[:data])
47
46
  end
48
47
  end
49
48
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  class Terser
4
4
  # Current version of Terser.
5
- VERSION = "1.0.0"
5
+ VERSION = "1.0.1"
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Rosicky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2020-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: execjs