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 16c0821 commit 849aee2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 92 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
16 changes: 9 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict';
const execa = require('execa');
const isJpg = require('is-jpg');
const mozjpeg = require('mozjpeg');
import {Buffer} from 'node:buffer';
import {execa} from 'execa';
import isJpg from 'is-jpg';
import mozjpeg from 'mozjpeg';

module.exports = options => async buffer => {
const imageminMozjpeg = options => async buffer => {
options = {
trellis: true,
trellisDC: true,
overshoot: true,
...options
...options,
};

if (!Buffer.isBuffer(buffer)) {
Expand Down Expand Up @@ -109,8 +109,10 @@ module.exports = options => async buffer => {
const {stdout} = await execa(mozjpeg, args, {
encoding: null,
input: buffer,
maxBuffer: Infinity
maxBuffer: Number.POSITIVE_INFINITY,
});

return stdout;
};

export default imageminMozjpeg;
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
"description": "Imagemin plugin for mozjpeg",
"license": "MIT",
"repository": "imagemin/imagemin-mozjpeg",
"type": "module",
"exports": "./index.js",
"author": {
"name": "Kevin Mårtensson",
"email": "[email protected]",
Expand All @@ -21,7 +23,7 @@
}
],
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -41,13 +43,13 @@
"optimize"
],
"dependencies": {
"execa": "^4.0.0",
"is-jpg": "^2.0.0",
"mozjpeg": "^7.0.0"
"execa": "^6.0.0",
"is-jpg": "^3.0.0",
"mozjpeg": "^8.0.0"
},
"devDependencies": {
"ava": "^3.8.0",
"ava": "^3.15.0",
"is-progressive": "^3.0.0",
"xo": "^0.30.0"
"xo": "^0.47.0"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ $ npm install imagemin-mozjpeg
## Usage

```js
const imagemin = require('imagemin');
const imageminMozjpeg = require('imagemin-mozjpeg');
import imagemin from 'imagemin';
import imageminMozjpeg from 'imagemin-mozjpeg';

(async () => {
await imagemin(['images/*.jpg'], {
Expand Down
34 changes: 15 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
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);
import {promises as fs} from 'node:fs';
import isJpg from 'is-jpg';
import isProgressive from 'is-progressive';
import test from 'ava';
import imageminMozjpeg from './index.js';

test('optimize a JPG', async t => {
const buf = await readFile(path.join(__dirname, 'fixture.jpg'));
const data = await m()(buf);
const buffer = await fs.readFile(new URL('fixture.jpg', import.meta.url));
const data = await imageminMozjpeg()(buffer);

t.true(data.length < buf.length);
t.true(data.length < buffer.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);
const buffer = await fs.readFile(new URL('fixture.jpg', import.meta.url));
const data = await imageminMozjpeg({progressive: false})(buffer);

t.false(isProgressive.buffer(data));
});

test('skip optimizing a non-JPG file', async t => {
const buf = await readFile(__filename);
const data = await m()(buf);
const buffer = await fs.readFile(new URL(import.meta.url));
const data = await imageminMozjpeg()(buffer);

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

test('throw error when a JPG is corrupt', async t => {
const buf = await readFile(path.join(__dirname, 'fixture-corrupt.jpg'));
const buffer = await fs.readFile(new URL('fixture-corrupt.jpg', import.meta.url));
await t.throwsAsync(async () => {
await m()(buf);
await imageminMozjpeg()(buffer);
}, {message: /Corrupt JPEG data/});
});

0 comments on commit 849aee2

Please sign in to comment.