yuicompressor 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +155 -0
- data/Rakefile +71 -0
- data/lib/yuicompressor/jruby.rb +85 -0
- data/lib/yuicompressor/shell.rb +82 -0
- data/lib/yuicompressor.rb +79 -0
- data/test/css_test.rb +57 -0
- data/test/helper.rb +9 -0
- data/test/js_test.rb +99 -0
- data/yuicompressor.gemspec +27 -0
- metadata +93 -0
data/README
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
YUICompressor
|
2
|
+
=============
|
3
|
+
|
4
|
+
YUICompressor is a Ruby module that may be used to create compressed versions
|
5
|
+
of JavaScript and CSS code quickly and easily using the Yahoo User Interface
|
6
|
+
(YUI) library compressor. The module is essentially a wrapper around the YUI
|
7
|
+
Compressor (a Java library) that supports two different modes of operation:
|
8
|
+
shell and native.
|
9
|
+
|
10
|
+
In shell mode the YUI Compressor library executes in a separate process. Code
|
11
|
+
is piped into and out of this process using the system shell. This approach
|
12
|
+
yields good performance and is the default for MRI and other Ruby versions that
|
13
|
+
are not able to execute Java code.
|
14
|
+
|
15
|
+
In native mode the compressor is invoked in the same process as Ruby. This is
|
16
|
+
only possible when using YUICompressor on JRuby. With this approach, compression
|
17
|
+
speeds dramatically improve because the system does not incur the overhead of
|
18
|
+
invoking a separate Java process for each compression.
|
19
|
+
|
20
|
+
In any case YUICompressor will automatically detect the Ruby platform it is
|
21
|
+
running on and choose the mode that will offer the best performance.
|
22
|
+
|
23
|
+
The inspiration for this project is Sam Stephenson's yui-compressor gem. The
|
24
|
+
goal of YUICompressor is to provide a more fluent interface, greater
|
25
|
+
flexibility, and better performance than its predecessor.
|
26
|
+
|
27
|
+
|
28
|
+
Installation
|
29
|
+
------------
|
30
|
+
|
31
|
+
Via RubyGems:
|
32
|
+
|
33
|
+
$ gem install yuicompressor
|
34
|
+
|
35
|
+
From a local copy:
|
36
|
+
|
37
|
+
$ git clone git://github.com/mjijackson/yuicompressor.git
|
38
|
+
$ cd yuicompressor
|
39
|
+
$ rake package && rake install
|
40
|
+
|
41
|
+
Usage
|
42
|
+
-----
|
43
|
+
|
44
|
+
The easiest way to compress JavaScript code using YUICompressor is to pass an IO
|
45
|
+
or a string containing your code to YUICompressor.compress_js. Similarly, CSS
|
46
|
+
code may similarly be compressed using YUICompressor.compress_css. Both methods
|
47
|
+
may take a second parameter that is a Hash of options to use during compression.
|
48
|
+
When compressing CSS, the following option is available:
|
49
|
+
|
50
|
+
:line_break The maximum number of characters that may appear in a
|
51
|
+
single line of compressed code. Defaults to no maximum
|
52
|
+
length. If set to 0 each line will be the minimum length
|
53
|
+
possible.
|
54
|
+
|
55
|
+
When compressing JavaScript, these additional options are available:
|
56
|
+
|
57
|
+
:munge Should be +true+ if the compressor should shorten
|
58
|
+
local variable names when possible. Defaults to
|
59
|
+
+false+.
|
60
|
+
:preserve_semicolons Should be +true+ if the compressor should preserve
|
61
|
+
all semicolons in the code. Defaults to +false+.
|
62
|
+
:optimize Should be +true+ if the compressor should enable all
|
63
|
+
micro optimizations. Defaults to +true+.
|
64
|
+
|
65
|
+
When running in shell mode (i.e. not on JRuby) a :java option may be used to
|
66
|
+
specify the path to the Java binary. The default value is "java".
|
67
|
+
|
68
|
+
Examples
|
69
|
+
--------
|
70
|
+
|
71
|
+
JavaScript compression:
|
72
|
+
|
73
|
+
require 'yuicompressor'
|
74
|
+
js = '(function () { var abc = {}; abc["key"] = "value"; })()'
|
75
|
+
YUICompressor.compress_js(js)
|
76
|
+
=> "(function(){var abc={};abc.key=\"value\"})();"
|
77
|
+
|
78
|
+
JavaScript compression with shortened local variable names (i.e. munging):
|
79
|
+
|
80
|
+
require 'yuicompressor'
|
81
|
+
js = '(function () { var abc = {}; abc["key"] = "value"; })()'
|
82
|
+
YUICompressor.compress_js(js, :munge => true)
|
83
|
+
=> "(function(){var a={};a.key=\"value\"})();"
|
84
|
+
|
85
|
+
CSS compression:
|
86
|
+
|
87
|
+
require 'yuicompressor'
|
88
|
+
css = <<'CODE'
|
89
|
+
div.main {
|
90
|
+
background-position: 0;
|
91
|
+
}
|
92
|
+
h1#title {
|
93
|
+
color: #FFFFFF;
|
94
|
+
}
|
95
|
+
CODE
|
96
|
+
YUICompressor.compress_css(css)
|
97
|
+
=> "div.main{background-position:0 0;}h1#title{color:#FFF;}"
|
98
|
+
|
99
|
+
CSS compression with every statement on a separate line:
|
100
|
+
|
101
|
+
require 'yuicompressor'
|
102
|
+
css = <<'CODE'
|
103
|
+
div.main {
|
104
|
+
background-position: 0;
|
105
|
+
}
|
106
|
+
h1#title {
|
107
|
+
color: #FFFFFF;
|
108
|
+
}
|
109
|
+
CODE
|
110
|
+
YUICompressor.compress_css(css, :line_break => 0)
|
111
|
+
=> "div.main{background-position:0 0;}\nh1#title{color:#FFF;}"
|
112
|
+
|
113
|
+
Including in another module:
|
114
|
+
|
115
|
+
require 'yuicompressor'
|
116
|
+
|
117
|
+
module MyJavaScriptCompressor
|
118
|
+
include YUICompressor
|
119
|
+
|
120
|
+
def compress(code)
|
121
|
+
compress_js(code)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
License
|
126
|
+
-------
|
127
|
+
|
128
|
+
Copyright 2010 Michael Jackson
|
129
|
+
|
130
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
131
|
+
of this software and associated documentation files (the "Software"), to deal
|
132
|
+
in the Software without restriction, including without limitation the rights
|
133
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
134
|
+
copies of the Software, and to permit persons to whom the Software is
|
135
|
+
furnished to do so, subject to the following conditions:
|
136
|
+
|
137
|
+
The above copyright notice and this permission notice shall be included in
|
138
|
+
all copies or substantial portions of the Software.
|
139
|
+
|
140
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
141
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
142
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
143
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
144
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
145
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
146
|
+
THE SOFTWARE.
|
147
|
+
|
148
|
+
Credits
|
149
|
+
-------
|
150
|
+
|
151
|
+
The YUI Compressor library is copyright Yahoo! Inc. The jar file is included in
|
152
|
+
the source distribution under the terms of the BSD license.
|
153
|
+
|
154
|
+
Also, thanks to Charles Nutter (@headius) for helping me understand how to
|
155
|
+
convert Ruby streams to Java and back when doing the JRuby module.
|
data/Rakefile
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'rake/clean'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
task :default => :test
|
5
|
+
|
6
|
+
# TESTS #######################################################################
|
7
|
+
|
8
|
+
Rake::TestTask.new do |t|
|
9
|
+
t.libs += ['lib', 'test']
|
10
|
+
t.test_files = FileList['test/*_test.rb']
|
11
|
+
t.verbose = true
|
12
|
+
end
|
13
|
+
|
14
|
+
# DOCS ########################################################################
|
15
|
+
|
16
|
+
desc "Generate API documentation"
|
17
|
+
task :api => FileList['lib/**/*.rb'] do |t|
|
18
|
+
output_dir = ENV['OUTPUT_DIR'] || 'api'
|
19
|
+
rm_rf output_dir
|
20
|
+
sh((<<-SH).gsub(/[\s\n]+/, ' ').strip)
|
21
|
+
hanna
|
22
|
+
--op #{output_dir}
|
23
|
+
--promiscuous
|
24
|
+
--charset utf8
|
25
|
+
--fmt html
|
26
|
+
--inline-source
|
27
|
+
--line-numbers
|
28
|
+
--accessor option_accessor=RW
|
29
|
+
--main YUICompressor
|
30
|
+
--title 'YUICompressor API Documentation'
|
31
|
+
#{t.prerequisites.join(' ')}
|
32
|
+
SH
|
33
|
+
end
|
34
|
+
|
35
|
+
CLEAN.include 'api'
|
36
|
+
|
37
|
+
# PACKAGING & INSTALLATION ####################################################
|
38
|
+
|
39
|
+
if defined?(Gem)
|
40
|
+
$spec = eval("#{File.read('yuicompressor.gemspec')}")
|
41
|
+
|
42
|
+
directory 'dist'
|
43
|
+
|
44
|
+
def package(ext='')
|
45
|
+
"dist/#{$spec.name}-#{$spec.version}" + ext
|
46
|
+
end
|
47
|
+
|
48
|
+
file package('.gem') => %w< dist > + $spec.files do |f|
|
49
|
+
sh "gem build yuicompressor.gemspec"
|
50
|
+
mv File.basename(f.name), f.name
|
51
|
+
end
|
52
|
+
|
53
|
+
file package('.tar.gz') => %w< dist > + $spec.files do |f|
|
54
|
+
sh "git archive --format=tar HEAD | gzip > #{f.name}"
|
55
|
+
end
|
56
|
+
|
57
|
+
desc "Build packages"
|
58
|
+
task :package => %w< .gem .tar.gz >.map {|e| package(e) }
|
59
|
+
|
60
|
+
desc "Build and install as local gem"
|
61
|
+
task :install => package('.gem') do |t|
|
62
|
+
sh "gem install #{package('.gem')}"
|
63
|
+
end
|
64
|
+
|
65
|
+
desc "Upload gem to rubygems.org"
|
66
|
+
task :release => package('.gem') do |t|
|
67
|
+
sh "gem push #{package('.gem')}"
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
CLOBBER.include 'dist'
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'java'
|
2
|
+
|
3
|
+
module YUICompressor
|
4
|
+
# This module contains methods specific to the JRuby platform and is
|
5
|
+
# automatically used when JRuby is detected. It provides a significant
|
6
|
+
# increase in performance over the Shell module.
|
7
|
+
module JRuby
|
8
|
+
|
9
|
+
require JAR_FILE
|
10
|
+
|
11
|
+
import java.io.InputStreamReader
|
12
|
+
import java.io.OutputStreamWriter
|
13
|
+
import com.yahoo.platform.yui.compressor.JavaScriptCompressor
|
14
|
+
import com.yahoo.platform.yui.compressor.CssCompressor
|
15
|
+
|
16
|
+
class ErrorReporter #:nodoc:
|
17
|
+
def error(message, source_name, line, line_source, line_offset)
|
18
|
+
if line < 0
|
19
|
+
"\n[ERROR] %s" % message
|
20
|
+
else
|
21
|
+
"\n[ERROR] %s:%s:%s" % [line, line_offset, message]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def runtimeError(*args)
|
26
|
+
raise RuntimeError, 'Compression failed. %s' % error(*args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the set of arguments that are needed to instantiate a compressor
|
31
|
+
# using the given +options+.
|
32
|
+
def command_arguments(options={})
|
33
|
+
args = [ options[:line_break] ? options[:line_break].to_i : -1 ]
|
34
|
+
|
35
|
+
if options[:type].to_s == 'js'
|
36
|
+
args << !! options[:munge]
|
37
|
+
args << false # verbose?
|
38
|
+
args << !! options[:preserve_semicolons]
|
39
|
+
args << ! options[:optimize] # disable optimizations?
|
40
|
+
end
|
41
|
+
|
42
|
+
args
|
43
|
+
end
|
44
|
+
|
45
|
+
# Compresses the given +stream_or_string+ of code using the given +options+.
|
46
|
+
# When using this method directly, at least the +:type+ option must be
|
47
|
+
# specified, and should be one of <tt>"css"</tt> or <tt>"js"</tt>. See
|
48
|
+
# YUICompressor#compress_css and YUICompressor#compress_js for details about
|
49
|
+
# which options are acceptable for each type of compressor.
|
50
|
+
#
|
51
|
+
# If a block is given, it will receive the IO output object. Otherwise the
|
52
|
+
# output will be returned as a string.
|
53
|
+
def compress(stream_or_string, options={})
|
54
|
+
raise ArgumentError, 'Option :type required' unless options.key?(:type)
|
55
|
+
|
56
|
+
stream = streamify(stream_or_string)
|
57
|
+
output = StringIO.new
|
58
|
+
|
59
|
+
reader = InputStreamReader.new(stream.to_inputstream)
|
60
|
+
writer = OutputStreamWriter.new(output.to_outputstream)
|
61
|
+
|
62
|
+
compressor = case options[:type].to_s
|
63
|
+
when 'js'
|
64
|
+
options = default_js_options.merge(options)
|
65
|
+
JavaScriptCompressor.new(reader, ErrorReporter.new)
|
66
|
+
when 'css'
|
67
|
+
options = default_css_options.merge(options)
|
68
|
+
CssCompressor.new(reader)
|
69
|
+
else
|
70
|
+
raise ArgumentError, 'Unknown resource type: %s' % options[:type]
|
71
|
+
end
|
72
|
+
|
73
|
+
compressor.compress(writer, *command_arguments(options))
|
74
|
+
writer.flush
|
75
|
+
output.rewind
|
76
|
+
|
77
|
+
if block_given?
|
78
|
+
yield output
|
79
|
+
else
|
80
|
+
output.read
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
begin
|
2
|
+
require 'open3'
|
3
|
+
rescue LoadError
|
4
|
+
require 'win32/open3'
|
5
|
+
end
|
6
|
+
|
7
|
+
module YUICompressor
|
8
|
+
# This module contains methods that allow the YUI Compressor to be used by
|
9
|
+
# piping IO to a separate Java process via the system shell. It is used on all
|
10
|
+
# Ruby platforms except for JRuby.
|
11
|
+
module Shell
|
12
|
+
|
13
|
+
# Returns an array of flags that should be passed to the jar file on the
|
14
|
+
# command line for the given set of +options+.
|
15
|
+
def command_arguments(options={})
|
16
|
+
args = []
|
17
|
+
args.concat(['--type', options[:type].to_s]) if options[:type]
|
18
|
+
args.concat(['--line-break', options[:line_break].to_s]) if options[:line_break]
|
19
|
+
|
20
|
+
if options[:type].to_s == 'js'
|
21
|
+
args << '--nomunge' unless options[:munge]
|
22
|
+
args << '--preserve-semi' if options[:preserve_semicolons]
|
23
|
+
args << '--disable-optimizations' unless options[:optimize]
|
24
|
+
end
|
25
|
+
|
26
|
+
args
|
27
|
+
end
|
28
|
+
|
29
|
+
# Compresses the given +stream_or_string+ of code using the given +options+.
|
30
|
+
# When using this method directly, at least the +:type+ option must be
|
31
|
+
# specified, and should be one of <tt>"css"</tt> or <tt>"js"</tt>. See
|
32
|
+
# YUICompressor#compress_css and YUICompressor#compress_js for details about
|
33
|
+
# which options are acceptable for each type of compressor.
|
34
|
+
#
|
35
|
+
# In addition to the standard options, this method also accepts a
|
36
|
+
# <tt>:java</tt> option that can be used to specify the location of the Java
|
37
|
+
# binary. This option will default to using <tt>"java"</tt> unless otherwise
|
38
|
+
# specified.
|
39
|
+
#
|
40
|
+
# If a block is given, it will receive the IO output object. Otherwise the
|
41
|
+
# output will be returned as a string.
|
42
|
+
def compress(stream_or_string, options={})
|
43
|
+
raise ArgumentError, 'Option :type required' unless options.key?(:type)
|
44
|
+
|
45
|
+
stream = streamify(stream_or_string)
|
46
|
+
|
47
|
+
case options[:type].to_s
|
48
|
+
when 'js'
|
49
|
+
options = default_js_options.merge(options)
|
50
|
+
when 'css'
|
51
|
+
options = default_css_options.merge(options)
|
52
|
+
else
|
53
|
+
raise ArgumentError, 'Unknown resource type: %s' % options[:type]
|
54
|
+
end
|
55
|
+
|
56
|
+
command = [ options.delete(:java) || 'java', '-jar', JAR_FILE ]
|
57
|
+
command.concat(command_arguments(options))
|
58
|
+
|
59
|
+
Open3.popen3(command.join(' ')) do |input, output, stderr|
|
60
|
+
begin
|
61
|
+
while buffer = stream.read(4096)
|
62
|
+
input.write(buffer)
|
63
|
+
end
|
64
|
+
|
65
|
+
input.close
|
66
|
+
|
67
|
+
err = stderr.read
|
68
|
+
raise err unless err.empty?
|
69
|
+
|
70
|
+
if block_given?
|
71
|
+
yield output
|
72
|
+
else
|
73
|
+
output.read
|
74
|
+
end
|
75
|
+
rescue Exception => e
|
76
|
+
raise RuntimeError, 'Compression failed. %s' % e
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
|
3
|
+
module YUICompressor
|
4
|
+
|
5
|
+
# The path to the YUI Compressor jar file.
|
6
|
+
JAR_FILE = File.expand_path('../yuicompressor-2.4.2.jar', __FILE__)
|
7
|
+
|
8
|
+
autoload :JRuby, 'yuicompressor/jruby'
|
9
|
+
autoload :Shell, 'yuicompressor/shell'
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
# Returns +true+ if the Ruby platform is JRuby.
|
14
|
+
def jruby?
|
15
|
+
!! (RUBY_PLATFORM =~ /java/)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Compress the given CSS +stream_or_string+ using the given +options+.
|
19
|
+
# Options should be a Hash with any of the following keys:
|
20
|
+
#
|
21
|
+
# +:line_break+:: The maximum number of characters that may appear in a
|
22
|
+
# single line of compressed code. Defaults to no maximum
|
23
|
+
# length. If set to 0 each line will be the minimum length
|
24
|
+
# possible.
|
25
|
+
def compress_css(stream_or_string, options={}, &block)
|
26
|
+
compress(stream_or_string, options.merge(:type => 'css'), &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
# Compress the given JavaScript +stream_or_string+ using the given +options+.
|
30
|
+
# Options should be a Hash with any of the following keys:
|
31
|
+
#
|
32
|
+
# +:line_break+:: The maximum number of characters that may appear in a
|
33
|
+
# single line of compressed code. Defaults to no maximum
|
34
|
+
# length. If set to 0 each line will be the minimum length
|
35
|
+
# possible.
|
36
|
+
# +:munge+:: Should be +true+ if the compressor should shorten local
|
37
|
+
# variable names when possible. Defaults to +false+.
|
38
|
+
# +:preserve_semicolons+:: Should be +true+ if the compressor should preserve
|
39
|
+
# all semicolons in the code. Defaults to +false+.
|
40
|
+
# +:optimize+:: Should be +true+ if the compressor should enable all
|
41
|
+
# micro optimizations. Defaults to +true+.
|
42
|
+
def compress_js(stream_or_string, options={}, &block)
|
43
|
+
compress(stream_or_string, options.merge(:type => 'js'), &block)
|
44
|
+
end
|
45
|
+
|
46
|
+
def default_css_options #:nodoc:
|
47
|
+
{ :line_break => nil }
|
48
|
+
end
|
49
|
+
|
50
|
+
def default_js_options #:nodoc:
|
51
|
+
default_css_options.merge(
|
52
|
+
:munge => false,
|
53
|
+
:preserve_semicolons => false,
|
54
|
+
:optimize => true
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
def streamify(stream_or_string) #:nodoc:
|
59
|
+
if IO === stream_or_string || StringIO === stream_or_string
|
60
|
+
stream_or_string
|
61
|
+
elsif String === stream_or_string
|
62
|
+
StringIO.new(stream_or_string.to_s)
|
63
|
+
else
|
64
|
+
raise ArgumentError, 'Stream or string required'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# If we're on JRuby we can use the YUI Compressor Java classes directly. This
|
69
|
+
# gives a huge speed boost. Otherwise we need to make a system call to the
|
70
|
+
# Java interpreter and stream IO to/from the shell.
|
71
|
+
mod = jruby? ? JRuby : Shell
|
72
|
+
|
73
|
+
include mod
|
74
|
+
|
75
|
+
mod.instance_methods.each do |name|
|
76
|
+
module_function name
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
data/test/css_test.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class CSSTest < Test::Unit::TestCase
|
4
|
+
FIXTURE = <<'CODE'
|
5
|
+
.a-class {
|
6
|
+
background-color: red;
|
7
|
+
background-position: 0;
|
8
|
+
}
|
9
|
+
|
10
|
+
div#an-id { color: #FFFFFF; }
|
11
|
+
CODE
|
12
|
+
|
13
|
+
def test_default_command_arguments
|
14
|
+
args = command_arguments(default_css_options)
|
15
|
+
|
16
|
+
if jruby?
|
17
|
+
assert_equal([-1], args)
|
18
|
+
else
|
19
|
+
assert_equal(%w< --charset utf-8 >, args)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_command_arguments
|
24
|
+
if jruby?
|
25
|
+
args = command_arguments(:type => 'css')
|
26
|
+
assert_equal([-1], args)
|
27
|
+
|
28
|
+
args = command_arguments(:type => 'css', :line_break => 80)
|
29
|
+
assert_equal([80], args)
|
30
|
+
|
31
|
+
args = command_arguments(:type => 'css', :non_existent => true)
|
32
|
+
assert_equal([-1], args)
|
33
|
+
else
|
34
|
+
args = command_arguments(:type => 'css')
|
35
|
+
assert_equal(%w< --type css >, args)
|
36
|
+
|
37
|
+
args = command_arguments(:type => 'css', :line_break => 80)
|
38
|
+
assert_equal(%w< --type css --line-break 80 >, args)
|
39
|
+
|
40
|
+
args = command_arguments(:type => 'css', :non_existent => true)
|
41
|
+
assert_equal(%w< --type css >, args)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_default_options
|
46
|
+
assert_equal (<<'CODE').chomp, compress_css(FIXTURE)
|
47
|
+
.a-class{background-color:red;background-position:0 0;}div#an-id{color:#FFF;}
|
48
|
+
CODE
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_line_break_option
|
52
|
+
assert_equal (<<'CODE').chomp, compress_css(FIXTURE, :line_break => 0)
|
53
|
+
.a-class{background-color:red;background-position:0 0;}
|
54
|
+
div#an-id{color:#FFF;}
|
55
|
+
CODE
|
56
|
+
end
|
57
|
+
end
|
data/test/helper.rb
ADDED
data/test/js_test.rb
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class JSTest < Test::Unit::TestCase
|
4
|
+
FIXTURE = <<'CODE'
|
5
|
+
// here's a comment
|
6
|
+
var Foo = { "a": 1 };
|
7
|
+
Foo["bar"] = (function(baz) {
|
8
|
+
/* here's a
|
9
|
+
multiline comment */
|
10
|
+
if (false) {
|
11
|
+
doSomething();
|
12
|
+
} else {
|
13
|
+
for (var index = 0; index < baz.length; index++) {
|
14
|
+
doSomething(baz[index]);
|
15
|
+
}
|
16
|
+
}
|
17
|
+
})("hello");
|
18
|
+
CODE
|
19
|
+
|
20
|
+
def test_default_command_arguments
|
21
|
+
if jruby?
|
22
|
+
args = command_arguments(default_js_options)
|
23
|
+
assert_equal([-1], args)
|
24
|
+
else
|
25
|
+
args = command_arguments(default_js_options)
|
26
|
+
assert_equal(%w< --charset utf-8 >, args)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_command_arguments
|
31
|
+
if jruby?
|
32
|
+
args = command_arguments(:type => 'js')
|
33
|
+
assert_equal([-1, false, false, false, true], args)
|
34
|
+
|
35
|
+
args = command_arguments(:type => 'js', :optimize => true)
|
36
|
+
assert_equal([-1, false, false, false, false], args)
|
37
|
+
|
38
|
+
args = command_arguments(:type => 'js', :munge => true)
|
39
|
+
assert_equal([-1, true, false, false, true], args)
|
40
|
+
|
41
|
+
args = command_arguments(:type => 'js', :non_existent => true)
|
42
|
+
assert_equal([-1, false, false, false, true], args)
|
43
|
+
else
|
44
|
+
args = command_arguments(:type => 'js')
|
45
|
+
assert_equal(%w< --type js --nomunge --disable-optimizations >, args)
|
46
|
+
|
47
|
+
args = command_arguments(:type => 'js', :optimize => true)
|
48
|
+
assert_equal(%w< --type js --nomunge >, args)
|
49
|
+
|
50
|
+
args = command_arguments(:type => 'js', :munge => true)
|
51
|
+
assert_equal(%w< --type js --disable-optimizations >, args)
|
52
|
+
|
53
|
+
args = command_arguments(:type => 'js', :non_existent => true)
|
54
|
+
assert_equal(%w< --type js --nomunge --disable-optimizations >, args)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_default_options
|
59
|
+
assert_equal (<<'CODE').chomp, compress_js(FIXTURE)
|
60
|
+
var Foo={a:1};Foo.bar=(function(baz){if(false){doSomething()}else{for(var index=0;index<baz.length;index++){doSomething(baz[index])}}})("hello");
|
61
|
+
CODE
|
62
|
+
end
|
63
|
+
|
64
|
+
def test_line_break_option
|
65
|
+
assert_equal (<<'CODE').chomp, compress_js(FIXTURE, :line_break => 0)
|
66
|
+
var Foo={a:1};
|
67
|
+
Foo.bar=(function(baz){if(false){doSomething()
|
68
|
+
}else{for(var index=0;
|
69
|
+
index<baz.length;
|
70
|
+
index++){doSomething(baz[index])
|
71
|
+
}}})("hello");
|
72
|
+
CODE
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_munge_option
|
76
|
+
assert_equal (<<'CODE').chomp, compress_js(FIXTURE, :munge => true)
|
77
|
+
var Foo={a:1};Foo.bar=(function(b){if(false){doSomething()}else{for(var a=0;a<b.length;a++){doSomething(b[a])}}})("hello");
|
78
|
+
CODE
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_optimize_option
|
82
|
+
assert_equal (<<'CODE').chomp, compress_js(FIXTURE, :optimize => false)
|
83
|
+
var Foo={"a":1};Foo["bar"]=(function(baz){if(false){doSomething()}else{for(var index=0;index<baz.length;index++){doSomething(baz[index])}}})("hello");
|
84
|
+
CODE
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_preserve_semicolons_option
|
88
|
+
assert_equal (<<'CODE').chomp, compress_js(FIXTURE, :preserve_semicolons => true)
|
89
|
+
var Foo={a:1};Foo.bar=(function(baz){if(false){doSomething();}else{for(var index=0;index<baz.length;index++){doSomething(baz[index]);}}})("hello");
|
90
|
+
CODE
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_errors
|
94
|
+
assert_raise RuntimeError do
|
95
|
+
# Should trigger a compilation error.
|
96
|
+
compress_js('var a = function(){;')
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'yuicompressor'
|
3
|
+
s.version = '1.0'
|
4
|
+
s.date = '2010-08-02'
|
5
|
+
|
6
|
+
s.summary = 'A YUI JavaScript and CSS compressor for Ruby and JRuby'
|
7
|
+
s.description = 'A YUI JavaScript and CSS compressor for Ruby and JRuby'
|
8
|
+
|
9
|
+
s.author = 'Michael Jackson'
|
10
|
+
s.email = 'mjijackson@gmail.com'
|
11
|
+
|
12
|
+
s.require_paths = %w< lib >
|
13
|
+
|
14
|
+
s.files = Dir['lib/**/*.rb'] +
|
15
|
+
Dir['test/*.rb'] +
|
16
|
+
%w< yuicompressor.gemspec Rakefile README >
|
17
|
+
|
18
|
+
s.test_files = s.files.select {|path| path =~ /^test\/.*_test.rb/ }
|
19
|
+
|
20
|
+
s.add_development_dependency('rake')
|
21
|
+
|
22
|
+
s.has_rdoc = true
|
23
|
+
s.rdoc_options = %w< --line-numbers --inline-source --title YUICompressor --main YUICompressor >
|
24
|
+
s.extra_rdoc_files = %w< README >
|
25
|
+
|
26
|
+
s.homepage = 'http://github.com/mjijackson/yuicompressor'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yuicompressor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: "1.0"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Michael Jackson
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-02 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: A YUI JavaScript and CSS compressor for Ruby and JRuby
|
35
|
+
email: mjijackson@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- README
|
42
|
+
files:
|
43
|
+
- lib/yuicompressor/jruby.rb
|
44
|
+
- lib/yuicompressor/shell.rb
|
45
|
+
- lib/yuicompressor.rb
|
46
|
+
- test/css_test.rb
|
47
|
+
- test/helper.rb
|
48
|
+
- test/js_test.rb
|
49
|
+
- yuicompressor.gemspec
|
50
|
+
- Rakefile
|
51
|
+
- README
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/mjijackson/yuicompressor
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --line-numbers
|
59
|
+
- --inline-source
|
60
|
+
- --title
|
61
|
+
- YUICompressor
|
62
|
+
- --main
|
63
|
+
- YUICompressor
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.7
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: A YUI JavaScript and CSS compressor for Ruby and JRuby
|
91
|
+
test_files:
|
92
|
+
- test/css_test.rb
|
93
|
+
- test/js_test.rb
|