From 28bc80172ad9bf071543a1790d45fc4baa2e4d83 Mon Sep 17 00:00:00 2001 From: 1000ch Date: Fri, 22 Oct 2021 17:41:18 +0900 Subject: [PATCH] Use native ESM --- .github/workflows/test.yml | 2 +- cli.js | 6 +++--- index.js | 5 +++-- lib/index.js | 15 +++++++++------ lib/install.js | 12 ++++++------ package.json | 11 ++++++----- readme.md | 4 ++-- test/test.js | 31 ++++++++++++++++--------------- 8 files changed, 46 insertions(+), 40 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e868b7d..084e470 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 f2eb3b9..0961cb1 100644 --- a/cli.js +++ b/cli.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -'use strict'; -const {spawn} = require('child_process'); -const guetzli = require('.'); +import {spawn} from 'node:child_process'; +import process from 'node:process'; +import guetzli 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 5856c98..056359a 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.githubusercontent.com/imagemin/guetzli-bin/v${pkg.version}/vendor/`; -module.exports = new BinWrapper() +const binWrapper = new BinWrapper() .src(`${url}macos/guetzli`, 'darwin') .src(`${url}linux/guetzli`, 'linux') .src(`${url}win/guetzli.exe`, 'win32') - .dest(path.resolve(__dirname, '../vendor')) + .dest(fileURLToPath(new URL('../vendor', import.meta.url))) .use(process.platform === 'win32' ? 'guetzli.exe' : 'guetzli'); + +export default binWrapper; diff --git a/lib/install.js b/lib/install.js index 4daaff3..8ce6d51 100644 --- a/lib/install.js +++ b/lib/install.js @@ -1,7 +1,6 @@ -'use strict'; -const path = require('path'); -const binBuild = require('bin-build'); -const bin = require('.'); +import {fileURLToPath} from 'node:url'; +import binBuild from 'bin-build'; +import bin from './index.js'; bin.run(['--verbose']).then(() => { console.log('guetzli pre-build test passed successfully'); @@ -11,9 +10,10 @@ bin.run(['--verbose']).then(() => { console.info('compiling from source'); try { - binBuild.file(path.resolve(__dirname, '../vendor/source/guetzli-1.0.1.tar.gz'), [ + const source = fileURLToPath(new URL('../vendor/source/guetzli-1.0.1.tar.gz', import.meta.url)); + binBuild.file(source, [ `mkdir -p ${bin.dest()}`, - `make && mv bin/Release/${bin.use()} ${bin.path()}` + `make && mv bin/Release/${bin.use()} ${bin.path()}`, ]); console.log('guetzli built successfully'); diff --git a/package.json b/package.json index 42c7ac2..826095e 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,12 @@ "description": "guetzli wrapper that makes it seamlessly available as a local dependency", "license": "MIT", "repository": "imagemin/guetzli-bin", + "type": "module", "bin": { "guetzli": "cli.js" }, "engines": { - "node": ">=10" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "postinstall": "node lib/install.js", @@ -36,12 +37,12 @@ "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", + "execa": "^5.1.1", "executable": "^4.1.0", - "tempy": "^0.5.0", - "xo": "^0.30.0" + "tempy": "^2.0.0", + "xo": "^0.45.0" } } diff --git a/readme.md b/readme.md index 67e7493..ea2d3c7 100644 --- a/readme.md +++ b/readme.md @@ -15,8 +15,8 @@ $ npm install guetzli ## Usage ```js -const {execFile} = require('child_process'); -const guetzli = require('guetzli'); +import {execFile} from 'node:child_process'; +import guetzli from 'guetzli'; execFile(guetzli, ['input.jpg', 'output.jpg'], error => { console.log('Image minified!'); diff --git a/test/test.js b/test/test.js index e980f42..2449b0e 100644 --- a/test/test.js +++ b/test/test.js @@ -1,37 +1,38 @@ -'use strict'; -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 executable = require('executable'); -const guetzli = require('..'); +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 executable from 'executable'; +import guetzli from '../index.js'; test('rebuild the guetzli binaries', async t => { const temporary = tempy.directory(); - await binBuild.file(path.resolve(__dirname, '../vendor/source/guetzli-1.0.1.tar.gz'), [ + const source = fileURLToPath(new URL('../vendor/source/guetzli-1.0.1.tar.gz', import.meta.url)); + await binBuild.file(source, [ `mkdir -p ${temporary}`, - `make && mv bin/Release/guetzli ${temporary}` + `make && mv bin/Release/guetzli ${temporary}`, ]); t.true(executable.sync(path.join(temporary, 'guetzli'))); }); test('return path to binary and verify that it is working', async t => { - const src = path.join(__dirname, 'fixtures/test.jpg'); + const src = fileURLToPath(new URL('fixtures/test.jpg', import.meta.url)); t.true(await binCheck(guetzli, [src, '/dev/null'])); }); 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 = [ src, - dest + dest, ]; await execa(guetzli, args);