jekyll-gzip 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: d86e365ec93b205a8c49378bf879c2331cb87a1f49152a5bee66f98c86c6eb22
4
- data.tar.gz: 1c907018e1dca4314bfaf82ed2ad030260d27f0a278688af76409bf151a0a7c6
3
+ metadata.gz: 0e6c5f628c1b543e3efe2a02b3690eaeb0d03590dbead795bd83ed7a51978b59
4
+ data.tar.gz: cc72f33252c97c3948c883f39fc12f5dd016ff49403dad6e63137fbfbb25fcb5
5
5
  SHA512:
6
- metadata.gz: 18fa320d5ae950b63597c6f3a84e8d1799b9342b3d47777e94dce6d8805b328bc749a774a45cedc43f93a91b9e461c30e1a8e5d0f5a21756097fe2f25d7e76ad
7
- data.tar.gz: ebd5c9f90b6f3bed402113d833fe62fd6dc97b92f5de9b264984f44da24b04bbaf6b674963085160d5807a724647a9175cd1f0ad15c68b984af1352c6a09efcc
6
+ metadata.gz: ab402cf215ff857fb4724da88fcfe48327830e208f34033204c17230ded4793f5f77d45bb14e1e10841e3b73373b13dd5cc7d04a1d135c941448f6085085b254
7
+ data.tar.gz: a2a882cb1d76ac8128069404106fe69b95aceb4698c21c240de29899ff5f8abd475e6efcbf5738f21f8a1d1d0b5af3fbb66902db0251f1636acb9a5e2361de60
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-gzip/compare/v1.1.0...v2.0.0)
8
+
9
+ ### Added
10
+
11
+ * Adds frozen string literal comments
12
+ * Tries to hook into Jekyll::Assets if available
13
+
14
+ ### Changed
15
+
16
+ * Uses built in `Jekyll.env` instead of `ENV["JEKYLL_ENV"]`
17
+ * Changes `Jekyll::Gzip::Compressor` to a module and implements a `compress_directory` method
18
+ * Moves Jekyll::Gzip::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-gzip/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-01
26
+ ## 1.0.0 (2018-01-01) [☰](https://github.com/philnash/jekyll-gzip/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.
4
4
 
5
- [![Build Status](https://travis-ci.org/philnash/jekyll-gzip.svg?branch=master)](https://travis-ci.org/philnash/jekyll-gzip)
5
+ [![Gem Version](https://badge.fury.io/rb/jekyll-gzip.svg)](https://rubygems.org/gems/jekyll-gzip) [![Build Status](https://travis-ci.org/philnash/jekyll-gzip.svg?branch=master)](https://travis-ci.org/philnash/jekyll-gzip) [![Maintainability](https://api.codeclimate.com/v1/badges/895369c1c7a17f879b00/maintainability)](https://codeclimate.com/github/philnash/jekyll-gzip/maintainability) [![Inline docs](https://inch-ci.org/github/philnash/jekyll-gzip.svg?branch=master)](https://inch-ci.org/github/philnash/jekyll-gzip)
6
+
7
+ [API docs](http://www.rubydoc.info/gems/jekyll-gzip/) | [GitHub repo](https://github.com/philnash/jekyll-gzip)
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
 
@@ -10,16 +14,20 @@ Why not just generate those gzip files at build time? And with the maximum compr
10
14
 
11
15
  `Jekyll::Gzip` does just that. Add the gem to your Jekyll application and when you build your site it will generate gzip files for all text based files (HTML, CSS, JavaScript, etc).
12
16
 
13
- ## Want even more compression?
17
+ ### Want even more compression?
14
18
 
15
19
  Zlib's gzipping capabilities don't quite squeeze all the compression out of our files that we could want. If you want a slower but better compression algorithm, check out [Jekyll::Zopfli](https://github.com/philnash/jekyll-zopfli).
16
20
 
21
+ 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).
22
+
17
23
  ## Installation
18
24
 
19
- Add this line to your application's Gemfile:
25
+ Add `Jekyll::Gzip` to your application's dependencies:
20
26
 
21
27
  ```ruby
22
- gem 'jekyll-gzip'
28
+ group :jekyll_plugins do
29
+ gem 'jekyll-gzip'
30
+ end
23
31
  ```
24
32
 
25
33
  And then execute:
@@ -28,12 +36,56 @@ And then execute:
28
36
  bundle install
29
37
  ```
30
38
 
39
+ Then add the plugin to the `plugins` key in your `_config.yml`
40
+
41
+ ```yml
42
+ plugins:
43
+ - jekyll-gzip
44
+ ```
45
+
31
46
  ## Usage
32
47
 
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.
48
+ Once you have the gem installed, build your Jekyll site in production mode. On Mac/Linux you can run
49
+
50
+ ```bash
51
+ JEKYLL_ENV=production bundle exec jekyll build
52
+ ```
53
+
54
+ 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:
55
+
56
+ ```bash
57
+ bundle exec jekyll build
58
+ ```
59
+
60
+ In your destination directory (`_site` by default) you will find gzipped versions of all your text files.
34
61
 
35
62
  `Jekyll::Gzip` 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.
36
63
 
64
+ ### Configuration
65
+
66
+ By default, `Jekyll::Gzip` will compress all files with the following extensions:
67
+
68
+ - '.html'
69
+ - '.css'
70
+ - '.js'
71
+ - '.txt'
72
+ - '.ttf'
73
+ - '.atom'
74
+ - '.stl'
75
+ - '.xml'
76
+ - '.svg'
77
+ - '.eot'
78
+
79
+ You can supply your own extensions by adding a `gzip` 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`:
80
+
81
+ ```yml
82
+ gzip:
83
+ extensions:
84
+ - '.html'
85
+ - '.css'
86
+ - '.js
87
+ ```
88
+
37
89
  ### Serving pre-compiled gzip files
38
90
 
39
91
  You will likely need to adjust your web server config to serve these precomputed gzip files. See below for common server configurations:
@@ -48,9 +100,20 @@ gzip_static on;
48
100
 
49
101
  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
102
 
103
+ #### Apache
104
+
105
+ In either a `<Directory>` section in your Apache config or in an `.htaccess` file, add the following:
106
+
107
+ ```
108
+ AddEncoding gzip .gz
109
+ RewriteCond %{HTTP:Accept-encoding} gzip
110
+ RewriteCond %{REQUEST_FILENAME}.gz -f
111
+ RewriteRule ^(.*)$ $1.gz [QSA,L]
112
+ ```
113
+
51
114
  #### Other web servers
52
115
 
53
- TODO: instructions for other web servers like Apache, HAProxy, etc.
116
+ TODO: instructions for other web servers like HAProxy, h2o etc.
54
117
 
55
118
  Do you know how to do this for a different server? Please open a [pull request](https://github.com/philnash/jekyll-gzip/pulls) or an [issue](https://github.com/philnash/jekyll-gzip/issues) with the details!
56
119
 
data/lib/jekyll-gzip.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "jekyll/gzip"
data/lib/jekyll/gzip.rb CHANGED
@@ -1,13 +1,34 @@
1
- require "jekyll/gzip/version"
2
- require "jekyll/gzip/compressor"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/gzip/version'
4
+ require 'jekyll/gzip/config'
5
+ require 'jekyll/gzip/compressor'
6
+ require 'pathname'
3
7
 
4
8
  module Jekyll
5
9
  module Gzip
6
10
  end
7
11
  end
8
12
 
13
+ Jekyll::Hooks.register :site, :after_init do |site|
14
+ config = site.config['gzip'] || {}
15
+ site.config['gzip'] = Jekyll::Gzip::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::Gzip::Compressor.new(site).compress
19
+ Jekyll::Gzip::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::Gzip::Compressor.compress_directory(path, env.jekyll)
29
+ end
12
30
  end
13
- end
31
+ rescue LoadError
32
+ # The Jekyll site doesn't use Jekyll::Assets, so no need to compress those
33
+ # files.
34
+ end
@@ -1,29 +1,69 @@
1
- require "zlib"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/gzip/config'
4
+ require 'zlib'
2
5
 
3
6
  module Jekyll
7
+ ##
8
+ # The main namespace for +Jekyll::Gzip+. 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 Zlib.
4
11
  module Gzip
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 Zlib.
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::Gzip::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::Gzip::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, extensions) }
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 Zlib, outputting the gzipped file under the name of
55
+ # the original file with an extra .gz extension.
56
+ #
57
+ # @example
58
+ # Jekyll::Gzip::Compressor.compress_file("~/blog/_site/index.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
  Zlib::GzipWriter.open(zipped, Zlib::BEST_COMPRESSION) do |gz|
29
69
  gz.mtime = File.mtime(file_name)
@@ -32,11 +72,11 @@ module Jekyll
32
72
  end
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['gzip'] && site.config['gzip']['extensions'] || Jekyll::Gzip::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 Gzip
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 Gzip
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-gzip
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
@@ -102,6 +102,7 @@ files:
102
102
  - lib/jekyll-gzip.rb
103
103
  - lib/jekyll/gzip.rb
104
104
  - lib/jekyll/gzip/compressor.rb
105
+ - lib/jekyll/gzip/config.rb
105
106
  - lib/jekyll/gzip/version.rb
106
107
  homepage: https://github.com/philnash/jekyll-gzip
107
108
  licenses:
@@ -123,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
124
  version: '0'
124
125
  requirements: []
125
126
  rubyforge_project:
126
- rubygems_version: 2.7.4
127
+ rubygems_version: 2.7.7
127
128
  signing_key:
128
129
  specification_version: 4
129
130
  summary: Generate gzipped assets and files for your Jekyll site at build time