Skip to content

Commit

Permalink
Use native ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Oct 22, 2021
1 parent 4c70dce commit 1ed87a2
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
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();
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,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')
Expand All @@ -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;
20 changes: 11 additions & 9 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
'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(() => {
Expand All @@ -21,14 +22,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');
Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
31 changes: 16 additions & 15 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -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')));
Expand All @@ -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);
Expand Down

0 comments on commit 1ed87a2

Please sign in to comment.