Navigation Menu

Skip to content

Commit

Permalink
Merged from documentcloud master.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 18, 2011
2 parents 80ec16f + 3dedc9d commit 24508fe
Show file tree
Hide file tree
Showing 15 changed files with 86 additions and 56 deletions.
68 changes: 44 additions & 24 deletions index.html
Expand Up @@ -87,13 +87,13 @@
<a href="http://github.com/documentcloud/jammit/">Jammit</a> is an
industrial strength asset packaging library for <b>Rails</b>, providing
both the CSS and JavaScript concatenation and compression
that you'd expect, as well as <b>YUI Compressor</b> and <b>Closure Compiler</b>
that you'd expect, as well as <b>YUI Compressor</b>, <b>Closure Compiler</b>, and <b>UglifyJS</b>
compatibility, ahead-of-time gzipping, built-in JavaScript template
support, and optional <b>Data-URI / MHTML image and font embedding</b>.
</p>

<p>
<b>Current Version:</b> <a href="http://rubygems.org/gems/jammit/">0.6.0</a>
<b>Current Version:</b> <a href="http://rubygems.org/gems/jammit/">0.6.3</a>
</p>

<p>
Expand All @@ -109,11 +109,11 @@

<h2>Table of Contents</h2>

<p style="line-height: 30px;">
<p style="line-height: 30px; width: 650px;">
<a href="#installation">Installation</a> |
<a href="#configuration">Configuration</a> |
<a href="#usage">Usage</a> |
<a href="#compressors">YUI &amp; Closure</a> |
<a href="#compressors">YUI, Closure &amp; UglifyJS</a> |
<a href="#precaching">Precaching Assets</a> <br />
<a href="#expires">Expires Headers</a> |
<a href="#embedding">Embedding Assets</a> |
Expand Down Expand Up @@ -161,8 +161,9 @@ <h2 id="installation">Installation</h2>

<p>
<i>Note: If you don't already have the
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a> or
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a>
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a>,
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a> or
<a href="https://github.com/lautis/uglifier">uglifier</a>
gems installed, downloading make take a minute &mdash; the jar files together
weigh in at 5 megabytes.</i>
</p>
Expand Down Expand Up @@ -231,7 +232,7 @@ <h2 id="configuration">Configuration</h2>
<td><tt>on&nbsp;|&nbsp;off</tt></td>
<td class="definition">
Defaults to <b>on</b>. When <b>off</b>, JavaScript and CSS packages
will be left uncompressed (by YUI or Closure). Disabling compression is only recommended
will be left uncompressed (by YUI, Closure, or UglifyJS). Disabling compression is only recommended
if you're packaging assets in development.
</td>
</tr>
Expand All @@ -254,12 +255,12 @@ <h2 id="configuration">Configuration</h2>
</tr>
<tr>
<td><b>javascript_compressor</b></td>
<td><tt>yui&nbsp;|&nbsp;closure</tt></td>
<td><tt>yui&nbsp;|&nbsp;closure&nbsp;|&nbsp;uglifier</tt></td>
<td class="definition">
Defaults to <b>yui</b>. As of <b>0.2.0</b>, the Jammit gem can use either the
<a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>
or the
<a href="http://code.google.com/closure/compiler/">Google Closure Compiler</a>
Defaults to <b>yui</b>. The Jammit gem can use either the
<a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>, the
<a href="http://code.google.com/closure/compiler/">Google Closure Compiler</a>, or
<a href="https://github.com/mishoo/UglifyJS/">UglifyJS</a>
to compress your JavaScript.
</td>
</tr>
Expand Down Expand Up @@ -321,8 +322,9 @@ <h2 id="configuration">Configuration</h2>
<td class="definition">
Pass an options hash directly to the underlying JavaScript compressor
to configure it. See the
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a> or
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a>
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a>,
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a>, or
<a href="https://github.com/lautis/uglifier">uglifier</a>
gem documentation for the full list of available options.
</td>
</tr>
Expand Down Expand Up @@ -384,23 +386,25 @@ <h2 id="usage">Usage</h2>
<pre>require 'jammit'
Jammit.package!</pre>

<h2 id="compressors">YUI &amp; Closure</h2>
<h2 id="compressors">YUI, Closure &amp; UglifyJS</h2>

<p>
Jammit can be configured to use either the
<a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a> or the
<a href="http://code.google.com/closure/compiler/">Google Closure Compiler</a>
Jammit can be configured to use the
<a href="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a>, the
<a href="http://code.google.com/closure/compiler/">Google Closure Compiler</a>, or
<a href="https://github.com/mishoo/UglifyJS/">UglifyJS</a>
to compress and optimize your JavaScript. (CSS is always run through the
YUI Compressor.) Specify the <b>javascript_compressor</b> to choose either <b>yui</b>
or <b>closure</b> backends. If left blank, Jammit defaults to <b>yui</b>.
YUI Compressor.) Specify the <b>javascript_compressor</b> to choose either <b>yui</b>,
<b>closure</b>, or <b>uglifier</b> backends. If left blank, Jammit defaults to <b>yui</b>.
</p>

<p>
You can configure the JavaScript compilation by adding <b>compressor_options</b>
to your <b>assets.yml</b>. The <b>compressor_options</b>
will be passed directly to the Gem backend of your chosen compressor. See the
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a> or
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a>
<a href="http://github.com/sstephenson/ruby-yui-compressor">ruby-yui-compressor</a>,
<a href="http://github.com/documentcloud/closure-compiler">closure-compiler</a>, or
<a href="https://github.com/lautis/uglifier">uglifier</a>
gem documentation for all the available options. For example, to configure
the Closure Compiler to use its
<a href="http://code.google.com/closure/compiler/docs/compilation_levels.html">advanced optimizations</a>,
Expand Down Expand Up @@ -463,8 +467,6 @@ <h2 id="precaching">Precaching Assets</h2>
<a href="http://rubyhitsquad.com/Vlad_the_Deployer.html">Vlad</a>,
or just good 'ol <a href="http://rake.rubyforge.org/">Rake</a>,
it shouldn't be too hard to add a step that calls the <tt>jammit</tt> command.
For an example Jammit setup under Capistrano and Apache, see
<a href="http://afreshcup.com/home/2010/1/18/notes-on-using-jammit-with-rails.html">Mike Gunderloy's blog post</a>.
</p>

<h2 id="expires">Expires Headers</h2>
Expand Down Expand Up @@ -697,6 +699,24 @@ <h2 id="jst">JavaScript Templates</h2>

<h2 id="changes">Change Log</h2>

<p>
<b class="header">0.6.3</b> &mdash; <small><i>May 26, 2011</i></small><br />
Quick bugfix release for Rails 3.1 compatibility, which changes the method
signatures of <tt>javascript_include_tag</tt> and <tt>stylesheet_link_tag</tt>
in a backwards-incompatible fashion.
</p>

<p>
<b class="header">0.6.1</b> &mdash; <small><i>May 25, 2011</i></small><br />
Jammit now supports <b>UglifyJS</b> as a Java-free alternative to YUI and Closure,
for JavaScript compression.
You can now pass <tt>--packages core,embed,ui</tt> to the <tt>jammit</tt>
command, in order to only build a subset of your asset packages.
The built-in JS templating function has been upgraded to the equivalent of
the latest <tt>_.template</tt>.
Environment-specific settings are now supported in your <tt>assets.yml</tt> file.
</p>

<p>
<b class="header">0.6.0</b><br />
You can now pass <tt>debug_assets=true</tt> as a query parameter to debug
Expand Down
2 changes: 1 addition & 1 deletion jammit.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = 'jammit'
s.version = '0.6.0' # Keep version in sync with jammit.rb
s.version = '0.6.3' # Keep version in sync with jammit.rb
s.date = Time.now.strftime('%Y-%m-%d')

s.homepage = "http://documentcloud.github.com/jammit/"
Expand Down
14 changes: 8 additions & 6 deletions lib/jammit.rb
Expand Up @@ -4,7 +4,7 @@
# to all of the configuration options.
module Jammit

VERSION = "0.6.0"
VERSION = "0.6.3"

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

Expand Down Expand Up @@ -36,9 +36,10 @@ module Jammit
# requested by a browser -- rendering a 404.
class PackageNotFound < NameError; end

# Jammit raises a ConfigurationNotFound exception when you try to load the
# configuration of an assets.yml file that doesn't exist.
class ConfigurationNotFound < NameError; end
# Jammit raises a MissingConfiguration exception when you try to load the
# configuration of an assets.yml file that doesn't exist, or are missing
# a piece of required configuration.
class MissingConfiguration < NameError; end

# Jammit raises an OutputNotWritable exception if the output directory for
# cached packages is locked.
Expand Down Expand Up @@ -67,11 +68,12 @@ class << self
def self.load_configuration(config_path, soft=false)
exists = config_path && File.exists?(config_path)
return false if soft && !exists
raise ConfigurationNotFound, "could not find the \"#{config_path}\" configuration file" unless exists
raise MissingConfiguration, "could not find the \"#{config_path}\" configuration file" unless exists
conf = YAML.load(ERB.new(File.read(config_path)).result)

# Optionally overwrite configuration based on the environment.
conf.merge! conf.delete RAILS_ENV if conf.has_key? RAILS_ENV
rails_env = defined?(Rails) ? Rails.env : ENV['RAILS_ENV']
conf.merge! conf.delete rails_env if conf.has_key? rails_env

@config_path = config_path
@configuration = symbolize_keys(conf)
Expand Down
2 changes: 1 addition & 1 deletion lib/jammit/command_line.rb
Expand Up @@ -63,7 +63,7 @@ def parse_options
opts.on('-f', '--force', 'force a rebuild of all assets') do |force|
@options[:force] = force
end
opts.on('-p', '--packages PACKAGE_NAMES', 'comma-separated list of packages (ex: "core,embed", default: all packages)') do |package_names|
opts.on('-p', '--packages LIST', 'list of packages to build (ex: "core,ui", default: all)') do |package_names|
@options[:package_names] = package_names.split(/,\s*/).map {|n| n.to_sym }
end
opts.on_tail('-v', '--version', 'display Jammit version') do
Expand Down
18 changes: 10 additions & 8 deletions lib/jammit/helper.rb
Expand Up @@ -16,7 +16,7 @@ module Helper
# compressed CSS, and in development the stylesheet URLs are passed verbatim.
def include_stylesheets(*packages)
options = packages.extract_options!
return individual_stylesheets(packages, options) unless should_package?
return html_safe(individual_stylesheets(packages, options)) unless should_package?
disabled = (options.delete(:embed_assets) == false) || (options.delete(:embed_images) == false)
return html_safe(packaged_stylesheets(packages, options)) if disabled || !Jammit.embed_assets
return html_safe(embedded_image_stylesheets(packages, options))
Expand All @@ -25,10 +25,11 @@ def include_stylesheets(*packages)
# Writes out the URL to the bundled and compressed javascript package,
# except in development, where it references the individual scripts.
def include_javascripts(*packages)
tags = packages.map do |pack|
html_safe packages.map {|pack|
should_package? ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
end
html_safe(javascript_include_tag(tags.flatten))
}.flatten.map {|pack|
javascript_include_tag pack
}.join("\n")
end

# Writes out the URL to the concatenated and compiled JST file -- we always
Expand Down Expand Up @@ -71,10 +72,11 @@ def embedded_image_stylesheets(packages, options)
# Generate the stylesheet tags for a batch of packages, with options, by
# yielding each package to a block.
def tags_with_options(packages, options)
packages = packages.dup
packages.map! {|package| yield package }.flatten!
packages.push(options) unless options.empty?
stylesheet_link_tag(*packages)
packages.dup.map {|package|
yield package
}.flatten.map {|package|
stylesheet_link_tag package, options
}.join("\n")
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/jammit/jst.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions lib/jammit/packager.rb
Expand Up @@ -43,8 +43,9 @@ def precache_all(output_dir=nil, base_url=nil)
cache(p, 'css', pack_stylesheets(p), output_dir)
if Jammit.embed_assets
cache(p, 'css', pack_stylesheets(p, :datauri), output_dir, :datauri)
if Jammit.mhtml_enabled && base_url
mtime = Time.now
if Jammit.mhtml_enabled
raise MissingConfiguration, "A --base-url option is required in order to generate MHTML." unless base_url
mtime = latest_mtime package_for(p, :css)[:paths]
asset_url = "#{base_url}#{Jammit.asset_url(p, :css, :mhtml, mtime)}"
cache(p, 'css', pack_stylesheets(p, :mhtml, asset_url), output_dir, :mhtml, mtime)
end
Expand All @@ -55,9 +56,10 @@ def precache_all(output_dir=nil, base_url=nil)
# Caches a single prebuilt asset package and gzips it at the highest
# compression level. Ensures that the modification time of both both
# variants is identical, for web server caching modules, as well as MHTML.
def cache(package, extension, contents, output_dir, suffix=nil, mtime=Time.now)
def cache(package, extension, contents, output_dir, suffix=nil, mtime=nil)
FileUtils.mkdir_p(output_dir) unless File.exists?(output_dir)
raise OutputNotWritable, "Jammit doesn't have permission to write to \"#{output_dir}\"" unless File.writable?(output_dir)
mtime ||= latest_mtime package_for(package, extension.to_sym)[:paths]
files = []
files << file_name = File.join(output_dir, Jammit.filename(package, extension, suffix))
File.open(file_name, 'wb+') {|f| f.write(contents) }
Expand Down Expand Up @@ -105,6 +107,12 @@ def glob_files(glob)
Jammit.warn("No assets match '#{glob}'") if paths.empty?
paths
end

# Get the latest mtime of a list of files (plus the config path).
def latest_mtime(paths)
paths += [Jammit.config_path]
paths.map {|p| File.mtime(p) }.max || Time.now
end

# Return a list of all of the packages that should be cached. If "force" is
# true, this is all of them -- otherwise only the packages that are missing
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/jammed/js_test_with_templates.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/jammed/jst_test-custom-namespace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/fixtures/jammed/jst_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24508fe

Please sign in to comment.