Skip to content

Commit

Permalink
merging in JST enhancements.
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Jul 13, 2010
1 parent a1391ae commit ca11de0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/jammit.rb
Expand Up @@ -50,7 +50,7 @@ class << self
:embed_assets, :package_assets, :compress_assets, :gzip_assets,
:package_path, :mhtml_enabled, :include_jst_script, :config_path,
:javascript_compressor, :compressor_options, :css_compressor_options,
:template_extension, :template_extension_regexp
:template_extension, :template_extension_matcher
end

# The minimal required configuration.
Expand Down Expand Up @@ -139,7 +139,7 @@ def self.set_template_namespace(value)
# Set the extension for JS templates.
def self.set_template_extension(value)
@template_extension = (value == true || value.nil? ? DEFAULT_JST_EXTENSION : value.to_s).gsub(/\A\.?(.*)\Z/, '\1')
@template_extension_regexp = /\.#{Regexp.escape(@template_extension)}\Z/
@template_extension_matcher = /\.#{Regexp.escape(@template_extension)}\Z/
end

# The YUI Compressor requires Java > 1.4, and Closure requires Java > 1.6.
Expand Down
40 changes: 20 additions & 20 deletions lib/jammit/compressor.rb
Expand Up @@ -63,7 +63,7 @@ def initialize
# Concatenate together a list of JavaScript paths, and pass them through the
# YUI Compressor (with munging enabled). JST can optionally be included.
def compress_js(paths)
if (jst_paths = paths.grep(Jammit.template_extension_regexp)).empty?
if (jst_paths = paths.grep(Jammit.template_extension_matcher)).empty?
js = concatenate(paths)
else
js = concatenate(paths - jst_paths) + compile_jst(jst_paths)
Expand Down Expand Up @@ -92,20 +92,13 @@ def compress_css(paths, variant=nil, asset_url=nil)
# specified your own preferred function, or turned it off.
# JST templates are named with the basename of their file.
def compile_jst(paths)
namespace = Jammit.template_namespace
paths = paths.grep(Jammit.template_extension_regexp)
base_path = find_base_path(paths)
compiled = paths.map do |path|
contents = File.read(path).gsub(/\n/, '').gsub("'", '\\\\\'')
if base_path
# If there is a common base path for all the templates (as there
# usually will be), remove the common prefix.
template_name = path.gsub(/\A#{base_path}\/(.*)\.#{Jammit.template_extension}\Z/, '\1')
else
# Otherwise, just use the filename.
template_name = File.basename(path, ".#{Jammit.template_extension}")
end
"#{namespace}['#{template_name}'] = #{Jammit.template_function}('#{contents}');"
namespace = Jammit.template_namespace
paths = paths.grep(Jammit.template_extension_matcher).sort
base_path = find_base_path(paths)
compiled = paths.map do |path|
contents = File.read(path).gsub(/\n/, '').gsub("'", '\\\\\'')
name = template_name(path, base_path)
"#{namespace}['#{name}'] = #{Jammit.template_function}('#{contents}');"
end
compiler = Jammit.include_jst_script ? File.read(DEFAULT_JST_SCRIPT) : '';
setup_namespace = "#{namespace} = #{namespace} || {};"
Expand All @@ -118,17 +111,24 @@ def compile_jst(paths)
# Given a set of paths, find a common prefix path.
def find_base_path(paths)
return nil if paths.length <= 1
arr = paths.sort
f = arr.first.split('/')
l = arr.last.split('/')
paths.sort!
first = paths.first.split('/')
last = paths.last.split('/')
i = 0
while f[i] == l[i] && i <= f.length
while first[i] == last[i] && i <= first.length
i += 1
end
res = f.slice(0, i).join('/')
res = first.slice(0, i).join('/')
res.empty? ? nil : res
end

# Determine the name of a JS template. If there's a common base path, use
# the namespaced prefix. Otherwise, simply use the filename.
def template_name(path, base_path)
return File.basename(path, ".#{Jammit.template_extension}") unless base_path
path.gsub(/\A#{base_path}\/(.*)\.#{Jammit.template_extension}\Z/, '\1')
end

# In order to support embedded assets from relative paths, we need to
# expand the paths before contatenating the CSS together and losing the
# location of the original stylesheet path. Validate the assets while we're
Expand Down
17 changes: 8 additions & 9 deletions lib/jammit/packager.rb
Expand Up @@ -127,8 +127,13 @@ def cacheable(extension, output_dir)
end
end

# Compiles the list of assets that goes into each package. Runs an ordered
# list of Dir.globs, taking the merged unique result.
# Compiles the list of assets that goes into each package. Runs an
# ordered list of Dir.globs, taking the merged unique result.
# If there are JST files in this package we need to add an extra
# path for when package_assets is off (e.g. in a dev environment).
# This package (e.g. /assets/package-name.jst) will never exist as
# an actual file but will be dynamically generated by Jammit on
# every request.
def create_packages(config)
packages = {}
return packages if !config
Expand All @@ -137,13 +142,7 @@ def create_packages(config)
packages[name] = {}
paths = globs.flatten.uniq.map {|glob| glob_files(glob) }.flatten.uniq
packages[name][:paths] = paths
# Check for JST.
if !paths.grep(Jammit.template_extension_regexp).empty?
# If there are JST files in this package we need to add an extra
# path for when package_assets is off (e.g. in a dev environment).
# This package (e.g. /assets/package-name.jst) will never exist as
# an actual file but will be dynamically generated by Jammit on
# every request.
if !paths.grep(Jammit.template_extension_matcher).empty?
packages[name][:urls] = paths.grep(JS_EXTENSION).map {|path| path.sub(PATH_TO_URL, '') }
packages[name][:urls] += [Jammit.asset_url(name, Jammit.template_extension)]
else
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/jammed/jst_test_diff_ext_and_nested.js
@@ -1,7 +1,7 @@
(function(){
window.JST = window.JST || {};
var template = function(str){var fn = new Function('obj', 'var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(\''+str.replace(/[\r\t\n]/g, " ").replace(/'(?=[^%]*%>)/g,"\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g,"',$1,'").split("<%").join("');").split("%>").join("p.push('")+"');}return p.join('');"); return fn;};
window.JST['template3'] = template('<h1>Hello, {{name}}</h1>');
window.JST['nested/double_nested/double_nested'] = template('<h1>Hello again, {{name}}!</h1>');
window.JST['nested/nested3'] = template('<h1>Goodbye, {{name}}</h1>');
window.JST['template3'] = template('<h1>Hello, {{name}}</h1>');
})();
4 changes: 2 additions & 2 deletions test/fixtures/jammed/jst_test_nested.js
@@ -1,9 +1,9 @@
(function(){
window.JST = window.JST || {};
var template = function(str){var fn = new Function('obj', 'var p=[],print=function(){p.push.apply(p,arguments);};with(obj){p.push(\''+str.replace(/[\r\t\n]/g, " ").replace(/'(?=[^%]*%>)/g,"\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g,"',$1,'").split("<%").join("');").split("%>").join("p.push('")+"');}return p.join('');"); return fn;};
window.JST['template1'] = template('<a href="<%= to_somewhere %>"><%= saying_something %></a>');
window.JST['template2'] = template('<% _([1,2,3]).each(function(num) { %> <li class="number"> <%= num %> </li><% }) %>');
window.JST['nested/double_nested/double_nested'] = template('<p><%= hello_world %></p>');
window.JST['nested/nested1'] = template('<a href="<%= to_somewhere %>"><%= saying_something %></a>');
window.JST['nested/nested2'] = template('<% _([1,2,3]).each(function(num) { %> <li class="number"> <%= num %> </li><% }) %>');
window.JST['template1'] = template('<a href="<%= to_somewhere %>"><%= saying_something %></a>');
window.JST['template2'] = template('<% _([1,2,3]).each(function(num) { %> <li class="number"> <%= num %> </li><% }) %>');
})();

0 comments on commit ca11de0

Please sign in to comment.