Skip to content

Commit b532386

Browse files
author
Shogo Sensui
authored
Use native ESM (#54)
1 parent 2aa3a04 commit b532386

File tree

8 files changed

+57
-48
lines changed

8 files changed

+57
-48
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ jobs:
99
strategy:
1010
matrix:
1111
node-version:
12+
- 16
1213
- 14
1314
- 12
14-
- 10
1515
os:
1616
- ubuntu-latest
1717
- macos-latest

cli.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
const {spawn} = require('child_process');
4-
const binPath = require('.');
2+
import process from 'node:process';
3+
import {spawn} from 'node:child_process';
4+
import binPath from './index.js';
55

66
spawn(binPath, process.argv.slice(2), {stdio: 'inherit'})
77
.on('exit', process.exit);

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
'use strict';
2-
module.exports = require('./lib').path();
1+
import lib from './lib/index.js';
2+
3+
export default lib.path();

lib/index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
'use strict';
2-
const path = require('path');
3-
const BinWrapper = require('bin-wrapper');
4-
const pkg = require('../package.json');
1+
import fs from 'node:fs';
2+
import process from 'node:process';
3+
import {fileURLToPath} from 'node:url';
4+
import BinWrapper from 'bin-wrapper';
55

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

8-
module.exports = new BinWrapper()
9+
const binWrapper = new BinWrapper()
910
.src(`${url}osx/cwebp`, 'darwin')
1011
.src(`${url}linux/x86/cwebp`, 'linux', 'x86')
1112
.src(`${url}linux/x64/cwebp`, 'linux', 'x64')
1213
.src(`${url}win/x64/cwebp.exe`, 'win32', 'x64')
13-
.dest(path.join(__dirname, '../vendor'))
14+
.dest(fileURLToPath(new URL('../vendor', import.meta.url)))
1415
.use(process.platform === 'win32' ? 'cwebp.exe' : 'cwebp');
16+
17+
export default binWrapper;

lib/install.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
'use strict';
2-
const binBuild = require('bin-build');
3-
const path = require('path');
4-
const bin = require('.');
1+
import process from 'node:process';
2+
import {fileURLToPath} from 'node:url';
3+
import binBuild from 'bin-build';
4+
import bin from './index.js';
55

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

13-
binBuild.file(path.resolve(__dirname, '../vendor/source/libwebp-1.1.0.tar.gz'), [
14-
`./configure --disable-shared --prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
15-
'make && make install'
16-
]).then(() => { // eslint-disable-line promise/prefer-await-to-then
13+
try {
14+
const source = fileURLToPath(new URL('../vendor/source/libwebp-1.1.0.tar.gz', import.meta.url));
15+
16+
binBuild.file(source, [
17+
`./configure --disable-shared --prefix="${bin.dest()}" --bindir="${bin.dest()}"`,
18+
'make && make install',
19+
]);
20+
1721
console.log('cwebp built successfully');
18-
}).catch(error => {
22+
} catch (error) {
1923
console.error(error.stack);
2024

2125
// eslint-disable-next-line unicorn/no-process-exit
2226
process.exit(1);
23-
});
27+
}
2428
});

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
"description": "cwebp wrapper that makes it seamlessly available as a local dependency",
55
"license": "MIT",
66
"repository": "imagemin/cwebp-bin",
7+
"type": "module",
78
"funding": "https://github.com/imagemin/cwebp-bin?sponsor=1",
89
"bin": {
910
"cwebp": "cli.js"
1011
},
1112
"engines": {
12-
"node": ">=10"
13+
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
1314
},
1415
"scripts": {
1516
"postinstall": "node lib/install.js",
@@ -38,11 +39,11 @@
3839
"bin-wrapper": "^4.0.1"
3940
},
4041
"devDependencies": {
41-
"ava": "^3.8.0",
42+
"ava": "^3.15.0",
4243
"bin-check": "^4.1.0",
4344
"compare-size": "^3.0.0",
44-
"execa": "^1.0.0",
45-
"tempy": "^0.5.0",
46-
"xo": "^0.30.0"
45+
"execa": "^5.1.1",
46+
"tempy": "^2.0.0",
47+
"xo": "^0.45.0"
4748
}
4849
}

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ $ npm install cwebp-bin
1515
## Usage
1616

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

2121
execFile(cwebp, ['input.png', '-o', 'output.webp'], err => {
2222
if (err) {

test/test.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
'use strict';
2-
const fs = require('fs');
3-
const path = require('path');
4-
const test = require('ava');
5-
const execa = require('execa');
6-
const tempy = require('tempy');
7-
const binCheck = require('bin-check');
8-
const binBuild = require('bin-build');
9-
const compareSize = require('compare-size');
10-
const cwebp = require('..');
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import {fileURLToPath} from 'node:url';
4+
import test from 'ava';
5+
import execa from 'execa';
6+
import tempy from 'tempy';
7+
import binCheck from 'bin-check';
8+
import binBuild from 'bin-build';
9+
import compareSize from 'compare-size';
10+
import cwebp from '../index.js';
1111

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

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

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

2828
test('minify and convert a PNG to WebP', async t => {
2929
const temporary = tempy.directory();
30-
const src = path.join(__dirname, 'fixtures/test.png');
30+
const src = fileURLToPath(new URL('./fixtures/test.png', import.meta.url));
3131
const dest = path.join(temporary, 'test-png.webp');
3232
const args = [
3333
src,
3434
'-o',
35-
dest
35+
dest,
3636
];
3737

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

4444
test('minify and convert a JPG to WebP', async t => {
4545
const temporary = tempy.directory();
46-
const src = path.join(__dirname, 'fixtures/test.jpg');
46+
const src = fileURLToPath(new URL('./fixtures/test.jpg', import.meta.url));
4747
const dest = path.join(temporary, 'test-jpg.webp');
4848
const args = [
4949
src,
5050
'-o',
51-
dest
51+
dest,
5252
];
5353

5454
await execa(cwebp, args);

0 commit comments

Comments
 (0)