Skip to content

Commit

Permalink
Use native ESM (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch authored Oct 20, 2021
1 parent 2aa3a04 commit b532386
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 48 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 binPath = require('.');
import process from 'node:process';
import {spawn} from 'node:child_process';
import binPath from './index.js';

spawn(binPath, process.argv.slice(2), {stdio: 'inherit'})
.on('exit', process.exit);
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,14 +1,17 @@
'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/cwebp-bin/v${pkg.version}/vendor/`;

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

export default binWrapper;
24 changes: 14 additions & 10 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';
const binBuild = require('bin-build');
const path = require('path');
const bin = require('.');
import process from 'node:process';
import {fileURLToPath} from 'node:url';
import binBuild from 'bin-build';
import bin from './index.js';

bin.run(['-version']).then(() => {
console.log('cwebp pre-build test passed successfully');
Expand All @@ -10,15 +10,19 @@ bin.run(['-version']).then(() => {
console.warn('cwebp pre-build test failed');
console.info('compiling from source');

binBuild.file(path.resolve(__dirname, '../vendor/source/libwebp-1.1.0.tar.gz'), [
`./configure --disable-shared --prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
'make && make install'
]).then(() => { // eslint-disable-line promise/prefer-await-to-then
try {
const source = fileURLToPath(new URL('../vendor/source/libwebp-1.1.0.tar.gz', import.meta.url));

binBuild.file(source, [
`./configure --disable-shared --prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
'make && make install',
]);

console.log('cwebp built successfully');
}).catch(error => {
} catch (error) {
console.error(error.stack);

// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
});
}
});
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"description": "cwebp wrapper that makes it seamlessly available as a local dependency",
"license": "MIT",
"repository": "imagemin/cwebp-bin",
"type": "module",
"funding": "https://github.com/imagemin/cwebp-bin?sponsor=1",
"bin": {
"cwebp": "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 @@ -38,11 +39,11 @@
"bin-wrapper": "^4.0.1"
},
"devDependencies": {
"ava": "^3.8.0",
"ava": "^3.15.0",
"bin-check": "^4.1.0",
"compare-size": "^3.0.0",
"execa": "^1.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 cwebp-bin
## Usage

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

execFile(cwebp, ['input.png', '-o', 'output.webp'], err => {
if (err) {
Expand Down
38 changes: 19 additions & 19 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'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 cwebp = 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 cwebp from '../index.js';

test('rebuild the cwebp binaries', async t => {
const temporary = tempy.directory();
const source = fileURLToPath(new URL('../vendor/source/libwebp-1.1.0.tar.gz', import.meta.url));

await binBuild
.file(path.resolve(__dirname, '../vendor/source/libwebp-1.1.0.tar.gz'), [
`./configure --disable-shared --prefix="${temporary}" --bindir="${temporary}"`,
'make && make install'
]);
await binBuild.file(source, [
`./configure --disable-shared --prefix="${temporary}" --bindir="${temporary}"`,
'make && make install',
]);

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

test('minify and convert a PNG to WebP', 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.webp');
const args = [
src,
'-o',
dest
dest,
];

await execa(cwebp, args);
Expand All @@ -43,12 +43,12 @@ test('minify and convert a PNG to WebP', async t => {

test('minify and convert a JPG to WebP', 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.webp');
const args = [
src,
'-o',
dest
dest,
];

await execa(cwebp, args);
Expand Down

0 comments on commit b532386

Please sign in to comment.