Class: Jammit::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/jammit-core/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'
}
EMBED_EXTS =

Font extensions for which we allow embedding:

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

Maximum size for embeddable images (an IE8 limitation).

32 * 1024
EMBED_DETECTOR =

CSS asset-embedding regexes for URL rewriting.

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

MHTML file constants.

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

JST file constants.

"(function(){"
JST_END =
"})();"
COMPRESSORS =
{
  :yui     => YUI::JavaScriptCompressor,
  :closure => Closure::Compiler
}
DEFAULT_OPTIONS =
{
  :yui     => {:munge => true},
  :closure => {}
}

Instance Method Summary collapse

Constructor Details

#initializeCompressor

Creating a compressor initializes the internal YUI Compressor from the “yui-compressor” gem, or the internal Closure Compiler from the “closure-compiler” gem.


56
57
58
59
60
61
# File 'lib/jammit-core/compressor.rb', line 56

def initialize
  @css_compressor = YUI::CssCompressor.new(Jammit.config[:css_compressor_options] || {})
  flavor          = Jammit.config[:javascript_compressor] || Jammit::DEFAULT_COMPRESSOR
  @options        = DEFAULT_OPTIONS[flavor].merge(Jammit.config[: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.


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/jammit-core/compressor.rb', line 89

def compile_jst(paths)
  namespace = Jammit.template_namespace
  compiled = paths.map do |path|
    template_name = File.basename(path, File.extname(path))
    contents      = File.read(path).gsub(/\n/, '').gsub("'", '\\\\\'')
    "#{namespace}.#{template_name} = #{Jammit.template_function}('#{contents}');"
  end
  compiler = Jammit.config[:include_jst_script] ? File.read(Jammit::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.


73
74
75
76
77
78
79
80
81
82
# File 'lib/jammit-core/compressor.rb', line 73

def compress_css(paths, variant=nil, asset_url=nil)
  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).


65
66
67
68
# File 'lib/jammit-core/compressor.rb', line 65

def compress_js(paths)
  js = concatenate(paths)
  Jammit.config[:compress_assets] ? @js_compressor.compress(js) : js
end