Skip to content

Commit

Permalink
Move to ESM (#84)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <[email protected]>
  • Loading branch information
localnerve and sindresorhus authored May 7, 2024
1 parent 76dc84a commit 5ca38bd
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 121 deletions.
5 changes: 5 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"reporter": ["lcov", "json"],
"reports-dir": "coverage",
"exclude": ["tmp", "coverage", "node_modules", ".github"]
}
30 changes: 17 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,31 @@ name: test
on:
push:
branches:
- master
- main
pull_request:
branches:
- master
- main

jobs:
linux:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
- name: Install Prerequisites
run: sudo apt-get -y install libpng-dev libimagequant-dev
- name: Npm Install
run: npm install
- name: Run Test
run: npm test
env:
CI: true

Expand All @@ -32,12 +36,12 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand All @@ -50,12 +54,12 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [16.x, 18.x, 20.x]

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
coverage
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface Options {
export interface Options { // eslint-disable-line @typescript-eslint/consistent-type-definitions
/**
Speed `10` has 5% lower quality, but is about 8 times faster than the default. Speed `11` disables dithering and lowers compression level.
Expand Down
140 changes: 70 additions & 70 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
'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);
import {execa} from 'execa';
import isPng from 'is-png';
import {isStream} from 'is-stream';
import pngquant from 'pngquant-bin';
import ow from 'ow';

export function imageminPngquant(options = {}) {
return input => {
const isBuffer = Buffer.isBuffer(input);

Check failure on line 9 in index.js

View workflow job for this annotation

GitHub Actions / macos (16.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 9 in index.js

View workflow job for this annotation

GitHub Actions / linux (18.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 9 in index.js

View workflow job for this annotation

GitHub Actions / linux (20.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 9 in index.js

View workflow job for this annotation

GitHub Actions / windows (20.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

if (!isBuffer && !isStream(input)) {
return Promise.reject(new TypeError(`Expected a Buffer or Stream, got ${typeof 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);
}

if (isBuffer && !isPng(input)) {
return Promise.resolve(input);
}
const args = ['-'];

const args = ['-'];
if (options.speed !== undefined) {
ow(options.speed, ow.number.integer.inRange(1, 11));
args.push('--speed', options.speed);
}

if (typeof options.speed !== 'undefined') {
ow(options.speed, ow.number.integer.inRange(1, 11));
args.push('--speed', options.speed);
}
if (options.strip !== undefined) {
ow(options.strip, ow.boolean);

if (typeof options.strip !== 'undefined') {
ow(options.strip, ow.boolean);
if (options.strip) {
args.push('--strip');
}
}

if (options.strip) {
args.push('--strip');
if (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.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 (options.dithering !== undefined) {
ow(options.dithering, ow.any(ow.number.inRange(0, 1), ow.boolean.false));

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.dithering === 'number') {
args.push(`--floyd=${options.dithering}`);
} else if (options.dithering === false) {
args.push('--ordered');
if (options.posterize !== undefined) {
ow(options.posterize, ow.number);
args.push('--posterize', options.posterize);
}

if (options.verbose !== undefined) {
ow(options.verbose, ow.boolean);
args.push('--verbose');
}
}

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 => {
// 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;
}

error.message = error.stderr || error.message;
throw error;
const subprocess = execa(pngquant, args, {
encoding: null,
maxBuffer: Number.POSITIVE_INFINITY,
input,
});

subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line promise/prefer-await-to-then
subprocess.stdout.catch = promise.catch.bind(promise);
const promise = subprocess
.then(result => result.stdout)
.catch(error => {
// 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;
}

error.message = error.stderr || error.message;
throw error;
});

subprocess.stdout.then = promise.then.bind(promise); // eslint-disable-line unicorn/no-thenable
subprocess.stdout.catch = promise.catch.bind(promise);

return subprocess.stdout;
};
return subprocess.stdout;
};
}

module.exports = imageminPngquant;
module.exports.default = imageminPngquant;
export {imageminPngquant as default};
22 changes: 13 additions & 9 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as fs from 'fs';
import * as path from 'path';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as url from 'node:url';
import {expectType} from 'tsd';
import imageminPngquant from '.';
import pngquant from './index.js';

const buffer = fs.readFileSync(path.join(__dirname, 'fixture.png'));
const thisDirname = url.fileURLToPath(new URL('.', import.meta.url));
const buffer = fs.readFileSync(path.join(thisDirname, 'fixture.png'));

(async () => {
expectType<Buffer>(await imageminPngquant()(buffer));
expectType<Buffer>(await imageminPngquant({
async function test() {
expectType<Buffer>(await pngquant()(buffer));

Check failure on line 11 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / macos (16.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 11 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / linux (18.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 11 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / linux (20.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 11 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / windows (20.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.
expectType<Buffer>(await pngquant({

Check failure on line 12 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / macos (16.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 12 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / linux (18.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 12 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / linux (20.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.

Check failure on line 12 in index.test-d.ts

View workflow job for this annotation

GitHub Actions / windows (20.x)

Unexpected use of the global variable 'Buffer'. Use 'require("buffer").Buffer' instead.
speed: 10,
quality: [0.8, 1]
quality: [0.8, 1],
})(buffer));
})();
}

await test();
31 changes: 19 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
"description": "Imagemin plugin for `pngquant`",
"license": "MIT",
"repository": "imagemin/imagemin-pngquant",
"type": "module",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": ">=10"
"node": ">=16"
},
"scripts": {
"test": "xo && ava && tsd"
"test": "xo && npm run test:cover && tsd",
"test:cover": "c8 --check-coverage --statements 90 ava"
},
"files": [
"index.js",
Expand All @@ -24,17 +30,18 @@
"pngquant"
],
"dependencies": {
"execa": "^4.0.0",
"is-png": "^2.0.0",
"is-stream": "^2.0.0",
"ow": "^0.17.0",
"pngquant-bin": "^6.0.0"
"execa": "^8.0.1",
"is-png": "^3.0.1",
"is-stream": "^3.0.0",
"ow": "^1.1.1",
"pngquant-bin": "^8.0.1"
},
"devDependencies": {
"@types/node": "^12.0.3",
"ava": "^3.8.0",
"get-stream": "^5.1.0",
"tsd": "^0.11.0",
"xo": "^0.30.0"
"@types/node": "^20.8.3",
"c8": "^8.0.1",
"ava": "^5.3.1",
"get-stream": "^6.0.1",
"tsd": "^0.29.0",
"xo": "^0.56.0"
}
}
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# imagemin-pngquant ![GitHub Actions Status](https://github.com/imagemin/imagemin-pngquant/workflows/test/badge.svg?branch=master)
# imagemin-pngquant

> [Imagemin](https://github.com/imagemin/imagemin) plugin for [`pngquant`](https://github.com/kornelski/pngquant)
Expand All @@ -8,7 +8,11 @@
```
$ npm install imagemin-pngquant
```

### Prerequisites
> **Linux** machines must have the following packages prior to install: `libpng-dev libimagequant-dev`
```
sudo apt-get -y install libpng-dev libimagequant-dev
```

## Usage

Expand Down
Loading

0 comments on commit 5ca38bd

Please sign in to comment.