Commit 3688613f authored by rockyl's avatar rockyl

init

parent 0675bea6
...@@ -55,7 +55,7 @@ export interface Options { ...@@ -55,7 +55,7 @@ export interface Options {
/** /**
Buffer or stream to optimize. Buffer or stream to optimize.
*/ */
export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise<Buffer> export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise<Buffer>;
/** /**
Imagemin plugin for pngquant. Imagemin plugin for pngquant.
......
'use strict'; 'use strict';
const execa = require('execa'); const execa = require('execa');
const isPng = require('is-png'); const isPng = require('is-png');
const path = require('path');
const isStream = require('is-stream'); const isStream = require('is-stream');
//const pngquant = require('pngquant-bin'); const pngquant = require('pngquant-bin');
const ow = require('ow'); const ow = require('ow');
const pngquant = path.join(__dirname, 'vendor/pngquant');
const imageminPngquant = (options = {}) => input => { const imageminPngquant = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input); const isBuffer = Buffer.isBuffer(input);
...@@ -65,10 +63,16 @@ const imageminPngquant = (options = {}) => input => { ...@@ -65,10 +63,16 @@ const imageminPngquant = (options = {}) => input => {
const promise = subprocess const promise = subprocess
.then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then .then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then
.catch(error => { .catch(error => {
if (error.code === 99) { // We use `error.exitCode` to check for a special condition when running the pngquant binary.
// See details on handling of "99" code at https://pngquant.org (search for "status code 99").
if (error.exitCode === 99) {
return input; return input;
} }
if(error.code === 'EPIPE'){
return error.stdout;
}
error.message = error.stderr || error.message; error.message = error.stderr || error.message;
throw error; throw error;
}); });
......
...@@ -3,10 +3,12 @@ import * as path from 'path'; ...@@ -3,10 +3,12 @@ import * as path from 'path';
import {expectType} from 'tsd'; import {expectType} from 'tsd';
import imageminPngquant from '.'; import imageminPngquant from '.';
const buffer = await fs.readFileSync(path.join(__dirname, 'fixture.png')); const buffer = fs.readFileSync(path.join(__dirname, 'fixture.png'));
expectType<Buffer>(await imageminPngquant()(buffer)); (async () => {
expectType<Buffer>(await imageminPngquant({ expectType<Buffer>(await imageminPngquant()(buffer));
speed: 10, expectType<Buffer>(await imageminPngquant({
quality: [0.8, 1] speed: 10,
})(buffer)); quality: [0.8, 1]
})(buffer));
})();
{ {
"name": "imagemin-pngquant", "name": "imagemin-pngquant",
"version": "8.0.0", "version": "9.0.1",
"description": "Imagemin plugin for `pngquant`", "description": "Imagemin plugin for `pngquant`",
"license": "MIT", "license": "MIT",
"repository": "imagemin/imagemin-pngquant", "repository": "imagemin/imagemin-pngquant",
"engines": { "engines": {
"node": ">=8" "node": ">=10"
}, },
"scripts": { "scripts": {
"test": "xo && ava && tsd" "test": "xo && ava && tsd"
...@@ -24,16 +24,17 @@ ...@@ -24,16 +24,17 @@
"pngquant" "pngquant"
], ],
"dependencies": { "dependencies": {
"execa": "^1.0.0", "execa": "^4.0.0",
"is-png": "^2.0.0", "is-png": "^2.0.0",
"is-stream": "^2.0.0", "is-stream": "^2.0.0",
"ow": "^0.13.2" "ow": "^0.17.0",
"pngquant-bin": "^6.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^12.0.3", "@types/node": "^12.0.3",
"ava": "^1.0.1", "ava": "^3.8.0",
"get-stream": "^5.1.0", "get-stream": "^5.1.0",
"tsd": "^0.7.3", "tsd": "^0.11.0",
"xo": "^0.24.0" "xo": "^0.30.0"
} }
} }
...@@ -17,7 +17,8 @@ const imagemin = require('imagemin'); ...@@ -17,7 +17,8 @@ const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant'); const imageminPngquant = require('imagemin-pngquant');
(async () => { (async () => {
await imagemin(['images/*.png'], 'build/images', { await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [ plugins: [
imageminPngquant() imageminPngquant()
] ]
......
import fs from 'fs'; const {promisify} = require('util');
import path from 'path'; const fs = require('fs');
import test from 'ava'; const path = require('path');
import getStream from 'get-stream'; const test = require('ava');
import isPng from 'is-png'; const getStream = require('get-stream');
import imageminPngquant from '.'; const isPng = require('is-png');
const imageminPngquant = require('.');
const readFile = promisify(fs.readFile);
test('optimize a PNG', async t => { test('optimize a PNG', async t => {
const buffer = await fs.readFileSync(path.join(__dirname, 'fixture.png')); const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const data = await imageminPngquant()(buffer); const data = await imageminPngquant()(buffer);
t.true(data.length < buffer.length); t.true(data.length < buffer.length);
t.true(isPng(data)); t.true(isPng(data));
}); });
test('support pngquant options', async t => { test('support pngquant options', async t => {
const buffer = await fs.readFileSync(path.join(__dirname, 'fixture.png')); const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const data = await imageminPngquant({ const data = await imageminPngquant({
speed: 10, speed: 10,
quality: [0.8, 1] quality: [0.8, 1]
...@@ -23,7 +26,7 @@ test('support pngquant options', async t => { ...@@ -23,7 +26,7 @@ test('support pngquant options', async t => {
}); });
test('support streams', async t => { test('support streams', async t => {
const buffer = await fs.readFileSync(path.join(__dirname, 'fixture.png')); const buffer = await readFile(path.join(__dirname, 'fixture.png'));
const stream = fs.createReadStream(path.join(__dirname, 'fixture.png')); const stream = fs.createReadStream(path.join(__dirname, 'fixture.png'));
const data = await getStream.buffer(imageminPngquant()(stream)); const data = await getStream.buffer(imageminPngquant()(stream));
t.true(data.length < buffer.length); t.true(data.length < buffer.length);
...@@ -31,14 +34,7 @@ test('support streams', async t => { ...@@ -31,14 +34,7 @@ test('support streams', async t => {
}); });
test('skip optimizing a non-PNG file', async t => { test('skip optimizing a non-PNG file', async t => {
const buffer = await fs.readFileSync(__filename); const buffer = await readFile(__filename);
const data = await imageminPngquant()(buffer); const data = await imageminPngquant()(buffer);
t.is(data.length, buffer.length); t.is(data.length, buffer.length);
}); });
test('skip optimizing a fully optimized PNG', async t => {
const buffer = await fs.readFileSync(path.join(__dirname, 'fixture-no-compress.png'));
const data = await imageminPngquant({quality: [0.8, 1]})(buffer);
t.is(data.length, buffer.length);
t.true(isPng(data));
});
Arguments:
/usr/local/bin/node /Users/rockyl/.yarn/bin/yarn.js add http://gitlab2.dui88.com/laoqifeng/pngquant-bin.git
PATH:
/Users/rockyl/.cargo/bin:/Users/rockyl/.yarn/bin:/Users/rockyl/.config/yarn/global/node_modules/.bin:/Users/rockyl/bin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/go/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS:/Users/rockyl/WorkSpaces/pngquant-bin/node_modules/.bin:/Users/rockyl/WorkSpaces/iot/xtensa-lx106-elf/bin:/Applications/microchip/xc8/v2.05/bin
Yarn version:
1.17.3
Node version:
11.13.0
Platform:
darwin x64
Trace:
SyntaxError: /Users/rockyl/WorkSpaces/imagemin-pngquant/package.json: Unexpected token } in JSON at position 512
at JSON.parse (<anonymous>)
at /Users/rockyl/.yarn/lib/cli.js:1625:59
at Generator.next (<anonymous>)
at step (/Users/rockyl/.yarn/lib/cli.js:304:30)
at /Users/rockyl/.yarn/lib/cli.js:315:13
npm manifest:
{
"name": "imagemin-pngquant",
"version": "8.0.0",
"description": "Imagemin plugin for `pngquant`",
"license": "MIT",
"repository": "imagemin/imagemin-pngquant",
"engines": {
"node": ">=8"
},
"scripts": {
"test": "xo && ava && tsd"
},
"files": [
"index.js",
"index.d.ts"
],
"keywords": [
"compress",
"image",
"imageminplugin",
"minify",
"optimize",
"png",
"pngquant"
],
"dependencies": {
"execa": "^1.0.0",
"is-png": "^2.0.0",
"is-stream": "^2.0.0",
"ow": "^0.13.2",
},
"devDependencies": {
"@types/node": "^12.0.3",
"ava": "^1.0.1",
"get-stream": "^5.1.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
}
}
yarn manifest:
No manifest
Lockfile:
No lockfile
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment