Skip to content

Commit

Permalink
Use native ESM (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Oct 22, 2021
1 parent 14f7065 commit edc0974
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 47 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
@@ -1,4 +1,4 @@
name: CI
name: test
on:
- push
- pull_request
Expand All @@ -9,9 +9,9 @@ jobs:
strategy:
matrix:
node-version:
- 16
- 14
- 12
- 10
os:
- ubuntu-latest
- macos-latest
Expand Down
6 changes: 3 additions & 3 deletions cli.js
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';
const {spawn} = require('child_process');
const optipng = require('.');
import {spawn} from 'node:child_process';
import process from 'node:process';
import optipng from './index.js';

const input = process.argv.slice(2);

Expand Down
5 changes: 3 additions & 2 deletions index.js
@@ -1,2 +1,3 @@
'use strict';
module.exports = require('./lib/index.js').path();
import lib from './lib/index.js';

export default lib.path();
17 changes: 10 additions & 7 deletions lib/index.js
@@ -1,11 +1,12 @@
'use strict';
const path = require('path');
const BinWrapper = require('bin-wrapper');
const packageJson = require('../package.json');
import fs from 'node:fs';
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import BinWrapper from 'bin-wrapper';

const url = `https://raw.githubusercontent.com/imagemin/optipng-bin/v${packageJson.version}/vendor/`;
const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url)));
const url = `https://raw.githubusercontent.com/imagemin/optipng-bin/v${pkg.version}/vendor/`;

module.exports = new BinWrapper()
const binWrapper = new BinWrapper()
.src(`${url}macos/optipng`, 'darwin')
.src(`${url}linux/x86/optipng`, 'linux', 'x86')
.src(`${url}linux/x64/optipng`, 'linux', 'x64')
Expand All @@ -14,5 +15,7 @@ module.exports = new BinWrapper()
.src(`${url}sunos/x86/optipng`, 'sunos', 'x86')
.src(`${url}sunos/x64/optipng`, 'sunos', 'x64')
.src(`${url}win/optipng.exe`, 'win32')
.dest(path.resolve(__dirname, '../vendor'))
.dest(fileURLToPath(new URL('../vendor', import.meta.url)))
.use(process.platform === 'win32' ? 'optipng.exe' : 'optipng');

export default binWrapper;
13 changes: 7 additions & 6 deletions lib/install.js
@@ -1,7 +1,7 @@
'use strict';
const path = require('path');
const binBuild = require('bin-build');
const bin = require('./index.js');
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import binBuild from 'bin-build';
import bin from './index.js';

(async () => {
try {
Expand All @@ -13,10 +13,11 @@ const bin = require('./index.js');
console.info('compiling from source');

try {
const source = fileURLToPath(new URL('../vendor/source/optipng.tar.gz', import.meta.url));
// From https://sourceforge.net/projects/optipng/files/OptiPNG/
await binBuild.file(path.resolve(__dirname, '../vendor/source/optipng.tar.gz'), [
await binBuild.file(source, [
`./configure --with-system-zlib --prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
'make install'
'make install',
]);

console.log('optipng built successfully');
Expand Down
9 changes: 5 additions & 4 deletions package.json
Expand Up @@ -4,11 +4,12 @@
"description": "OptiPNG wrapper that makes it seamlessly available as a local dependency",
"license": "MIT",
"repository": "imagemin/optipng-bin",
"type": "module",
"bin": {
"optipng": "cli.js"
},
"engines": {
"node": ">=10"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"postinstall": "node lib/install.js",
Expand Down Expand Up @@ -37,8 +38,8 @@
"ava": "^3.15.0",
"bin-check": "^4.0.1",
"compare-size": "^3.0.0",
"execa": "^5.0.0",
"tempy": "^1.0.0",
"xo": "^0.38.1"
"execa": "^5.1.1",
"tempy": "^2.0.0",
"xo": "^0.45.0"
}
}
14 changes: 5 additions & 9 deletions readme.md
@@ -1,4 +1,4 @@
# optipng-bin ![GitHub Actions Status](https://github.com/imagemin/optipng-bin/workflows/test/badge.svg)
# optipng-bin ![GitHub Actions Status](https://github.com/imagemin/optipng-bin/workflows/test/badge.svg?branch=main)

> [OptiPNG](http://optipng.sourceforge.net) is a PNG optimizer that recompresses image files to a smaller size, without losing any information
Expand All @@ -15,16 +15,12 @@ $ npm install --save optipng-bin
## Usage

```js
const {promisify} = require('util');
const {execFile} = require('child_process');
const optipng = require('optipng-bin');
import {execFile} from 'node:child_process';
import optipng from 'optipng-bin';

const execFileP = promsify(execFile);

(async () => {
await execFile(optipng, ['-out', 'output.png', 'input.png']);
execFile(optipng, ['-out', 'output.png', 'input.png'], error => {
console.log('Image minified!');
})();
});
```


Expand Down
30 changes: 16 additions & 14 deletions 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 optipng = 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 tempy from 'tempy';
import binCheck from 'bin-check';
import binBuild from 'bin-build';
import compareSize from 'compare-size';
import optipng from '../index.js';

test('rebuild the optipng binaries', async t => {
// Skip the test on Windows
Expand All @@ -17,10 +18,11 @@ test('rebuild the optipng binaries', async t => {
}

const temporary = tempy.directory();
const source = fileURLToPath(new URL('../vendor/source/optipng.tar.gz', import.meta.url));

await binBuild.file(path.resolve(__dirname, '../vendor/source/optipng.tar.gz'), [
await binBuild.file(source, [
`./configure --with-system-zlib --prefix="${temporary}" --bindir="${temporary}"`,
'make install'
'make install',
]);

t.true(fs.existsSync(path.join(temporary, 'optipng')));
Expand All @@ -32,15 +34,15 @@ test('return path to binary and verify that it is working', async t => {

test('minify a PNG', async t => {
const temporary = tempy.directory();
const sourcePath = path.join(__dirname, 'fixtures/test.png');
const sourcePath = fileURLToPath(new URL('./fixtures/test.png', import.meta.url));
const destinationPath = path.join(temporary, 'test.png');
const arguments_ = [
'-strip',
'all',
'-clobber',
'-out',
destinationPath,
sourcePath
sourcePath,
];

await execa(optipng, arguments_);
Expand Down

0 comments on commit edc0974

Please sign in to comment.