Commit 3688613f authored by rockyl's avatar rockyl

init

parent 0675bea6
......@@ -55,7 +55,7 @@ export interface Options {
/**
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.
......
'use strict';
const execa = require('execa');
const isPng = require('is-png');
const path = require('path');
const isStream = require('is-stream');
//const pngquant = require('pngquant-bin');
const pngquant = require('pngquant-bin');
const ow = require('ow');
const pngquant = path.join(__dirname, 'vendor/pngquant');
const imageminPngquant = (options = {}) => input => {
const isBuffer = Buffer.isBuffer(input);
......@@ -65,10 +63,16 @@ const imageminPngquant = (options = {}) => input => {
const promise = subprocess
.then(result => result.stdout) // eslint-disable-line promise/prefer-await-to-then
.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;
}
if(error.code === 'EPIPE'){
return error.stdout;
}
error.message = error.stderr || error.message;
throw error;
});
......
......@@ -3,10 +3,12 @@ import * as path from 'path';
import {expectType} from 'tsd';
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));
expectType<Buffer>(await imageminPngquant({
(async () => {
expectType<Buffer>(await imageminPngquant()(buffer));
expectType<Buffer>(await imageminPngquant({
speed: 10,
quality: [0.8, 1]
})(buffer));
})(buffer));
})();
{
"name": "imagemin-pngquant",
"version": "8.0.0",
"version": "9.0.1",
"description": "Imagemin plugin for `pngquant`",
"license": "MIT",
"repository": "imagemin/imagemin-pngquant",
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
......@@ -24,16 +24,17 @@
"pngquant"
],
"dependencies": {
"execa": "^1.0.0",
"execa": "^4.0.0",
"is-png": "^2.0.0",
"is-stream": "^2.0.0",
"ow": "^0.13.2"
"ow": "^0.17.0",
"pngquant-bin": "^6.0.0"
},
"devDependencies": {
"@types/node": "^12.0.3",
"ava": "^1.0.1",
"ava": "^3.8.0",
"get-stream": "^5.1.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
"tsd": "^0.11.0",
"xo": "^0.30.0"
}
}
......@@ -17,7 +17,8 @@ const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
await imagemin(['images/*.png'], 'build/images', {
await imagemin(['images/*.png'], {
destination: 'build/images',
plugins: [
imageminPngquant()
]
......
import fs from 'fs';
import path from 'path';
import test from 'ava';
import getStream from 'get-stream';
import isPng from 'is-png';
import imageminPngquant from '.';
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 imageminPngquant = require('.');
const readFile = promisify(fs.readFile);
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);
t.true(data.length < buffer.length);
t.true(isPng(data));
});
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({
speed: 10,
quality: [0.8, 1]
......@@ -23,7 +26,7 @@ test('support pngquant options', 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 data = await getStream.buffer(imageminPngquant()(stream));
t.true(data.length < buffer.length);
......@@ -31,14 +34,7 @@ test('support streams', 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);
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