Skip to content

Commit b656d6e

Browse files
author
Frank Macreery
committed
Merge branch 'master' into valid-mime-types
2 parents 67ef23e + 2bcbdff commit b656d6e

9 files changed

Lines changed: 49 additions & 23 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2009 Jeremy Ashkenas, DocumentCloud
1+
Copyright (c) 2009-2011 Jeremy Ashkenas, DocumentCloud
22

33
Permission is hereby granted, free of charge, to any person
44
obtaining a copy of this software and associated documentation

index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
</p>
9494

9595
<p>
96-
<b>Current Version:</b> <a href="http://rubygems.org/gems/jammit/">0.6.3</a>
96+
<b>Current Version:</b> <a href="http://rubygems.org/gems/jammit/">0.6.5</a>
9797
</p>
9898

9999
<p>
@@ -692,6 +692,12 @@ <h2 id="jst">JavaScript Templates</h2>
692692

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

695+
<p>
696+
<b class="header">0.6.5</b> &mdash; <small><i>Nov 30, 2011</i></small><br />
697+
Added the ability to pass HTML tag options to <tt>include_javascripts</tt>.
698+
Fixed a bug that would always cause JS to be recompiled, regardless of mtimes.
699+
</p>
700+
695701
<p>
696702
<b class="header">0.6.3</b> &mdash; <small><i>May 26, 2011</i></small><br />
697703
Quick bugfix release for Rails 3.1 compatibility, which changes the method

jammit.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22
s.name = 'jammit'
3-
s.version = '0.6.3' # Keep version in sync with jammit.rb
4-
s.date = '2011-05-26'
3+
s.version = '0.6.5' # Keep version in sync with jammit.rb
4+
s.date = '2011-11-30'
55

66
s.homepage = "http://documentcloud.github.com/jammit/"
77
s.summary = "Industrial Strength Asset Packaging for Rails"

lib/jammit.rb

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

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

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

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

13-
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

1515
DEFAULT_CONFIG_PATH = File.join(ASSET_ROOT, 'config', 'assets.yml')
1616

@@ -51,12 +51,14 @@ class << self
5151
:embed_assets, :package_assets, :compress_assets, :gzip_assets,
5252
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
5353
:javascript_compressor, :compressor_options, :css_compressor_options,
54-
:template_extension, :template_extension_matcher, :allow_debugging
54+
:template_extension, :template_extension_matcher, :allow_debugging,
55+
:public_root
5556
attr_accessor :compressors
5657
end
5758

5859
# The minimal required configuration.
5960
@configuration = {}
61+
@public_root = DEFAULT_PUBLIC_ROOT
6062
@package_path = DEFAULT_PACKAGE_PATH
6163
@compressors = COMPRESSORS
6264

@@ -87,6 +89,7 @@ def self.load_configuration(config_path, soft=false)
8789
set_template_function(conf[:template_function])
8890
set_template_namespace(conf[:template_namespace])
8991
set_template_extension(conf[:template_extension])
92+
set_public_root(conf[:public_root]) if conf[:public_root]
9093
symbolize_keys(conf[:stylesheets]) if conf[:stylesheets]
9194
symbolize_keys(conf[:javascripts]) if conf[:javascripts]
9295
check_for_deprecations
@@ -124,16 +127,24 @@ def self.package!(options={})
124127
:config_path => Jammit::DEFAULT_CONFIG_PATH,
125128
:output_folder => nil,
126129
:base_url => nil,
130+
:public_root => nil,
127131
:force => false
128132
}.merge(options)
129133
load_configuration(options[:config_path])
134+
set_public_root(options[:public_root]) if options[:public_root]
130135
packager.force = options[:force]
131136
packager.package_names = options[:package_names]
132137
packager.precache_all(options[:output_folder], options[:base_url])
133138
end
134139

135140
private
136141

142+
# Allows command-line definition of `PUBLIC_ROOT`, for those using Jammit
143+
# outside of Rails.
144+
def self.set_public_root(public_root=nil)
145+
@public_root = public_root if public_root
146+
end
147+
137148
# Ensure that the JavaScript compressor is a valid choice.
138149
def self.set_javascript_compressor(value)
139150
value = value && value.to_sym

lib/jammit/command_line.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ def parse_options
6666
opts.on('-p', '--packages LIST', 'list of packages to build (ex: "core,ui", default: all)') do |package_names|
6767
@options[:package_names] = package_names.split(/,\s*/).map {|n| n.to_sym }
6868
end
69+
opts.on('-P', '--public-root PATH', 'path to public assets (default: "public")') do |public_root|
70+
puts "Option for PUBLIC_ROOT"
71+
@options[:public_root] = public_root
72+
end
6973
opts.on_tail('-v', '--version', 'display Jammit version') do
7074
puts "Jammit version #{Jammit::VERSION}"
7175
exit

lib/jammit/compressor.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ def construct_asset_path(asset_path, css_path, variant)
187187
# not be relative, given the path of the stylesheet that contains it.
188188
def absolute_path(asset_pathname, css_pathname)
189189
(asset_pathname.absolute? ?
190-
Pathname.new(File.join(PUBLIC_ROOT, asset_pathname)) :
190+
Pathname.new(File.join(Jammit.public_root, asset_pathname)) :
191191
css_pathname.dirname + asset_pathname).cleanpath
192192
end
193193

194194
# CSS assets that are referenced by relative paths, and are *not* being
195195
# embedded, must be rewritten relative to the newly-merged stylesheet path.
196196
def relative_path(absolute_path)
197-
File.join('../', absolute_path.sub(PUBLIC_ROOT, ''))
197+
File.join('../', absolute_path.sub(Jammit.public_root, ''))
198198
end
199199

200200
# Similar to the AssetTagHelper's method of the same name, this will
@@ -246,7 +246,7 @@ def concatenate(paths)
246246

247247
# `File.read`, but in "binary" mode.
248248
def read_binary_file(path)
249-
File.open(path, 'rb') {|f| f.read }
249+
File.open(path, 'rb:UTF-8') {|f| f.read }
250250
end
251251
end
252252

lib/jammit/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Controller < ActionController::Base
1111

1212
SUFFIX_STRIPPER = /-(datauri|mhtml)\Z/
1313

14-
NOT_FOUND_PATH = "#{PUBLIC_ROOT}/404.html"
14+
NOT_FOUND_PATH = "#{Jammit.public_root}/404.html"
1515

1616
# The "package" action receives all requests for asset packages that haven't
1717
# yet been cached. The package will be built, cached, and gzipped.

lib/jammit/helper.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ def include_stylesheets(*packages)
2525
# Writes out the URL to the bundled and compressed javascript package,
2626
# except in development, where it references the individual scripts.
2727
def include_javascripts(*packages)
28+
options = packages.extract_options!
2829
html_safe packages.map {|pack|
2930
should_package? ? Jammit.asset_url(pack, :js) : Jammit.packager.individual_urls(pack.to_sym, :js)
3031
}.flatten.map {|pack|
31-
javascript_include_tag pack
32+
javascript_include_tag pack, options
3233
}.join("\n")
3334
end
3435

@@ -72,8 +73,8 @@ def embedded_image_stylesheets(packages, options)
7273
# Generate the stylesheet tags for a batch of packages, with options, by
7374
# yielding each package to a block.
7475
def tags_with_options(packages, options)
75-
packages.dup.map {|package|
76-
yield package
76+
packages.dup.map {|package|
77+
yield package
7778
}.flatten.map {|package|
7879
stylesheet_link_tag package, options
7980
}.join("\n")

lib/jammit/packager.rb

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ module Jammit
66
# with the correct timestamps.
77
class Packager
88

9-
# In Rails, the difference between a path and an asset URL is "public".
10-
PATH_DIFF = PUBLIC_ROOT.sub(ASSET_ROOT, '')
11-
PATH_TO_URL = /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(PATH_DIFF)})?/
12-
139
# Set force to false to allow packages to only be rebuilt when their source
1410
# files have changed since the last time their package was built.
1511
attr_accessor :force, :package_names
@@ -37,7 +33,7 @@ def initialize
3733
# Unless forced, will only rebuild assets whose source files have been
3834
# changed since their last package build.
3935
def precache_all(output_dir=nil, base_url=nil)
40-
output_dir ||= File.join(PUBLIC_ROOT, Jammit.package_path)
36+
output_dir ||= File.join(Jammit.public_root, Jammit.package_path)
4137
cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) }
4238
cacheable(:css, output_dir).each do |p|
4339
cache(p, 'css', pack_stylesheets(p), output_dir)
@@ -89,6 +85,7 @@ def pack_javascripts(package)
8985
def pack_templates(package)
9086
@compressor.compile_jst(package_for(package, :js)[:paths])
9187
end
88+
9289

9390
private
9491

@@ -107,6 +104,11 @@ def glob_files(glob)
107104
Jammit.warn("No assets match '#{glob}'") if paths.empty?
108105
paths
109106
end
107+
108+
# In Rails, the difference between a path and an asset URL is "public".
109+
def path_to_url
110+
@path_to_url ||= /\A#{Regexp.escape(ASSET_ROOT)}(\/?#{Regexp.escape(Jammit.public_root.sub(ASSET_ROOT, ''))})?/
111+
end
110112

111113
# Get the latest mtime of a list of files (plus the config path).
112114
def latest_mtime(paths)
@@ -125,8 +127,10 @@ def cacheable(extension, output_dir)
125127
return names.select do |name|
126128
pack = package_for(name, extension)
127129
cached = [Jammit.filename(name, extension)]
128-
cached.push Jammit.filename(name, extension, :datauri) if Jammit.embed_assets
129-
cached.push Jammit.filename(name, extension, :mhtml) if Jammit.mhtml_enabled
130+
if extension == :css
131+
cached.push Jammit.filename(name, extension, :datauri) if Jammit.embed_assets
132+
cached.push Jammit.filename(name, extension, :mhtml) if Jammit.mhtml_enabled
133+
end
130134
cached.map! {|file| File.join(output_dir, file) }
131135
if cached.any? {|file| !File.exists?(file) }
132136
true
@@ -153,10 +157,10 @@ def create_packages(config)
153157
paths = globs.flatten.uniq.map {|glob| glob_files(glob) }.flatten.uniq
154158
packages[name][:paths] = paths
155159
if !paths.grep(Jammit.template_extension_matcher).empty?
156-
packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(PATH_TO_URL, '') }
160+
packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(path_to_url, '') }
157161
packages[name][:urls] += [Jammit.asset_url(name, Jammit.template_extension)]
158162
else
159-
packages[name][:urls] = paths.map {|path| path.sub(PATH_TO_URL, '') }
163+
packages[name][:urls] = paths.map {|path| path.sub(path_to_url, '') }
160164
end
161165
end
162166
packages

0 commit comments

Comments
 (0)