diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..1e0ef91 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,27 @@ +name: test +on: + - push + - pull_request +jobs: + test: + name: Node.js ${{ matrix.node-version }} on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + strategy: + matrix: + node-version: + - 18 + - 16 + - 14 + os: + - ubuntu-latest + - macos-latest + - windows-latest + steps: + - uses: actions/checkout@v2 + - if: contains(matrix.os, 'macos') + run: brew install mozjpeg + - uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + - run: npm install + - run: npm test diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9cc9de4..0000000 --- a/.travis.yml +++ /dev/null @@ -1,11 +0,0 @@ -language: node_js -node_js: - - '14' - - '12' - - '10' -os: - - linux - - windows - - osx -before_install: - - if [[ $TRAVIS_OS_NAME == 'osx' ]]; then brew install mozjpeg; fi diff --git a/cli.js b/cli.js index 0b0686b..3e2e403 100644 --- a/cli.js +++ b/cli.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -'use strict'; -const {spawn} = require('child_process'); -const jpegRecompress = require('.'); +import {spawn} from 'node:child_process'; +import process from 'node:process'; +import jpegRecompress from './index.js'; const input = process.argv.slice(2); diff --git a/index.js b/index.js index fb0971d..b4ce886 100644 --- a/index.js +++ b/index.js @@ -1,2 +1,3 @@ -'use strict'; -module.exports = require('./lib').path(); +import lib from './lib/index.js'; + +export default lib.path(); diff --git a/lib/index.js b/lib/index.js index db60ef9..a433fcf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,13 +1,16 @@ -'use strict'; -const path = require('path'); -const BinWrapper = require('bin-wrapper'); -const pkg = require('../package.json'); +import fs from 'node:fs'; +import process from 'node:process'; +import {fileURLToPath} from 'node:url'; +import BinWrapper from 'bin-wrapper'; +const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url))); const url = `https://raw.github.com/imagemin/jpeg-recompress-bin/v${pkg.version}/vendor/`; -module.exports = new BinWrapper() +const binWrapper = new BinWrapper() .src(`${url}osx/jpeg-recompress`, 'darwin') .src(`${url}linux/jpeg-recompress`, 'linux') .src(`${url}win/jpeg-recompress.exe`, 'win32') - .dest(path.resolve(__dirname, '../vendor')) + .dest(fileURLToPath(new URL('../vendor', import.meta.url))) .use(process.platform === 'win32' ? 'jpeg-recompress.exe' : 'jpeg-recompress'); + +export default binWrapper; diff --git a/lib/install.js b/lib/install.js index 30db7dd..6a64823 100644 --- a/lib/install.js +++ b/lib/install.js @@ -1,31 +1,32 @@ -'use strict'; -const path = require('path'); -const binBuild = require('bin-build'); -const log = require('logalot'); -const bin = require('.'); +import process from 'node:process'; +import {fileURLToPath} from 'node:url'; +import binBuild from 'bin-build'; +import bin from './index.js'; bin.run(['--version']).then(() => { - log.success('jpeg-recompress pre-build test passed successfully'); + console.log('jpeg-recompress pre-build test passed successfully'); }).catch(async error => { - log.warn(error.message); - log.warn('jpeg-recompress pre-build test failed'); + console.warn(error.message); + console.warn('jpeg-recompress pre-build test failed'); if (process.platform === 'win32' || process.platform === 'linux') { // eslint-disable-next-line unicorn/no-process-exit process.exit(1); } - log.info('compiling from source'); + console.info('compiling from source'); try { - await binBuild.file(path.resolve(__dirname, '../vendor/source/jpeg-archive-2.2.0.tar.gz'), [ + const source = fileURLToPath(new URL('../vendor/source/jpeg-archive-2.2.0.tar.gz', import.meta.url)); + + await binBuild.file(source, [ `mkdir -p ${bin.dest()}`, - `make && mv ${bin.use()} ${bin.path()}` + `make && mv ${bin.use()} ${bin.path()}`, ]); - log.success('jpeg-recompress built successfully'); + console.log('jpeg-recompress built successfully'); } catch (error) { - log.error(error.stack); + console.error(error.stack); // eslint-disable-next-line unicorn/no-process-exit process.exit(1); diff --git a/package.json b/package.json index bf9b878..d264ac8 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,11 @@ { "name": "jpeg-recompress-bin", - "version": "5.1.1", + "version": "7.0.0", "description": "jpeg-recompress wrapper that makes it seamlessly available as a local dependency", "license": "MIT", "repository": "imagemin/jpeg-recompress-bin", + "type": "module", + "exports": "./index.js", "author": { "name": "Shogo Sensui", "email": "shogosensui@gmail.com", @@ -29,11 +31,11 @@ "jpeg-recompress": "cli.js" }, "engines": { - "node": ">=10" + "node": "^14.13.1 || >=16.0.0" }, "scripts": { "postinstall": "node lib/install.js", - "test": "xo && ava" + "test": "xo && ava --timeout=120s" }, "files": [ "cli.js", @@ -54,15 +56,14 @@ ], "dependencies": { "bin-build": "^3.0.0", - "bin-wrapper": "^4.1.0", - "logalot": "^2.0.0" + "bin-wrapper": "^4.1.0" }, "devDependencies": { - "ava": "^3.8.0", + "ava": "^4.2.0", "bin-check": "^4.0.1", "compare-size": "^3.0.0", - "execa": "^4.0.0", - "tempy": "^0.5.0", - "xo": "^0.30.0" + "execa": "^6.1.0", + "tempy": "^3.0.0", + "xo": "^0.48.0" } } diff --git a/readme.md b/readme.md index 61061f5..2b550fa 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# jpeg-recompress-bin [![Build Status](https://travis-ci.org/imagemin/jpeg-recompress-bin.svg?branch=master)](https://travis-ci.org/imagemin/jpeg-recompress-bin) +# jpeg-recompress-bin ![GitHub Actions Status](https://github.com/imagemin/jpeg-recompress-bin/workflows/test/badge.svg?branch=main) > Compress JPEGs by re-encoding to the smallest JPEG quality while keeping perceived visual quality the same and by making sure huffman tables are optimized @@ -15,10 +15,10 @@ $ npm install --save jpeg-recompress-bin ## Usage ```js -const {execFile} = require('child_process'); -const jpegRecompress = require('jpeg-recompress-bin'); +import {execFile} from 'node:child_process'; +import jpegRecompress from 'jpeg-recompress-bin'; -execFile(jpegRecompress, ['--quality high', '--min 60', 'input.jpg', 'output.jpg'], err => { +execFile(jpegRecompress, ['--quality high', '--min 60', 'input.jpg', 'output.jpg'], error => { console.log('Image minified'); }); ``` diff --git a/test/test.js b/test/test.js index d2fc715..c7fcb26 100644 --- a/test/test.js +++ b/test/test.js @@ -1,13 +1,14 @@ -'use strict'; -const fs = require('fs'); -const path = require('path'); -const test = require('ava'); -const execa = require('execa'); -const tempy = require('tempy'); -const binCheck = require('bin-check'); -const binBuild = require('bin-build'); -const compareSize = require('compare-size'); -const jpegRecompress = require('..'); +import fs from 'node:fs'; +import path from 'node:path'; +import process from 'node:process'; +import {fileURLToPath} from 'node:url'; +import test from 'ava'; +import {execa} from 'execa'; +import {temporaryDirectory} from 'tempy'; +import binCheck from 'bin-check'; +import binBuild from 'bin-build'; +import compareSize from 'compare-size'; +import jpegRecompress from '../index.js'; test('rebuild the jpeg-recompress binaries', async t => { if (process.platform === 'win32' || process.platform === 'linux') { @@ -15,11 +16,12 @@ test('rebuild the jpeg-recompress binaries', async t => { return; } - const temporary = tempy.directory(); + const temporary = temporaryDirectory(); + const source = fileURLToPath(new URL('../vendor/source/jpeg-archive-2.2.0.tar.gz', import.meta.url)); - await binBuild.file(path.resolve(__dirname, '../vendor/source/jpeg-archive-2.2.0.tar.gz'), [ + await binBuild.file(source, [ `mkdir -p ${temporary}`, - `make && mv jpeg-recompress ${path.join(temporary, 'jpeg-recompress')}` + `make && mv jpeg-recompress ${path.join(temporary, 'jpeg-recompress')}`, ]); t.true(fs.existsSync(path.join(temporary, 'jpeg-recompress'))); @@ -30,8 +32,8 @@ test('return path to binary and verify that it is working', async t => { }); test('minify a JPG', async t => { - const temporary = tempy.directory(); - const src = path.join(__dirname, 'fixtures/test.jpg'); + const temporary = temporaryDirectory(); + const src = fileURLToPath(new URL('fixtures/test.jpg', import.meta.url)); const dest = path.join(temporary, 'test.jpg'); const args = [ '--quality', @@ -39,7 +41,7 @@ test('minify a JPG', async t => { '--min', '60', src, - dest + dest, ]; await execa(jpegRecompress, args);