Skip to content

Commit

Permalink
Use native ESM (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Oct 22, 2021
1 parent c64a13a commit ef40da8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env node
'use strict';
const execa = require('execa');
const m = require('.');
import process from 'node:process';
import execa from 'execa';
import gifsicle from './index.js';

execa(m, process.argv.slice(2), {stdio: 'inherit'});
execa(gifsicle, process.argv.slice(2), {stdio: 'inherit'});
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
'use strict';
module.exports = require('./lib').path(); /* eslint-disable-line import/extensions */
import lib from './lib/index.js';

export default lib.path();
15 changes: 9 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
'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/gifsicle-bin/v${pkg.version}/vendor/`;

module.exports = new BinWrapper()
const binWrapper = new BinWrapper()
.src(`${url}macos/gifsicle`, 'darwin')
.src(`${url}linux/x86/gifsicle`, 'linux', 'x86')
.src(`${url}linux/x64/gifsicle`, 'linux', 'x64')
.src(`${url}freebsd/x86/gifsicle`, 'freebsd', 'x86')
.src(`${url}freebsd/x64/gifsicle`, 'freebsd', 'x64')
.src(`${url}win/x86/gifsicle.exe`, 'win32', 'x86')
.src(`${url}win/x64/gifsicle.exe`, 'win32', 'x64')
.dest(path.join(__dirname, '../vendor'))
.dest(fileURLToPath(new URL('../vendor', import.meta.url)))
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');

export default binWrapper;
15 changes: 8 additions & 7 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'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';

(async () => {
try {
Expand All @@ -14,14 +14,15 @@ const bin = require('.');

const config = [
'./configure --disable-gifview --disable-gifdiff',
`--prefix="${bin.dest()}" --bindir="${bin.dest()}"`
`--prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
].join(' ');

try {
await binBuild.file(path.resolve(__dirname, '../vendor/source/gifsicle-1.92.tar.gz'), [
const source = fileURLToPath(new URL('../vendor/source/gifsicle-1.92.tar.gz', import.meta.url));
await binBuild.file(source, [
'autoreconf -ivf',
config,
'make install'
'make install',
]);

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

> gifsicle manipulates GIF image files in many different ways. Depending on command line options, it can merge several GIFs into a GIF animation; explode an animation into its component frames; change individual frames in an animation; turn interlacing on and off; add transparency and much more.
Expand All @@ -13,10 +13,10 @@ $ npm install gifsicle
## Usage

```js
const {execFile} = require('child_process');
const gifsicle = require('gifsicle');
import {execFile} from 'node:child_process';
import gifsicle from 'gifsicle';

execFile(gifsicle, ['-o', 'output.gif', 'input.gif'], err => {
execFile(gifsicle, ['-o', 'output.gif', 'input.gif'], error => {
console.log('Image minified!');
});
```
Expand Down
36 changes: 19 additions & 17 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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 m = 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 gifsicle from '../index.js';

test.serial('rebuild the gifsicle binaries', async t => {
// Skip the test on Windows
Expand All @@ -19,33 +20,34 @@ test.serial('rebuild the gifsicle binaries', async t => {
const temporary = tempy.directory();
const cfg = [
'./configure --disable-gifview --disable-gifdiff',
`--prefix="${temporary}" --bindir="${temporary}"`
`--prefix="${temporary}" --bindir="${temporary}"`,
].join(' ');

await binBuild.file(path.resolve(__dirname, '../vendor/source/gifsicle-1.92.tar.gz'), [
const source = fileURLToPath(new URL('../vendor/source/gifsicle-1.92.tar.gz', import.meta.url));
await binBuild.file(source, [
'autoreconf -ivf',
cfg,
'make install'
'make install',
]);

t.true(fs.existsSync(path.join(temporary, 'gifsicle')));
});

test.serial('verify binary', async t => {
t.true(await binCheck(m, ['--version']));
t.true(await binCheck(gifsicle, ['--version']));
});

test.serial('minify a gif', async t => {
const temporary = tempy.directory();
const src = path.join(__dirname, 'fixtures/test.gif');
const src = fileURLToPath(new URL('fixtures/test.gif', import.meta.url));
const dest = path.join(temporary, 'test.gif');
const args = [
'-o',
dest,
src
src,
];

await execa(m, args);
await execa(gifsicle, args);
const result = await compareSize(src, dest);

t.true(result[dest] < result[src]);
Expand Down

0 comments on commit ef40da8

Please sign in to comment.