bundle bin inside

This commit is contained in:
Sun Liang
2020-08-04 13:08:36 +08:00
commit 3bf6c9f85b
17 changed files with 435 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

2
.gitattributes vendored Normal file
View File

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

2
.github/funding.yml vendored Normal file
View File

@@ -0,0 +1,2 @@
github: sindresorhus
tidelift: npm/imagemin-mozjpeg

3
.github/security.md vendored Normal file
View File

@@ -0,0 +1,3 @@
# Security Policy
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.

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

10
.travis.yml Normal file
View File

@@ -0,0 +1,10 @@
os:
- linux
- windows
- osx
osx_image: xcode9.3
language: node_js
node_js:
- '14'
- '12'
- '10'

BIN
fixture-corrupt.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 441 B

BIN
fixture.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

116
index.js Normal file
View File

@@ -0,0 +1,116 @@
'use strict';
const execa = require('execa');
const isJpg = require('is-jpg');
const mozjpeg = require('./mozjpeg');
module.exports = options => async buffer => {
options = {
trellis: true,
trellisDC: true,
overshoot: true,
...options
};
if (!Buffer.isBuffer(buffer)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
if (!isJpg(buffer)) {
return Promise.resolve(buffer);
}
// TODO: Remove these sometime far in the future
if (options.fastcrush) {
return Promise.reject(new Error('Option `fastcrush` was renamed to `fastCrush`'));
}
if (options.maxmemory) {
return Promise.reject(new Error('Option `maxmemory` was renamed to `maxMemory`'));
}
if (options.notrellis) {
return Promise.reject(new Error('Option `notrellis` was renamed to `trellis` and inverted'));
}
if (options.noovershoot) {
return Promise.reject(new Error('Option `noovershoot` was renamed to `overshoot` and inverted'));
}
const args = [];
if (typeof options.quality !== 'undefined') {
args.push('-quality', options.quality);
}
if (options.progressive === false) {
args.push('-baseline');
}
if (options.targa) {
args.push('-targa');
}
if (options.revert) {
args.push('-revert');
}
if (options.fastCrush) {
args.push('-fastcrush');
}
if (typeof options.dcScanOpt !== 'undefined') {
args.push('-dc-scan-opt', options.dcScanOpt);
}
if (!options.trellis) {
args.push('-notrellis');
}
if (!options.trellisDC) {
args.push('-notrellis-dc');
}
if (options.tune) {
args.push(`-tune-${options.tune}`);
}
if (!options.overshoot) {
args.push('-noovershoot');
}
if (options.arithmetic) {
args.push('-arithmetic');
}
if (options.dct) {
args.push('-dct', options.dct);
}
if (options.quantBaseline) {
args.push('-quant-baseline', options.quantBaseline);
}
if (typeof options.quantTable !== 'undefined') {
args.push('-quant-table', options.quantTable);
}
if (options.smooth) {
args.push('-smooth', options.smooth);
}
if (options.maxMemory) {
args.push('-maxmemory', options.maxMemory);
}
if (options.sample) {
args.push('-sample', options.sample.join(','));
}
const {stdout} = await execa(mozjpeg, args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
});
return stdout;
};

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
mozjpeg/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' ? 'cjpeg.exe' : 'cjpeg')
.path();

BIN
mozjpeg/vendor/cjpeg vendored Executable file

Binary file not shown.

BIN
mozjpeg/vendor/cjpeg.exe vendored Executable file

Binary file not shown.

53
package.json Normal file
View File

@@ -0,0 +1,53 @@
{
"name": "imagemin-mozjpeg",
"version": "9.0.0",
"description": "Imagemin plugin for mozjpeg",
"license": "MIT",
"repository": "imagemin/imagemin-mozjpeg",
"author": {
"name": "Kevin Mårtensson",
"email": "kevinmartensson@gmail.com",
"url": "github.com/kevva"
},
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
{
"name": "Shinnosuke Watanabe",
"url": "github.com/shinnn"
}
],
"engines": {
"node": ">=10"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"compress",
"image",
"imageminplugin",
"img",
"jpeg",
"jpg",
"minify",
"mozjpeg",
"optimize"
],
"dependencies": {
"execa": "^4.0.0",
"is-jpg": "^2.0.0",
"bin-wrapper": "^4.0.1"
},
"devDependencies": {
"ava": "^3.8.0",
"is-progressive": "^3.0.0",
"xo": "^0.30.0"
}
}

178
readme.md Normal file
View File

@@ -0,0 +1,178 @@
# imagemin-mozjpeg [![Build Status](https://travis-ci.org/imagemin/imagemin-mozjpeg.svg?branch=master)](https://travis-ci.org/imagemin/imagemin-mozjpeg)
> [Imagemin](https://github.com/imagemin/imagemin) plugin for [mozjpeg](https://github.com/mozilla/mozjpeg)
## Install
```
$ npm install imagemin-mozjpeg
```
## Usage
```js
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
(async () => {
await imagemin(['images/*.jpg'], {
destination: 'build/images',
plugins: [
imageminMozjpeg()
]
});
console.log('Images optimized');
})();
```
## API
### imageminMozjpeg([options])(buffer)
Returns a `Promise<Buffer>`.
#### options
##### quality
Type: `number`
Compression quality, in range `0` (worst) to `100` (perfect).
##### progressive
Type: `boolean`<br>
Default: `true`
`false` creates baseline JPEG file.
##### targa
Type: `boolean`<br>
Default: `false`
Input file is Targa format (usually not needed).
##### revert
Type: `boolean`<br>
Default: `false`
Revert to standard defaults instead of mozjpeg defaults.
##### fastCrush
Type: `boolean`<br>
Default: `false`
Disable progressive scan optimization.
##### dcScanOpt
Type: `number`<br>
Default: `1`
Set DC scan optimization mode.
- `0` One scan for all components
- `1` One scan per component
- `2` Optimize between one scan for all components and one scan for 1st component plus one scan for remaining components
##### trellis
Type: `boolean`<br>
Default: `true`
[Trellis optimization](https://en.wikipedia.org/wiki/Trellis_quantization).
##### trellisDC
Type: `boolean`<br>
Default: `true`
Trellis optimization of DC coefficients.
##### tune
Type: `string`<br>
Default: `hvs-psnr`
Set Trellis optimization method. Available methods: `psnr`, `hvs-psnr`, `ssim`, `ms-ssim`
##### overshoot
Type: `boolean`<br>
Default: `true`
Black-on-white deringing via overshoot.
##### arithmetic
Type: `boolean`<br>
Default: `false`
Use [arithmetic coding](https://en.wikipedia.org/wiki/Arithmetic_coding).
##### dct
Type: `string`<br>
Default: `int`
Set [DCT](https://en.wikipedia.org/wiki/Discrete_cosine_transform) method:
- `int` Use integer DCT
- `fast` Use fast integer DCT (less accurate)
- `float` Use floating-point DCT
##### quantBaseline
Type: `boolean`<br>
Default: `false`
Use 8-bit quantization table entries for baseline JPEG compatibility.
##### quantTable
Type: `number`
Use predefined quantization table.
- `0` JPEG Annex K
- `1` Flat
- `2` Custom, tuned for MS-SSIM
- `3` ImageMagick table by N. Robidoux
- `4` Custom, tuned for PSNR-HVS
- `5` Table from paper by Klein, Silverstein and Carney
##### smooth
Type: `number`
Set the strength of smooth dithered input. (1...100)
##### maxMemory
Type: `number`
Set the maximum memory to use in kilobytes.
##### sample
Type: `string[]`
Set component sampling factors. Each item should be in the format `HxV`, for example `2x1`.
#### buffer
Type: `buffer`
Buffer to optimize.
## License
MIT © [Imagemin](https://github.com/imagemin)

39
test.js Normal file
View File

@@ -0,0 +1,39 @@
const {promisify} = require('util');
const fs = require('fs');
const path = require('path');
const isJpg = require('is-jpg');
const isProgressive = require('is-progressive');
const test = require('ava');
const m = require('.');
const readFile = promisify(fs.readFile);
test('optimize a JPG', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m()(buf);
t.true(data.length < buf.length);
t.true(isJpg(data));
t.true(isProgressive.buffer(data));
});
test('support mozjpeg options', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m({progressive: false})(buf);
t.false(isProgressive.buffer(data));
});
test('skip optimizing a non-JPG file', async t => {
const buf = await readFile(__filename);
const data = await m()(buf);
t.deepEqual(data, buf);
});
test('throw error when a JPG is corrupt', async t => {
const buf = await readFile(path.join(__dirname, 'fixture-corrupt.jpg'));
await t.throwsAsync(async () => {
await m()(buf);
}, {message: /Corrupt JPEG data/});
});