Skip to content

Commit

Permalink
Use native ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Dec 17, 2021
1 parent 3835836 commit bcdf27d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 90 deletions.
77 changes: 19 additions & 58 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,64 +1,25 @@
name: test

on:
push:
branches:
- master
pull_request:
branches:
- master

- push
- pull_request
jobs:
linux:
runs-on: ubuntu-latest

test:
name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]

node-version:
- 16
- 14
- 12
os:
- ubuntu-latest
- macos-latest
- windows-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
env:
CI: true

macos:
runs-on: macos-latest

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

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
env:
CI: true

windows:
runs-on: windows-latest

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

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
env:
CI: true
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test
18 changes: 10 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';
const execBuffer = require('exec-buffer');
const isCwebpReadable = require('is-cwebp-readable');
const cwebp = require('cwebp-bin');
import {Buffer} from 'node:buffer';
import execBuffer from 'exec-buffer';
import isCwebpReadable from 'is-cwebp-readable';
import cwebp from 'cwebp-bin';

module.exports = (options = {}) => input => {
const imageminWebp = (options = {}) => input => {
if (!Buffer.isBuffer(input)) {
return Promise.reject(new TypeError(`Expected \`input\` to be of type \`Buffer\` but received type \`${typeof input}\``));
}
Expand All @@ -14,7 +14,7 @@ module.exports = (options = {}) => input => {

const args = [
'-quiet',
'-mt'
'-mt',
];

if (options.preset) {
Expand All @@ -33,7 +33,7 @@ module.exports = (options = {}) => input => {
args.push('-m', options.method);
}

if (options.size) {
if (options.size > 0) {
args.push('-size', options.size);
}

Expand Down Expand Up @@ -78,9 +78,11 @@ module.exports = (options = {}) => input => {
return execBuffer({
args,
bin: cwebp,
input
input,
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
});
};

export default imageminWebp;
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"description": "WebP imagemin plugin",
"license": "MIT",
"repository": "imagemin/imagemin-webp",
"type": "module",
"exports": "./index.js",
"author": {
"name": "Kevin Mårtensson",
"email": "kevinmartensson@gmail.com",
Expand All @@ -17,7 +19,7 @@
}
],
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -39,13 +41,13 @@
"webp"
],
"dependencies": {
"cwebp-bin": "^6.0.0",
"exec-buffer": "^3.0.0",
"cwebp-bin": "^7.0.1",
"exec-buffer": "^3.2.0",
"is-cwebp-readable": "^3.0.0"
},
"devDependencies": {
"ava": "^3.8.0",
"is-webp": "^1.0.0",
"xo": "^0.30.0"
"is-webp": "^2.0.0",
"xo": "^0.47.0"
}
}
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# imagemin-webp ![GitHub Actions Status](https://github.com/imagemin/imagemin-webp/workflows/test/badge.svg?branch=master)
# imagemin-webp ![GitHub Actions Status](https://github.com/imagemin/imagemin-webp/workflows/test/badge.svg?branch=main)

> WebP [imagemin](https://github.com/imagemin/imagemin) plugin
Expand All @@ -13,8 +13,8 @@ $ npm install imagemin-webp
## Usage

```js
const imagemin = require('imagemin');
const imageminWebp = require('imagemin-webp');
import imagemin from 'imagemin';
import imageminWebp from 'imagemin-webp';

(async () => {
await imagemin(['images/*.{jpg,png}'], {
Expand Down
28 changes: 12 additions & 16 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
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);
import {promises as fs} from 'node:fs';
import isWebP from 'is-webp';
import test from 'ava';
import imageminWebp from './index.js';

test('convert an image into a WebP', async t => {
const buf = await readFile(path.join(__dirname, 'fixtures/test.png'));
const data = await imageminWebp()(buf);
const buffer = await fs.readFile(new URL('fixtures/test.png', import.meta.url));
const data = await imageminWebp()(buffer);

t.true(data.length < buf.length);
t.true(data.length < buffer.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);
const buffer = await fs.readFile(new URL('fixtures/test-unsupported.bmp', import.meta.url));
const data = await imageminWebp()(buffer);

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

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/});
const buffer = await fs.readFile(new URL('fixtures/test-corrupt.webp', import.meta.url));
await t.throwsAsync(() => imageminWebp()(buffer), {message: /BITSTREAM_ERROR/});
});

0 comments on commit bcdf27d

Please sign in to comment.