From 30f7304a8449746033d8d688df2889da28664123 Mon Sep 17 00:00:00 2001 From: 1000ch Date: Fri, 22 Oct 2021 15:56:08 +0900 Subject: [PATCH] Use native ESM --- .github/workflows/test.yml | 2 +- cli.js | 6 +++--- index.js | 5 +++-- lib/index.js | 15 +++++++++------ lib/install.js | 19 ++++++++++--------- package.json | 11 ++++++----- readme.md | 4 ++-- test/test.js | 31 ++++++++++++++++--------------- 8 files changed, 50 insertions(+), 43 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fa02eba..c9f8a54 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,9 +9,9 @@ jobs: strategy: matrix: node-version: + - 16 - 14 - 12 - - 10 os: - ubuntu-latest - macos-latest diff --git a/cli.js b/cli.js index a1842cf..cc6ab54 100755 --- a/cli.js +++ b/cli.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -'use strict'; -const {spawn} = require('child_process'); -const jpegtran = require('.'); +import {spawn} from 'node:child_process'; +import process from 'node:process'; +import jpegtran 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 3d18329..47fca6f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,11 +1,12 @@ -'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.githubusercontent.com/imagemin/jpegtran-bin/v${pkg.version}/vendor/`; -module.exports = new BinWrapper() +const binWrapper = new BinWrapper() .src(`${url}macos/jpegtran`, 'darwin') .src(`${url}linux/x86/jpegtran`, 'linux', 'x86') .src(`${url}linux/x64/jpegtran`, 'linux', 'x64') @@ -17,5 +18,7 @@ module.exports = new BinWrapper() .src(`${url}win/x64/jpegtran.exe`, 'win32', 'x64') .src(`${url}win/x86/libjpeg-62.dll`, 'win32', 'x86') .src(`${url}win/x64/libjpeg-62.dll`, 'win32', 'x64') - .dest(path.join(__dirname, '../vendor')) + .dest(fileURLToPath(new URL('../vendor', import.meta.url))) .use(process.platform === 'win32' ? 'jpegtran.exe' : 'jpegtran'); + +export default binWrapper; diff --git a/lib/install.js b/lib/install.js index 5b523b3..f507edf 100644 --- a/lib/install.js +++ b/lib/install.js @@ -1,15 +1,15 @@ -'use strict'; -const path = require('path'); -const binBuild = require('bin-build'); -const bin = require('.'); +import process from 'node:process'; +import {fileURLToPath} from 'node:url'; +import binBuild from 'bin-build'; +import bin from './index.js'; const args = [ '-copy', 'none', '-optimize', '-outfile', - path.join(__dirname, '../test/fixtures/test-optimized.jpg'), - path.join(__dirname, '../test/fixtures/test.jpg') + fileURLToPath(new URL('../test/fixtures/test-optimized.jpg', import.meta.url)), + fileURLToPath(new URL('../test/fixtures/test.jpg', import.meta.url)), ]; bin.run(args).then(() => { @@ -21,14 +21,15 @@ bin.run(args).then(() => { const cfg = [ './configure --disable-shared', - `--prefix="${bin.dest()}" --bindir="${bin.dest()}"` + `--prefix="${bin.dest()}" --bindir="${bin.dest()}"`, ].join(' '); try { - await binBuild.file(path.resolve(__dirname, '../vendor/source/libjpeg-turbo-1.5.1.tar.gz'), [ + const source = fileURLToPath(new URL('../vendor/source/libjpeg-turbo-1.5.1.tar.gz', import.meta.url)); + await binBuild.file(source, [ 'touch configure.ac aclocal.m4 configure Makefile.am Makefile.in', cfg, - 'make install' + 'make install', ]); console.log('jpegtran built successfully'); diff --git a/package.json b/package.json index b3c6495..08a346c 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "description": "jpegtran (part of libjpeg-turbo) bin-wrapper that makes it seamlessly available as a local dependency", "license": "MIT", "repository": "imagemin/jpegtran-bin", + "type": "module", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", @@ -24,7 +25,7 @@ "jpegtran": "cli.js" }, "engines": { - "node": ">=10" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "postinstall": "node lib/install.js", @@ -53,11 +54,11 @@ "bin-wrapper": "^4.0.0" }, "devDependencies": { - "ava": "^3.8.0", + "ava": "^3.15.0", "bin-check": "^4.0.1", "compare-size": "^3.0.0", - "execa": "^4.0.0", - "tempy": "^0.5.0", - "xo": "^0.30.0" + "execa": "^5.1.1", + "tempy": "^2.0.0", + "xo": "^0.45.0" } } diff --git a/readme.md b/readme.md index 8b39cf2..6ad7091 100644 --- a/readme.md +++ b/readme.md @@ -15,8 +15,8 @@ $ npm install --save jpegtran-bin ## Usage ```js -const {execFile} = require('child_process'); -const jpegtran = require('jpegtran-bin'); +import {execFile} from 'node:child_process'; +import jpegtran from 'jpegtran-bin'; execFile(jpegtran, ['-outfile', 'output.jpg', 'input.jpg'], error => { console.log('Image minified!'); diff --git a/test/test.js b/test/test.js index f4ce160..d8fe26c 100644 --- a/test/test.js +++ b/test/test.js @@ -1,24 +1,25 @@ -'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 jpegtran = require('..'); +import fs from 'node:fs'; +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import test from 'ava'; +import execa from 'execa'; +import tempy from 'tempy'; +import binCheck from 'bin-check'; +import binBuild from 'bin-build'; +import compareSize from 'compare-size'; +import jpegtran from '../index.js'; test('rebuild the jpegtran binaries', async t => { const temporary = tempy.directory(); const cfg = [ './configure --disable-shared', - `--prefix="${temporary}" --bindir="${temporary}"` + `--prefix="${temporary}" --bindir="${temporary}"`, ].join(' '); - await binBuild.file(path.resolve(__dirname, '../vendor/source/libjpeg-turbo-1.5.1.tar.gz'), [ + const source = fileURLToPath(new URL('../vendor/source/libjpeg-turbo-1.5.1.tar.gz', import.meta.url)); + await binBuild.file(source, [ cfg, - 'make install' + 'make install', ]); t.true(fs.existsSync(path.join(temporary, 'jpegtran'))); @@ -30,12 +31,12 @@ 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 src = fileURLToPath(new URL('fixtures/test.jpg', import.meta.url)); const dest = path.join(temporary, 'test.jpg'); const args = [ '-outfile', dest, - src + src, ]; await execa(jpegtran, args);