lib/jammit/packager.rb in jammit-0.4.4 vs lib/jammit/packager.rb in jammit-0.5.0

- old
+ new

@@ -19,18 +19,16 @@ # create a new Packager. def initialize @compressor = Compressor.new @force = false @config = { - :css => (Jammit.configuration[:stylesheets] || {}).symbolize_keys, - :js => (Jammit.configuration[:javascripts] || {}).symbolize_keys, - :jst => (Jammit.configuration[:templates] || {}).symbolize_keys + :css => (Jammit.configuration[:stylesheets] || {}), + :js => (Jammit.configuration[:javascripts] || {}) } @packages = { :css => create_packages(@config[:css]), - :js => create_packages(@config[:js]), - :jst => create_packages(@config[:jst]) + :js => create_packages(@config[:js]) } end # Ask the packager to precache all defined assets, along with their gzip'd # versions. In order to prebuild the MHTML stylesheets, we need to know the @@ -38,11 +36,10 @@ # Unless forced, will only rebuild assets whose source files have been # changed since their last package build. def precache_all(output_dir=nil, base_url=nil) output_dir ||= File.join(PUBLIC_ROOT, Jammit.package_path) cacheable(:js, output_dir).each {|p| cache(p, 'js', pack_javascripts(p), output_dir) } - cacheable(:jst, output_dir).each {|p| cache(p, 'jst', pack_templates(p), output_dir) } cacheable(:css, output_dir).each do |p| 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 @@ -85,14 +82,13 @@ @compressor.compress_js(package_for(package, :js)[:paths]) end # Return the compiled contents of a JST package. def pack_templates(package) - @compressor.compile_jst(package_for(package, :jst)[:paths]) + @compressor.compile_jst(package_for(package, :js)[:paths]) end - private # Look up a package asset list by name, raising an exception if the # package has gone missing. def package_for(package, extension) @@ -108,34 +104,52 @@ Jammit.warn("No assets match '#{glob}'") if paths.empty? paths 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 whose source - # files have changed since the last package build. + # true, this is all of them -- otherwise only the packages that are missing + # or whose source files have changed since the last package build. def cacheable(extension, output_dir) names = @packages[extension].keys return names if @force + config_mtime = File.mtime(Jammit.config_path) return names.select do |name| - pack = package_for(name, extension) - cached = File.join(output_dir, Jammit.filename(name, extension)) - since = File.exists?(cached) && File.mtime(cached) - !since || pack[:paths].any? {|src| File.mtime(src) > since } + pack = package_for(name, extension) + cached = [Jammit.filename(name, extension)] + cached.push Jammit.filename(name, extension, :datauri) if Jammit.embed_assets + cached.push Jammit.filename(name, extension, :mhtml) if Jammit.mhtml_enabled + cached.map! {|file| File.join(output_dir, file) } + if cached.any? {|file| !File.exists?(file) } + true + else + since = cached.map {|file| File.mtime(file) }.min + config_mtime > since || pack[:paths].any? {|src| File.mtime(src) > since } + end 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 config.each do |name, globs| globs ||= [] packages[name] = {} - paths = globs.map {|glob| glob_files(glob) }.flatten.uniq + paths = globs.flatten.uniq.map {|glob| glob_files(glob) }.flatten.uniq packages[name][:paths] = paths - packages[name][:urls] = paths.map {|path| path.sub(PATH_TO_URL, '') } + 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 + packages[name][:urls] = paths.map {|path| path.sub(PATH_TO_URL, '') } + end end packages end # Raise a PackageNotFound exception for missing packages... @@ -143,6 +157,6 @@ raise PackageNotFound, "assets.yml does not contain a \"#{package}\" #{extension.to_s.upcase} package" end end -end \ No newline at end of file +end