init OxiPNG plugin for Imagemin

This commit is contained in:
Sun Liang
2020-08-03 21:29:27 +08:00
commit 4663adbe08
16 changed files with 255 additions and 0 deletions

12
.editorconfig Normal file
View File

@@ -0,0 +1,12 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space
indent_size = 2

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
* text=auto eol=lf

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
node_modules
yarn.lock

1
.npmrc Normal file
View File

@@ -0,0 +1 @@
package-lock=false

8
.travis.yml Normal file
View File

@@ -0,0 +1,8 @@
os:
- linux
- windows
language: node_js
node_js:
- '14'
- '12'
- '10'

BIN
fixture.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 KiB

27
index.d.ts vendored Normal file
View File

@@ -0,0 +1,27 @@
export interface Options {
/**
Optimization: -o 1 through -o 6, lower is faster, higher is better compression. The default (-o 2) is sufficiently fast on a modern CPU and provides 30-50% compression gains over an unoptimized PNG. -o 4 is 6 times slower than -o 2 but can provide 5-10% extra compression over -o 2. Using any setting higher than -o 4 is unlikely to give any extra compression gains and is not recommended.
Values: `1` (brute-force) to `6` (fastest)
@default 2
*/
quality?: number;
/**
Remove optional metadata.
*/
strip?: 'safe' | 'all';
}
/**
Buffer or stream to optimize.
*/
export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise<Buffer>;
/**
Imagemin plugin for Oxipng.
@returns An Imagemin plugin.
*/
export default function imageminOxipng(options?: Options): Plugin;

55
index.js Normal file
View File

@@ -0,0 +1,55 @@
'use strict';
const execa = require('execa');
const isPng = require('is-png');
const isStream = require('is-stream');
const oxipng = require('./oxipng-bin');
const ow = require('ow');
const imageminOxipng = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input);
if (!isBuffer && !isStream(input)) {
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof input}`));
}
if (isBuffer && !isPng(input)) {
return Promise.resolve(input);
}
const args = ['-'];
if (typeof options.strip !== 'undefined') {
ow(options.strip, ow.string);
args.push('--strip', options.strip);
}
if (typeof options.quality !== 'undefined') {
ow(options.quality, ow.number.integer.inRange(1, 6));
args.push('-o', options.quality);
}
const subprocess = execa(oxipng, args, {
encoding: null,
maxBuffer: Infinity,
input
});
const promise = subprocess
.then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then
.catch(error => {
if (error.code === 99) {
return input;
}
error.message = error.stderr || error.message;
throw error;
});
subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line promise/prefer-await-to-then
subprocess.stdout.catch = promise.catch.bind(promise);
return subprocess.stdout;
};
module.exports = imageminOxipng;
module.exports.default = imageminOxipng;

13
index.test-d.ts Normal file
View File

@@ -0,0 +1,13 @@
import * as fs from 'fs';
import * as path from 'path';
import {expectType} from 'tsd';
import imageminOxipng from '.';
const buffer = fs.readFileSync(path.join(__dirname, 'fixture.png'));
(async () => {
expectType<Buffer>(await imageminOxipng()(buffer));
expectType<Buffer>(await imageminOxipng({
quality: 2,
})(buffer));
})();

9
license Normal file
View File

@@ -0,0 +1,9 @@
MIT License
Copyright (c) Imagemin
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

8
oxipng-bin/index.js Normal file
View File

@@ -0,0 +1,8 @@
'use strict';
const path = require('path');
const BinWrapper = require('bin-wrapper');
module.exports = new BinWrapper()
.dest(path.resolve(__dirname, './vendor'))
.use(process.platform === 'win32' ? 'oxipng.exe' : 'oxipng')
.path();

BIN
oxipng-bin/vendor/oxipng vendored Executable file

Binary file not shown.

BIN
oxipng-bin/vendor/oxipng.exe vendored Normal file

Binary file not shown.

42
package.json Normal file
View File

@@ -0,0 +1,42 @@
{
"name": "imagemin-oxipng",
"version": "1.0.0",
"description": "Imagemin plugin for `oxipng`",
"license": "MIT",
"repository": "imagemin/imagemin-oxipng",
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"compress",
"image",
"imageminplugin",
"minify",
"optimize",
"png",
"pngquant"
],
"dependencies": {
"execa": "^4.0.0",
"is-png": "^2.0.0",
"is-stream": "^2.0.0",
"ow": "^0.17.0",
"bin-build": "^3.0.0",
"bin-wrapper": "^4.0.1",
"logalot": "^2.0.0"
},
"devDependencies": {
"@types/node": "^12.0.3",
"ava": "^3.8.0",
"get-stream": "^5.1.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
}
}

38
readme.md Normal file
View File

@@ -0,0 +1,38 @@
# OxiPNG plugin for Imagemin
## Usage
```js
const imagemin = require('imagemin');
const imageminOxipng = require('imagemin-oxipng');
(async () => {
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminOxipng()
]
});
console.log('Images optimized');
})();
```
## API
### imageminOxipng(options?)(input)
Returns `Promise<Buffer>`.
#### options
Type: `object`
##### quality
Type: `number`<br>
Default: `2`<br>
Values: `1` (faster) to `6` (better compression)
Optimization: -o 1 through -o 6, lower is faster, higher is better compression. The default (-o 2) is sufficiently fast on a modern CPU and provides 30-50% compression gains over an unoptimized PNG. -o 4 is 6 times slower than -o 2 but can provide 5-10% extra compression over -o 2. Using any setting higher than -o 4 is unlikely to give any extra compression gains and is not recommended.

39
test.js Normal file
View File

@@ -0,0 +1,39 @@
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const test = require('ava');
const getStream = require('get-stream');
const isPng = require('is-png');
const imageminOxipng = require('.');
const readFile = promisify(fs.readFile);
test('optimize a PNG', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const data = await imageminOxipng()(buffer);
t.true(data.length < buffer.length);
t.true(isPng(data));
});
test('support oxipng options', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const data = await imageminOxipng({
quality: 1,
})(buffer);
t.true(data.length > 30000);
t.true(isPng(data));
});
test('support streams', async t => {
const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const stream = fs.createReadStream(path.join(__dirname, 'fixture.png'));
const data = await getStream.buffer(imageminOxipng()(stream));
t.true(data.length < buffer.length);
t.true(isPng(data));
});
test('skip optimizing a non-PNG file', async t => {
const buffer = await readFile(__filename);
const data = await imageminOxipng()(buffer);
t.is(data.length, buffer.length);
});