Skip to content

Commit

Permalink
ES2015ify and require Node.js 4
Browse files Browse the repository at this point in the history
  • Loading branch information
1000ch committed Mar 29, 2017
1 parent 9232d28 commit 6df6b97
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
* text=auto
*.js text eol=lf
6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
sudo: false
language: node_js
node_js:
- '5'
- '4'
- '0.12'
- '0.10'
- 6
- 4
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# cwebp-bin [![Build Status](https://travis-ci.org/imagemin/cwebp-bin.svg?branch=master)](https://travis-ci.org/imagemin/cwebp-bin)

> WebP is a new image format that provides lossless and lossy compression for images on the web. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller in size compared to JPEG images at equivalent SSIM index.
> [WebP](https://developers.google.com/speed/webp/) is a new image format that provides lossless and lossy compression for images on the web. WebP lossless images are 26% smaller in size compared to PNGs. WebP lossy images are 25-34% smaller in size compared to JPEG images at equivalent SSIM index.
You probably want [`imagemin-webp`](https://github.com/imagemin/imagemin-webp) instead.


## Install
Expand All @@ -13,10 +15,10 @@ $ npm install --save cwebp-bin
## Usage

```js
var execFile = require('child_process').execFile;
var cwebp = require('cwebp-bin');
const execFile = require('child_process').execFile;
const cwebp = require('cwebp-bin');

execFile(cwebp, ['input.png', '-o', 'output.webp'], function (err) {
execFile(cwebp, ['input.png', '-o', 'output.webp'], err => {
if (err) {
throw err;
}
Expand All @@ -39,4 +41,4 @@ $ cwebp --help

## License

MIT © [imagemin](https://github.com/imagemin)
MIT © [Imagemin](https://github.com/imagemin)
5 changes: 2 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/env node
'use strict';

var binPath = require('./');
var spawn = require('child_process').spawn;
const spawn = require('child_process').spawn;
const binPath = require('.');

spawn(binPath, process.argv.slice(2), {stdio: 'inherit'})
.on('exit', process.exit);
18 changes: 9 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
const path = require('path');
const BinWrapper = require('bin-wrapper');
const pkg = require('../package.json');

var path = require('path');
var BinWrapper = require('bin-wrapper');
var pkg = require('../package.json');
var url = 'https://raw.githubusercontent.com/imagemin/cwebp-bin/v' + pkg.version + '/vendor/';
const url = `https://raw.githubusercontent.com/imagemin/cwebp-bin/v${pkg.version}/vendor/`;

module.exports = new BinWrapper()
.src(url + 'osx/cwebp', 'darwin')
.src(url + 'linux/x86/cwebp', 'linux', 'x86')
.src(url + 'linux/x64/cwebp', 'linux', 'x64')
.src(url + 'win/x86/cwebp.exe', 'win32', 'x86')
.src(url + 'win/x64/cwebp.exe', 'win32', 'x64')
.src(`${url}osx/cwebp`, 'darwin')
.src(`${url}linux/x86/cwebp`, 'linux', 'x86')
.src(`${url}linux/x64/cwebp`, 'linux', 'x64')
.src(`${url}win/x86/cwebp.exe`, 'win32', 'x86')
.src(`${url}win/x64/cwebp.exe`, 'win32', 'x64')
.dest(path.join(__dirname, '../vendor'))
.use(process.platform === 'win32' ? 'cwebp.exe' : 'cwebp');
20 changes: 7 additions & 13 deletions lib/install.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
'use strict';
const BinBuild = require('bin-build');
const log = require('logalot');
const bin = require('.');

var bin = require('./');
var BinBuild = require('bin-build');
var log = require('logalot');

bin.run(['-version'], function (err) {
bin.run(['-version'], err => {
if (err) {
log.warn(err.message);
log.warn('cwebp pre-build test failed');
log.info('compiling from source');

var cfg = [
'./configure --disable-shared --prefix="' + bin.dest() + '"',
'--bindir="' + bin.dest() + '"'
].join(' ');

var builder = new BinBuild()
const builder = new BinBuild()
.src('http://downloads.webmproject.org/releases/webp/libwebp-0.5.1.tar.gz')
.cmd(cfg)
.cmd(`./configure --disable-shared --prefix="${bin.dest()}" --bindir="${bin.dest()}"`)
.cmd('make && make install');

return builder.run(function (err) {
return builder.run(err => {
if (err) {
log.error(err.stack);
return;
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md → license
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) imagemin
Copyright (c) Imagemin

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,23 @@
"url": "github.com/shinnn"
}
],
"engines": {
"node": ">=0.10.0"
},
"bin": {
"cwebp": "cli.js"
},
"engines": {
"node": ">=4"
},
"scripts": {
"postinstall": "node lib/install.js",
"test": "xo && mkdir -p test/tmp && node test/test.js && rm -rf test/tmp"
"test": "xo && ava"
},
"files": [
"cli.js",
"index.js",
"lib"
],
"keywords": [
"imagemin",
"compress",
"image",
"img",
Expand All @@ -52,15 +53,16 @@
"webp"
],
"dependencies": {
"bin-build": "^2.0.0",
"bin-build": "^2.2.0",
"bin-wrapper": "^3.0.1",
"logalot": "^2.0.0"
},
"devDependencies": {
"ava": "^0.0.4",
"bin-check": "^1.0.0",
"compare-size": "^1.0.1",
"path-exists": "^1.0.0",
"xo": "^0.12.1"
"ava": "*",
"bin-check": "^4.0.1",
"compare-size": "^3.0.0",
"execa": "^0.6.3",
"tempy": "^0.1.0",
"xo": "*"
}
}
92 changes: 36 additions & 56 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,78 +1,58 @@
'use strict';

var execFile = require('child_process').execFile;
var path = require('path');
var binCheck = require('bin-check');
var BinBuild = require('bin-build');
var compareSize = require('compare-size');
var pathExists = require('path-exists');
var cwebp = require('../');
var test = require('ava');
var tmp = path.join(__dirname, 'tmp');

test('rebuild the cwebp binaries', function (t) {
t.plan(3);

var builder = new BinBuild()
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('..');

test.cb('rebuild the cwebp binaries', t => {
const tmp = tempy.directory();
const builder = new BinBuild()
.src('http://downloads.webmproject.org/releases/webp/libwebp-0.5.1.tar.gz')
.cmd('./configure --disable-shared --prefix="' + tmp + '" --bindir="' + tmp + '"')
.cmd(`./configure --disable-shared --prefix="${tmp}" --bindir="${tmp}"`)
.cmd('make && make install');

builder.run(function (err) {
t.assert(!err, err);

pathExists(path.join(tmp, 'cwebp'), function (err, exists) {
t.assert(!err, err);
t.assert(exists);
});
builder.run(err => {
t.ifError(err);
t.true(fs.existsSync(path.join(tmp, 'cwebp')));
t.end();
});
});

test('return path to binary and verify that it is working', function (t) {
t.plan(2);

binCheck(cwebp, ['-version'], function (err, works) {
t.assert(!err, err);
t.assert(works);
});
test('return path to binary and verify that it is working', async t => {
t.true(await binCheck(cwebp, ['-version']));
});

test('minify and convert a PNG to WebP', function (t) {
t.plan(3);

var src = path.join(__dirname, 'fixtures/test.png');
var dest = path.join(tmp, 'test-png.webp');
var args = [
test('minify and convert a PNG to WebP', async t => {
const tmp = tempy.directory();
const src = path.join(__dirname, 'fixtures/test.png');
const dest = path.join(tmp, 'test-png.webp');
const args = [
src,
'-o', dest
];

execFile(cwebp, args, function (err) {
t.assert(!err, err);
await execa(cwebp, args);
const res = await compareSize(src, dest);

compareSize(src, dest, function (err, res) {
t.assert(!err, err);
t.assert(res[dest] < res[src]);
});
});
t.true(res[dest] < res[src]);
});

test('minify and convert a JPG to WebP', function (t) {
t.plan(3);

var src = path.join(__dirname, 'fixtures/test.jpg');
var dest = path.join(tmp, 'test-jpg.webp');
var args = [
test('minify and convert a JPG to WebP', async t => {
const tmp = tempy.directory();
const src = path.join(__dirname, 'fixtures/test.jpg');
const dest = path.join(tmp, 'test-jpg.webp');
const args = [
src,
'-o', dest
];

execFile(cwebp, args, function (err) {
t.assert(!err);
await execa(cwebp, args);
const res = await compareSize(src, dest);

compareSize(src, dest, function (err, res) {
t.assert(!err, err);
t.assert(res[dest] < res[src]);
});
});
t.true(res[dest] < res[src]);
});

0 comments on commit 6df6b97

Please sign in to comment.