yui-compressor 0.9.1 → 0.9.3
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.
- data/lib/yui/compressor.rb +27 -25
- data/{vendor → lib}/yuicompressor-2.4.2.jar +0 -0
- data/lib/yuicompressor-2.4.4.jar +0 -0
- data/test/compressor_test.rb +9 -4
- metadata +19 -6
data/lib/yui/compressor.rb
CHANGED
@@ -3,12 +3,14 @@ require "stringio"
|
|
3
3
|
|
4
4
|
module YUI #:nodoc:
|
5
5
|
class Compressor
|
6
|
+
VERSION = "0.9.3"
|
7
|
+
|
6
8
|
class Error < StandardError; end
|
7
9
|
class OptionError < Error; end
|
8
10
|
class RuntimeError < Error; end
|
9
|
-
|
11
|
+
|
10
12
|
attr_reader :options, :command
|
11
|
-
|
13
|
+
|
12
14
|
def self.default_options #:nodoc:
|
13
15
|
{ :charset => "utf-8", :line_break => nil }
|
14
16
|
end
|
@@ -21,7 +23,7 @@ module YUI #:nodoc:
|
|
21
23
|
@options = self.class.default_options.merge(options)
|
22
24
|
@command = [path_to_java, "-jar", path_to_jar_file, *(command_option_for_type + command_options)]
|
23
25
|
end
|
24
|
-
|
26
|
+
|
25
27
|
# Compress a stream or string of code with YUI Compressor. (A stream is
|
26
28
|
# any object that responds to +read+ and +close+ like an IO.) If a block
|
27
29
|
# is given, you can read the compressed code from the block's argument.
|
@@ -60,20 +62,20 @@ module YUI #:nodoc:
|
|
60
62
|
Open3.popen3(*command) do |stdin, stdout, stderr|
|
61
63
|
begin
|
62
64
|
transfer(stream, stdin)
|
63
|
-
|
65
|
+
|
64
66
|
if block_given?
|
65
67
|
yield stdout
|
66
68
|
else
|
67
69
|
stdout.read
|
68
70
|
end
|
69
|
-
|
71
|
+
|
70
72
|
rescue Exception => e
|
71
73
|
raise RuntimeError, "compression failed"
|
72
74
|
end
|
73
75
|
end
|
74
76
|
end
|
75
77
|
end
|
76
|
-
|
78
|
+
|
77
79
|
private
|
78
80
|
def command_options
|
79
81
|
options.inject([]) do |command_options, (name, argument)|
|
@@ -92,7 +94,7 @@ module YUI #:nodoc:
|
|
92
94
|
end
|
93
95
|
|
94
96
|
def path_to_jar_file
|
95
|
-
options.delete(:jar_file) || File.join(File.dirname(__FILE__), *%w"..
|
97
|
+
options.delete(:jar_file) || File.join(File.dirname(__FILE__), *%w".. yuicompressor-2.4.4.jar")
|
96
98
|
end
|
97
99
|
|
98
100
|
def streamify(stream_or_string)
|
@@ -102,14 +104,14 @@ module YUI #:nodoc:
|
|
102
104
|
yield StringIO.new(stream_or_string.to_s)
|
103
105
|
end
|
104
106
|
end
|
105
|
-
|
107
|
+
|
106
108
|
def transfer(from_stream, to_stream)
|
107
109
|
while buffer = from_stream.read(4096)
|
108
110
|
to_stream.write(buffer)
|
109
111
|
end
|
110
112
|
to_stream.close
|
111
113
|
end
|
112
|
-
|
114
|
+
|
113
115
|
def command_option_for_type
|
114
116
|
["--type", self.class.compressor_type.to_s]
|
115
117
|
end
|
@@ -117,25 +119,25 @@ module YUI #:nodoc:
|
|
117
119
|
def command_option_for_charset(charset)
|
118
120
|
["--charset", charset.to_s]
|
119
121
|
end
|
120
|
-
|
122
|
+
|
121
123
|
def command_option_for_line_break(line_break)
|
122
124
|
line_break ? ["--line-break", line_break.to_s] : []
|
123
125
|
end
|
124
126
|
end
|
125
|
-
|
127
|
+
|
126
128
|
class CssCompressor < Compressor
|
127
129
|
def self.compressor_type #:nodoc:
|
128
130
|
"css"
|
129
131
|
end
|
130
|
-
|
132
|
+
|
131
133
|
# Creates a new YUI::CssCompressor for minifying CSS code.
|
132
134
|
#
|
133
135
|
# Options are:
|
134
136
|
#
|
135
|
-
# <tt>:charset</tt>:: Specifies the character encoding to use. Defaults to
|
137
|
+
# <tt>:charset</tt>:: Specifies the character encoding to use. Defaults to
|
136
138
|
# <tt>"utf-8"</tt>.
|
137
139
|
# <tt>:line_break</tt>:: By default, CSS will be compressed onto a single
|
138
|
-
# line. Use this option to specify the maximum
|
140
|
+
# line. Use this option to specify the maximum
|
139
141
|
# number of characters in each line before a newline
|
140
142
|
# is added. If <tt>:line_break</tt> is 0, a newline
|
141
143
|
# is added after each CSS rule.
|
@@ -144,12 +146,12 @@ module YUI #:nodoc:
|
|
144
146
|
super
|
145
147
|
end
|
146
148
|
end
|
147
|
-
|
149
|
+
|
148
150
|
class JavaScriptCompressor < Compressor
|
149
151
|
def self.compressor_type #:nodoc:
|
150
152
|
"js"
|
151
153
|
end
|
152
|
-
|
154
|
+
|
153
155
|
def self.default_options #:nodoc:
|
154
156
|
super.merge(
|
155
157
|
:munge => false,
|
@@ -157,17 +159,17 @@ module YUI #:nodoc:
|
|
157
159
|
:preserve_semicolons => false
|
158
160
|
)
|
159
161
|
end
|
160
|
-
|
162
|
+
|
161
163
|
# Creates a new YUI::JavaScriptCompressor for minifying JavaScript code.
|
162
164
|
#
|
163
165
|
# Options are:
|
164
166
|
#
|
165
|
-
# <tt>:charset</tt>:: Specifies the character encoding to use. Defaults to
|
167
|
+
# <tt>:charset</tt>:: Specifies the character encoding to use. Defaults to
|
166
168
|
# <tt>"utf-8"</tt>.
|
167
|
-
# <tt>:line_break</tt>:: By default, JavaScript will be compressed onto a
|
169
|
+
# <tt>:line_break</tt>:: By default, JavaScript will be compressed onto a
|
168
170
|
# single line. Use this option to specify the
|
169
171
|
# maximum number of characters in each line before a
|
170
|
-
# newline is added. If <tt>:line_break</tt> is 0, a
|
172
|
+
# newline is added. If <tt>:line_break</tt> is 0, a
|
171
173
|
# newline is added after each JavaScript statement.
|
172
174
|
# <tt>:munge</tt>:: Specifies whether YUI Compressor should shorten local
|
173
175
|
# variable names when possible. Defaults to +false+.
|
@@ -175,9 +177,9 @@ module YUI #:nodoc:
|
|
175
177
|
# JavaScript object property access and object literal
|
176
178
|
# declarations to use as few characters as possible.
|
177
179
|
# Defaults to +true+.
|
178
|
-
# <tt>:preserve_semicolons</tt>:: Defaults to +false+. If +true+, YUI
|
179
|
-
# Compressor will ensure semicolons exist
|
180
|
-
# after each statement to appease tools like
|
180
|
+
# <tt>:preserve_semicolons</tt>:: Defaults to +false+. If +true+, YUI
|
181
|
+
# Compressor will ensure semicolons exist
|
182
|
+
# after each statement to appease tools like
|
181
183
|
# JSLint.
|
182
184
|
#
|
183
185
|
def initialize(options = {})
|
@@ -188,11 +190,11 @@ module YUI #:nodoc:
|
|
188
190
|
def command_option_for_munge(munge)
|
189
191
|
munge ? [] : ["--nomunge"]
|
190
192
|
end
|
191
|
-
|
193
|
+
|
192
194
|
def command_option_for_optimize(optimize)
|
193
195
|
optimize ? [] : ["--disable-optimizations"]
|
194
196
|
end
|
195
|
-
|
197
|
+
|
196
198
|
def command_option_for_preserve_semicolons(preserve_semicolons)
|
197
199
|
preserve_semicolons ? ["--preserve-semi"] : []
|
198
200
|
end
|
File without changes
|
Binary file
|
data/test/compressor_test.rb
CHANGED
@@ -12,6 +12,10 @@ module YUI
|
|
12
12
|
background: red;
|
13
13
|
color: white;
|
14
14
|
}
|
15
|
+
|
16
|
+
@media screen and (max-device-width: 640px) {
|
17
|
+
body { font-size: 90%; }
|
18
|
+
}
|
15
19
|
END_CSS
|
16
20
|
|
17
21
|
FIXTURE_JS = <<-END_JS
|
@@ -38,7 +42,7 @@ module YUI
|
|
38
42
|
|
39
43
|
def test_css_should_be_compressed
|
40
44
|
@compressor = YUI::CssCompressor.new
|
41
|
-
assert_equal "div.warning{display:none
|
45
|
+
assert_equal "div.warning{display:none}div.error{background:red;color:white}@media screen and (max-device-width:640px){body{font-size:90%}}", @compressor.compress(FIXTURE_CSS)
|
42
46
|
end
|
43
47
|
|
44
48
|
def test_js_should_be_compressed
|
@@ -55,20 +59,21 @@ module YUI
|
|
55
59
|
|
56
60
|
def test_compress_should_accept_an_io_argument
|
57
61
|
@compressor = YUI::CssCompressor.new
|
58
|
-
assert_equal "div.warning{display:none
|
62
|
+
assert_equal "div.warning{display:none}div.error{background:red;color:white}@media screen and (max-device-width:640px){body{font-size:90%}}", @compressor.compress(StringIO.new(FIXTURE_CSS))
|
59
63
|
end
|
60
64
|
|
61
65
|
def test_compress_should_accept_a_block_and_yield_an_io
|
62
66
|
@compressor = YUI::CssCompressor.new
|
63
67
|
@compressor.compress(FIXTURE_CSS) do |stream|
|
64
68
|
assert_kind_of IO, stream
|
65
|
-
assert_equal "div.warning{display:none
|
69
|
+
assert_equal "div.warning{display:none}div.error{background:red;color:white}@media screen and (max-device-width:640px){body{font-size:90%}}", stream.read
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
69
73
|
def test_line_break_option_should_insert_line_breaks_in_css
|
70
74
|
@compressor = YUI::CssCompressor.new(:line_break => 0)
|
71
|
-
assert_equal "div.warning{display:none
|
75
|
+
assert_equal "div.warning{display:none}\ndiv.error{background:red;color:white}\n@media screen and (max-device-width:640px){body{font-size:90%}\n}", @compressor.compress(FIXTURE_CSS)
|
76
|
+
|
72
77
|
end
|
73
78
|
|
74
79
|
def test_line_break_option_should_insert_line_breaks_in_js
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yui-compressor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 61
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 9
|
9
|
+
- 3
|
10
|
+
version: 0.9.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Sam Stephenson
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2011-01-18 00:00:00 -06:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -24,8 +30,9 @@ extra_rdoc_files: []
|
|
24
30
|
files:
|
25
31
|
- Rakefile
|
26
32
|
- lib/yui/compressor.rb
|
33
|
+
- lib/yuicompressor-2.4.2.jar
|
34
|
+
- lib/yuicompressor-2.4.4.jar
|
27
35
|
- test/compressor_test.rb
|
28
|
-
- vendor/yuicompressor-2.4.2.jar
|
29
36
|
has_rdoc: true
|
30
37
|
homepage: http://github.com/sstephenson/ruby-yui-compressor/
|
31
38
|
licenses: []
|
@@ -36,21 +43,27 @@ rdoc_options: []
|
|
36
43
|
require_paths:
|
37
44
|
- lib
|
38
45
|
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
39
47
|
requirements:
|
40
48
|
- - ">="
|
41
49
|
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
42
53
|
version: "0"
|
43
|
-
version:
|
44
54
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
45
56
|
requirements:
|
46
57
|
- - ">="
|
47
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
48
62
|
version: "0"
|
49
|
-
version:
|
50
63
|
requirements: []
|
51
64
|
|
52
65
|
rubyforge_project: yui
|
53
|
-
rubygems_version: 1.3.
|
66
|
+
rubygems_version: 1.3.7
|
54
67
|
signing_key:
|
55
68
|
specification_version: 3
|
56
69
|
summary: JavaScript and CSS minification library
|