Class Jammit::Compressor
In: lib/jammit/compressor.rb
Parent: Object

Uses the YUI Compressor or Closure Compiler to compress JavaScript. Always uses YUI to compress CSS (Which means that Java must be installed.) Also knows how to create a concatenated JST file. If "embed_assets" is turned on, creates "mhtml" and "datauri" versions of all stylesheets, with all enabled assets inlined into the css.

Methods

Constants

EMBED_MIME_TYPES = { '.png' => 'image/png', '.jpg' => 'image/jpeg', '.jpeg' => 'image/jpeg', '.gif' => 'image/gif', '.tif' => 'image/tiff', '.tiff' => 'image/tiff', '.ttf' => 'font/truetype', '.otf' => 'font/opentype', '.woff' => 'font/woff'   Mapping from extension to mime-type of all embeddable assets.
EMBED_EXTS = EMBED_MIME_TYPES.keys   Font extensions for which we allow embedding:
EMBED_FONTS = ['.ttf', '.otf', '.woff']
MAX_IMAGE_SIZE = 32700   (32k - padding) maximum length for data-uri assets (an IE8 limitation).
EMBED_DETECTOR = /url\(['"]?([^\s)]+\.[a-z]+)(\?\d+)?['"]?\)/   CSS asset-embedding regexes for URL rewriting.
EMBEDDABLE = /[\A\/]embed\//
EMBED_REPLACER = /url\(__EMBED__(.+?)(\?\d+)?\)/
MHTML_START = "/*\r\nContent-Type: multipart/related; boundary=\"MHTML_MARK\"\r\n\r\n"   MHTML file constants.
MHTML_SEPARATOR = "--MHTML_MARK\r\n"
MHTML_END = "\r\n--MHTML_MARK--\r\n*/\r\n"
JST_START = "(function(){"   JST file constants.
JST_END = "})();"
COMPRESSORS = { :yui => YUI::JavaScriptCompressor, :closure => Jammit.compressors.include?(:closure) ? Closure::Compiler : nil, :uglifier => Jammit.compressors.include?(:uglifier) ? Jammit::Uglifier : nil
DEFAULT_OPTIONS = { :yui => {:munge => true}, :closure => {}, :uglifier => {:copyright => false}

Public Class methods

The css compressor is always the YUI Compressor. JS compression can be provided with YUI Compressor, Google Closure Compiler or UglifyJS.

Public Instance methods

Compiles a single JST file by writing out a javascript that adds template properties to a top-level template namespace object. Adds a JST-compilation function to the top of the package, unless you‘ve specified your own preferred function, or turned it off. JST templates are named with the basename of their file.

Concatenate and compress a list of CSS stylesheets. When compressing a :datauri or :mhtml variant, post-processes the result to embed referenced assets.

Concatenate together a list of JavaScript paths, and pass them through the YUI Compressor (with munging enabled). JST can optionally be included.

Private Instance methods

Get the site-absolute public path for an asset file path that may or may not be relative, given the path of the stylesheet that contains it.

Concatenate together a list of asset files.

In order to support embedded assets from relative paths, we need to expand the paths before contatenating the CSS together and losing the location of the original stylesheet path. Validate the assets while we‘re at it.

Return a rewritten asset URL for a new stylesheet — the asset should be tagged for embedding if embeddable, and referenced at the correct level if relative.

An asset is valid for embedding if it exists, is less than 32K, and is stored somewhere inside of a folder named "embed". IE does not support Data-URIs larger than 32K, and you probably shouldn‘t be embedding assets that large in any case. Because we need to check the base64 length here, save it so that we don‘t have to compute it again later.

Return the Base64-encoded contents of an asset on a single line.

Given a set of paths, find a common prefix path.

Grab the mime-type of an asset, by filename.

Similar to the AssetTagHelper‘s method of the same name, this will determine the correct asset id for a file.

`File.read`, but in "binary" mode.

CSS assets that are referenced by relative paths, and are not being embedded, must be rewritten relative to the newly-merged stylesheet path.

Similar to the AssetTagHelper‘s method of the same name, this will append the RAILS_ASSET_ID cache-buster to URLs, if it‘s defined.

Determine the name of a JS template. If there‘s a common base path, use the namespaced prefix. Otherwise, simply use the filename.

Re-write all enabled asset URLs in a stylesheet with their corresponding Data-URI Base-64 encoded asset contents.

Re-write all enabled asset URLs in a stylesheet with the MHTML equivalent. The newlines ("\r\n") in the following method are critical. Without them your MHTML will look identical, but won‘t work.

[Validate]