Skip to content

Commit 8ea37fb

Browse files
committed
Merge branch 'master' of https://github.com/documentcloud/jammit into bin_root
* 'master' of https://github.com/documentcloud/jammit: Fixes a typo. Fixed SASS test, it actually cares about single vs double quotes. Actually sets :jsmin as DEFAULT_JAVASCRIPT_COMPRESSOR AND :cssmin as DEFAULT_CSS_COMPRESSOR. They both are loaded inside `lib/jammit/dependencies.rb` cssmin and jsmin is the new minimum requirements of jammit. YUI is now optional. Added sass, cssmin, and jsmin to Gemfile as part of development+test requirements. Removed unecessary abstraction of YUI compressors. Refactor of Jammit. Late instantiation of Jammit::Compressor Added test for no-rewrite-relative-paths. Replace invalid font/* MIME types with valid ones, to suppress WebKit warnings Bad merge. Added rewrite_relative_paths. Erase has_rdoc Edit s.date = to current date time with proper format Fix for undefined RAILS_ENV (when running under Rake there's no Rails.env).
2 parents af7369d + ef53145 commit 8ea37fb

31 files changed

Lines changed: 252 additions & 77 deletions

Gemfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
source 'http://rubygems.org'
2+
3+
gemspec
4+
5+
group :development, :test do
6+
gem "rake", "0.9.2.2"
7+
gem "rails", "2.3.14"
8+
gem "cssmin", "1.0.2"
9+
gem "jsmin", "1.0.1"
10+
gem "yui-compressor", "0.9.6"
11+
gem "closure-compiler", "1.1.6"
12+
gem "uglifier", "1.2.4"
13+
gem "sass", "3.1.7"
14+
end
15+
16+
group :development do
17+
gem "RedCloth", "4.2.9"
18+
end

doc/Jammit/Compressor.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ <h2>Constant Summary</h2>
234234
<span class='rbrace token'>}</span>
235235
</pre></dd>
236236

237-
<dt id="DEFAULT_OPTIONS-constant" class="">DEFAULT_OPTIONS =
237+
<dt id="JAVASCRIPT_DEFAULT_OPTIONS-constant" class="">JAVASCRIPT_DEFAULT_OPTIONS =
238238

239239
</dt>
240240
<dd><pre class="code"><span class='lbrace token'>{</span>

index.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ <h2 id="installation">Installation</h2>
164164
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a>,
165165
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a> or
166166
<a href="https://github.com/lautis/uglifier">uglifier</a>
167-
gems installed, downloading make take a minute &mdash; the jar files together
167+
gems installed, downloading may take a minute &mdash; the jar files together
168168
weigh in at 5 megabytes.</i>
169169
</p>
170170

@@ -236,6 +236,13 @@ <h2 id="configuration">Configuration</h2>
236236
if you're packaging assets in development.
237237
</td>
238238
</tr>
239+
<tr>
240+
<td><b>rewrite_relative_paths</b></td>
241+
<td><tt>on&nbsp;|&nbsp;off</tt></td>
242+
<td class="definition">
243+
Defaults to <b>on</b>. When <b>off</b>, relative paths with CSS will be left unchanged.
244+
</td>
245+
</tr>
239246
<tr>
240247
<td><b>gzip_assets</b></td>
241248
<td><tt>on&nbsp;|&nbsp;off</tt></td>

jammit.gemspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Gem::Specification.new do |s|
2626
'--main' << 'README' <<
2727
'--all'
2828

29-
s.add_dependency 'yui-compressor', ['>= 0.9.3']
29+
s.add_dependency 'cssmin', ['>= 1.0.2']
30+
s.add_dependency 'jsmin', ['>= 1.0.1']
3031

3132
s.files = Dir['lib/**/*', 'bin/*', 'rails/*', 'jammit.gemspec', 'LICENSE', 'README']
3233
end

lib/jammit.rb

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,31 @@
44
# to all of the configuration options.
55
module Jammit
66

7-
VERSION = "0.6.5"
7+
VERSION = "0.6.5"
88

9-
ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
9+
ROOT = File.expand_path(File.dirname(__FILE__) + '/..')
1010

11-
ASSET_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ENV['RAILS_ROOT'] || ".") unless defined?(ASSET_ROOT)
11+
ASSET_ROOT = File.expand_path((defined?(Rails) && Rails.root.to_s.length > 0) ? Rails.root : ENV['RAILS_ROOT'] || ".") unless defined?(ASSET_ROOT)
1212

13-
DEFAULT_PUBLIC_ROOT = (defined?(Rails) && Rails.public_path.to_s.length > 0) ? Rails.public_path : File.join(ASSET_ROOT, 'public') unless defined?(PUBLIC_ROOT)
13+
DEFAULT_PUBLIC_ROOT = (defined?(Rails) && Rails.public_path.to_s.length > 0) ? Rails.public_path : File.join(ASSET_ROOT, 'public') unless defined?(PUBLIC_ROOT)
1414

15-
DEFAULT_CONFIG_PATH = File.join(ASSET_ROOT, 'config', 'assets.yml')
15+
DEFAULT_CONFIG_PATH = File.join(ASSET_ROOT, 'config', 'assets.yml')
1616

17-
DEFAULT_PACKAGE_PATH = "assets"
17+
DEFAULT_PACKAGE_PATH = "assets"
1818

19-
DEFAULT_JST_SCRIPT = File.join(ROOT, 'lib/jammit/jst.js')
19+
DEFAULT_JST_SCRIPT = File.join(ROOT, 'lib/jammit/jst.js')
2020

21-
DEFAULT_JST_COMPILER = "template"
21+
DEFAULT_JST_COMPILER = "template"
2222

23-
DEFAULT_JST_NAMESPACE = "window.JST"
23+
DEFAULT_JST_NAMESPACE = "window.JST"
2424

25-
COMPRESSORS = [:yui, :closure, :uglifier]
25+
JAVASCRIPT_COMPRESSORS = [:jsmin, :yui, :closure, :uglifier]
2626

27-
DEFAULT_COMPRESSOR = :yui
27+
DEFAULT_JAVASCRIPT_COMPRESSOR = :jsmin
28+
29+
CSS_COMPRESSORS = [:cssmin, :yui, :sass]
30+
31+
DEFAULT_CSS_COMPRESSOR = :cssmin
2832

2933
# Extension matchers for JavaScript and JST, which need to be disambiguated.
3034
JS_EXTENSION = /\.js\Z/
@@ -35,7 +39,7 @@ module Jammit
3539
class PackageNotFound < NameError; end
3640

3741
# Jammit raises a MissingConfiguration exception when you try to load the
38-
# configuration of an assets.yml file that doesn't exist, or are missing
42+
# configuration of an assets.yml file that doesn't exist, or are missing
3943
# a piece of required configuration.
4044
class MissingConfiguration < NameError; end
4145

@@ -50,17 +54,20 @@ class << self
5054
attr_reader :configuration, :template_function, :template_namespace,
5155
:embed_assets, :package_assets, :compress_assets, :gzip_assets,
5256
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
53-
:javascript_compressor, :compressor_options, :css_compressor_options,
54-
:template_extension, :template_extension_matcher, :allow_debugging,
55-
:public_root
56-
attr_accessor :compressors
57+
:javascript_compressor, :compressor_options, :css_compressor,
58+
:css_compressor_options, :template_extension,
59+
:template_extension_matcher, :allow_debugging,
60+
:rewrite_relative_paths, :public_root
61+
attr_accessor :javascript_compressors, :css_compressors
5762
end
5863

5964
# The minimal required configuration.
6065
@configuration = {}
6166
@public_root = DEFAULT_PUBLIC_ROOT
6267
@package_path = DEFAULT_PACKAGE_PATH
63-
@compressors = COMPRESSORS
68+
69+
@javascript_compressors = JAVASCRIPT_COMPRESSORS
70+
@css_compressors = CSS_COMPRESSORS
6471

6572
# Load the complete asset configuration from the specified @config_path@.
6673
# If we're loading softly, don't let missing configuration error out.
@@ -71,20 +78,22 @@ def self.load_configuration(config_path, soft=false)
7178
conf = YAML.load(ERB.new(File.read(config_path)).result)
7279

7380
# Optionally overwrite configuration based on the environment.
74-
rails_env = defined?(Rails) ? Rails.env : ENV['RAILS_ENV']
81+
rails_env = (defined?(Rails) ? ::Rails.env : ENV['RAILS_ENV'] || "development")
7582
conf.merge! conf.delete rails_env if conf.has_key? rails_env
7683

7784
@config_path = config_path
7885
@configuration = symbolize_keys(conf)
7986
@package_path = conf[:package_path] || DEFAULT_PACKAGE_PATH
8087
@embed_assets = conf[:embed_assets] || conf[:embed_images]
8188
@compress_assets = !(conf[:compress_assets] == false)
89+
@rewrite_relative_paths = !(conf[:rewrite_relative_paths] == false)
8290
@gzip_assets = !(conf[:gzip_assets] == false)
8391
@allow_debugging = !(conf[:allow_debugging] == false)
8492
@mhtml_enabled = @embed_assets && @embed_assets != "datauri"
8593
@compressor_options = symbolize_keys(conf[:compressor_options] || {})
8694
@css_compressor_options = symbolize_keys(conf[:css_compressor_options] || {})
8795
set_javascript_compressor(conf[:javascript_compressor])
96+
set_css_compressor(conf[:css_compressor])
8897
set_package_assets(conf[:package_assets])
8998
set_template_function(conf[:template_function])
9099
set_template_namespace(conf[:template_namespace])
@@ -148,7 +157,13 @@ def self.set_public_root(public_root=nil)
148157
# Ensure that the JavaScript compressor is a valid choice.
149158
def self.set_javascript_compressor(value)
150159
value = value && value.to_sym
151-
@javascript_compressor = compressors.include?(value) ? value : DEFAULT_COMPRESSOR
160+
@javascript_compressor = javascript_compressors.include?(value) ? value : DEFAULT_JAVASCRIPT_COMPRESSOR
161+
end
162+
163+
# Ensure that the CSS compressor is a valid choice.
164+
def self.set_css_compressor(value)
165+
value = value && value.to_sym
166+
@css_compressor = css_compressors.include?(value) ? value : DEFAULT_CSS_COMPRESSOR
152167
end
153168

154169
# Turn asset packaging on or off, depending on configuration and environment.

lib/jammit/compressor.rb

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class Compressor
1515
'.gif' => 'image/gif',
1616
'.tif' => 'image/tiff',
1717
'.tiff' => 'image/tiff',
18-
'.ttf' => 'font/truetype',
18+
'.ttf' => 'application/x-font-ttf',
1919
'.otf' => 'font/opentype',
20-
'.woff' => 'font/woff'
20+
'.woff' => 'application/x-font-woff'
2121
}
2222

2323
# Font extensions for which we allow embedding:
@@ -41,26 +41,39 @@ class Compressor
4141
JST_START = "(function(){"
4242
JST_END = "})();"
4343

44-
COMPRESSORS = {
45-
:yui => YUI::JavaScriptCompressor,
46-
:closure => Jammit.compressors.include?(:closure) ? Closure::Compiler : nil,
47-
:uglifier => Jammit.compressors.include?(:uglifier) ? Jammit::Uglifier : nil
44+
JAVASCRIPT_COMPRESSORS = {
45+
:jsmin => Jammit.javascript_compressors.include?(:jsmin) ? Jammit::JsminCompressor : nil,
46+
:yui => Jammit.javascript_compressors.include?(:yui) ? YUI::JavaScriptCompressor : nil,
47+
:closure => Jammit.javascript_compressors.include?(:closure) ? Closure::Compiler : nil,
48+
:uglifier => Jammit.javascript_compressors.include?(:uglifier) ? Jammit::Uglifier : nil
4849
}
4950

50-
DEFAULT_OPTIONS = {
51+
CSS_COMPRESSORS = {
52+
:cssmin => Jammit.css_compressors.include?(:cssmin) ? Jammit::CssminCompressor : nil,
53+
:yui => Jammit.css_compressors.include?(:yui) ? YUI::CssCompressor : nil,
54+
:sass => Jammit.css_compressors.include?(:sass) ? Jammit::SassCompressor : nil
55+
}
56+
57+
JAVASCRIPT_DEFAULT_OPTIONS = {
58+
:jsmin => {},
5159
:yui => {:munge => true},
5260
:closure => {},
5361
:uglifier => {:copyright => false}
5462
}
5563

56-
# The css compressor is always the YUI Compressor. JS compression can be
57-
# provided with YUI Compressor, Google Closure Compiler or UglifyJS.
64+
# CSS compression can be provided with YUI Compressor or sass. JS
65+
# compression can be provided with YUI Compressor, Google Closure
66+
# Compiler or UglifyJS.
5867
def initialize
59-
Jammit.check_java_version
60-
@css_compressor = YUI::CssCompressor.new(Jammit.css_compressor_options || {})
61-
flavor = Jammit.javascript_compressor || Jammit::DEFAULT_COMPRESSOR
62-
@options = DEFAULT_OPTIONS[flavor].merge(Jammit.compressor_options || {})
63-
@js_compressor = COMPRESSORS[flavor].new(@options)
68+
if Jammit.javascript_compressors.include?(:yui) || Jammit.javascript_compressors.include?(:closure) || Jammit.css_compressors.include?(:yui)
69+
Jammit.check_java_version
70+
end
71+
72+
css_flavor = Jammit.css_compressor || Jammit::DEFAULT_CSS_COMPRESSOR
73+
@css_compressor = CSS_COMPRESSORS[css_flavor].new(Jammit.css_compressor_options || {})
74+
js_flavor = Jammit.javascript_compressor || Jammit::DEFAULT_JAVASCRIPT_COMPRESSOR
75+
@options = JAVASCRIPT_DEFAULT_OPTIONS[js_flavor].merge(Jammit.compressor_options || {})
76+
@js_compressor = JAVASCRIPT_COMPRESSORS[js_flavor].new(@options)
6477
end
6578

6679
# Concatenate together a list of JavaScript paths, and pass them through the
@@ -179,7 +192,7 @@ def with_mhtml(css, asset_url)
179192
def construct_asset_path(asset_path, css_path, variant)
180193
public_path = absolute_path(asset_path, css_path)
181194
return "__EMBED__#{public_path}" if embeddable?(public_path, variant)
182-
source = asset_path.absolute? ? asset_path.to_s : relative_path(public_path)
195+
source = asset_path.absolute? || ! Jammit.rewrite_relative_paths ? asset_path.to_s : relative_path(public_path)
183196
rewrite_asset_path(source, public_path)
184197
end
185198

lib/jammit/cssmin_compressor.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Wraps CSSMin compressor to use the same API as the rest of
2+
# Jammit's compressors.
3+
class Jammit::CssminCompressor
4+
def initialize(options = {})
5+
end
6+
7+
def compress(css)
8+
CSSMin.minify(css)
9+
end
10+
end

lib/jammit/dependencies.rb

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,45 @@
77
require 'pathname'
88
require 'fileutils'
99

10-
# Include YUI as the default
11-
require 'yui/compressor'
10+
# Try Uglifier.
11+
begin
12+
require 'uglifier'
13+
require 'jammit/uglifier'
14+
rescue LoadError
15+
Jammit.javascript_compressors.delete :uglifier
16+
end
17+
18+
# Try YUI
19+
begin
20+
require 'yui/compressor'
21+
rescue LoadError
22+
Jammit.javascript_compressors.delete :yui
23+
Jammit.css_compressors.delete :yui
24+
end
1225

1326
# Try Closure.
1427
begin
1528
require 'closure-compiler'
1629
rescue LoadError
17-
Jammit.compressors.delete :closure
30+
Jammit.javascript_compressors.delete :closure
1831
end
1932

20-
# Try Uglifier.
33+
# Try Sass
2134
begin
22-
require 'uglifier'
35+
require 'sass'
36+
require 'jammit/sass_compressor'
2337
rescue LoadError
24-
Jammit.compressors.delete :uglifier
38+
Jammit.css_compressors.delete :sass
2539
end
2640

2741
# Load initial configuration before the rest of Jammit.
2842
Jammit.load_configuration(Jammit::DEFAULT_CONFIG_PATH, true) if defined?(Rails)
2943

3044
# Jammit Core:
31-
require 'jammit/uglifier' if Jammit.compressors.include? :uglifier
45+
require 'jsmin'
46+
require 'cssmin'
47+
require 'jammit/jsmin_compressor'
48+
require 'jammit/cssmin_compressor'
3249
require 'jammit/compressor'
3350
require 'jammit/packager'
3451

lib/jammit/jsmin_compressor.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Wraps JSMin compressor to use the same API as the rest of
2+
# Jammit's compressors.
3+
class Jammit::JsminCompressor
4+
def initialize(options = {})
5+
end
6+
7+
def compress(js)
8+
JSMin.minify(js)
9+
end
10+
end

lib/jammit/packager.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Packager
1414
# Jammit.configuration. When assets.yml is being changed on the fly,
1515
# create a new Packager.
1616
def initialize
17-
@compressor = Compressor.new
1817
@force = false
1918
@package_names = nil
2019
@config = {
@@ -71,21 +70,25 @@ def individual_urls(package, extension)
7170
package_for(package, extension)[:urls]
7271
end
7372

73+
def compressor
74+
@compressor ||= Compressor.new
75+
end
76+
7477
# Return the compressed contents of a stylesheet package.
7578
def pack_stylesheets(package, variant=nil, asset_url=nil)
76-
@compressor.compress_css(package_for(package, :css)[:paths], variant, asset_url)
79+
compressor.compress_css(package_for(package, :css)[:paths], variant, asset_url)
7780
end
7881

7982
# Return the compressed contents of a javascript package.
8083
def pack_javascripts(package)
81-
@compressor.compress_js(package_for(package, :js)[:paths])
84+
compressor.compress_js(package_for(package, :js)[:paths])
8285
end
8386

8487
# Return the compiled contents of a JST package.
8588
def pack_templates(package)
86-
@compressor.compile_jst(package_for(package, :js)[:paths])
89+
compressor.compile_jst(package_for(package, :js)[:paths])
8790
end
88-
91+
8992

9093
private
9194

@@ -109,7 +112,7 @@ def glob_files(glob)
109112
def path_to_url
110113
@path_to_url ||= /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(Jammit.public_root.sub(ASSET_ROOT, ''))})?/
111114
end
112-
115+
113116
# Get the latest mtime of a list of files (plus the config path).
114117
def latest_mtime(paths)
115118
paths += [Jammit.config_path]

0 commit comments

Comments
 (0)