-
Notifications
You must be signed in to change notification settings - Fork 6
/
README
157 lines (119 loc) · 5.54 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
YUICompressor
=============
YUICompressor is a Ruby module that may be used to create compressed versions
of JavaScript and CSS code quickly and easily using the Yahoo User Interface
(YUI) library compressor. The module is essentially a wrapper around the YUI
Compressor (a Java library) that supports two different modes of operation:
shell and native.
In shell mode the YUI Compressor library executes in a separate process. Code
is piped into and out of this process using the system shell. This approach
yields good performance and is the default for MRI and other Ruby versions that
are not able to execute Java code.
In native mode the compressor is invoked in the same process as Ruby. This is
only possible when using YUICompressor on JRuby. With this approach, compression
speeds dramatically improve because the system does not incur the overhead of
invoking a separate Java process for each compression.
In any case YUICompressor will automatically detect the Ruby platform it is
running on and choose the mode that will offer the best performance.
The inspiration for this project is Sam Stephenson's yui-compressor gem. The
goal of YUICompressor is to provide a more fluent interface, greater
flexibility, and better performance than its predecessor.
Installation
------------
Via RubyGems:
$ gem install yuicompressor
From a local copy:
$ git clone git://github.com/mjijackson/yuicompressor.git
$ cd yuicompressor
$ rake package && rake install
Usage
-----
The easiest way to compress JavaScript code using YUICompressor is to pass an IO
or a string containing your code to YUICompressor.compress_js. Similarly, CSS
code may similarly be compressed using YUICompressor.compress_css. Both methods
may take a second parameter that is a Hash of options to use during compression.
When compressing CSS, the following option is available:
:line_break The maximum number of characters that may appear in a
single line of compressed code. Defaults to no maximum
length. If set to 0 each line will be the minimum length
possible.
:charset Sests the carset in which to read the input file. Passed
through to the YUICompressor Java library. Default: utf8
When compressing JavaScript, these additional options are available:
:munge Should be +true+ if the compressor should shorten
local variable names when possible. Defaults to
+false+.
:preserve_semicolons Should be +true+ if the compressor should preserve
all semicolons in the code. Defaults to +false+.
:optimize Should be +true+ if the compressor should enable all
micro optimizations. Defaults to +true+.
When running in shell mode (i.e. not on JRuby) a :java option may be used to
specify the path to the Java binary. The default value is "java".
Examples
--------
JavaScript compression:
require 'yuicompressor'
js = '(function () { var abc = {}; abc["key"] = "value"; })()'
YUICompressor.compress_js(js)
=> "(function(){var abc={};abc.key=\"value\"})();"
JavaScript compression with shortened local variable names (i.e. munging):
require 'yuicompressor'
js = '(function () { var abc = {}; abc["key"] = "value"; })()'
YUICompressor.compress_js(js, :munge => true)
=> "(function(){var a={};a.key=\"value\"})();"
CSS compression:
require 'yuicompressor'
css = <<'CODE'
div.main {
background-position: 0;
}
h1#title {
color: #FFFFFF;
}
CODE
YUICompressor.compress_css(css)
=> "div.main{background-position:0 0}h1#title{color:#fff}"
CSS compression with every statement on a separate line:
require 'yuicompressor'
css = <<'CODE'
div.main {
background-position: 0;
}
h1#title {
color: #FFFFFF;
}
CODE
YUICompressor.compress_css(css, :line_break => 0)
=> "div.main{background-position:0 0}\nh1#title{color:#fff}"
Including in another module:
require 'yuicompressor'
module MyJavaScriptCompressor
include YUICompressor
def compress(code)
compress_js(code)
end
end
License
-------
Copyright 2010 Michael Jackson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Credits
-------
The YUI Compressor library is copyright Yahoo! Inc. The jar file is included in
the source distribution under the terms of the BSD license.
Also, thanks to Charles Nutter (@headius) for helping me understand how to
convert Ruby streams to Java and back when doing the JRuby module.