Skip to content

Instantly share code, notes, and snippets.

@FrozenCanuck
Created December 15, 2010 19:43
Show Gist options
  • Save FrozenCanuck/742494 to your computer and use it in GitHub Desktop.
Save FrozenCanuck/742494 to your computer and use it in GitHub Desktop.
proposed patch for Abbot's Target bundle_info method
Example Buildfile:
config :foo do |c|
c[:required] = [:bar]
c[:minify_javascript] = true
c[:use_packed_javascript] = false # Flag that we don't want javascript-packed.js
end
This should produce a bundle info in the bundle_info.js file, like so:
;(function() {
var target_name = 'foo' ;
if (!SC.BUNDLE_INFO) throw "SC.BUNDLE_INFO is not defined!" ;
if (SC.BUNDLE_INFO[target_name]) return ;
SC.BUNDLE_INFO[target_name] = {
requires: ['bar'],
styles: ['/static/foo/en/GA/stylesheet-packed.css'],
scripts: ['/static/foo/en/GA/javascript.js']
}
})();
Notice that the javascript-packed.js file is not being added, which is what we want. So to make this actually happen, Abbot's Target class must be the following:
class Target < HashStruct
def bundle_info(opts ={})
if self[:target_type] == :app
raise "bundle_info called on an app target"
else
requires = required_targets(opts) # only go one-level deep!
# Targets that aren't pre-loaded can't be packed together. That leaves
# loading css and js individually and/or loading the combined or
# minified css and js.
combine_css = config[:combine_stylesheets]
combine_js = config[:combine_javascript]
minify_css = config[:minify_css]
minify_css = config[:minify] if minify_css.nil?
minify_js = config[:minify_javascript]
minify_js = config[:minify] if minify_js.nil?
pack_css = config[:use_packed_stylesheet]
pack_css = config[:use_packed] if pack_css.nil?
pack_js = config[:use_packed_javascript]
pack_js = config[:use_packed] if pack_js.nil?
# sort entries...
css_entries = {}
javascript_entries = {}
manifest_for(opts[:variation]).build!.entries.each do |entry|
if entry[:resource].nil?
entry[:resource] = ''
end
# look for CSS or JS type entries
case entry[:entry_type]
when :css
(css_entries[entry[:resource]] ||= []) << entry
when :javascript
(javascript_entries[entry[:resource]] ||= []) << entry
end
end
css_urls = []
css_entries.each do |resource_name, entries|
SC::Helpers::EntrySorter.sort(entries).each do |entry|
css_urls << entry.cacheable_url if _include_entry?(entry, minify_css, pack_css, combine_css)
end
end
js_urls = []
javascript_entries.each do |resource_name, entries|
resource_name = resource_name.ext('js')
pf = (resource_name == 'javascript.js') ? %w(source/lproj/strings.js source/core.js source/utils.js) : []
SC::Helpers::EntrySorter.sort(entries, pf).each do |entry|
js_urls << entry.cacheable_url if _include_entry?(entry, minify_js, pack_js, combine_js)
end
end
SC::HashStruct.new :requires => requires, :css_urls => css_urls, :js_urls => js_urls
end
end
private
# Check if the given entry should be considered included
def _include_entry?(entry, minified, packed, combined)
# The order matters here
return false if !minified && entry[:minified]
return false if !packed && entry[:packed]
return true if minified && entry[:minified]
return true if packed && entry[:packed]
return true if combined && entry[:combined] && !entry[:packed] && !entry[:minified]
return true if !entry[:combined] && !entry[:packed] && !entry[:minified]
return false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment