Skip to content

Commit 6e091ed

Browse files
committedMay 30, 2019
Require Node.js 8
1 parent c1c3346 commit 6e091ed

File tree

5 files changed

+41
-51
lines changed

5 files changed

+41
-51
lines changed
 

‎.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3+
- '12'
34
- '10'
45
- '8'
5-
- '6'

‎index.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -37,43 +37,45 @@ module.exports = (plugins, options) => {
3737
plugins = null;
3838
}
3939

40-
options = Object.assign({
40+
options = {
4141
// TODO: Remove this when Gulp gets a real logger with levels
42-
verbose: process.argv.includes('--verbose')
43-
}, options);
42+
verbose: process.argv.includes('--verbose'),
43+
...options
44+
};
4445

45-
const validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
46+
const validExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
4647

4748
let totalBytes = 0;
4849
let totalSavedBytes = 0;
4950
let totalFiles = 0;
5051

5152
return through.obj({
5253
maxConcurrency: 8
53-
}, (file, enc, cb) => {
54+
}, (file, encoding, callback) => {
5455
if (file.isNull()) {
55-
cb(null, file);
56+
callback(null, file);
5657
return;
5758
}
5859

5960
if (file.isStream()) {
60-
cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
61+
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
6162
return;
6263
}
6364

64-
if (!validExts.includes(path.extname(file.path).toLowerCase())) {
65+
if (!validExtensions.includes(path.extname(file.path).toLowerCase())) {
6566
if (options.verbose) {
6667
log(`${PLUGIN_NAME}: Skipping unsupported image ${chalk.blue(file.relative)}`);
6768
}
6869

69-
cb(null, file);
70+
callback(null, file);
7071
return;
7172
}
7273

7374
const use = plugins || getDefaultPlugins();
7475

75-
imagemin.buffer(file.contents, {use})
76-
.then(data => {
76+
(async () => {
77+
try {
78+
const data = await imagemin.buffer(file.contents, {use});
7779
const originalSize = file.contents.length;
7880
const optimizedSize = data.length;
7981
const saved = originalSize - optimizedSize;
@@ -92,12 +94,12 @@ module.exports = (plugins, options) => {
9294
}
9395

9496
file.contents = data;
95-
cb(null, file);
96-
})
97-
.catch(error => {
98-
cb(new PluginError(PLUGIN_NAME, error, {fileName: file.path}));
99-
});
100-
}, cb => {
97+
callback(null, file);
98+
} catch (error) {
99+
callback(new PluginError(PLUGIN_NAME, error, {fileName: file.path}));
100+
}
101+
})();
102+
}, callback => {
101103
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
102104
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
103105

@@ -106,7 +108,7 @@ module.exports = (plugins, options) => {
106108
}
107109

108110
log(`${PLUGIN_NAME}:`, msg);
109-
cb();
111+
callback();
110112
});
111113
};
112114

‎package.json

+7-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"url": "sindresorhus.com"
1111
},
1212
"engines": {
13-
"node": ">=6"
13+
"node": ">=8"
1414
},
1515
"scripts": {
1616
"test": "xo && ava"
@@ -38,18 +38,17 @@
3838
"chalk": "^2.4.1",
3939
"fancy-log": "^1.3.2",
4040
"plugin-error": "^1.0.1",
41-
"imagemin": "^6.0.0",
41+
"imagemin": "^6.1.0",
4242
"plur": "^3.0.1",
4343
"pretty-bytes": "^5.1.0",
4444
"through2-concurrent": "^2.0.0"
4545
},
4646
"devDependencies": {
47-
"ava": "^0.25.0",
48-
"get-stream": "^4.1.0",
49-
"imagemin-pngquant": "^6.0.0",
50-
"pify": "^4.0.1",
51-
"xo": "^0.23.0",
52-
"vinyl": "^2.2.0"
47+
"ava": "^1.4.1",
48+
"get-stream": "^5.1.0",
49+
"imagemin-pngquant": "^7.0.0",
50+
"vinyl": "^2.2.0",
51+
"xo": "^0.24.0"
5352
},
5453
"optionalDependencies": {
5554
"imagemin-gifsicle": "^6.0.1",

‎readme.md

+2-13
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
55
*Issues with the output should be reported on the [`imagemin` issue tracker](https://github.com/imagemin/imagemin/issues).*
66

7-
---
8-
9-
<p align="center"><sup>🦄 Support <a href="https://github.com/sindresorhus">my open-source work</a> by buying this awesome video course:</sup><br><b><a href="https://learnnode.com/friend/AWESOME">Learn to build apps and APIs with Node.js</a> by Wes Bos</b><br><sub>Try his free <a href="https://javascript30.com/friend/AWESOME">JavaScript 30</a> course for a taste of what to expect & check out his <a href="https://ES6.io/friend/AWESOME">ES6</a>, <a href="https://ReactForBeginners.com/friend/AWESOME">React</a>, <a href="https://SublimeTextBook.com/friend/AWESOME">Sublime</a> courses.</sub></p>
10-
11-
---
12-
137

148
## Install
159

@@ -98,7 +92,7 @@ Comes bundled with the following **lossless** optimizers:
9892

9993
These are bundled for convenience and most users will not need anything else.
10094

101-
### imagemin([plugins], [options])
95+
### imagemin(plugins?, options?)
10296

10397
Unsupported files are ignored.
10498

@@ -111,7 +105,7 @@ Default: `[imagemin.gifsicle(), imagemin.jpegtran(), imagemin.optipng(), imagemi
111105

112106
#### options
113107

114-
Type: `Object`
108+
Type: `object`
115109

116110
##### verbose
117111

@@ -124,8 +118,3 @@ Enabling this will log info on every image passed to `gulp-imagemin`:
124118
gulp-imagemin: ✔ image1.png (already optimized)
125119
gulp-imagemin: ✔ image2.png (saved 91 B - 0.4%)
126120
```
127-
128-
129-
## License
130-
131-
MIT © [Sindre Sorhus](https://sindresorhus.com)

‎test.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1+
import {promisify} from 'util';
12
import fs from 'fs';
23
import path from 'path';
34
import imageminPngquant from 'imagemin-pngquant';
4-
import pify from 'pify';
55
import Vinyl from 'vinyl';
66
import getStream from 'get-stream';
77
import test from 'ava';
8-
import m from '.';
8+
import gulpImagemin from '.';
99

10-
const fsP = pify(fs);
10+
const readFile = promisify(fs.readFile);
1111

1212
const createFixture = async (plugins, file = 'fixture.png') => {
13-
const buf = await fsP.readFile(path.join(__dirname, file));
14-
const stream = m(plugins);
13+
const buffer = await readFile(path.join(__dirname, file));
14+
const stream = gulpImagemin(plugins);
1515

1616
stream.end(new Vinyl({
1717
path: path.join(__dirname, file),
18-
contents: buf
18+
contents: buffer
1919
}));
2020

21-
return {buf, stream};
21+
return {buffer, stream};
2222
};
2323

2424
test('minify images', async t => {
25-
const {buf, stream} = await createFixture();
25+
const {buffer, stream} = await createFixture();
2626
const file = await getStream.array(stream);
2727

28-
t.true(file[0].contents.length < buf.length);
28+
t.true(file[0].contents.length < buffer.length);
2929
});
3030

3131
test('use custom plugins', async t => {
@@ -44,7 +44,7 @@ test('use custom svgo settings', async t => {
4444
pretty: true
4545
}
4646
};
47-
const {stream} = await createFixture([m.svgo(svgoOpts)], 'fixture-svg-logo.svg');
47+
const {stream} = await createFixture([gulpImagemin.svgo(svgoOpts)], 'fixture-svg-logo.svg');
4848
const compareStream = (await createFixture(null, 'fixture-svg-logo.svg')).stream;
4949
const file = await getStream.array(stream);
5050
const compareFile = await getStream.array(compareStream);
@@ -53,7 +53,7 @@ test('use custom svgo settings', async t => {
5353
});
5454

5555
test('skip unsupported images', async t => {
56-
const stream = m();
56+
const stream = gulpImagemin();
5757
stream.end(new Vinyl({path: path.join(__dirname, 'fixture.bmp')}));
5858
const file = await getStream.array(stream);
5959

0 commit comments

Comments
 (0)
Please sign in to comment.