Module AssetHat::CSS

  1. lib/asset_hat/css.rb

Methods for minifying and optimizing CSS.

Classes and Modules

Module AssetHat::CSS::Engines

Constants

ENGINES = [:weak, :cssmin]   A list of supported minification engine names.

Public class methods

add_asset_commit_ids (css)

Given a string containing CSS, appends each referenced asset’s last commit ID to its URL, e.g., background: url(/images/foo.png?ab12cd34e). This enables cache busting: If the user’s browser has cached a copy of foo.png from a previous deployment, this new URL forces the browser to ignore that cache and request the latest version.

[show source]
    # File lib/asset_hat/css.rb, line 43
43:     def self.add_asset_commit_ids(css)
44:       css.gsub(/url[\s]*\((\/(images|htc)\/[^)]+)\)/) do |match|
45:         src = $1
46: 
47:         # Get absolute path
48:         filepath = File.join(ASSETS_DIR, src)
49: 
50:         # Convert to relative path
51:         filepath.sub!(/^#{FileUtils.pwd}#{File::SEPARATOR}/, '')
52: 
53:         commit_id = AssetHat.last_commit_id(filepath)
54:         commit_id.present? ? "url(#{src}?#{commit_id})" : "url(#{src})"
55:       end
56:     end
add_asset_hosts (css, asset_host)

Arguments:

  • A string containing CSS;
  • A string containing the app’s asset host, e.g., ‘http://assets%d.example.com’. This value is typically taken from config.action_controller.asset_host in the app’s config/environments/production.rb.

An asset host is added to every asset URL in the CSS, e.g., background: url(http://assets2.example.com/images/foo.png); if %d in the asset host, it is replaced with an arbitrary number in 0-3, inclusive.

[show source]
    # File lib/asset_hat/css.rb, line 70
70:     def self.add_asset_hosts(css, asset_host)
71:       return if asset_host.blank?
72:       css.gsub(/url[\s]*\((\/(images|htc)\/[^)]+)\)/) do |match|
73:         src = $1
74:         "url(#{(asset_host =~ /%d/) ? asset_host % (src.hash % 4) : asset_host}#{src})"
75:       end
76:     end
min_filepath (filepath)

Returns the expected path for the minified version of a CSS asset:

AssetHat::CSS.min_filepath('public/stylesheets/bundles/application.css')
  # => 'public/stylesheets/bundles/application.min.css'
[show source]
    # File lib/asset_hat/css.rb, line 14
14:     def self.min_filepath(filepath)
15:       AssetHat.min_filepath(filepath, 'css')
16:     end
minify (input_string, options={})

Accepts a string of CSS, and returns that CSS minified. Options:

engine
Default is :cssmin; see Engines.cssmin. Allowed values are in ENGINES.
[show source]
    # File lib/asset_hat/css.rb, line 23
23:     def self.minify(input_string, options={})
24:       options.reverse_merge!(:engine => :cssmin)
25: 
26:       engine = options[:engine].to_sym
27:       unless ENGINES.include?(engine)
28:         raise %Q{
29:           Unknown CSS minification engine '#{engine}'.
30:           Allowed: #{ENGINES.map{ |e| "'#{e}'" }.join(', ')}
31:         }.strip.gsub(/\s+/, ' ') and return
32:       end
33: 
34:       AssetHat::CSS::Engines.send(engine, input_string)
35:     end