uglifier 4.0.2 → 4.2.0
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 +5 -5
- data/.github/workflows/ruby.yml +14 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +11 -10
- data/CHANGELOG.md +94 -1
- data/CONTRIBUTING.md +9 -4
- data/Gemfile +1 -1
- data/README.md +36 -8
- data/Rakefile +110 -17
- data/lib/uglifier.rb +81 -23
- data/lib/uglifier/version.rb +1 -1
- data/lib/uglify-harmony.js +21 -11
- data/lib/uglify.js +21 -11
- data/uglifier.gemspec +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6eb829d19c603a2d3760e02f0235d9d654f640111734caa9317449378e40fa26
|
|
4
|
+
data.tar.gz: '0774580cb6353b90d72e5febfdc6777bc05c6d6d773b09e4aab3b82178fe8acd'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 81f5c10b9dd4b9c84a6fe850dfb45b30bb25e0d9b04fa089ff745683028d1733e6b0b1c92846788af96197ee8774d8a7d105b424bc88f38b0c5abab5971bded0
|
|
7
|
+
data.tar.gz: 3120b0abc086792140a2cdb3a8ecae725327c8c8ea7b55c49d98be196e97de526087583c5bb2b6a0c169e86277474e8ae8a82d6a02cc23cd58cf0276defff03a
|
data/.rubocop.yml
CHANGED
|
@@ -6,6 +6,9 @@ AllCops:
|
|
|
6
6
|
- "vendor/**/*"
|
|
7
7
|
- "gemfiles/vendor/**/*"
|
|
8
8
|
|
|
9
|
+
Metrics/AbcSize:
|
|
10
|
+
Max: 20
|
|
11
|
+
|
|
9
12
|
Metrics/BlockLength:
|
|
10
13
|
Enabled: true
|
|
11
14
|
Exclude:
|
|
@@ -46,3 +49,6 @@ Style/Alias:
|
|
|
46
49
|
|
|
47
50
|
Style/MutableConstant:
|
|
48
51
|
Enabled: false
|
|
52
|
+
|
|
53
|
+
Style/ExpandPathArguments:
|
|
54
|
+
Enabled: false
|
data/.travis.yml
CHANGED
|
@@ -5,29 +5,30 @@ rvm:
|
|
|
5
5
|
- 1.9.3
|
|
6
6
|
- 2.0.0
|
|
7
7
|
- 2.1.10
|
|
8
|
-
- 2.2.
|
|
9
|
-
- 2.3.
|
|
10
|
-
- 2.4.
|
|
8
|
+
- 2.2.10
|
|
9
|
+
- 2.3.8
|
|
10
|
+
- 2.4.5
|
|
11
|
+
- 2.5.3
|
|
12
|
+
- 2.6.0
|
|
11
13
|
- ruby-head
|
|
12
|
-
- jruby-
|
|
13
|
-
- jruby-9.1.13.0
|
|
14
|
+
- jruby-9.2.5.0
|
|
14
15
|
before_install:
|
|
15
|
-
- gem install bundler -v 1.
|
|
16
|
+
- gem install bundler -v 1.17.3
|
|
16
17
|
git:
|
|
17
18
|
submodules: false
|
|
18
19
|
gemfile:
|
|
19
20
|
- Gemfile
|
|
20
21
|
matrix:
|
|
21
22
|
include:
|
|
22
|
-
- rvm: 2.
|
|
23
|
+
- rvm: 2.5.3
|
|
23
24
|
gemfile: gemfiles/rubyracer
|
|
24
|
-
- rvm: jruby-9.
|
|
25
|
+
- rvm: jruby-9.2.5.0
|
|
25
26
|
gemfile: gemfiles/rubyrhino
|
|
26
|
-
- rvm: 2.
|
|
27
|
+
- rvm: 2.5.3
|
|
27
28
|
gemfile: gemfiles/alaska
|
|
28
29
|
env: ALASKA=1
|
|
29
30
|
allow_failures:
|
|
30
|
-
- rvm: 2.
|
|
31
|
+
- rvm: 2.5.3
|
|
31
32
|
gemfile: gemfiles/alaska
|
|
32
33
|
env: ALASKA=1
|
|
33
34
|
- rvm: ruby-head
|
data/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,97 @@
|
|
|
1
|
-
## 4.0
|
|
1
|
+
## 4.2.0 (25 September 2019)
|
|
2
|
+
|
|
3
|
+
- show code context for error messages (#167)
|
|
4
|
+
- defer initialisation of ExecJS context to fix #165
|
|
5
|
+
|
|
6
|
+
## 4.1.20 (17 November 2018)
|
|
7
|
+
|
|
8
|
+
- update UglifyJS to [3.4.8](https://github.com/mishoo/UglifyJS2/compare/v3.4.9...v3.4.8)
|
|
9
|
+
|
|
10
|
+
## 4.1.19 (11 September 2018)
|
|
11
|
+
|
|
12
|
+
- update UglifyJS to [3.4.9](https://github.com/mishoo/UglifyJS2/compare/v3.4.7...v3.4.9)
|
|
13
|
+
|
|
14
|
+
## 4.1.18 (09 August 2018)
|
|
15
|
+
|
|
16
|
+
- update UglifyJS to [3.4.7](https://github.com/mishoo/UglifyJS2/compare/v3.4.6...v3.4.7)
|
|
17
|
+
|
|
18
|
+
## 4.1.17 (29 July 2018)
|
|
19
|
+
|
|
20
|
+
- update UglifyJS to [3.4.6](https://github.com/mishoo/UglifyJS2/compare/v3.4.5...v3.4.6)
|
|
21
|
+
|
|
22
|
+
## 4.1.16 (22 July 2018)
|
|
23
|
+
|
|
24
|
+
- update UglifyJS to [3.4.5](https://github.com/mishoo/UglifyJS2/compare/v3.4.4...v3.4.5)
|
|
25
|
+
|
|
26
|
+
## 4.1.15 (13 July 2018)
|
|
27
|
+
|
|
28
|
+
- update UglifyJS to [3.4.4](https://github.com/mishoo/UglifyJS2/compare/v3.4.3...v3.4.4)
|
|
29
|
+
|
|
30
|
+
## 4.1.14 (02 July 2018)
|
|
31
|
+
|
|
32
|
+
- update UglifyJS to [3.4.3](https://github.com/mishoo/UglifyJS2/compare/v3.4.2...v3.4.3)
|
|
33
|
+
|
|
34
|
+
## 4.1.13 (30 June 2018)
|
|
35
|
+
|
|
36
|
+
- update UglifyJS to [3.4.2](https://github.com/mishoo/UglifyJS2/compare/v3.4.1...v3.4.2)
|
|
37
|
+
|
|
38
|
+
## 4.1.12 (20 June 2018)
|
|
39
|
+
|
|
40
|
+
- update UglifyJS to [3.4.1](https://github.com/mishoo/UglifyJS2/compare/v3.4.0...v3.4.1)
|
|
41
|
+
|
|
42
|
+
## 4.1.11 (02 June 2018)
|
|
43
|
+
|
|
44
|
+
- update UglifyJS to [3.3.28](https://github.com/mishoo/UglifyJS2/compare/v3.3.22...v3.3.28)
|
|
45
|
+
|
|
46
|
+
## 4.1.10 (21 April 2018)
|
|
47
|
+
|
|
48
|
+
- update UglifyJS to [3.3.22](https://github.com/mishoo/UglifyJS2/compare/v3.3.20...v3.3.22)
|
|
49
|
+
|
|
50
|
+
## 4.1.9 (11 April 2018)
|
|
51
|
+
|
|
52
|
+
- update UglifyJS to [3.3.20](https://github.com/mishoo/UglifyJS2/compare/v3.3.16...v3.3.20)
|
|
53
|
+
|
|
54
|
+
## 4.1.8 (14 March 2018)
|
|
55
|
+
|
|
56
|
+
- update UglifyJS to [3.3.15](https://github.com/mishoo/UglifyJS2/compare/v3.3.14...v3.3.15)
|
|
57
|
+
|
|
58
|
+
## 4.1.7 (11 March 2018)
|
|
59
|
+
|
|
60
|
+
- update UglifyJS to [3.3.14](https://github.com/mishoo/UglifyJS2/compare/v3.3.10...v3.3.14)
|
|
61
|
+
|
|
62
|
+
## 4.1.6 (08 February 2018)
|
|
63
|
+
|
|
64
|
+
- update UglifyJS to [3.3.10](https://github.com/mishoo/UglifyJS2/compare/v3.3.9...v3.3.10)
|
|
65
|
+
- update uglify-es to [3.3.10](https://github.com/mishoo/UglifyJS2/compare/harmony-v3.3.9...harmony-v3.3.10)
|
|
66
|
+
|
|
67
|
+
## 4.1.5 (28 January 2018)
|
|
68
|
+
|
|
69
|
+
- update UglifyJS to [3.3.9](https://github.com/mishoo/UglifyJS2/compare/v3.3.8...v3.3.9)
|
|
70
|
+
- update uglify-es to [3.3.9](https://github.com/mishoo/UglifyJS2/compare/harmony-v3.3.8...harmony-v3.3.9)
|
|
71
|
+
|
|
72
|
+
## 4.1.4 (22 January 2018)
|
|
73
|
+
|
|
74
|
+
- update UglifyJS to [3.3.8](https://github.com/mishoo/UglifyJS2/compare/v3.3.5...v3.3.8)
|
|
75
|
+
- update uglify-es to [3.3.8](https://github.com/mishoo/UglifyJS2/compare/harmony-v3.3.5...harmony-v3.3.8)
|
|
76
|
+
|
|
77
|
+
## 4.1.3 (7 January 2018)
|
|
78
|
+
|
|
79
|
+
- update UglifyJS to 3.3.5
|
|
80
|
+
|
|
81
|
+
## 4.1.2 (30 December 2017)
|
|
82
|
+
|
|
83
|
+
- update UglifyJS to 3.3.4
|
|
84
|
+
|
|
85
|
+
## 4.1.1 (29 December 2017)
|
|
86
|
+
|
|
87
|
+
- update UglifyJS to 3.3.3
|
|
88
|
+
|
|
89
|
+
## 4.1.0 (28 December 2017)
|
|
90
|
+
|
|
91
|
+
- update UglifyJS to 3.3.2
|
|
92
|
+
- `cascade` compress option is merged into `collapse_vars`
|
|
93
|
+
|
|
94
|
+
## 4.0.2 (10 December 2017)
|
|
2
95
|
|
|
3
96
|
- update UglifyJS to 3.2.2
|
|
4
97
|
- show harmony suggestion when using for-of loops
|
data/CONTRIBUTING.md
CHANGED
|
@@ -28,14 +28,19 @@ Then run tests using rake:
|
|
|
28
28
|
|
|
29
29
|
git submodule update --init
|
|
30
30
|
|
|
31
|
-
After that, UglifyJS
|
|
31
|
+
After that, UglifyJS can be updated to a specific version with rake task.
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
rake uglifyjs:update VERSION=3.3.4
|
|
34
34
|
|
|
35
|
-
|
|
35
|
+
To compile JS with dependencies, run
|
|
36
36
|
|
|
37
|
-
|
|
37
|
+
rake uglifyjs:build
|
|
38
38
|
|
|
39
|
+
You can even write custom patches to UglifyJS in `vendor/uglifyjs` and `vendor/uglifyjs-harmony` directories and compile the bundles JS using the command above. However, for the changes to be releasable, they should be in UglifyJS repository.
|
|
40
|
+
|
|
41
|
+
To automatically update UglifyJS version and commit changes
|
|
42
|
+
|
|
43
|
+
rake uglifyjs VERSION=3.3.4
|
|
39
44
|
|
|
40
45
|
## Reporting issues
|
|
41
46
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
|
@@ -3,16 +3,44 @@
|
|
|
3
3
|
Ruby wrapper for [UglifyJS](https://github.com/mishoo/UglifyJS2) JavaScript
|
|
4
4
|
compressor.
|
|
5
5
|
|
|
6
|
-
UglifyJS currently is
|
|
7
|
-
ES6/Harmony support can be enabled with `:harmony => true` option.
|
|
8
|
-
See [UglifyJS2#448](https://github.com/mishoo/UglifyJS2/issues/448) for more
|
|
9
|
-
details.
|
|
6
|
+
UglifyJS currently is extensively tested with ES5, but also includes experimental ES6/ES2015+/Harmony support.
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
recommended. For directly minifying ES6 code, [Closure Compiler](https://github.com/documentcloud/closure-compiler) is a more suitable option.
|
|
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.
|
|
13
9
|
|
|
14
10
|
[](https://travis-ci.org/lautis/uglifier)
|
|
15
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
|
+
```
|
|
27
|
+
|
|
28
|
+
### Rails
|
|
29
|
+
|
|
30
|
+
When used in Rails, replace
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
config.assets.js_compressor = :uglifier
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
with
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
config.assets.js_compressor = Uglifier.new(harmony: true)
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
in `config/environments/production.rb`.
|
|
43
|
+
|
|
16
44
|
## Installation
|
|
17
45
|
|
|
18
46
|
Uglifier is available as a ruby gem.
|
|
@@ -75,7 +103,7 @@ Available options and their defaults are
|
|
|
75
103
|
},
|
|
76
104
|
:mangle => {
|
|
77
105
|
:eval => false, # Mangle names when eval of when is used in scope
|
|
78
|
-
:
|
|
106
|
+
:reserved => ["$super"], # Argument names to be excluded from mangling
|
|
79
107
|
:sort => false, # Assign shorter names to most frequently used variables. Often results in bigger output after gzip.
|
|
80
108
|
:toplevel => false, # Mangle names declared in the toplevel scope
|
|
81
109
|
:properties => false, # Mangle property names
|
|
@@ -107,7 +135,6 @@ Available options and their defaults are
|
|
|
107
135
|
:hoist_vars => false, # Hoist var declarations
|
|
108
136
|
:if_return => true, # Optimizations for if/return and if/continue
|
|
109
137
|
:join_vars => true, # Join consecutive var statements
|
|
110
|
-
:cascade => true, # Cascade sequences
|
|
111
138
|
:collapse_vars => false, # Collapse single-use var and const definitions when possible.
|
|
112
139
|
:reduce_funcs => false, # Inline single-use functions as function expressions. Depends on reduce_vars.
|
|
113
140
|
:reduce_vars => false, # Collapse variables assigned with and used as constant values.
|
|
@@ -141,6 +168,7 @@ Available options and their defaults are
|
|
|
141
168
|
:output_filename => nil, # The filename or URL where the minified output can be found
|
|
142
169
|
:input_source_map => nil # The contents of the source map describing the input
|
|
143
170
|
},
|
|
171
|
+
:error_context_lines => 8, # How many context lines surrounding the error line. Env var ERROR_CONTEXT_LINES overrides this option
|
|
144
172
|
:harmony => false # Enable ES6/Harmony mode (experimental). Disabling mangling and compressing is recommended with Harmony mode.
|
|
145
173
|
}
|
|
146
174
|
```
|
data/Rakefile
CHANGED
|
@@ -7,34 +7,127 @@ RSpec::Core::RakeTask.new(:spec) do |spec|
|
|
|
7
7
|
spec.pattern = FileList['spec/**/*_spec.rb']
|
|
8
8
|
end
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
def version
|
|
11
|
+
ENV.fetch('VERSION')
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
HEADER = "## next"
|
|
15
|
+
|
|
16
|
+
def changelog_tail
|
|
17
|
+
changelog = File.read("CHANGELOG.md")
|
|
18
|
+
if changelog.start_with?(HEADER)
|
|
19
|
+
changelog[HEADER.length + 2..-1]
|
|
20
|
+
else
|
|
21
|
+
"\n" + changelog
|
|
14
22
|
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def compare_url(from, to)
|
|
26
|
+
"https://github.com/mishoo/UglifyJS2/compare/#{from}...#{to}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def previous_version
|
|
30
|
+
match = File.read("CHANGELOG.md").scan(/- update UglifyJS to \[(.*)\]\(/)
|
|
31
|
+
match ? match[0][0].chomp : nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def git_commit(files, message)
|
|
35
|
+
`git add #{files.join(' ')}`
|
|
36
|
+
`git commit -S -m "#{message.gsub('"', "\\\"")}"`
|
|
37
|
+
end
|
|
15
38
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
39
|
+
# rubocop:disable Metrics/BlockLength
|
|
40
|
+
namespace :uglifyjs do
|
|
41
|
+
desc "Update UglifyJS source to version specified in VERSION environment variable"
|
|
42
|
+
task :update do
|
|
43
|
+
cd 'vendor/uglifyjs' do
|
|
44
|
+
`git fetch && git checkout v#{version}`
|
|
45
|
+
end
|
|
19
46
|
end
|
|
20
47
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
48
|
+
desc "Rebuild lib/uglify*.js"
|
|
49
|
+
task :build do
|
|
50
|
+
cd 'vendor/source-map/' do
|
|
51
|
+
`npm install --no-package-lock --no-save`
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
cd 'vendor/uglifyjs/' do
|
|
55
|
+
# required to run ./uglifyjs2 --self; not bundled.
|
|
56
|
+
`npm install --no-package-lock --no-save`
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
cd 'vendor/uglifyjs-harmony' do
|
|
60
|
+
# required to run ./uglifyjs2 --self; not bundled.
|
|
61
|
+
`npm install --no-package-lock --no-save`
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
FileUtils.cp("vendor/source-map/dist/source-map.js", "lib/source-map.js")
|
|
65
|
+
|
|
66
|
+
source = `./vendor/uglifyjs/bin/uglifyjs --self --comments /Copyright/`
|
|
67
|
+
File.write("lib/uglify.js", source)
|
|
68
|
+
|
|
69
|
+
harmony_source = `./vendor/uglifyjs-harmony/bin/uglifyjs --self --comments /Copyright/`
|
|
70
|
+
File.write("lib/uglify-harmony.js", harmony_source)
|
|
71
|
+
|
|
72
|
+
FileUtils.cp("vendor/split/split.js", "lib/split.js")
|
|
73
|
+
`patch -p1 -i patches/es5-string-split.patch`
|
|
24
74
|
end
|
|
25
75
|
|
|
26
|
-
|
|
76
|
+
desc "Add UglifyJS version bump to changelog"
|
|
77
|
+
task :changelog do
|
|
78
|
+
url = compare_url("v#{previous_version}", "v#{version}")
|
|
79
|
+
item = "- update UglifyJS to [#{version}](#{url})"
|
|
80
|
+
changelog = "#{HEADER}\n\n#{item}\n#{changelog_tail}"
|
|
81
|
+
File.write("CHANGELOG.md", changelog)
|
|
82
|
+
end
|
|
27
83
|
|
|
28
|
-
|
|
29
|
-
|
|
84
|
+
desc "Commit changes from UglifyJS version bump"
|
|
85
|
+
task :commit do
|
|
86
|
+
files = [
|
|
87
|
+
'CHANGELOG.md',
|
|
88
|
+
'lib/uglify.js',
|
|
89
|
+
'lib/uglify-harmony.js',
|
|
90
|
+
'vendor/uglifyjs',
|
|
91
|
+
'vendor/uglifyjs-harmony'
|
|
92
|
+
]
|
|
93
|
+
git_commit(files, "Update UglifyJS to #{version}")
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
# rubocop:enable Metrics/BlockLength
|
|
97
|
+
|
|
98
|
+
desc "Update UglifyJS to version specified in VERSION environment variable"
|
|
99
|
+
task :uglifyjs => ['uglifyjs:update', 'uglifyjs:build', 'uglifyjs:changelog', 'uglifyjs:commit']
|
|
30
100
|
|
|
31
|
-
|
|
32
|
-
|
|
101
|
+
namespace :version do
|
|
102
|
+
desc "Write version to CHANGELOG.md"
|
|
103
|
+
task :changelog do
|
|
104
|
+
content = File.read("CHANGELOG.md")
|
|
105
|
+
date = Time.now.strftime("%d %B %Y")
|
|
106
|
+
File.write("CHANGELOG.md", content.gsub("## next", "## #{version} (#{date})"))
|
|
107
|
+
end
|
|
33
108
|
|
|
34
|
-
|
|
35
|
-
|
|
109
|
+
desc "Write version to uglifier.rb"
|
|
110
|
+
task :ruby do
|
|
111
|
+
file = "lib/uglifier/version.rb"
|
|
112
|
+
content = File.read("lib/uglifier/version.rb")
|
|
113
|
+
File.write(file, content.gsub(/VERSION = "(.*)"/, "VERSION = \"#{version}\""))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
desc "Commit changes from Uglifier version bump"
|
|
117
|
+
task :commit do
|
|
118
|
+
files = ["CHANGELOG.md", "lib/uglifier/version.rb"]
|
|
119
|
+
git_commit(files, "Bump version to #{version}")
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
desc "Create git tag for version"
|
|
123
|
+
task :tag do
|
|
124
|
+
`git tag -s -m "Version #{version}" v#{version}`
|
|
125
|
+
end
|
|
36
126
|
end
|
|
37
127
|
|
|
128
|
+
desc "Update Uglifier to version specified in VERSION environment variable"
|
|
129
|
+
task :version => ['version:changelog', 'version:ruby', 'version:commit', 'version:tag']
|
|
130
|
+
|
|
38
131
|
begin
|
|
39
132
|
require 'rubocop/rake_task'
|
|
40
133
|
RuboCop::RakeTask.new(:rubocop)
|
data/lib/uglifier.rb
CHANGED
|
@@ -72,7 +72,6 @@ class Uglifier
|
|
|
72
72
|
:hoist_vars => false, # Hoist var declarations
|
|
73
73
|
:if_return => true, # Optimizations for if/return and if/continue
|
|
74
74
|
:join_vars => true, # Join consecutive var statements
|
|
75
|
-
:cascade => true, # Cascade sequences
|
|
76
75
|
:collapse_vars => true, # Collapse single-use var and const definitions when possible.
|
|
77
76
|
:reduce_funcs => false, # Inline single-use functions as function expressions. Depends on reduce_vars.
|
|
78
77
|
:reduce_vars => false, # Collapse variables assigned with and used as constant values.
|
|
@@ -99,6 +98,7 @@ class Uglifier
|
|
|
99
98
|
:toplevel => false,
|
|
100
99
|
:ie8 => true, # Generate safe code for IE8
|
|
101
100
|
:source_map => false, # Generate source map
|
|
101
|
+
:error_context_lines => 8, # How many lines surrounding the error line
|
|
102
102
|
:harmony => false # Enable ES6/Harmony mode (experimental). Disabling mangling and compressing is recommended with Harmony mode.
|
|
103
103
|
}
|
|
104
104
|
|
|
@@ -150,9 +150,6 @@ class Uglifier
|
|
|
150
150
|
raise ArgumentError, "Invalid option: #{missing}"
|
|
151
151
|
end
|
|
152
152
|
@options = options
|
|
153
|
-
|
|
154
|
-
source = harmony? ? source_with(HarmonySourcePath) : source_with(SourcePath)
|
|
155
|
-
@context = ExecJS.compile(source)
|
|
156
153
|
end
|
|
157
154
|
|
|
158
155
|
# Minifies JavaScript code
|
|
@@ -181,6 +178,13 @@ class Uglifier
|
|
|
181
178
|
|
|
182
179
|
private
|
|
183
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
|
+
|
|
184
188
|
def source_map_comments
|
|
185
189
|
return '' unless @options[:source_map].respond_to?(:[])
|
|
186
190
|
|
|
@@ -189,9 +193,7 @@ class Uglifier
|
|
|
189
193
|
suffix += "\n//# sourceMappingURL=" + @options[:source_map][:map_url]
|
|
190
194
|
end
|
|
191
195
|
|
|
192
|
-
if @options[:source_map][:url]
|
|
193
|
-
suffix += "\n//# sourceURL=" + @options[:source_map][:url]
|
|
194
|
-
end
|
|
196
|
+
suffix += "\n//# sourceURL=" + @options[:source_map][:url] if @options[:source_map][:url]
|
|
195
197
|
suffix
|
|
196
198
|
end
|
|
197
199
|
|
|
@@ -216,25 +218,77 @@ class Uglifier
|
|
|
216
218
|
:ie8 => ie8?
|
|
217
219
|
}
|
|
218
220
|
|
|
219
|
-
parse_result(
|
|
221
|
+
parse_result(context.call("uglifier", options), generate_map, options)
|
|
220
222
|
end
|
|
221
223
|
|
|
222
224
|
def harmony?
|
|
223
225
|
@options[:harmony]
|
|
224
226
|
end
|
|
225
227
|
|
|
226
|
-
def
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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)
|
|
231
263
|
else
|
|
232
|
-
""
|
|
264
|
+
"#{options[:line_format] % (options[:base_index] + index + 1)}#{line}"
|
|
233
265
|
end
|
|
266
|
+
end
|
|
267
|
+
end
|
|
268
|
+
|
|
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}"
|
|
234
288
|
end
|
|
235
289
|
|
|
236
|
-
def parse_result(result, generate_map)
|
|
237
|
-
raise Error, error_message(result) if result.has_key?('error')
|
|
290
|
+
def parse_result(result, generate_map, options)
|
|
291
|
+
raise Error, error_message(result, options) if result.has_key?('error')
|
|
238
292
|
|
|
239
293
|
if generate_map
|
|
240
294
|
[result['code'] + source_map_comments, result['map']]
|
|
@@ -347,10 +401,16 @@ class Uglifier
|
|
|
347
401
|
end
|
|
348
402
|
|
|
349
403
|
def output_options
|
|
350
|
-
DEFAULTS[:output].merge(@options[:output] || {})
|
|
351
|
-
:comments => comment_options,
|
|
352
|
-
|
|
353
|
-
|
|
404
|
+
migrate_braces(DEFAULTS[:output].merge(@options[:output] || {}))
|
|
405
|
+
.merge(:comments => comment_options, :quote_style => quote_style)
|
|
406
|
+
end
|
|
407
|
+
|
|
408
|
+
def migrate_braces(options)
|
|
409
|
+
if harmony?
|
|
410
|
+
options
|
|
411
|
+
else
|
|
412
|
+
options.merge(:braces => options[:bracketize]).delete_if { |key| key == :bracketize }
|
|
413
|
+
end
|
|
354
414
|
end
|
|
355
415
|
|
|
356
416
|
def ie8?
|
|
@@ -450,9 +510,7 @@ class Uglifier
|
|
|
450
510
|
source_map_options = @options[:source_map].is_a?(Hash) ? @options[:source_map] : {}
|
|
451
511
|
sanitize_map_root(source_map_options.fetch(:input_source_map) do
|
|
452
512
|
url = extract_source_mapping_url(source)
|
|
453
|
-
if url && url.start_with?("data:")
|
|
454
|
-
Base64.strict_decode64(url.split(",", 2)[-1])
|
|
455
|
-
end
|
|
513
|
+
Base64.strict_decode64(url.split(",", 2)[-1]) if url && url.start_with?("data:")
|
|
456
514
|
end)
|
|
457
515
|
rescue ArgumentError, JSON::ParserError
|
|
458
516
|
nil
|