lib/jekyll-minifier.rb in jekyll-minifier-0.1.9 vs lib/jekyll-minifier.rb in jekyll-minifier-0.1.10
- old
+ new
@@ -1,8 +1,9 @@
require 'uglifier'
require 'htmlcompressor'
require 'cssminify2'
+require 'json/minify'
module Jekyll
module Compressor
def output_file(dest, content)
FileUtils.mkdir_p(File.dirname(dest))
@@ -17,10 +18,12 @@
if path.end_with?('.min.js')
output_file(path, context)
else
output_js(path, context)
end
+ when '.json'
+ output_json(path, context)
when '.css'
if path.end_with?('.min.css')
output_file(path, context)
else
output_css(path, context)
@@ -36,11 +39,11 @@
js_args = {}
opts = @site.config['jekyll-minifier']
if ( !opts.nil? )
# Javascript Arguments
- js_args += opts[:uglifier_args] if opts.has_key?(:uglifier_args)
+ js_args[:uglifier_args] = Hash[opts['uglifier_args'].map{|(k,v)| [k.to_sym,v]}] if opts.has_key?('uglifier_args')
# HTML Arguments
html_args[:remove_spaces_inside_tags] = opts['remove_spaces_inside_tags'] if opts.has_key?('remove_spaces_inside_tags')
html_args[:remove_multi_spaces] = opts['remove_multi_spaces'] if opts.has_key?('remove_multi_spaces')
html_args[:remove_comments] = opts['remove_comments'] if opts.has_key?('remove_comments')
@@ -64,12 +67,12 @@
html_args[:preserve_patterns] += opts[:preserve_patterns].map { |pattern| Regexp.new(pattern)} if opts.has_key?(:preserve_patterns)
end
html_args[:css_compressor] = CSSminify2.new()
- if ( !js_args.nil? )
- html_args[:javascript_compressor] = Uglifier.new(js_args)
+ if ( !js_args[:uglifier_args].nil? )
+ html_args[:javascript_compressor] = Uglifier.new(js_args[:uglifier_args])
else
html_args[:javascript_compressor] = Uglifier.new()
end
compressor = HtmlCompressor::Compressor.new(html_args)
@@ -79,20 +82,21 @@
end
end
def output_js(path, content)
if ( ENV['JEKYLL_ENV'] == "production" )
- opts = @site.config['jekyll-minifier']
+ js_args = {}
+ opts = @site.config['jekyll-minifier']
compress = true
if ( !opts.nil? )
- compress = opts['compress_javascript'] if opts.has_key?('compress_javascript')
- js_args += opts[:js_args] if opts.has_key?(:js_args)
+ compress = opts['compress_javascript'] if opts.has_key?('compress_javascript')
+ js_args[:uglifier_args] = Hash[opts['uglifier_args'].map{|(k,v)| [k.to_sym,v]}] if opts.has_key?('uglifier_args')
end
if ( compress )
- if ( !js_args.nil? )
- compressor = Uglifier.new(js_args)
+ if ( !js_args[:uglifier_args].nil? )
+ compressor = Uglifier.new(js_args[:uglifier_args])
else
compressor = Uglifier.new()
end
output_file(path, compressor.compile(content))
@@ -102,10 +106,28 @@
else
output_file(path, content)
end
end
+ def output_json(path, content)
+ if ( ENV['JEKYLL_ENV'] == "production" )
+ opts = @site.config['jekyll-minifier']
+ compress = true
+ if ( !opts.nil? )
+ compress = opts['compress_json'] if opts.has_key?('compress_json')
+ end
+
+ if ( compress )
+ output_file(path, JSON.minify(content))
+ else
+ output_file(path, content)
+ end
+ else
+ output_file(path, content)
+ end
+ end
+
def output_css(path, content)
if ( ENV['JEKYLL_ENV'] == "production" )
opts = @site.config['jekyll-minifier']
compress = true
if ( !opts.nil? )
@@ -185,9 +207,11 @@
if dest_path.end_with?('.min.js')
copy_file(path, dest_path)
else
output_js(dest_path, File.read(path))
end
+ when '.json'
+ output_json(dest_path, File.read(path))
when '.css'
if dest_path.end_with?('.min.css')
copy_file(path, dest_path)
else
output_css(dest_path, File.read(path))