terser 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sprockets/digest_utils'
4
+ require 'sprockets/source_map_utils' if Gem::Version.new(::Sprockets::VERSION) >= Gem::Version.new('4.x')
5
+
6
+ class Terser
7
+ # A wrapper for Sprockets
8
+ class Compressor
9
+ VERSION = '1'
10
+
11
+ def initialize(options = {})
12
+ options[:comments] ||= :none
13
+ @options = options
14
+ @cache_key = -"Terser:#{::Terser::VERSION}:#{VERSION}:#{::Sprockets::DigestUtils.digest(options)}"
15
+ end
16
+
17
+ def self.instance
18
+ @instance ||= new
19
+ end
20
+
21
+ def self.call(input)
22
+ instance.call(input)
23
+ end
24
+
25
+ def self.cache_key
26
+ instance.cache_key
27
+ end
28
+
29
+ attr_reader :cache_key
30
+
31
+ if Gem::Version.new(::Sprockets::VERSION) >= Gem::Version.new('4.x')
32
+ def call(input)
33
+ input_options = { :source_map => { :filename => input[:filename] } }
34
+ terser = ::Terser.new(@options.merge(input_options))
35
+
36
+ js, map = terser.compile_with_map(input[:data])
37
+
38
+ map = ::Sprockets::SourceMapUtils.format_source_map(JSON.parse(map), input)
39
+ map = ::Sprockets::SourceMapUtils.combine_source_maps(input[:metadata][:map], map)
40
+
41
+ { :data => js, :map => map }
42
+ end
43
+ else
44
+ def call(input)
45
+ terser = ::Terser.new(@options)
46
+ terser.compile(input[:data])
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Terser
4
+ # Current version of Terser.
5
+ VERSION = "1.0.0"
6
+ end
@@ -0,0 +1,51 @@
1
+ // Set source-map.js sourceMap to terser.js MOZ_SourceMap
2
+ MOZ_SourceMap = sourceMap;
3
+
4
+ function comments(option) {
5
+ if (Object.prototype.toString.call(option) === '[object Array]') {
6
+ return new RegExp(option[0], option[1]);
7
+ } else if (option == "jsdoc") {
8
+ return function(node, comment) {
9
+ if (comment.type == "comment2") {
10
+ return /@preserve|@license|@cc_on/i.test(comment.value);
11
+ } else {
12
+ return false;
13
+ }
14
+ };
15
+ } else {
16
+ return option;
17
+ }
18
+ }
19
+
20
+ function regexOption(options) {
21
+ if (typeof options === 'object' && options.regex) {
22
+ return new RegExp(options.regex[0], options.regex[1]);
23
+ } else {
24
+ return null;
25
+ }
26
+ }
27
+
28
+ function terser_wrapper(options) {
29
+ var source = options.source;
30
+ options.output.comments = comments(options.output.comments);
31
+
32
+ if (options.mangle) {
33
+ if (options.mangle.properties) {
34
+ options.mangle.properties.regex = regexOption(options.mangle.properties);
35
+ }
36
+ }
37
+ delete options.source;
38
+
39
+
40
+ var inputFilename = '0';
41
+ if (options.sourceMap) {
42
+ inputFilename = options.sourceMap.input;
43
+ delete options.sourceMap.input;
44
+ }
45
+
46
+ var inputs = {};
47
+ inputs[inputFilename] = source;
48
+ var result = Terser.minify(inputs, options);
49
+ //if (result.error) throw result.error;
50
+ return result;
51
+ }
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ lib = File.expand_path('../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'terser/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = "terser"
10
+ spec.version = Terser::VERSION
11
+ spec.authors = ["Pavel Rosicky"]
12
+ spec.email = ["pdahorek@seznam.cz"]
13
+ spec.homepage = "http://github.com/ahorek/terser-ruby"
14
+ spec.summary = "Ruby wrapper for Terser JavaScript compressor"
15
+ spec.description = "Terser minifies JavaScript files by wrapping \
16
+ TerserJS to be accessible in Ruby"
17
+ spec.license = "MIT"
18
+
19
+ spec.required_ruby_version = '>= 2.3.0'
20
+
21
+ spec.extra_rdoc_files = [
22
+ "LICENSE.txt",
23
+ "README.md",
24
+ "CHANGELOG.md",
25
+ "CONTRIBUTING.md"
26
+ ]
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
29
+ f.match(%r{^(spec|vendor|gemfiles|patches|benchmark)/})
30
+ end
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_runtime_dependency "execjs", [">= 0.3.0", "< 3"]
34
+ spec.add_development_dependency "bundler", ">= 1.3"
35
+ spec.add_development_dependency "rake", "~> 12.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ spec.add_development_dependency "sourcemap", "~> 0.1.1"
38
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terser
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Pavel Rosicky
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: execjs
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.3.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '1.3'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '12.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: sourcemap
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: 0.1.1
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: 0.1.1
89
+ description: Terser minifies JavaScript files by wrapping TerserJS to be accessible
90
+ in Ruby
91
+ email:
92
+ - pdahorek@seznam.cz
93
+ executables: []
94
+ extensions: []
95
+ extra_rdoc_files:
96
+ - LICENSE.txt
97
+ - README.md
98
+ - CHANGELOG.md
99
+ - CONTRIBUTING.md
100
+ files:
101
+ - ".document"
102
+ - ".gitignore"
103
+ - ".gitmodules"
104
+ - ".rspec"
105
+ - ".rubocop.yml"
106
+ - ".travis.yml"
107
+ - ".yardopts"
108
+ - CHANGELOG.md
109
+ - CONTRIBUTING.md
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - lib/es5.js
115
+ - lib/source-map.js
116
+ - lib/split.js
117
+ - lib/terser.js
118
+ - lib/terser.rb
119
+ - lib/terser/compressor.rb
120
+ - lib/terser/version.rb
121
+ - lib/terser_wrapper.js
122
+ - terser.gemspec
123
+ homepage: http://github.com/ahorek/terser-ruby
124
+ licenses:
125
+ - MIT
126
+ metadata: {}
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: 2.3.0
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.1.2
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Ruby wrapper for Terser JavaScript compressor
146
+ test_files: []