Skip to content

Commit 60e1775

Browse files
authored
Merge pull request #103 from dscafati/master
Added webp support for the sharp adapter
2 parents 2c5fee5 + a720a17 commit 60e1775

File tree

9 files changed

+413
-912
lines changed

9 files changed

+413
-912
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ test/build
44
test/**/build/*.js
55
test/**/build/**/*.png
66
test/**/build/**/*.jpg
7+
test/**/build/**/*.webp
78
.node-version
89
/lib

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ os: osx
22
language: node_js
33
cache: yarn
44
node_js:
5-
- '8'
6-
- '6'
5+
- '10'

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ ReactDOM.render(
122122
| `min` | `integer` | | As an alternative to manually specifying `sizes`, you can specify `min`, `max` and `steps`, and the sizes will be generated for you. |
123123
| `max` | `integer` | | See `min` above |
124124
| `steps` | `integer` |`4` | Configure the number of images generated between `min` and `max` (inclusive) |
125-
| `quality` | `integer` | `85` | JPEG compression quality |
126-
| `format` | `string` | *original format* | Either `png` or `jpg`; use to convert to another format |
125+
| `quality` | `integer` | `85` | JPEG and WEBP compression quality |
126+
| `format` | `string` | *original format* | Either `png` or `jpg`; use to convert to another format. `webp` is also supported, but only by the sharp adapter |
127127
| `placeholder` | `boolean` | `false` | A true or false value to specify wether to output a placeholder image as a data URI |
128128
| `placeholderSize` | `integer` | `40` | A number value specifying the width of the placeholder image, if enabled with the option above |
129129
| `adapter` | `Adapter` | JIMP | Specify which adapter to use. Can only be specified in the loader options. |
@@ -139,6 +139,8 @@ ReactDOM.render(
139139

140140
- `background: string` — Background fill when converting transparent to opaque images. E.g. `#FFFFFF`
141141

142+
- `format: webp` — Conversion to the `image/webp` format. Recognizes the `quality` option.
143+
142144

143145
### Examples
144146

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "A webpack loader for responsive images",
55
"main": "lib/index.js",
66
"engines": {
7-
"node": ">=4"
7+
"node": ">=10"
88
},
99
"scripts": {
1010
"clean-test": "rm -f test/**/build/*.jpg test/**/build/*.png test/**/build/**/*.jpg test/**/build/**/*.png test/**/build/test.js",
@@ -50,7 +50,7 @@
5050
"flow-bin": "^0.85.0",
5151
"jest": "^20.0.4",
5252
"jimp": "^0.2.21",
53-
"sharp": "^0.18.2",
53+
"sharp": "^0.23.2",
5454
"webpack": "^3.1.0"
5555
},
5656
"jest": {

src/adapters/sharp.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,22 @@ module.exports = (imagePath: string) => {
1313
.resize(width, null);
1414

1515
if (options.background) {
16-
resized = resized.background(options.background)
17-
.flatten();
16+
resized = resized
17+
.flatten({
18+
background: options.background
19+
});
1820
}
1921

2022
if (mime === 'image/jpeg') {
2123
resized = resized.jpeg({
2224
quality: options.quality
2325
});
2426
}
27+
if (mime === 'image/webp') {
28+
resized = resized.webp({
29+
quality: options.quality
30+
});
31+
}
2532

2633
resized.toBuffer((err, data, {height}) => {
2734
if (err) {

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ const loaderUtils = require('loader-utils');
66
const MIMES = {
77
'jpg': 'image/jpeg',
88
'jpeg': 'image/jpeg',
9-
'png': 'image/png'
9+
'png': 'image/png',
10+
'webp': 'image/webp'
1011
};
1112

1213
const EXTS = {
1314
'image/jpeg': 'jpg',
14-
'image/png': 'png'
15+
'image/png': 'png',
16+
'image/webp': 'webp'
1517
};
1618

1719
type Config = {
@@ -29,7 +31,7 @@ type Config = {
2931
background: string | number | void,
3032
placeholder: string | boolean | void,
3133
adapter: ?Function,
32-
format: 'png' | 'jpg' | 'jpeg',
34+
format: 'png' | 'jpg' | 'jpeg' | 'webp',
3335
disable: ?boolean,
3436
};
3537

@@ -71,7 +73,7 @@ module.exports = function loader(content: Buffer) {
7173
const outputContext: string = config.context || this.rootContext || this.options && this.options.context;
7274
const outputPlaceholder: boolean = Boolean(config.placeholder) || false;
7375
const placeholderSize: number = parseInt(config.placeholderSize, 10) || 40;
74-
// JPEG compression
76+
// JPEG and WEBP compression
7577
const quality: number = parseInt(config.quality, 10) || 85;
7678
// Useful when converting from PNG to JPG
7779
const background: string | number | void = config.background;

0 commit comments

Comments
 (0)