From 62635e16d7c2f5b8460f8a372eafd4f02dcbb550 Mon Sep 17 00:00:00 2001 From: 1000ch Date: Fri, 22 Oct 2021 15:23:04 +0900 Subject: [PATCH] Use native ESM --- .github/workflows/test.yml | 2 +- cli.js | 6 +++--- index.js | 5 +++-- lib/index.js | 15 +++++++++------ lib/install.js | 11 +++++------ package.json | 15 ++++++++------- readme.md | 8 ++++---- test/test.js | 18 +++++++++--------- 8 files changed, 42 insertions(+), 38 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ac41fed..56438df 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 110ee59..795206e 100644 --- a/cli.js +++ b/cli.js @@ -1,7 +1,7 @@ #!/usr/bin/env node -'use strict'; -const {spawn} = require('child_process'); -const pngout = require('.'); +import {spawn} from 'node:child_process'; +import process from 'node:process'; +import pngout 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 a981c1b..5f66f68 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,16 +1,19 @@ -'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/pngout-bin/v${pkg.version}/vendor/`; -module.exports = new BinWrapper() +const binWrapper = new BinWrapper() .src(`${url}osx/pngout`, 'darwin') .src(`${url}linux/x86/pngout`, 'linux', 'x86') .src(`${url}linux/x64/pngout`, 'linux', 'x64') .src(`${url}freebsd/x86/pngout`, 'freebsd', 'x86') .src(`${url}freebsd/x64/pngout`, 'freebsd', 'x64') .src(`${url}win32/pngout.exe`, 'win32') - .dest(path.resolve(__dirname, '../vendor')) + .dest(fileURLToPath(new URL('../vendor', import.meta.url))) .use(process.platform === 'win32' ? 'pngout.exe' : 'pngout'); + +export default binWrapper; diff --git a/lib/install.js b/lib/install.js index ca9797a..59f7b2c 100644 --- a/lib/install.js +++ b/lib/install.js @@ -1,13 +1,12 @@ -'use strict'; -const path = require('path'); -const bin = require('.'); +import {fileURLToPath} from 'node:url'; +import bin from './index.js'; const args = [ - path.join(__dirname, '../test/fixtures/test.png'), - path.join(__dirname, '../test/fixtures/test-optimized.png'), + fileURLToPath(new URL('../test/fixtures/test.png', import.meta.url)), + fileURLToPath(new URL('../test/fixtures/test-optimized.png', import.meta.url)), '-s4', '-c6', - '-y' + '-y', ]; bin.run(args).then(() => { diff --git a/package.json b/package.json index cb26c8c..634b0d1 100644 --- a/package.json +++ b/package.json @@ -4,9 +4,10 @@ "description": "pngout wrapper that makes it seamlessly available as a local dependency", "license": "MIT", "repository": "imagemin/pngout-bin", + "type": "module", "author": { - "name": "1000ch", - "email": "shogo.sensui@gmail.com", + "name": "Shogo Sensui", + "email": "shogosensui@gmail.com", "url": "github.com/1000ch" }, "maintainers": [ @@ -29,7 +30,7 @@ "pngout": "cli.js" }, "engines": { - "node": ">=10" + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" }, "scripts": { "postinstall": "node lib/install.js", @@ -55,10 +56,10 @@ "bin-wrapper": "^4.0.0" }, "devDependencies": { - "ava": "^3.8.0", + "ava": "^3.15.0", "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 270db5d..fd1cfde 100644 --- a/readme.md +++ b/readme.md @@ -1,4 +1,4 @@ -# pngout-bin ![GitHub Actions Status](https://github.com/imagemin/pngout-bin/workflows/test/badge.svg?branch=master) +# pngout-bin ![GitHub Actions Status](https://github.com/imagemin/pngout-bin/workflows/test/badge.svg) > [pngout](http://advsys.net/ken/util/pngout.htm) optimizes the size of PNG files losslessly @@ -15,10 +15,10 @@ $ npm install --save pngout-bin ## Usage ```js -const {execFile} = require('child_process'); -const pngout = require('pngout-bin'); +import {execFile} from 'node:child_process'; +import pngout from 'pngout-bin'; -execFile(pngout, ['input.png', 'output.png', '-s0', '-k0', '-f0'], err => { +execFile(pngout, ['input.png', 'output.png', '-s0', '-k0', '-f0'], error => { console.log('Image minified!'); }); ``` diff --git a/test/test.js b/test/test.js index 78d2486..daf2e78 100644 --- a/test/test.js +++ b/test/test.js @@ -1,21 +1,21 @@ -'use strict'; -const path = require('path'); -const test = require('ava'); -const execa = require('execa'); -const tempy = require('tempy'); -const compareSize = require('compare-size'); -const pngout = require('..'); +import path from 'node:path'; +import {fileURLToPath} from 'node:url'; +import test from 'ava'; +import execa from 'execa'; +import tempy from 'tempy'; +import compareSize from 'compare-size'; +import pngout from '../index.js'; test('minify a PNG', async t => { const temporary = tempy.directory(); - const src = path.join(__dirname, 'fixtures/test.png'); + const src = fileURLToPath(new URL('fixtures/test.png', import.meta.url)); const dest = path.join(temporary, 'test.png'); const args = [ src, dest, '-s4', '-c6', - '-y' + '-y', ]; await execa(pngout, args);