jekyll-brotli 1.0.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: dd14849e1f513842567d342b744f99ace866d1dcdaa41a599b24158f0c3fc802
4
- data.tar.gz: e51a6bdc41376a8f74e82bf4375fb34e87a1c46c68e1ba6c9fe21282f9ebb8fd
3
+ metadata.gz: e258a9af9ac7724c0d5f8c94b7a887a8c00c034500932c5c70e59b48e9335198
4
+ data.tar.gz: 7e79bf93c991886027600ffa5aa7160ce26298b01c0c2d5d6cf3efaf04182b17
5
5
  SHA512:
6
- metadata.gz: cef9b2cb82419a5731176fb20ee9a0a9b653908c798dda732998b8af90598e2e946589a911151487130abe70a28d80abe19b83fa4db3a595d082ceb336d7f3d3
7
- data.tar.gz: 37e61722a6497d82c7c37cd172251331ac308486142f1604e7577f4194e2d8014d681d6991040d9b2053d1c31c029b63f31dcc4582d229e11028f12d2d31181d
6
+ metadata.gz: b25959e083aafbe1495a362d56f6e284a189ca9817b2bc0476b3eec7cdae339ba9b59407a4342e5f431d6cf5fe24e99a8783d79a9782b2dfb3ec25cdcbb71644
7
+ data.tar.gz: ae0ae4eb22939055d3cfd7db6c1e9b5f04b32c75b709d60fdf0c90a795407a46f924c735c5e192bcf5c8da27718a960e0687b0e955a51c8b43219dc14cfc6910
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/.travis.yml CHANGED
@@ -6,4 +6,4 @@ rvm:
6
6
  - 2.3
7
7
  - 2.2
8
8
  before_install: gem install bundler -v 1.16.1
9
- script: bundle exec rake
9
+ script: bundle exec rspec
data/CHANGELOG.md CHANGED
@@ -1,6 +1,23 @@
1
1
  # Changelog
2
2
 
3
- ## 1.0.0 / 2018-01-03
3
+ ## Ongoing [☰](https://github.com/philnash/jekyll-brotli/compare/v1.1.0...master)
4
+
5
+ ...
6
+
7
+ ## 2.0.0 (2018-11-24) [☰](https://github.com/philnash/jekyll-brotli/compare/v1.0.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::Brotli::Compressor` to a module and implements a `compress_directory` method
18
+ * Moves Jekyll::Brotli::COMPRESSABLE_EXTENSIONS into plugin config that can overwritten in the site config
19
+
20
+ ## 1.0.0 (2018-01-03) [☰](https://github.com/philnash/jekyll-brotli/commits/v1.0.0)
4
21
 
5
22
  ### Added
6
23
 
data/README.md CHANGED
@@ -2,7 +2,11 @@
2
2
 
3
3
  Generate compressed assets and files for your Jekyll site at build time using [Brotli](https://github.com/google/brotli) compression.
4
4
 
5
- [![Build Status](https://travis-ci.org/philnash/jekyll-brotli.svg?branch=master)](https://travis-ci.org/philnash/jekyll-brotli)
5
+ [![Gem Version](https://badge.fury.io/rb/jekyll-brotli.svg)](https://rubygems.org/gems/jekyll-brotli) [![Build Status](https://travis-ci.org/philnash/jekyll-brotli.svg?branch=master)](https://travis-ci.org/philnash/jekyll-brotli) [![Maintainability](https://api.codeclimate.com/v1/badges/9deb43d992a0c47a55e4/maintainability)](https://codeclimate.com/github/philnash/jekyll-brotli/maintainability) [![Inline docs](https://inch-ci.org/github/philnash/jekyll-brotli.svg?branch=master)](https://inch-ci.org/github/philnash/jekyll-brotli)
6
+
7
+ [API docs](http://www.rubydoc.info/gems/jekyll-brotli/) | [GitHub repo](https://github.com/philnash/jekyll-brotli)
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 could it be better?
8
12
 
data/lib/jekyll/brotli.rb CHANGED
@@ -1,13 +1,34 @@
1
- require "jekyll/brotli/version"
2
- require "jekyll/brotli/compressor"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/brotli/version'
4
+ require 'jekyll/brotli/config'
5
+ require 'jekyll/brotli/compressor'
6
+ require 'pathname'
3
7
 
4
8
  module Jekyll
5
9
  module Brotli
6
10
  end
7
11
  end
8
12
 
13
+ Jekyll::Hooks.register :site, :after_init do |site|
14
+ config = site.config['brotli'] || {}
15
+ site.config['brotli'] = Jekyll::Brotli::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::Brotli::Compressor.new(site).compress
19
+ Jekyll::Brotli::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::Brotli::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,72 @@
1
- require "brotli"
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/brotli/config'
4
+ require 'brotli'
2
5
 
3
6
  module Jekyll
7
+ ##
8
+ # The main namespace for +Jekyll::Brotli+. 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 Brotli.
4
11
  module Brotli
5
- class Compressor
6
- COMPRESSABLE_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 Brotli.
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::Brotli::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(
29
+ file.destination(site.dest),
30
+ compressable_extensions(site)
31
+ )
32
+ end
33
+ end
20
34
 
21
- def initialize(site)
22
- @site = site
35
+ ##
36
+ # Takes a directory path and maps over the files within compressing them
37
+ # in place.
38
+ #
39
+ # @example
40
+ # Jekyll::Brotli::Compressor.compress_directory("~/blog/_site", site)
41
+ #
42
+ # @param dir [Pathname, String] The path to a directory of files ready for
43
+ # compression.
44
+ # @param site [Jekyll::Site] An instance of the `Jekyll::Site` used for
45
+ # config.
46
+ #
47
+ # @return void
48
+ def self.compress_directory(dir, site)
49
+ extensions = compressable_extensions(site).join(',')
50
+ files = Dir.glob(dir + "**/*{#{extensions}}")
51
+ files.each { |file| compress_file(file, compressable_extensions(site)) }
23
52
  end
24
53
 
25
- def compress_file(file_name)
26
- return unless COMPRESSABLE_EXTENSIONS.include?(File.extname(file_name))
54
+ ##
55
+ # Takes a file name and an array of extensions. If the file name extension
56
+ # matches one of the extensions in the array then the file is loaded and
57
+ # compressed using Brotli, outputting the compressed file under the name
58
+ # of the original file with an extra .br extension.
59
+ #
60
+ # @example
61
+ # Jekyll::Brotli::Compressor.compress_file("~/blog/_site/index.html")
62
+ #
63
+ # @param file_name [String] The file name of the file we want to compress
64
+ # @param extensions [Array<String>] The extensions of files that will be
65
+ # compressed.
66
+ #
67
+ # @return void
68
+ def self.compress_file(file_name, extensions)
69
+ return unless extensions.include?(File.extname(file_name))
27
70
  compressed = "#{file_name}.br"
28
71
  contents = ::Brotli.deflate(File.read(file_name), quality: 11)
29
72
  File.open(compressed, "w+") do |file|
@@ -32,11 +75,11 @@ module Jekyll
32
75
  File.utime(File.atime(file_name), File.mtime(file_name), compressed)
33
76
  end
34
77
 
35
- def compress
36
- site.each_site_file do |file|
37
- compress_file(file.destination(site.dest))
38
- end
78
+ private
79
+
80
+ def self.compressable_extensions(site)
81
+ site.config['brotli'] && site.config['brotli']['extensions'] || Jekyll::Brotli::DEFAULT_CONFIG['extensions']
39
82
  end
40
83
  end
41
84
  end
42
- end
85
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module Brotli
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 Brotli
3
- VERSION = "1.0.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-brotli
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.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-03 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-brotli.rb
118
118
  - lib/jekyll/brotli.rb
119
119
  - lib/jekyll/brotli/compressor.rb
120
+ - lib/jekyll/brotli/config.rb
120
121
  - lib/jekyll/brotli/version.rb
121
122
  homepage: https://github.com/philnash/jekyll-brotli
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 brotli compressed assets and files for your Jekyll site at build