Skip to content

Commit

Permalink
Merge pull request #46 from bmeurer/json-minify
Browse files Browse the repository at this point in the history
Add support for JSON minification using json/minify.
  • Loading branch information
digitalsparky authored Jan 7, 2019
2 parents a72a097 + 4a985d1 commit ddf4d97
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Requires Ruby 2.3+

Minifies HTML, XML, CSS, and Javascript both inline and as separate files utilising yui-compressor and htmlcompressor.
Minifies HTML, XML, CSS, JSON and JavaScript both inline and as separate files utilising yui-compressor and htmlcompressor.

This was created due to the previous minifier (jekyll-press) not being CSS3 compatible, which made me frown.

Expand Down Expand Up @@ -37,6 +37,7 @@ and toggle features and settings using:
remove_quotes: false # Default: false
compress_css: true # Default: true
compress_javascript: true # Default: true
compress_json: true # Default: true
simple_doctype: false # Default: false
remove_script_attributes: false # Default: false
remove_style_attributes: false # Default: false
Expand Down
1 change: 1 addition & 0 deletions jekyll-minifier.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Gem::Specification.new do |gem|
gem.add_dependency "uglifier", "~> 4.1"
gem.add_dependency "htmlcompressor", "~> 0.4"
gem.add_dependency "cssminify2", "~> 2.0"
gem.add_dependency "json-minify", "~> 0.0.3"

gem.add_development_dependency "rake", "~> 12.3"
gem.add_development_dependency "rspec", "~> 3.8"
Expand Down
23 changes: 23 additions & 0 deletions lib/jekyll-minifier.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'uglifier'
require 'htmlcompressor'
require 'cssminify2'
require 'json/minify'

module Jekyll
module Compressor
Expand All @@ -19,6 +20,8 @@ def output_compressed(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)
Expand Down Expand Up @@ -105,6 +108,24 @@ def output_js(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']
Expand Down Expand Up @@ -188,6 +209,8 @@ def write(dest)
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)
Expand Down

0 comments on commit ddf4d97

Please sign in to comment.