lib/awestruct/extensions/minify.rb in awestruct-0.4.8 vs lib/awestruct/extensions/minify.rb in awestruct-0.5.0.cr
- old
+ new
@@ -1,18 +1,18 @@
require 'shellwords'
require 'fileutils'
require 'htmlcompressor'
-require 'yui/compressor'
+require 'uglifier'
##
# Awestruct:Extensions:Minify is a transformer that minimizes JavaScript, CSS and HTML files.
# The transform runs on the rendered stream before it's written to the output path.
#
-# Minification is performed by the following three libraries:
+# Minification is performed by the following libraries:
#
# htmlcompressor (minifies HTML): http://code.google.com/p/htmlcompressor/
-# yuicompressor (minifies JavaScript and CSS): http://developer.yahoo.com/yui/compressor/
+# uglifier (minifies javascript): http://github.com/lautis/uglifier
# pngcrush (minifies PNG): http://pmt.sourceforge.net/pngcrush/
#
# These commands must be available on your PATH in order to use them.
#
# This class is loaded as a transformer into the Awestruct pipeline. The
@@ -20,16 +20,15 @@
#
# transformer Awestruct::Extensions::Minify.new
#
# This transform recognizes the following symbols:
#
-# :css - CSS files with extension .css
# :js - JavaScript files with extension .js
# :html - HTML files with extension .html
# :png - PNG files with extension .png
#
-# If no types are specified, the default value [:css, :js] is used.
+# If no types are specified, the default value [:js] is used.
#
# In addition to registering the transformer in the pipeline, it must be enabled
# by setting the following site property in _ext/config.yml:
#
# minify: true
@@ -44,19 +43,18 @@
# how you specify options arguments for the htmlcompressor command:
#
# minify_html_opts:
# remove_intertag_spaces: true
# compress_js: true
-# compress_css: true
#
# Note that any hypen (-) must be represented as an underscore (_) in the configuration.
module Awestruct
module Extensions
class Minify
- def initialize(types = [ :css, :js ])
+ def initialize(types = [ :js ])
@types = types
end
def transform(site, page, input)
if site.minify
@@ -67,15 +65,16 @@
case ext_sym
when :html
print "minifying html #{page.output_path}"
input = htmlcompressor(page, input, site.minify_html_opts)
when :css
- print "minifying css #{page.output_path}"
- input = yuicompressor_css(page, input)
+ # TODO: Figure out how to do this is sass / less and document it
+ #print "minifying css #{page.output_path}"
+ #input = yuicompressor_css(page, input)
when :js
print "minifying js #{page.output_path}"
- input = yuicompressor_js(page, input)
+ input = js_compressor(page, input)
when :png
print "minifying png #{page.output_path}"
input = pngcrush(page, input)
end
end
@@ -84,34 +83,36 @@
input
end
private
+ class JSCompressor
+ def compress( input )
+ Uglifier.new(:mangle => false).compile(input)
+ end
+ end
+
def htmlcompressor(page, input, minify_html_opts)
opts = minify_html_opts.nil? ? {}:minify_html_opts
compressor(page, input, HtmlCompressor::Compressor.new(opts))
end
- def yuicompressor_css(page, input)
- compressor(page, input, YUI::CssCompressor.new)
+ def js_compressor(page, input)
+ compressor(page, input, JSCompressor.new)
end
- def yuicompressor_js(page, input)
- compressor(page, input, YUI::JavaScriptCompressor.new)
- end
-
def compressor(page, input, compressor)
output = compressor.compress input
input_len = input.length
output_len = output.length
if input_len > output_len
- puts " %d bytes -> %d bytes = %.1f%%" % [ input_len, output_len, 100 * output_len/input_len ]
+ $LOG.debug " %d bytes -> %d bytes = %.1f%%" % [ input_len, output_len, 100 * output_len/input_len ] if $LOG.debug?
output
else
- puts " no gain"
+ $LOG.debug " no gain" if $LOG.debug?
input
end
end
def pngcrush(page, input)
@@ -125,13 +126,13 @@
input_len = File.stat(filename).size
output_len = output.length
if input_len > output_len
- puts " %d bytes -> %d bytes = %.1f%%" % [ input_len, output_len, 100 * output_len/input_len ]
+ $LOG.debug " %d bytes -> %d bytes = %.1f%%" % [ input_len, output_len, 100 * output_len/input_len ] if $LOG.debug?
output
else
- puts " no gain"
+ $LOG.debug " no gain" if $LOG.debug?
input
end
end
end