uglifier 1.3.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of uglifier might be problematic. Click here for more details.

@@ -0,0 +1,86 @@
1
+ # encoding: UTF-8
2
+ require 'stringio'
3
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
+
5
+ describe "Uglifier" do
6
+ it "generates source maps" do
7
+ source = File.open("lib/uglify.js", "r:UTF-8").read
8
+ minified, map = Uglifier.new.compile_with_map(source)
9
+ minified.length.should < source.length
10
+ map.length.should > 0
11
+ lambda {
12
+ JSON.parse(map)
13
+ }.should_not raise_error
14
+ end
15
+
16
+ it "generates source maps with the correct meta-data" do
17
+ source = <<-JS
18
+ function hello () {
19
+ function world () {
20
+ return 2;
21
+ };
22
+
23
+ return world() + world();
24
+ };
25
+ JS
26
+
27
+ minified, map = Uglifier.compile_with_map(source,
28
+ :source_filename => "ahoy.js",
29
+ :output_filename => "ahoy.min.js",
30
+ :source_root => "http://localhost/")
31
+
32
+ map = SourceMap.from_s(map)
33
+ map.file.should == "ahoy.min.js"
34
+ map.sources.should == ["ahoy.js"]
35
+ map.names.should == ["hello", "world"]
36
+ map.source_root.should == "http://localhost/"
37
+ map.mappings.first[:generated_line].should == 1
38
+ end
39
+
40
+ it "should skip copyright lines in source maps" do
41
+ source = <<-JS
42
+ /* @copyright Conrad Irwin */
43
+ function hello () {
44
+ function world () {
45
+ return 2;
46
+ };
47
+
48
+ return world() + world();
49
+ };
50
+ JS
51
+
52
+ minified, map = Uglifier.compile_with_map(source,
53
+ :source_filename => "ahoy.js",
54
+ :source_root => "http://localhost/")
55
+ map = SourceMap.from_s(map)
56
+ map.mappings.first[:generated_line].should == 2
57
+ end
58
+
59
+ it "should be able to handle an input source map" do
60
+ source = <<-JS
61
+ function hello () {
62
+ function world () {
63
+ return 2;
64
+ };
65
+
66
+ return world() + world();
67
+ };
68
+ JS
69
+
70
+ minified1, map1 = Uglifier.compile_with_map(source,
71
+ :source_filename => "ahoy.js",
72
+ :source_root => "http://localhost/",
73
+ :mangle => false)
74
+
75
+ minified2, map2 = Uglifier.compile_with_map(source,
76
+ :input_source_map => map1,
77
+ :mangle => true)
78
+
79
+ minified1.lines.to_a.length.should == 1
80
+
81
+ map = SourceMap.from_s(map2)
82
+ map.sources.should == ["http://localhost/ahoy.js"]
83
+ map.mappings.first[:source_line].should == 1
84
+ map.mappings.last[:source_line].should == 6
85
+ end
86
+ end
@@ -1,6 +1,7 @@
1
1
  # encoding: UTF-8
2
2
  require 'uglifier'
3
3
  require 'rspec'
4
+ require 'source_map'
4
5
 
5
6
  # Requires supporting files with custom matchers and macros, etc,
6
7
  # in ./support/ and its subdirectories.
@@ -13,64 +13,113 @@ describe "Uglifier" do
13
13
  end
14
14
 
15
15
  it "throws an exception when compilation fails" do
16
- lambda {
16
+ expect {
17
17
  Uglifier.new.compile(")(")
18
- }.should raise_error(Uglifier::Error)
18
+ }.to raise_error(Uglifier::Error)
19
19
  end
20
20
 
21
- it "doesn't omit null character in strings" do
22
- Uglifier.new.compile('var foo="\0bar"').should match(/(\0|\\0)/)
21
+ it "throws an exception on invalid option" do
22
+ expect {
23
+ Uglifier.new(:foo => true)
24
+ }.to raise_error(ArgumentError)
23
25
  end
24
26
 
25
- it "doesn't try to mangle $super by default to avoid breaking PrototypeJS" do
26
- Uglifier.new.compile('function foo($super) {return $super}').should include("$super")
27
+ it "doesn't omit null character in strings" do
28
+ Uglifier.new.compile('var foo="\0bar"').should match(/(\0|\\0)/)
27
29
  end
28
30
 
29
31
  it "adds trailing semicolon to minified source" do
30
- source = "function id(i) {return i;};"
32
+ source = "(function id(i) {return i;}());"
31
33
  Uglifier.new.compile(source)[-1].should eql(";"[0])
32
34
  end
33
35
 
34
- describe "Copyright Preservation" do
35
- before :all do
36
- @source = <<-EOS
37
- /* Copyright Notice */
36
+ describe "argument name mangling" do
37
+ it "doesn't try to mangle $super by default to avoid breaking PrototypeJS" do
38
+ Uglifier.compile('function foo($super) {return $super}').should include("$super")
39
+ end
40
+
41
+ it "allows variables to be excluded from mangling" do
42
+ code = "function bar(foo) {return foo + 'bar'};"
43
+ Uglifier.compile(code, :mangle => {:except => ["foo"]}).should include("(foo)")
44
+ end
45
+
46
+ it "skips mangling when set to false" do
47
+ code = "function bar(foo) {return foo + 'bar'};"
48
+ Uglifier.compile(code, :mangle => false).should include("(foo)")
49
+ end
50
+
51
+ it "mangles argumen names by default" do
52
+ code = "function bar(foo) {return foo + 'bar'};"
53
+ Uglifier.compile(code, :mangle => true).should_not include("(foo)")
54
+ end
55
+ end
56
+
57
+ describe "comment preservation" do
58
+ let(:source) {
59
+ <<-EOS
60
+ /* @preserve Copyright Notice */
38
61
  /* (c) 2011 */
39
62
  // INCLUDED
40
63
  function identity(p) { return p; }
64
+ /* Another Copyright */
65
+ function add(a, b) { return a + b; }
41
66
  EOS
42
- @minified = Uglifier.compile(@source, :copyright => true)
43
- end
67
+ }
44
68
 
45
- it "preserves copyright notice" do
46
- @minified.should match /Copyright Notice/
47
- end
69
+ describe ":copyright" do
70
+ subject { Uglifier.compile(source, :comments => :copyright) }
48
71
 
49
- it "handles multiple copyright blocks" do
50
- @minified.should match /\(c\) 2011/
72
+ it "preserves comments with string Copyright" do
73
+ subject.should match /Copyright Notice/
74
+ subject.should match /Another Copyright/
75
+ end
76
+
77
+ it "ignores other comments" do
78
+ subject.should_not match /INCLUDED/
79
+ end
51
80
  end
52
81
 
53
- it "does include different comment types" do
54
- @minified.should match /INCLUDED/
82
+ describe ":jsdoc" do
83
+ subject { Uglifier.compile(source, :output => {:comments => :jsdoc}) }
84
+
85
+ it "preserves jsdoc license/preserve blocks" do
86
+ subject.should match /Copyright Notice/
87
+ end
88
+
89
+ it "ignores other comments" do
90
+ subject.should_not match /Another Copyright/
91
+ end
55
92
  end
56
93
 
57
- it "puts comments on own lines" do
58
- @minified.split("\n").should have(4).items
94
+ describe ":all" do
95
+ subject { Uglifier.compile(source, :comments => :all) }
96
+
97
+ it "preserves all comments" do
98
+ subject.should match /INCLUDED/
99
+ subject.should match /2011/
100
+ end
59
101
  end
60
102
 
61
- it "omits copyright notification if copyright parameter is set to false" do
62
- Uglifier.compile(@source, :copyright => false).should_not match /Copyright/
103
+ describe ":none" do
104
+ subject { Uglifier.compile(source, :comments => :none) }
105
+
106
+ it "omits all comments" do
107
+ subject.should_not match /\/\//
108
+ subject.should_not match /\/\*/
109
+ end
63
110
  end
64
- end
65
111
 
66
- it "does additional squeezing when unsafe options is true" do
67
- unsafe_input = "function a(b){b.toString();}"
68
- Uglifier.new(:unsafe => true).compile(unsafe_input).length.should < Uglifier.new(:unsafe => false).compile(unsafe_input).length
69
- end
112
+ describe "regular expression" do
113
+ subject { Uglifier.compile(source, :comments => /included/i) }
114
+
115
+ it "matches comment blocks with regex" do
116
+ subject.should match /INCLUDED/
117
+ end
70
118
 
71
- it "mangles variables only if mangle is set to true" do
72
- code = "function longFunctionName(){};"
73
- Uglifier.new(:mangle => false).compile(code).length.should == code.length
119
+ it "omits other blocks" do
120
+ subject.should_not match /2011/
121
+ end
122
+ end
74
123
  end
75
124
 
76
125
  it "squeezes code only if squeeze is set to true" do
@@ -78,46 +127,29 @@ describe "Uglifier" do
78
127
  Uglifier.compile(code, :squeeze => false).length.should > Uglifier.compile(code, :squeeze => true).length
79
128
  end
80
129
 
81
- it "should allow top level variables to be mangled" do
82
- code = "var foo = 123"
83
- Uglifier.compile(code, :toplevel => true).should_not include("var foo")
84
- end
85
-
86
- it "allows variables to be excluded from mangling" do
87
- code = "var foo = {bar: 123};"
88
- Uglifier.compile(code, :except => ["foo"], :toplevel => true).should include("var foo")
89
- end
90
-
91
- it "allows disabling of function name mangling" do
92
- code = "function sample() {var bar = 1; function foo() { return bar; }}"
93
- mangled = Uglifier.compile(code, :mangle => :vars)
94
- mangled.should include("foo()")
95
- mangled.should_not include("bar")
96
- end
97
-
98
130
  it "honors max line length" do
99
- code = "var foo = 123;var bar = 123456"
100
- Uglifier.compile(code, :max_line_length => 8).split("\n").length.should == 2
131
+ code = "var foo = 123;function bar() { return foo; }"
132
+ Uglifier.compile(code, :output => {:max_line_len => 16}, :compress => false).split("\n").length.should == 2
101
133
  end
102
134
 
103
- it "lifts vars to top of the scope" do
135
+ it "hoists vars to top of the scope" do
104
136
  code = "function something() { var foo = 123; foo = 1234; var bar = 123456; return foo + bar}"
105
- Uglifier.compile(code, :lift_vars => true).should match /var \w,\w/
137
+ Uglifier.compile(code, :compress => {:hoist_vars => true}).should match /var \w,\w/
106
138
  end
107
139
 
108
140
  it "can be configured to output only ASCII" do
109
141
  code = "function emoji() { return '\\ud83c\\ude01'; }"
110
- Uglifier.compile(code, :ascii_only => true).should include("\\ud83c\\ude01")
142
+ Uglifier.compile(code, :output => {:ascii_only => true}).should include("\\ud83c\\ude01")
111
143
  end
112
144
 
113
145
  it "escapes </script when asked to" do
114
146
  code = "function test() { return '</script>';}"
115
- Uglifier.compile(code, :inline_script => true).should_not include("</script>")
147
+ Uglifier.compile(code, :output => {:inline_script => true}).should_not include("</script>")
116
148
  end
117
149
 
118
150
  it "quotes keys" do
119
151
  code = "var a = {foo: 1}"
120
- Uglifier.compile(code, :quote_keys => true).should include('"foo"')
152
+ Uglifier.compile(code, :output => {:quote_keys => true}).should include('"foo"')
121
153
  end
122
154
 
123
155
  it "handles constant definitions" do
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "uglifier"
8
- s.version = "1.3.0"
8
+ s.version = "2.0.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ville Lautanala"]
12
- s.date = "2012-09-02"
12
+ s.date = "2013-04-06"
13
13
  s.email = "lautis@gmail.com"
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
@@ -20,20 +20,22 @@ Gem::Specification.new do |s|
20
20
  ".gitmodules",
21
21
  ".rspec",
22
22
  ".travis.yml",
23
+ "CONTRIBUTING.md",
23
24
  "Gemfile",
24
25
  "LICENSE.txt",
25
26
  "README.md",
26
27
  "Rakefile",
27
28
  "VERSION",
28
- "build.js",
29
29
  "lib/es5.js",
30
30
  "lib/uglifier.rb",
31
31
  "lib/uglify.js",
32
+ "spec/source_map_spec.rb",
32
33
  "spec/spec_helper.rb",
33
34
  "spec/uglifier_spec.rb",
34
35
  "uglifier.gemspec"
35
36
  ]
36
37
  s.homepage = "http://github.com/lautis/uglifier"
38
+ s.licenses = ["MIT"]
37
39
  s.require_paths = ["lib"]
38
40
  s.rubygems_version = "1.8.23"
39
41
  s.summary = "Ruby wrapper for UglifyJS JavaScript compressor"
@@ -47,14 +49,16 @@ Gem::Specification.new do |s|
47
49
  s.add_development_dependency(%q<rspec>, ["~> 2.7"])
48
50
  s.add_development_dependency(%q<bundler>, ["~> 1.0"])
49
51
  s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
50
- s.add_development_dependency(%q<rdoc>, ["~> 3.11"])
52
+ s.add_development_dependency(%q<rdoc>, [">= 3.11"])
53
+ s.add_development_dependency(%q<source_map>, [">= 0"])
51
54
  else
52
55
  s.add_dependency(%q<execjs>, [">= 0.3.0"])
53
56
  s.add_dependency(%q<multi_json>, [">= 1.0.2", "~> 1.0"])
54
57
  s.add_dependency(%q<rspec>, ["~> 2.7"])
55
58
  s.add_dependency(%q<bundler>, ["~> 1.0"])
56
59
  s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
57
- s.add_dependency(%q<rdoc>, ["~> 3.11"])
60
+ s.add_dependency(%q<rdoc>, [">= 3.11"])
61
+ s.add_dependency(%q<source_map>, [">= 0"])
58
62
  end
59
63
  else
60
64
  s.add_dependency(%q<execjs>, [">= 0.3.0"])
@@ -62,7 +66,8 @@ Gem::Specification.new do |s|
62
66
  s.add_dependency(%q<rspec>, ["~> 2.7"])
63
67
  s.add_dependency(%q<bundler>, ["~> 1.0"])
64
68
  s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
65
- s.add_dependency(%q<rdoc>, ["~> 3.11"])
69
+ s.add_dependency(%q<rdoc>, [">= 3.11"])
70
+ s.add_dependency(%q<source_map>, [">= 0"])
66
71
  end
67
72
  end
68
73
 
metadata CHANGED
@@ -1,35 +1,34 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uglifier
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
5
4
  prerelease:
5
+ version: 2.0.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ville Lautanala
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-02 00:00:00.000000000 Z
12
+ date: 2013-04-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: execjs
16
- requirement: !ruby/object:Gem::Requirement
15
+ version_requirements: !ruby/object:Gem::Requirement
17
16
  none: false
18
17
  requirements:
19
18
  - - ! '>='
20
19
  - !ruby/object:Gem::Version
21
20
  version: 0.3.0
22
- type: :runtime
21
+ name: execjs
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
23
+ requirement: !ruby/object:Gem::Requirement
25
24
  none: false
26
25
  requirements:
27
26
  - - ! '>='
28
27
  - !ruby/object:Gem::Version
29
28
  version: 0.3.0
29
+ type: :runtime
30
30
  - !ruby/object:Gem::Dependency
31
- name: multi_json
32
- requirement: !ruby/object:Gem::Requirement
31
+ version_requirements: !ruby/object:Gem::Requirement
33
32
  none: false
34
33
  requirements:
35
34
  - - ! '>='
@@ -38,9 +37,9 @@ dependencies:
38
37
  - - ~>
39
38
  - !ruby/object:Gem::Version
40
39
  version: '1.0'
41
- type: :runtime
40
+ name: multi_json
42
41
  prerelease: false
43
- version_requirements: !ruby/object:Gem::Requirement
42
+ requirement: !ruby/object:Gem::Requirement
44
43
  none: false
45
44
  requirements:
46
45
  - - ! '>='
@@ -49,8 +48,16 @@ dependencies:
49
48
  - - ~>
50
49
  - !ruby/object:Gem::Version
51
50
  version: '1.0'
51
+ type: :runtime
52
52
  - !ruby/object:Gem::Dependency
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ version: '2.7'
53
59
  name: rspec
60
+ prerelease: false
54
61
  requirement: !ruby/object:Gem::Requirement
55
62
  none: false
56
63
  requirements:
@@ -58,15 +65,15 @@ dependencies:
58
65
  - !ruby/object:Gem::Version
59
66
  version: '2.7'
60
67
  type: :development
61
- prerelease: false
68
+ - !ruby/object:Gem::Dependency
62
69
  version_requirements: !ruby/object:Gem::Requirement
63
70
  none: false
64
71
  requirements:
65
72
  - - ~>
66
73
  - !ruby/object:Gem::Version
67
- version: '2.7'
68
- - !ruby/object:Gem::Dependency
74
+ version: '1.0'
69
75
  name: bundler
76
+ prerelease: false
70
77
  requirement: !ruby/object:Gem::Requirement
71
78
  none: false
72
79
  requirements:
@@ -74,15 +81,15 @@ dependencies:
74
81
  - !ruby/object:Gem::Version
75
82
  version: '1.0'
76
83
  type: :development
77
- prerelease: false
84
+ - !ruby/object:Gem::Dependency
78
85
  version_requirements: !ruby/object:Gem::Requirement
79
86
  none: false
80
87
  requirements:
81
88
  - - ~>
82
89
  - !ruby/object:Gem::Version
83
- version: '1.0'
84
- - !ruby/object:Gem::Dependency
90
+ version: 1.8.3
85
91
  name: jeweler
92
+ prerelease: false
86
93
  requirement: !ruby/object:Gem::Requirement
87
94
  none: false
88
95
  requirements:
@@ -90,29 +97,38 @@ dependencies:
90
97
  - !ruby/object:Gem::Version
91
98
  version: 1.8.3
92
99
  type: :development
93
- prerelease: false
100
+ - !ruby/object:Gem::Dependency
94
101
  version_requirements: !ruby/object:Gem::Requirement
95
102
  none: false
96
103
  requirements:
97
- - - ~>
104
+ - - ! '>='
98
105
  - !ruby/object:Gem::Version
99
- version: 1.8.3
100
- - !ruby/object:Gem::Dependency
106
+ version: '3.11'
101
107
  name: rdoc
108
+ prerelease: false
102
109
  requirement: !ruby/object:Gem::Requirement
103
110
  none: false
104
111
  requirements:
105
- - - ~>
112
+ - - ! '>='
106
113
  - !ruby/object:Gem::Version
107
114
  version: '3.11'
108
115
  type: :development
109
- prerelease: false
116
+ - !ruby/object:Gem::Dependency
110
117
  version_requirements: !ruby/object:Gem::Requirement
111
118
  none: false
112
119
  requirements:
113
- - - ~>
120
+ - - ! '>='
114
121
  - !ruby/object:Gem::Version
115
- version: '3.11'
122
+ version: '0'
123
+ name: source_map
124
+ prerelease: false
125
+ requirement: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ! '>='
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ type: :development
116
132
  description:
117
133
  email: lautis@gmail.com
118
134
  executables: []
@@ -125,20 +141,22 @@ files:
125
141
  - .gitmodules
126
142
  - .rspec
127
143
  - .travis.yml
144
+ - CONTRIBUTING.md
128
145
  - Gemfile
129
146
  - LICENSE.txt
130
147
  - README.md
131
148
  - Rakefile
132
149
  - VERSION
133
- - build.js
134
150
  - lib/es5.js
135
151
  - lib/uglifier.rb
136
152
  - lib/uglify.js
153
+ - spec/source_map_spec.rb
137
154
  - spec/spec_helper.rb
138
155
  - spec/uglifier_spec.rb
139
156
  - uglifier.gemspec
140
157
  homepage: http://github.com/lautis/uglifier
141
- licenses: []
158
+ licenses:
159
+ - MIT
142
160
  post_install_message:
143
161
  rdoc_options: []
144
162
  require_paths:
@@ -148,10 +166,10 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
166
  requirements:
149
167
  - - ! '>='
150
168
  - !ruby/object:Gem::Version
151
- version: '0'
152
169
  segments:
153
170
  - 0
154
- hash: -550840341360460746
171
+ hash: 2998740811060118090
172
+ version: '0'
155
173
  required_rubygems_version: !ruby/object:Gem::Requirement
156
174
  none: false
157
175
  requirements: