sprockets-webp-exporter 0.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d2b364531e65037a1359bcef8d22316de697cb9ed6fe13b4f16880b809c7cf2
4
+ data.tar.gz: c0a61581b97f3a19c537a1b00ba69a8dc777da2373ab2ff7368f2a51820ad5c5
5
+ SHA512:
6
+ metadata.gz: e2e680319385f4a3c0746eea3a802e59d01efa538968e767d2a7519e37e0b0cf00a4a222fb520671629fd85699a83bd6e4a1087de76264118dc1496b1d5d8216
7
+ data.tar.gz: aa06849b72d16cf26cf27c4d402d6623d6bf5ffdfaf5050a7cb7de93c19ad2c0c08f135c3e9ac533b8b89c9cc5bb18565d3cc463a30c11c3cdfbd5393fccecc4
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+ git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3
+
4
+ # Specify your gem's dependencies in sprockets-webp-exporter.gemspec
5
+ gemspec
6
+
7
+ gem 'sprockets', '>= 4.0.0.beta8', github: 'rails/sprockets'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Max Riveiro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,137 @@
1
+ # Sprockets::WebP
2
+
3
+ [![unstable](https://rawgithub.com/hughsk/stability-badges/master/dist/unstable.svg)](http://github.com/hughsk/stability-badges)
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/sprockets-webp.png)](http://badge.fury.io/rb/sprockets-webp)
6
+ [![Code Climate](https://codeclimate.com/github/alsemyonov/sprockets-webp-exporter.png)](https://codeclimate.com/github/alsemyonov/sprockets-webp-exporter)
7
+ [![Dependency Status](https://gemnasium.com/alsemyonov/sprockets-webp-exporter.png)](https://gemnasium.com/alsemyonov/sprockets-webp-exporter)
8
+ [![Still Maintained](http://stillmaintained.com/alsemyonov/sprockets-webp-exporter.png)](http://stillmaintained.com/alsemyonov/sprockets-webp-exporter)
9
+
10
+ [![Coderwall](https://api.coderwall.com/alsemyonov/endorsecount.png)](https://coderwall.com/alsemyonov)
11
+
12
+ **Warning**: This gem depends on Sprockets 4 (that is in beta right now), because of using new `#register_exporter`.
13
+ There is no guarantee for stable API until `sprockets-4.0.0` and `sprockets-webp-exporter-1.0.0` released.
14
+
15
+ _Big thanks to [Max Riveiro](https://github.com/kavu) for his [sprockets-webp](https://github.com/kavu/sprockets-webp)
16
+ gem that worked with previous versions of Sprockets, but is not working now. I hope we could merge our gems in future._
17
+
18
+ This gem provides a WebP Exporter for converting PNG and JPEG assets to the WebP format using Sprockets 4.
19
+
20
+ ## Requirements
21
+
22
+ The main requirement is obviously [libwebp](https://developers.google.com/speed/webp/) itself. Please, consult the [webp-ffi](https://github.com/le0pard/webp-ffi) README for the installation instructions.
23
+
24
+ ## Installation
25
+
26
+ ### Rails 5
27
+
28
+ If you're using Rails 5 you need to add gem to the ```:production``` group in to your application's Gemfile:
29
+
30
+ ```ruby
31
+ group :production do
32
+ # ...
33
+ gem 'sprockets-webp-exporter'
34
+ # ...
35
+ end
36
+ ```
37
+
38
+ ## Configuration
39
+
40
+ You can configure encode options for webp by using `encode_options` (in example default options):
41
+
42
+ ```ruby
43
+ Sprockets::WebP.encode_options = { quality: 100, lossless: 1, method: 6, alpha_filtering: 2, alpha_compression: 0, alpha_quality: 100 }
44
+ ```
45
+
46
+ More options you can find in [web-ffi readme](https://github.com/le0pard/webp-ffi#encode-webp-image).
47
+
48
+ ## Testing
49
+
50
+ Drop some PNGs and JPGs into ```app/assets/images``` and you can test converter locally with the Rake task:
51
+
52
+ ```bash
53
+ bundle exec rake assets:precompile RAILS_ENV=production
54
+ ```
55
+
56
+ ## Web Server
57
+
58
+ As not all browsers support ``webp`` images (see [Can I Use](http://caniuse.com/webp)), so they need to be served conditionally based on the HTTP ``Accept`` header sent by browsers capable to display this format.
59
+
60
+ ### Nginx
61
+
62
+ Here is a simple [nginx](http://nginx.org) recipe, which contrary to popular beliefs, do not require ``if`` nor ``rewrite``, instead use lightweight ``map`` and ``try_files``
63
+
64
+ ```nginx
65
+ http {
66
+ # IMPORTANT!!! Make sure that mime.types below lists WebP like that:
67
+ # image/webp webp;
68
+ include /etc/nginx/mime.types;
69
+
70
+ ##
71
+ # Is webp supported?
72
+ # (needs to be part of http section)
73
+ ##
74
+
75
+ map $http_accept $webp_suffix {
76
+ default "";
77
+ "~*webp" ".webp";
78
+ }
79
+
80
+ ##
81
+ # Server
82
+ ##
83
+
84
+ server {
85
+ location ~* ^/images/.+\.(png|jpg)$ {
86
+ if ($webp_suffix != "") {
87
+ add_header Vary Accept;
88
+ }
89
+
90
+ try_files $uri$webp_suffix $uri =404;
91
+ }
92
+ }
93
+ }
94
+ ```
95
+
96
+ Make sure that webp is defined in `mime.types` file:
97
+
98
+ ```nginx
99
+ image/webp webp;
100
+ ```
101
+
102
+ ## CDN
103
+
104
+ If you serve your assets using CDN, you need to make sure that it forwards `Accept` header allowing to conditionally choose webp for browsers which support it.
105
+
106
+ ### Amazon AWS CloudFront
107
+
108
+ Following solution would not work if your CloudFront distribution points to S3. Instead it should point to your webserver, which will host the webp serving logic.
109
+
110
+ Take following steps to enable `Accept` header forwarding:
111
+
112
+ * visit your CloudFront distributions page
113
+ * select distribution
114
+ * choose `Behaviors` tab
115
+ * select behaviourrepresenting your assets end hit `Edit`
116
+ * select `Whitelist` for the `Forward Headers` option
117
+ * add `Accept` to the list on the right
118
+ * approve your changes clicking `Yes, Edit`
119
+ * wait until refreshed distribution will be deployed
120
+
121
+ Test with:
122
+
123
+ ```
124
+ curl -I -H "Accept: image/webp" http://yourdomain.com/yourimage.png
125
+ curl -I http://yourdomain.com/yourimage.png
126
+ ```
127
+
128
+ Returned `Content-Type` should be `image/webp` in the first case and `image/png` in the second.
129
+
130
+
131
+ ## Contributing
132
+
133
+ 1. Fork it
134
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
135
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
136
+ 4. Push to the branch (`git push origin my-new-feature`)
137
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,27 @@
1
+ require 'sprockets/exporters/base'
2
+ require 'webp-ffi'
3
+
4
+ module Sprockets
5
+ module Exporters
6
+ # Generates a `.webp` file using the webp-ffi
7
+ class WebpExporter < Exporters::Base
8
+ def setup
9
+ @webp_target = "#{ target }.webp"
10
+ end
11
+
12
+ def skip?(logger)
13
+ if ::File.exist?(@webp_target)
14
+ logger.debug "Skipping #{ @webp_target }, already exists"
15
+ true
16
+ else
17
+ logger.info "Writing #{ @webp_target }"
18
+ false
19
+ end
20
+ end
21
+
22
+ def call
23
+ ::WebP.encode(target, @webp_target, Sprockets::WebP.encode_options)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'sprockets/exporters/webp_exporter'
4
+
5
+ module Sprockets
6
+ module WebP
7
+ class Railtie < ::Rails::Railtie
8
+ initializer :webp, group: :all do |app|
9
+ app.config.assets.configure do |env|
10
+ env.register_exporter 'image/jpeg', Exporters::WebpExporter
11
+ env.register_exporter 'image/png', Exporters::WebpExporter
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+
3
+ module Sprockets
4
+ module WebP
5
+ VERSION = '0.1.0.rc1'
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ require 'sprockets/webp/version'
4
+ require 'sprockets/webp/railtie' if defined?(Rails)
5
+
6
+ module Sprockets
7
+ module WebP
8
+ # configure web encode options
9
+ #
10
+ # Sprockets::WebP.encode_options = { quality: 100, lossless: 1, method: 6, alpha_filtering: 2, alpha_compression: 0, alpha_quality: 100 }
11
+ #
12
+ class << self
13
+ attr_writer :encode_options
14
+
15
+ def encode_options
16
+ @encode_options ||= { quality: 100, lossless: 1, method: 6, alpha_filtering: 2, alpha_compression: 0, alpha_quality: 100 }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'sprockets/webp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'sprockets-webp-exporter'
8
+ spec.version = Sprockets::WebP::VERSION
9
+ spec.authors = ['Alex Semyonov']
10
+ spec.email = ['alex@semyonov.us']
11
+ spec.summary = %q{Sprockets 4 exporter of PNG and JPEG assets to WebP}
12
+ spec.description = spec.summary
13
+ spec.homepage = 'https://github.com/alsemyonov/sprockets-webp-exporter'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.test_files = spec.files.grep(%r{^spec/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.required_ruby_version = '>= 1.9.3'
21
+
22
+ spec.add_dependency 'sprockets', '>= 4.0.0.beta8'
23
+ spec.add_dependency 'webp-ffi', '~> 0.2.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.3'
26
+ spec.add_development_dependency 'rake'
27
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sprockets-webp-exporter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.rc1
5
+ platform: ruby
6
+ authors:
7
+ - Alex Semyonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sprockets
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0.beta8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0.beta8
27
+ - !ruby/object:Gem::Dependency
28
+ name: webp-ffi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Sprockets 4 exporter of PNG and JPEG assets to WebP
70
+ email:
71
+ - alex@semyonov.us
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/sprockets-webp-exporter.rb
82
+ - lib/sprockets/exporters/webp_exporter.rb
83
+ - lib/sprockets/webp/railtie.rb
84
+ - lib/sprockets/webp/version.rb
85
+ - sprockets-webp-exporter.gemspec
86
+ homepage: https://github.com/alsemyonov/sprockets-webp-exporter
87
+ licenses:
88
+ - MIT
89
+ metadata: {}
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: 1.9.3
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">"
102
+ - !ruby/object:Gem::Version
103
+ version: 1.3.1
104
+ requirements: []
105
+ rubygems_version: 3.0.1
106
+ signing_key:
107
+ specification_version: 4
108
+ summary: Sprockets 4 exporter of PNG and JPEG assets to WebP
109
+ test_files: []