Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Jammit to be extended to package and compress other types of assests #229

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/jammit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ def self.load_configuration(config_path, soft=false)
set_template_namespace(conf[:template_namespace])
set_template_extension(conf[:template_extension])
set_public_root(conf[:public_root]) if conf[:public_root]
symbolize_keys(conf[:stylesheets]) if conf[:stylesheets]
symbolize_keys(conf[:javascripts]) if conf[:javascripts]
conf.keys.each {|key| symbolize_keys(conf[key]) rescue nil}
check_for_deprecations
self
end
Expand Down Expand Up @@ -146,6 +145,13 @@ def self.package!(options={})
packager.precache_all(options[:output_folder], options[:base_url])
end

def self.custom_assets
@configuration.select { |key, val|
lambda{ |val| val.respond_to?(:keys) rescue false}.call(val) &&
![:javascripts, :stylesheets].include?(key)
}
end

private

# Allows command-line definition of `PUBLIC_ROOT`, for those using Jammit
Expand Down
3 changes: 3 additions & 0 deletions lib/jammit/dependencies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,6 @@
require 'jammit/routes'
end

# Load user extentions
require 'jammit/extensions'

3 changes: 3 additions & 0 deletions lib/jammit/extensions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module Jammit
require File.join(ASSET_ROOT, 'lib', 'extensions', 'jammit.rb') if File.exists?(File.join(ASSET_ROOT, 'lib', 'extensions', 'jammit.rb'))
end
13 changes: 11 additions & 2 deletions lib/jammit/packager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ def initialize
@config = {
:css => (Jammit.configuration[:stylesheets] || {}),
:js => (Jammit.configuration[:javascripts] || {})
}
}.merge!(Jammit.custom_assets)
@packages = {
:css => create_packages(@config[:css]),
:js => create_packages(@config[:js])
}
}.merge!(create_custom_packages(Jammit.custom_assets))
end

# Ask the packager to precache all defined assets, along with their gzip'd
Expand All @@ -46,6 +46,9 @@ def precache_all(output_dir=nil, base_url=nil)
end
end
end
@packages.keys.keep_if{|extension| ![:js, :css].include?(extension)}.each do |extension|
cacheable(extension, output_dir).each {|p| cache(p, extension.to_s, self.send(:"pack_#{extension}s", p), output_dir)}
end
end

# Caches a single prebuilt asset package and gzips it at the highest
Expand Down Expand Up @@ -169,6 +172,12 @@ def create_packages(config)
packages
end

def create_custom_packages(custom_assets)
results = {}
custom_assets.each { |key, val| results[:"#{key.to_s.singularize}"] = create_packages(val)}
results
end

# Raise a PackageNotFound exception for missing packages...
def not_found(package, extension)
raise PackageNotFound, "assets.yml does not contain a \"#{package}\" #{extension.to_s.upcase} package"
Expand Down