jekyll-zopfli 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2bf1c26f0d54b5d5ae1168ddef0f4b2dc09377b15dbf44bdec2c4348931ded5a
4
- data.tar.gz: 805cb2c1623856635697dadd413de1d0b9c77a39375aefac88b9176f84d3ea7e
3
+ metadata.gz: 77a932d1c765327ad80c8acb0b7ffe107bd5b90b5937f48693ed8579a8fd816f
4
+ data.tar.gz: d1b62939ee22db47dbaec6c23ab6561b4cb138223c87f8bb8d11d288ffcac116
5
5
  SHA512:
6
- metadata.gz: d1a01caa7da984a2855e300ece77fb473130657f874d327d4d35497a69cf3b1f1c6360a5df8b535aed4a51003aed022dfb30763f1b960d9945b796a76aa47ad3
7
- data.tar.gz: 1b48d8e7f514bcb52dc0e6ca883196a6228a1ce5f64dd2a76d5a42a678287ab4d6a86dd3f195ee9accd652c4a159e1a40d9f5446d254c95400a57d973d19d4f9
6
+ metadata.gz: 0d6b369cb4341a67c837aa99b8cbe0a3ee28881ffe2afde2a6d70651293bb9b366a37d2d0e3a376a0eb4af0bb9470ff7ebde1b4d1ff46f3cdf913d2d7b961473
7
+ data.tar.gz: 419182038aa67dcd245e035687ea3a016280f3aca5b3c0c590ba957229759df795bb75a606ef9cd72afb63f355839b89aed6a40d2962d034f4ec21b210577937
data/.gitignore CHANGED
@@ -7,7 +7,6 @@
7
7
  /spec/reports/
8
8
  /tmp/
9
9
  Gemfile.lock
10
- spec
11
10
 
12
11
  # rspec failure tracking
13
12
  .rspec_status
data/CHANGELOG.md CHANGED
@@ -1,12 +1,29 @@
1
1
  # Changelog
2
2
 
3
- ## 1.1.0 / 2018-01-03
3
+ ## Ongoing [☰](https://github.com/philnash/jekyll-gzip/compare/v1.1.0...master)
4
+
5
+ ...
6
+
7
+ ## 2.0.0 (2018-11-24) [☰](https://github.com/philnash/jekyll-zopfli/compare/v1.1.0...v2.0.0)
8
+
9
+ ### Added
10
+
11
+ * Hooks into Jekyll::Assets if available
12
+ * Adds frozen string literal comment
13
+
14
+ ### Changed
15
+
16
+ * Uses built in `Jekyll.env` instead of `ENV["JEKYLL_ENV"]`
17
+ * Changes `Jekyll::Zopfli::Compressor` to a module and implements a `compress_directory` method
18
+ * Moves Jekyll::Zopfli::ZIPPABLE_EXTENSIONS into plugin config that can overwritten in the site config
19
+
20
+ ## 1.1.0 (2018-01-03) [☰](https://github.com/philnash/jekyll-zopfli/compare/v1.0.0...v1.1.0)
4
21
 
5
22
  ### Changed
6
23
 
7
24
  * Only run the post write hook when the environment variable `JEKYLL_ENV` is `production`
8
25
 
9
- ## 1.0.0 / 2018-01-02
26
+ ## 1.0.0 (2018-01-02) [☰](https://github.com/philnash/jekyll-zopfli/commits/v1.0.0)
10
27
 
11
28
  ### Added
12
29
 
data/README.md CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  Generate gzipped assets and files for your Jekyll site at build time using [Zopfli](https://github.com/google/zopfli) compression.
4
4
 
5
- [![Build Status](https://travis-ci.org/philnash/jekyll-zopfli.svg?branch=master)](https://travis-ci.org/philnash/jekyll-zopfli)
5
+ [![Gem Version](https://badge.fury.io/rb/jekyll-zopfli.svg)](https://rubygems.org/gems/jekyll-zopfli) [![Build Status](https://travis-ci.org/philnash/jekyll-zopfli.svg?branch=master)](https://travis-ci.org/philnash/jekyll-zopfli) [![Maintainability](https://api.codeclimate.com/v1/badges/177f76759bc3b996e9e2/maintainability)](https://codeclimate.com/github/philnash/jekyll-zopfli/maintainability) [![Inline docs](https://inch-ci.org/github/philnash/jekyll-zopfli.svg?branch=master)](https://inch-ci.org/github/philnash/jekyll-zopfli)
6
+
7
+ [API docs](http://www.rubydoc.info/gems/jekyll-zopfli/) | [GitHub repo](https://github.com/philnash/jekyll-zopfli)
8
+
9
+ ## Why?
6
10
 
7
11
  Performance in web applications is important. You know that, which is why you have created a static site using Jekyll. But you want a bit more performance. You're serving your assets and files gzipped, but you're making your webserver do it?
8
12
 
@@ -14,12 +18,18 @@ Why not just generate those gzip files at build time? And with Google's Zopfli a
14
18
 
15
19
  Zopfli is a much slower algorithm than Zlib, so this will likely significantly slow down your site build. If you still want to generate gzip files at build time with a faster build time and Zlib's compression level, check out [`Jekyll::Gzip`](https://github.com/philnash/jekyll-gzip).
16
20
 
21
+ ### Want even more compression?
22
+
23
+ Zopfli is about the best compression we can get out of the gzip format, but there's more! [Brotli](https://en.wikipedia.org/wiki/Brotli) is a relatively new compression format that is now [supported by many browsers](https://caniuse.com/#search=brotli) and can produce even smaller files. You can use brotli compression alongside gzip in your Sinatra app with [`Jekyll::Brotli`](http://github.com/philnash/jekyll-brotli).
24
+
17
25
  ## Installation
18
26
 
19
27
  Add this line to your application's Gemfile:
20
28
 
21
29
  ```ruby
22
- gem 'jekyll-zopfli'
30
+ group :jekyll_plugins do
31
+ gem 'jekyll-zopfli'
32
+ end
23
33
  ```
24
34
 
25
35
  And then execute:
@@ -28,11 +38,55 @@ And then execute:
28
38
  bundle install
29
39
  ```
30
40
 
41
+ Then add the plugin to the `plugins` key in your `_config.yml`
42
+
43
+ ```yml
44
+ plugins:
45
+ - jekyll-zopfli
46
+ ```
47
+
31
48
  ## Usage
32
49
 
33
- Once you have the gem installed, run `JEKYLL_ENV=production bundle exec jekyll build`. In your destination directory (`_site` by default) you will find gzipped files.
50
+ Once you have the gem installed, build your Jekyll site in production mode. On Mac/Linux you can run
51
+
52
+ ```bash
53
+ JEKYLL_ENV=production bundle exec jekyll build
54
+ ```
55
+
56
+ On Windows, set the `JEKYLL_ENV` environment variable to `"production"`. Check out [this blog post on setting environment variables on Windows](https://www.twilio.com/blog/2017/01/how-to-set-environment-variables.html). Then run:
57
+
58
+ ```bash
59
+ bundle exec jekyll build
60
+ ```
61
+
62
+ In your destination directory (`_site` by default) you will find gzipped versions of all your text files.
63
+
64
+ `Jekyll::Zopfli` only runs when the environment variable `JEKYLL_ENV` is set to `production` as dealing with gzipping files is unnecessary in development mode and just slows down the site build.
65
+
66
+ ### Configuration
67
+
68
+ By default, `Jekyll::Zopfli` will compress all files with the following extensions:
69
+
70
+ - '.html'
71
+ - '.css'
72
+ - '.js'
73
+ - '.txt'
74
+ - '.ttf'
75
+ - '.atom'
76
+ - '.stl'
77
+ - '.xml'
78
+ - '.svg'
79
+ - '.eot'
80
+
81
+ You can supply your own extensions by adding a `zopfli` key to your site's `_config.yml` listing the extensions that you want to compress. For example to only compress HTML, CSS and JavaScript files, add the following to `_config.yml`:
34
82
 
35
- Due to the time it takes to generate zopfli files, this plugin only works when the `JEKYLL_ENV` environment variable is set to `production`.
83
+ ```yml
84
+ zopfli:
85
+ extensions:
86
+ - '.html'
87
+ - '.css'
88
+ - '.js
89
+ ```
36
90
 
37
91
  ### Serving pre-compiled gzip files
38
92
 
@@ -48,9 +102,20 @@ gzip_static on;
48
102
 
49
103
  The `ngx_http_gzip_static_module` module is not built by default, so you may need to enable using the `--with-http_gzip_static_module` configuration parameter.
50
104
 
105
+ #### Apache
106
+
107
+ In either a `<Directory>` section in your Apache config or in an `.htaccess` file, add the following:
108
+
109
+ ```
110
+ AddEncoding gzip .gz
111
+ RewriteCond %{HTTP:Accept-encoding} gzip
112
+ RewriteCond %{REQUEST_FILENAME}.gz -f
113
+ RewriteRule ^(.*)$ $1.gz [QSA,L]
114
+ ```
115
+
51
116
  #### Other web servers
52
117
 
53
- TODO: instructions for other web servers like Apache, HAProxy, etc.
118
+ TODO: instructions for other web servers like HAProxy, h2o etc.
54
119
 
55
120
  Do you know how to do this for a different server? Please open a [pull request](https://github.com/philnash/jekyll-zopfli/pulls) or an [issue](https://github.com/philnash/jekyll-zopfli/issues) with the details!
56
121
 
data/lib/jekyll-zopfli.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "jekyll/zopfli"
data/lib/jekyll/zopfli.rb CHANGED
@@ -1,13 +1,34 @@
1
- require "jekyll/zopfli/version"
2
- require "jekyll/zopfli/compressor"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/zopfli/version'
4
+ require 'jekyll/zopfli/config'
5
+ require 'jekyll/zopfli/compressor'
6
+ require 'pathname'
3
7
 
4
8
  module Jekyll
5
9
  module Zopfli
6
10
  end
7
11
  end
8
12
 
13
+ Jekyll::Hooks.register :site, :after_init do |site|
14
+ config = site.config['zopfli'] || {}
15
+ site.config['zopfli'] = Jekyll::Zopfli::DEFAULT_CONFIG.merge(config) || {}
16
+ end
17
+
9
18
  Jekyll::Hooks.register :site, :post_write do |site|
10
- if ENV["JEKYLL_ENV"] == "production"
11
- Jekyll::Zopfli::Compressor.new(site).compress
19
+ Jekyll::Zopfli::Compressor.compress_site(site) if Jekyll.env == 'production'
20
+ end
21
+
22
+ begin
23
+ require 'jekyll-assets'
24
+
25
+ Jekyll::Assets::Hook.register :env, :after_write do |env|
26
+ if Jekyll.env == 'production'
27
+ path = Pathname.new("#{env.jekyll.config['destination']}#{env.prefix_url}")
28
+ Jekyll::Zopfli::Compressor.compress_directory(path, env.jekyll)
29
+ end
12
30
  end
31
+ rescue LoadError
32
+ # The Jekyll site doesn't use Jekyll::Assets, so no need to compress those
33
+ # files.
13
34
  end
@@ -1,29 +1,69 @@
1
- require "zopfli"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/zopfli/config'
4
+ require 'zopfli'
2
5
 
3
6
  module Jekyll
7
+ ##
8
+ # The main namespace for +Jekyll::Zopfli+. Includes the +Compressor+ module
9
+ # which is used to map over files, either using an instance of +Jekyll::Site+
10
+ # or a directory path, and compress them using Zopfli.
4
11
  module Zopfli
5
- class Compressor
6
- ZIPPABLE_EXTENSIONS = [
7
- '.html',
8
- '.css',
9
- '.js',
10
- '.txt',
11
- '.ttf',
12
- '.atom',
13
- '.stl',
14
- '.xml',
15
- '.svg',
16
- '.eot'
17
- ]
18
-
19
- attr_reader :site
12
+ ##
13
+ # The module that does the compressing using Zopfli.
14
+ module Compressor
15
+ ##
16
+ # Takes an instance of +Jekyll::Site+ and maps over the site files
17
+ # compressing them in the destination directory.
18
+ # @example
19
+ # site = Jekyll::Site.new(site_config)
20
+ # Jekyll::Zopfli::Compressor.compress_site(site)
21
+ #
22
+ # @param site [Jekyll::Site] A Jekyll::Site object that has generated its
23
+ # site files ready for compression.
24
+ #
25
+ # @return void
26
+ def self.compress_site(site)
27
+ site.each_site_file do |file|
28
+ compress_file(file.destination(site.dest), zippable_extensions(site))
29
+ end
30
+ end
20
31
 
21
- def initialize(site)
22
- @site = site
32
+ ##
33
+ # Takes a directory path and maps over the files within compressing them
34
+ # in place.
35
+ #
36
+ # @example
37
+ # Jekyll::Zopfli::Compressor.compress_directory("~/blog/_site", site)
38
+ #
39
+ # @param dir [Pathname, String] The path to a directory of files ready for
40
+ # compression.
41
+ # @param site [Jekyll::Site] An instance of the `Jekyll::Site` used for
42
+ # config.
43
+ #
44
+ # @return void
45
+ def self.compress_directory(dir, site)
46
+ extensions = zippable_extensions(site).join(',')
47
+ files = Dir.glob(dir + "**/*{#{extensions}}")
48
+ files.each { |file| compress_file(file, zippable_extensions(site)) }
23
49
  end
24
50
 
25
- def compress_file(file_name)
26
- return unless ZIPPABLE_EXTENSIONS.include?(File.extname(file_name))
51
+ ##
52
+ # Takes a file name and an array of extensions. If the file name extension
53
+ # matches one of the extensions in the array then the file is loaded and
54
+ # compressed using Zopfli, outputting the gzipped file under the name of
55
+ # the original file with an extra .gz extension.
56
+ #
57
+ # @example
58
+ # Jekyll::Zopfli::Compressor.compress_file("~/blog/_site/index.html", ['.html')
59
+ #
60
+ # @param file_name [String] The file name of the file we want to compress
61
+ # @param extensions [Array<String>] The extensions of files that will be
62
+ # compressed.
63
+ #
64
+ # @return void
65
+ def self.compress_file(file_name, extensions)
66
+ return unless extensions.include?(File.extname(file_name))
27
67
  zipped = "#{file_name}.gz"
28
68
  contents = ::Zopfli.deflate(File.read(file_name), format: :gzip)
29
69
  File.open(zipped, "w+") do |file|
@@ -32,11 +72,11 @@ module Jekyll
32
72
  File.utime(File.atime(file_name), File.mtime(file_name), zipped)
33
73
  end
34
74
 
35
- def compress
36
- site.each_site_file do |file|
37
- compress_file(file.destination(site.dest))
38
- end
75
+ private
76
+
77
+ def self.zippable_extensions(site)
78
+ site.config['zopfli'] && site.config['zopfli']['extensions'] || Jekyll::Zopfli::DEFAULT_CONFIG['extensions']
39
79
  end
40
80
  end
41
81
  end
42
- end
82
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Zopfli
5
+ DEFAULT_CONFIG = {
6
+ 'extensions' => [
7
+ '.html',
8
+ '.css',
9
+ '.js',
10
+ '.txt',
11
+ '.ttf',
12
+ '.atom',
13
+ '.stl',
14
+ '.xml',
15
+ '.svg',
16
+ '.eot'
17
+ ].freeze
18
+ }.freeze
19
+ end
20
+ end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jekyll
2
4
  module Zopfli
3
- VERSION = "1.1.0"
5
+ VERSION = "2.0.0"
4
6
  end
5
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-zopfli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Nash
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-02 00:00:00.000000000 Z
11
+ date: 2018-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -117,6 +117,7 @@ files:
117
117
  - lib/jekyll-zopfli.rb
118
118
  - lib/jekyll/zopfli.rb
119
119
  - lib/jekyll/zopfli/compressor.rb
120
+ - lib/jekyll/zopfli/config.rb
120
121
  - lib/jekyll/zopfli/version.rb
121
122
  homepage: https://github.com/philnash/jekyll-zopfli
122
123
  licenses:
@@ -138,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
139
  version: '0'
139
140
  requirements: []
140
141
  rubyforge_project:
141
- rubygems_version: 2.7.4
142
+ rubygems_version: 2.7.7
142
143
  signing_key:
143
144
  specification_version: 4
144
145
  summary: Generate gzipped assets and files for your Jekyll site at build time using