Skip to content

Commit

Permalink
Require Node.js 8 and update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 25, 2019
1 parent f9e7141 commit ae2e246
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ os:
- windows
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
34 changes: 16 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@ const execBuffer = require('exec-buffer');
const isPng = require('is-png');
const optipng = require('optipng-bin');

module.exports = options => input => {
options = Object.assign({
module.exports = options => async buffer => {
options = {
optimizationLevel: 3,
bitDepthReduction: true,
colorTypeReduction: true,
paletteReduction: true
}, options);
paletteReduction: true,
...options
};

if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError('Expected a buffer'));
if (!Buffer.isBuffer(buffer)) {
throw new TypeError('Expected a buffer');
}

if (!isPng(input)) {
return Promise.resolve(input);
if (!isPng(buffer)) {
return buffer;
}

const args = [
const arguments_ = [
'-strip',
'all',
'-clobber',
Expand All @@ -31,25 +32,22 @@ module.exports = options => input => {
];

if (!options.bitDepthReduction) {
args.push('-nb');
arguments_.push('-nb');
}

if (!options.colorTypeReduction) {
args.push('-nc');
arguments_.push('-nc');
}

if (!options.paletteReduction) {
args.push('-np');
arguments_.push('-np');
}

args.push(execBuffer.input);
arguments_.push(execBuffer.input);

return execBuffer({
input,
input: buffer,
bin: optipng,
args
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
args: arguments_
});
};
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"license": "MIT",
"repository": "imagemin/imagemin-optipng",
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -17,19 +17,18 @@
"imageminplugin",
"compress",
"image",
"img",
"minify",
"optimize",
"optipng",
"png"
],
"dependencies": {
"exec-buffer": "^3.0.0",
"is-png": "^1.0.0",
"optipng-bin": "^5.0.0"
"is-png": "^2.0.0",
"optipng-bin": "^6.0.0"
},
"devDependencies": {
"ava": "^1.0.1",
"xo": "^0.23.0"
"ava": "^1.4.1",
"xo": "^0.24.0"
}
}
21 changes: 11 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,27 @@ $ npm install imagemin-optipng
const imagemin = require('imagemin');
const imageminOptipng = require('imagemin-optipng');

imagemin(['images/*.png'], 'build/images', {use: [imageminOptipng()]}).then(() => {
console.log('Images optimized');
});
(async () => {
await imagemin(['images/*.png'], 'build/images', {
use: [
imageminOptipng()
]
});

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


## API

### imageminOptipng([options])(buffer)

Returns a `Promise` for a `Buffer`.
Returns a `Promise<Buffer>`.

#### options

Type: `Object`
Type: `object`

##### optimizationLevel

Expand Down Expand Up @@ -77,8 +83,3 @@ Apply palette reduction.
Type: `Buffer`

Buffer to optimize.


## License

MIT © [imagemin](https://github.com/imagemin)

0 comments on commit ae2e246

Please sign in to comment.