Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva committed Oct 3, 2019
1 parent c0c4f60 commit 6bb71ff
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 110 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ os:
- windows
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
68 changes: 33 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,84 +3,82 @@ const execBuffer = require('exec-buffer');
const isCwebpReadable = require('is-cwebp-readable');
const cwebp = require('cwebp-bin');

module.exports = opts => buf => {
opts = Object.assign({}, opts);

if (!Buffer.isBuffer(buf)) {
return Promise.reject(new TypeError('Expected a buffer'));
module.exports = (options = {}) => input => {
if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``));
}

if (!isCwebpReadable(buf)) {
return Promise.resolve(buf);
if (!isCwebpReadable(input)) {
return Promise.resolve(input);
}

const args = [
'-quiet',
'-mt'
];

if (opts.preset) {
args.push('-preset', opts.preset);
if (options.preset) {
args.push('-preset', options.preset);
}

if (opts.quality) {
args.push('-q', opts.quality);
if (options.quality) {
args.push('-q', options.quality);
}

if (opts.alphaQuality) {
args.push('-alpha_q', opts.alphaQuality);
if (options.alphaQuality) {
args.push('-alpha_q', options.alphaQuality);
}

if (opts.method) {
args.push('-m', opts.method);
if (options.method) {
args.push('-m', options.method);
}

if (opts.size) {
args.push('-size', opts.size);
if (options.size) {
args.push('-size', options.size);
}

if (opts.sns) {
args.push('-sns', opts.sns);
if (options.sns) {
args.push('-sns', options.sns);
}

if (opts.filter) {
args.push('-f', opts.filter);
if (options.filter) {
args.push('-f', options.filter);
}

if (opts.autoFilter) {
if (options.autoFilter) {
args.push('-af');
}

if (opts.sharpness) {
args.push('-sharpness', opts.sharpness);
if (options.sharpness) {
args.push('-sharpness', options.sharpness);
}

if (opts.lossless) {
if (options.lossless) {
args.push('-lossless');
}

if (opts.nearLossless) {
args.push('-near_lossless', opts.nearLossless);
if (options.nearLossless) {
args.push('-near_lossless', options.nearLossless);
}

if (opts.crop) {
args.push('-crop', opts.crop.x, opts.crop.y, opts.crop.width, opts.crop.height);
if (options.crop) {
args.push('-crop', options.crop.x, options.crop.y, options.crop.width, options.crop.height);
}

if (opts.resize) {
args.push('-resize', opts.resize.width, opts.resize.height);
if (options.resize) {
args.push('-resize', options.resize.width, options.resize.height);
}

if (opts.metadata) {
args.push('-metadata', Array.isArray(opts.metadata) ? opts.metadata.join(',') : opts.metadata);
if (options.metadata) {
args.push('-metadata', Array.isArray(options.metadata) ? options.metadata.join(',') : options.metadata);
}

args.push('-o', execBuffer.output, execBuffer.input);

return execBuffer({
input: buf,
args,
bin: cwebp,
args
input
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
Expand Down
104 changes: 50 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
{
"name": "imagemin-webp",
"version": "5.1.0",
"description": "WebP imagemin plugin",
"license": "MIT",
"repository": "imagemin/imagemin-webp",
"author": {
"name": "Kevin Mårtensson",
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
{
"name": "Shinnosuke Watanabe",
"url": "github.com/shinnn"
}
],
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"compress",
"cwebp",
"image",
"imageminplugin",
"img",
"jpg",
"minify",
"optimize",
"png",
"tif",
"webp"
],
"dependencies": {
"cwebp-bin": "^5.0.0",
"exec-buffer": "^3.0.0",
"is-cwebp-readable": "^2.0.1"
},
"devDependencies": {
"ava": "^0.25.0",
"is-webp": "^1.0.0",
"pify": "^4.0.0",
"xo": "^0.23.0"
}
"name": "imagemin-webp",
"version": "5.1.0",
"description": "WebP imagemin plugin",
"license": "MIT",
"repository": "imagemin/imagemin-webp",
"author": {
"name": "Kevin Mårtensson",
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
}
],
"engines": {
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"compress",
"cwebp",
"image",
"imageminplugin",
"img",
"jpg",
"minify",
"optimize",
"png",
"tif",
"webp"
],
"dependencies": {
"cwebp-bin": "^5.0.0",
"exec-buffer": "^3.0.0",
"is-cwebp-readable": "^3.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"is-webp": "^1.0.0",
"pify": "^4.0.0",
"xo": "^0.23.0"
}
}
29 changes: 14 additions & 15 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,27 @@ $ npm install imagemin-webp
const imagemin = require('imagemin');
const imageminWebp = require('imagemin-webp');

imagemin(['images/*.{jpg,png}'], 'build/images', {
use: [
imageminWebp({quality: 50})
]
}).then(() => {
(async () => {
await imagemin(['images/*.{jpg,png}'], 'build/images', {
use: [
imageminWebp({quality: 50})
]
});

console.log('Images optimized');
});
})();
```


## API

### imageminWebp([options])(buffer)
### imageminWebp(options?)(buffer)

Returns a `Promise<Buffer>` with the optimized image.

#### options

Type: `Object`
Type: `object`

##### preset

Expand Down Expand Up @@ -111,13 +115,13 @@ Encode losslessly with an additional [lossy pre-processing step](https://groups.

##### crop

Type: `Object { x: number, y: number, width: number, height: number }`
Type: `object { x: number, y: number, width: number, height: number }`

Crop the image.

##### resize

Type: `Object { width: number, height: number }`
Type: `object { width: number, height: number }`

Resize the image. Happens after `crop`.

Expand All @@ -134,8 +138,3 @@ A list of metadata to copy from the input to the output if present.
Type: `Buffer`

Buffer to optimize.


## License

MIT © [Imagemin](https://github.com/imagemin)
8 changes: 4 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ import path from 'path';
import isWebP from 'is-webp';
import pify from 'pify';
import test from 'ava';
import m from '.';
import imageminWebp from '.';

const fsP = pify(fs);

test('convert an image into a WebP', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures/test.png'));
const data = await m()(buf);
const data = await imageminWebp()(buf);

t.true(data.length < buf.length);
t.true(isWebP(data));
});

test('skip optimizing unsupported files', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures/test-unsupported.bmp'));
const data = await m()(buf);
const data = await imageminWebp()(buf);

t.deepEqual(data, buf);
});

test('throw error when an image is corrupt', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixtures/test-corrupt.webp'));
await t.throws(m()(buf), /BITSTREAM_ERROR/);
await t.throws(imageminWebp()(buf), /BITSTREAM_ERROR/);
});

0 comments on commit 6bb71ff

Please sign in to comment.