Module: AssetHat::CSS
- Defined in:
- lib/asset_hat/css.rb
Overview
Methods for minifying and optimizing CSS.
Defined Under Namespace
Modules: Engines
Constant Summary collapse
- ENGINES =
A list of supported minification <a href=CSS/Engines.html>engine</a> names.
[:weak, :cssmin]
Class Method Summary collapse
-
.add_asset_commit_ids(css) ⇒ Object
Given a string containing CSS, appends each referenced asset’s last commit ID to its URL, e.g.,
background: url(/images/foo.png?ab12cd3). -
.add_asset_hosts(css, asset_host, options = {}) ⇒ Object
Arguments:.
-
.min_filepath(filepath) ⇒ Object
Returns the expected path for the minified version of a CSS asset:.
-
.minify(input_string, options = {}) ⇒ Object
Accepts a string of CSS, and returns that CSS minified.
Class Method Details
.add_asset_commit_ids(css) ⇒ Object
Given a string containing CSS, appends each referenced asset’s last commit ID to its URL, e.g., background: url(/images/foo.png?ab12cd3). 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.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/asset_hat/css.rb', line 43 def self.add_asset_commit_ids(css) update_css_urls(css, %w[images htc]) do |src, quote| # Get absolute path filepath = File.join(ASSETS_DIR, src) # Convert to relative path filepath.sub!(/^#{FileUtils.pwd}#{File::SEPARATOR}/, '') commit_id = AssetHat.last_commit_id(filepath) if commit_id.present? "url(#{quote}#{src}#{src =~ /\?/ ? '&' : '?'}#{commit_id}#{quote})" else "url(#{quote}#{src}#{quote})" end end end |
.add_asset_hosts(css, asset_host, options = {}) ⇒ Object
Arguments:
-
A string containing CSS;
-
A string containing the app’s asset host, e.g., ‘http://cdn%d.example.com’. This value is typically taken from
config.action_controller.asset_hostin the app’sconfig/environments/production.rb.
An asset host is added to every image URL in the CSS, e.g., background: url(http\://cdn2.example.com/images/foo.png); if %d in the asset host, it is replaced with an arbitrary number in 0-3, inclusive.
Options:
- ssl
-
Set to
trueto simulate a request via SSL. Defaults tofalse.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/asset_hat/css.rb', line 77 def self.add_asset_hosts(css, asset_host, ={}) return css if asset_host.blank? .reverse_merge!(:ssl => false) update_css_urls(css, %w[images]) do |src, quote| computed_asset_host = AssetHat.compute_asset_host( asset_host, src, .slice(:ssl)) "url(#{quote}#{computed_asset_host}#{src}#{quote})" end end |
.min_filepath(filepath) ⇒ Object
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'
14 15 16 |
# File 'lib/asset_hat/css.rb', line 14 def self.min_filepath(filepath) AssetHat.min_filepath(filepath, 'css') end |
.minify(input_string, options = {}) ⇒ Object
Accepts a string of CSS, and returns that CSS minified. Options:
- engine
-
Default is
:cssmin; see <a href=CSS/Engines.html#method-c-cssmin>Engines.cssmin</a>. Allowed values are in ENGINES.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/asset_hat/css.rb', line 23 def self.minify(input_string, ={}) .reverse_merge!(:engine => :cssmin) engine = [:engine].to_sym unless ENGINES.include?(engine) raise %{ Unknown CSS minification engine '#{engine}'. Allowed: #{ENGINES.map{ |e| "'#{e}'" }.join(', ')} }.strip.gsub(/\s+/, ' ') and return end AssetHat::CSS::Engines.send(engine, input_string).strip end |