Commit f9947f68 authored by rockyl's avatar rockyl

init

parents
Pipeline #193929 failed with stages
in 0 seconds
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
* text=auto eol=lf
node_modules
yarn.lock
package-lock=false
os:
- linux
- windows
language: node_js
node_js:
- '12'
- '10'
- '8'
fixture.png

70.2 KB

<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
export interface Options {
/**
Speed `10` has 5% lower quality, but is about 8 times faster than the default. Speed `11` disables dithering and lowers compression level.
Values: `1` (brute-force) to `11` (fastest)
@default 3
*/
speed?: number;
/**
Remove optional metadata.
@default false
*/
strip?: boolean;
/**
Instructs pngquant to use the least amount of colors required to meet or exceed the max quality. If conversion results in quality below the min quality the image won't be saved.
Min and max are numbers in range 0 (worst) to 1 (perfect), similar to JPEG.
Values: `[0...1, 0...1]`
@example [0.3, 0.5]
*/
quality?: [number, number];
/**
Set the dithering level using a fractional number between 0 (none) and 1 (full).
Pass in `false` to disable dithering.
Values: 0...1
@default 1
*/
dithering?: number | boolean;
/**
Truncate number of least significant bits of color (per channel).
Use this when image will be output on low-depth displays (e.g. 16-bit RGB). pngquant will make almost-opaque pixels fully opaque and will reduce amount of semi-transparent colors.
*/
posterize?: number;
/**
Print verbose status messages.
@default false
*/
verbose?: boolean;
}
/**
Buffer or stream to optimize.
*/
export type Plugin = (input: Buffer | NodeJS.ReadableStream) => Promise<Buffer>
/**
Imagemin plugin for pngquant.
@returns An Imagemin plugin.
*/
export default function imageminPngquant(options?: Options): Plugin;
'use strict';
const execa = require('execa');
const isPng = require('is-png');
const isStream = require('is-stream');
const pngquant = require('pngquant-bin');
const ow = require('ow');
const imageminPngquant = (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.speed !== 'undefined') {
ow(options.speed, ow.number.integer.inRange(1, 11));
args.push('--speed', options.speed);
}
if (typeof options.strip !== 'undefined') {
ow(options.strip, ow.boolean);
args.push('--strip');
}
if (typeof options.quality !== 'undefined') {
ow(options.quality, ow.array.length(2).ofType(ow.number.inRange(0, 1)));
const [min, max] = options.quality;
args.push('--quality', `${Math.round(min * 100)}-${Math.round(max * 100)}`);
}
if (typeof options.dithering !== 'undefined') {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));
if (typeof options.dithering === 'number') {
args.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
args.push('--ordered');
}
}
if (typeof options.posterize !== 'undefined') {
ow(options.posterize, ow.number);
args.push('--posterize', options.posterize);
}
if (typeof options.verbose !== 'undefined') {
ow(options.verbose, ow.boolean);
args.push('--verbose');
}
const subprocess = execa(pngquant, 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 = imageminPngquant;
module.exports.default = imageminPngquant;
import * as fs from 'fs';
import * as path from 'path';
import {expectType} from 'tsd';
import imageminPngquant from '.';
const buffer = await fs.readFileSync(path.join(__dirname, 'fixture.png'));
expectType<Buffer>(await imageminPngquant()(buffer));
expectType<Buffer>(await imageminPngquant({
speed: 10,
quality: [0.8, 1]
})(buffer));
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.
{
"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",
"pngquant-bin": "git+ssh://git@gitlab2.dui88.com:laoqifeng/pngquant-bin.git"
},
"devDependencies": {
"@types/node": "^12.0.3",
"ava": "^1.0.1",
"get-stream": "^5.1.0",
"tsd": "^0.7.3",
"xo": "^0.24.0"
}
}
# imagemin-pngquant [![Build Status](https://travis-ci.org/imagemin/imagemin-pngquant.svg?branch=master)](https://travis-ci.org/imagemin/imagemin-pngquant)
> [Imagemin](https://github.com/imagemin/imagemin) plugin for [`pngquant`](https://github.com/kornelski/pngquant)
## Install
```
$ npm install imagemin-pngquant
```
## Usage
```js
const imagemin = require('imagemin');
const imageminPngquant = require('imagemin-pngquant');
(async () => {
await imagemin(['images/*.png'], 'build/images', {
plugins: [
imageminPngquant()
]
});
console.log('Images optimized');
})();
```
## API
### imageminPngquant(options?)(input)
Returns `Promise<Buffer>`.
#### options
Type: `object`
##### speed
Type: `number`<br>
Default: `4`<br>
Values: `1` (brute-force) to `11` (fastest)
Speed `10` has 5% lower quality, but is about 8 times faster than the default. Speed `11` disables dithering and lowers compression level.
##### strip
Type: `boolean`<br>
Default: `false`
Remove optional metadata.
##### quality
Type: `Array<min: number, max: number>`<br>
Values: `Array<0...1, 0...1>`<br>
Example: `[0.3, 0.5]`
Instructs pngquant to use the least amount of colors required to meet or exceed
the max quality. If conversion results in quality below the min quality the
image won't be saved.
Min and max are numbers in range 0 (worst) to 1 (perfect), similar to JPEG.
##### dithering
Type: `number | boolean`<br>
Default: `1` (full)<br>
Values: `0...1`
Set the dithering level using a fractional number between 0 (none) and 1 (full).
Pass in `false` to disable dithering.
##### posterize
Type: `number`
Truncate number of least significant bits of color (per channel). Use this when image will be output on low-depth displays (e.g. 16-bit RGB). pngquant will make almost-opaque pixels fully opaque and will reduce amount of semi-transparent colors.
##### verbose
Type: `boolean`<br>
Default: `false`
Print verbose status messages.
#### input
Type: `Buffer | Stream`
Buffer or stream to optimize.
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 '.';
test('optimize a PNG', async t => {
const buffer = await fs.readFileSync(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 data = await imageminPngquant({
speed: 10,
quality: [0.8, 1]
})(buffer);
t.true(data.length > 30000);
t.true(isPng(data));
});
test('support streams', async t => {
const buffer = await fs.readFileSync(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);
t.true(isPng(data));
});
test('skip optimizing a non-PNG file', async t => {
const buffer = await fs.readFileSync(__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