Class: Jammit::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/jammit/compressor.rb

Overview

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.

Constant Summary collapse

EMBED_MIME_TYPES =

Mapping from extension to mime-type of all embeddable assets.

{
  '.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'
}
EMBED_EXTS =

Font extensions for which we allow embedding:

EMBED_MIME_TYPES.keys
EMBED_FONTS =
['.ttf', '.otf', '.woff']
MAX_IMAGE_SIZE =

(32k - padding) maximum length for data-uri assets (an IE8 limitation).

32700
EMBED_DETECTOR =

CSS asset-embedding regexes for URL rewriting.

/url\(['"]?([^\s)]+\.[a-z]+)(\?\d+)?['"]?\)/
EMBEDDABLE =
/[\A\/]embed\//
EMBED_REPLACER =
/url\(__EMBED__(.+?)(\?\d+)?\)/
MHTML_START =

MHTML file constants.

"/*\r\nContent-Type: multipart/related; boundary=\"MHTML_MARK\"\r\n\r\n"
MHTML_SEPARATOR =
"--MHTML_MARK\r\n"
MHTML_END =
"\r\n--MHTML_MARK--\r\n*/\r\n"
JST_START =

JST file constants.

"(function(){"
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}
}

Instance Method Summary collapse

Constructor Details

#initializeCompressor

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



58
59
60
61
62
63
64
# File 'lib/jammit/compressor.rb', line 58

def initialize
  Jammit.check_java_version
  @css_compressor = YUI::CssCompressor.new(Jammit.css_compressor_options || {})
  flavor          = Jammit.javascript_compressor || Jammit::DEFAULT_COMPRESSOR
  @options        = DEFAULT_OPTIONS[flavor].merge(Jammit.compressor_options || {})
  @js_compressor  = COMPRESSORS[flavor].new(@options)
end

Instance Method Details

#compile_jst(paths) ⇒ Object

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.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jammit/compressor.rb', line 97

def compile_jst(paths)
  namespace   = Jammit.template_namespace
  paths       = paths.grep(Jammit.template_extension_matcher).sort
  base_path   = find_base_path(paths)
  compiled    = paths.map do |path|
    contents  = read_binary_file(path)
    contents  = contents.gsub(/\r?\n/, "\\n").gsub("'", '\\\\\'')
    name      = template_name(path, base_path)
    "#{namespace}['#{name}'] = #{Jammit.template_function}('#{contents}');"
  end
  compiler = Jammit.include_jst_script ? read_binary_file(DEFAULT_JST_SCRIPT) : '';
  setup_namespace = "#{namespace} = #{namespace} || {};"
  [JST_START, setup_namespace, compiler, compiled, JST_END].flatten.join("\n")
end

#compress_css(paths, variant = nil, asset_url = nil) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/jammit/compressor.rb', line 80

def compress_css(paths, variant=nil, asset_url=nil)
  @asset_contents = {}
  css = concatenate_and_tag_assets(paths, variant)
  css = @css_compressor.compress(css) if Jammit.compress_assets
  case variant
  when nil      then return css
  when :datauri then return with_data_uris(css)
  when :mhtml   then return with_mhtml(css, asset_url)
  else raise PackageNotFound, "\"#{variant}\" is not a valid stylesheet variant"
  end
end

#compress_js(paths) ⇒ Object

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



68
69
70
71
72
73
74
75
# File 'lib/jammit/compressor.rb', line 68

def compress_js(paths)
  if (jst_paths = paths.grep(Jammit.template_extension_matcher)).empty?
    js = concatenate(paths)
  else
    js = concatenate(paths - jst_paths) + compile_jst(jst_paths)
  end
  Jammit.compress_assets ? @js_compressor.compress(js) : js
end