Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
feat!: use system cjpeg if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
aaharu committed Apr 24, 2020
1 parent 4fcc153 commit a0a1513
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 69 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
2 changes: 0 additions & 2 deletions .github/funding.yml

This file was deleted.

3 changes: 0 additions & 3 deletions .github/security.md

This file was deleted.

28 changes: 28 additions & 0 deletions .github/workflows/nodejs.yml
@@ -0,0 +1,28 @@
name: Node CI

on: [push]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x]

steps:
- uses: actions/checkout@v1
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: yarn install, and test
run: yarn && yarn test && yarn coverage
env:
CI: true
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage/lcov.info
4 changes: 3 additions & 1 deletion .gitignore
@@ -1,2 +1,4 @@
node_modules
node_modules/
.nyc_output/
coverage/
yarn.lock
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

11 changes: 6 additions & 5 deletions index.js
@@ -1,19 +1,19 @@
'use strict';
const execa = require('execa');
const isJpg = require('is-jpg');
const mozjpeg = require('mozjpeg');
const mozjpeg = require('@mole-inc/mozjpeg');

module.exports = options => buffer => {
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'));
throw new TypeError('Expected a buffer');
}

if (!isJpg(buffer)) {
return Promise.resolve(buffer);
return buffer;
}

const args = [];
Expand Down Expand Up @@ -86,9 +86,10 @@ module.exports = options => buffer => {
args.push('-sample', options.sample.join(','));
}

return execa.stdout(mozjpeg, args, {
const {stdout} = await execa(mozjpeg, args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
});
return stdout;
};
4 changes: 1 addition & 3 deletions license
@@ -1,6 +1,4 @@
MIT License

Copyright (c) Imagemin
Copyright 2020-present MOLE inc. and its affiliates.

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:

Expand Down
44 changes: 20 additions & 24 deletions package.json
@@ -1,30 +1,22 @@
{
"name": "imagemin-mozjpeg",
"name": "@mole-inc/imagemin-mozjpeg",
"version": "8.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"
"repository": {
"type": "git",
"url": "https://github.com/mole-inc/imagemin-mozjpeg.git"
},
"maintainers": [
{
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
{
"name": "Shinnosuke Watanabe",
"url": "github.com/shinnn"
}
],
"engines": {
"node": ">=6"
"node": ">=10.13.0"
},
"scripts": {
"test": "xo && ava"
"test": "xo && nyc ava",
"coverage": "nyc report --reporter=lcov",
"release": "standard-version"
},
"publishConfig": {
"access": "public"
},
"files": [
"index.js"
Expand All @@ -41,14 +33,18 @@
"optimize"
],
"dependencies": {
"execa": "^1.0.0",
"execa": "^4.0.0",
"is-jpg": "^2.0.0",
"mozjpeg": "^6.0.0"
"@mole-inc/mozjpeg": "^7.0.2"
},
"devDependencies": {
"ava": "^1.0.1",
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"ava": "^3.7.0",
"husky": "^4.2.5",
"is-progressive": "^3.0.0",
"pify": "^4.0.0",
"xo": "^0.23.0"
"nyc": "^15.0.1",
"standard-version": "^7.1.0",
"xo": "^0.29.1"
}
}
14 changes: 8 additions & 6 deletions readme.md
@@ -1,20 +1,20 @@
# imagemin-mozjpeg [![Build Status](https://travis-ci.org/imagemin/imagemin-mozjpeg.svg?branch=master)](https://travis-ci.org/imagemin/imagemin-mozjpeg)
# imagemin-mozjpeg ![Node CI](https://github.com/mole-inc/imagemin-mozjpeg/workflows/Node%20CI/badge.svg)

> [Imagemin](https://github.com/imagemin/imagemin) plugin for [mozjpeg](https://github.com/mozilla/mozjpeg)
> [Imagemin](https://github.com/imagemin/imagemin) plugin for [mozjpeg](https://github.com/mole-inc/mozjpeg)

## Install

```
$ npm install imagemin-mozjpeg
$ npm install @mole-inc/imagemin-mozjpeg
```


## Usage

```js
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
const imageminMozjpeg = require('@mole-inc/imagemin-mozjpeg');

(async () => {
await imagemin(['images/*.jpg'], 'build/images', {
Expand All @@ -30,7 +30,7 @@ const imageminMozjpeg = require('imagemin-mozjpeg');

## API

### imageminMozjpeg([options])(buffer)
### imageminMozjpeg(options?)(buffer)

Returns a `Promise<Buffer>`.

Expand Down Expand Up @@ -174,4 +174,6 @@ Buffer to optimize.

## License

MIT © [Imagemin](https://github.com/imagemin)
This is a fork of [imagemin/imagemin-mozjpeg](https://github.com/imagemin/imagemin-mozjpeg).

see license file.
26 changes: 13 additions & 13 deletions test.js
@@ -1,15 +1,15 @@
import fs from 'fs';
import path from 'path';
import isJpg from 'is-jpg';
import isProgressive from 'is-progressive';
import pify from 'pify';
import test from 'ava';
import m from '.';
const fs = require('fs');
const path = require('path');
const isJpg = require('is-jpg');
const isProgressive = require('is-progressive');
const test = require('ava');
const {promisify} = require('util');

const fsP = pify(fs);
const m = require('.');
const readFileP = promisify(fs.readFile);

test('optimize a JPG', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixture.jpg'));
const buf = await readFileP(path.join(__dirname, 'fixture.jpg'));
const data = await m()(buf);

t.true(data.length < buf.length);
Expand All @@ -18,22 +18,22 @@ test('optimize a JPG', async t => {
});

test('support mozjpeg options', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixture.jpg'));
const buf = await readFileP(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 fsP.readFile(__filename);
const buf = await readFileP(__filename);
const data = await m()(buf);

t.deepEqual(data, buf);
});

test('throw error when a JPG is corrupt', async t => {
const buf = await fsP.readFile(path.join(__dirname, 'fixture-corrupt.jpg'));
const buf = await readFileP(path.join(__dirname, 'fixture-corrupt.jpg'));
await t.throwsAsync(async () => {
await m()(buf);
}, /Corrupt JPEG data/);
}, {instanceOf: Error, message: /Corrupt JPEG data/});
});

0 comments on commit a0a1513

Please sign in to comment.