bundle bin inside

This commit is contained in:
Sun Liang
2020-10-09 21:23:10 +08:00
commit 3dcdcfd595
17 changed files with 346 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'

7
cwebp-bin/index.js Normal file
View File

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

BIN
cwebp-bin/vendor/.DS_Store vendored Normal file

Binary file not shown.

BIN
cwebp-bin/vendor/cwebp vendored Executable file

Binary file not shown.

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

Binary file not shown.

BIN
fixtures/test-corrupt.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 776 B

BIN
fixtures/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB

86
index.js Normal file
View File

@@ -0,0 +1,86 @@
'use strict';
const execBuffer = require('exec-buffer');
const isCwebpReadable = require('is-cwebp-readable');
const cwebp = require('./cwebp-bin');
module.exports = (options = {}) => input => {
if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``));
}
if (!isCwebpReadable(input)) {
return Promise.resolve(input);
}
const args = [
'-quiet',
'-mt'
];
if (options.preset) {
args.push('-preset', options.preset);
}
if (options.quality) {
args.push('-q', options.quality);
}
if (options.alphaQuality) {
args.push('-alpha_q', options.alphaQuality);
}
if (options.method) {
args.push('-m', options.method);
}
if (options.size) {
args.push('-size', options.size);
}
if (options.sns) {
args.push('-sns', options.sns);
}
if (options.filter) {
args.push('-f', options.filter);
}
if (options.autoFilter) {
args.push('-af');
}
if (options.sharpness) {
args.push('-sharpness', options.sharpness);
}
if (options.lossless) {
args.push('-lossless');
}
if (options.nearLossless) {
args.push('-near_lossless', options.nearLossless);
}
if (options.crop) {
args.push('-crop', options.crop.x, options.crop.y, options.crop.width, options.crop.height);
}
if (options.resize) {
args.push('-resize', options.resize.width, options.resize.height);
}
if (options.metadata) {
args.push('-metadata', Array.isArray(options.metadata) ? options.metadata.join(',') : options.metadata);
}
args.push('-o', execBuffer.output, execBuffer.input);
return execBuffer({
args,
bin: cwebp,
input
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};

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.

51
package.json Normal file
View File

@@ -0,0 +1,51 @@
{
"name": "imagemin-webp",
"version": "6.0.0",
"description": "WebP imagemin plugin",
"license": "MIT",
"repository": "imagemin/imagemin-webp",
"author": {
"name": "Kevin Mårtensson",
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
}
],
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"compress",
"cwebp",
"image",
"imageminplugin",
"img",
"jpg",
"minify",
"optimize",
"png",
"tif",
"webp"
],
"dependencies": {
"exec-buffer": "^3.0.0",
"bin-wrapper": "^4.0.1",
"is-cwebp-readable": "^3.0.0"
},
"devDependencies": {
"ava": "^3.8.0",
"is-webp": "^1.0.0",
"xo": "^0.30.0"
}
}

141
readme.md Normal file
View File

@@ -0,0 +1,141 @@
# imagemin-webp [![Build Status](https://travis-ci.org/imagemin/imagemin-webp.svg?branch=master)](https://travis-ci.org/imagemin/imagemin-webp)
> WebP [imagemin](https://github.com/imagemin/imagemin) plugin
## Install
```
$ npm install imagemin-webp
```
## Usage
```js
const imagemin = require('imagemin');
const imageminWebp = require('imagemin-webp');
(async () => {
await imagemin(['images/*.{jpg,png}'], {
destination: 'build/images',
plugins: [
imageminWebp({quality: 50})
]
});
console.log('Images optimized');
})();
```
## API
### imageminWebp(options?)(buffer)
Returns a `Promise<Buffer>` with the optimized image.
#### options
Type: `object`
##### preset
Type: `string`<br>
Default: `default`
Preset setting, one of `default`, `photo`, `picture`, `drawing`, `icon` and `text`.
##### quality
Type: `number`<br>
Default: `75`
Set quality factor between `0` and `100`.
##### alphaQuality
Type: `number`<br>
Default: `100`
Set transparency-compression quality between `0` and `100`.
##### method
Type: `number`<br>
Default: `4`
Specify the compression method to use, between `0` (fastest) and `6` (slowest). This parameter controls the trade off between encoding speed and the compressed file size and quality.
##### size
Type: `number`<br>
Set target size in bytes.
##### sns
Type: `number`<br>
Default: `80`
Set the amplitude of spatial noise shaping between `0` and `100`.
##### filter
Type: `number`<br>
Set deblocking filter strength between `0` (off) and `100`.
##### autoFilter
Type: `boolean`<br>
Default: `false`<br>
Adjust filter strength automatically.
##### sharpness
Type: `number`<br>
Default: `0`
Set filter sharpness between `0` (sharpest) and `7` (least sharp).
##### lossless
Type: `boolean`<br>
Default: `false`
Encode images losslessly.
##### nearLossless
Type: `number`<br>
Default: `100`
Encode losslessly with an additional [lossy pre-processing step](https://groups.google.com/a/webmproject.org/forum/#!msg/webp-discuss/0GmxDmlexek/3ggyYsaYdFEJ), with a quality factor between `0` (maximum pre-processing) and `100` (same as `lossless`).
##### crop
Type: `object { x: number, y: number, width: number, height: number }`
Crop the image.
##### resize
Type: `object { width: number, height: number }`
Resize the image. Happens after `crop`.
##### metadata
Type: `string | string[]`<br>
Default: `none`<br>
Values: `all` `none` `exif` `icc` `xmp`
A list of metadata to copy from the input to the output if present.
#### buffer
Type: `Buffer`
Buffer to optimize.

28
test.js Normal file
View File

@@ -0,0 +1,28 @@
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const isWebP = require('is-webp');
const test = require('ava');
const imageminWebp = require('.');
const readFile = promisify(fs.readFile);
test('convert an image into a WebP', async t => {
const buf = await readFile(path.join(__dirname, 'fixtures/test.png'));
const data = await imageminWebp()(buf);
t.true(data.length < buf.length);
t.true(isWebP(data));
});
test('skip optimizing unsupported files', async t => {
const buf = await readFile(path.join(__dirname, 'fixtures/test-unsupported.bmp'));
const data = await imageminWebp()(buf);
t.deepEqual(data, buf);
});
test('throw error when an image is corrupt', async t => {
const buf = await readFile(path.join(__dirname, 'fixtures/test-corrupt.webp'));
await t.throwsAsync(() => imageminWebp()(buf), {message: /BITSTREAM_ERROR/});
});