Skip to content

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 30, 2019
1 parent c1c3346 commit 6e091ed
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 51 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
38 changes: 20 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,43 +37,45 @@ module.exports = (plugins, options) => {
plugins = null;
}

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

const validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
const validExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];

let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;

return through.obj({
maxConcurrency: 8
}, (file, enc, cb) => {
}, (file, encoding, callback) => {
if (file.isNull()) {
cb(null, file);
callback(null, file);
return;
}

if (file.isStream()) {
cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}

if (!validExts.includes(path.extname(file.path).toLowerCase())) {
if (!validExtensions.includes(path.extname(file.path).toLowerCase())) {
if (options.verbose) {
log(`${PLUGIN_NAME}: Skipping unsupported image ${chalk.blue(file.relative)}`);
}

cb(null, file);
callback(null, file);
return;
}

const use = plugins || getDefaultPlugins();

imagemin.buffer(file.contents, {use})
.then(data => {
(async () => {
try {
const data = await imagemin.buffer(file.contents, {use});
const originalSize = file.contents.length;
const optimizedSize = data.length;
const saved = originalSize - optimizedSize;
Expand All @@ -92,12 +94,12 @@ module.exports = (plugins, options) => {
}

file.contents = data;
cb(null, file);
})
.catch(error => {
cb(new PluginError(PLUGIN_NAME, error, {fileName: file.path}));
});
}, cb => {
callback(null, file);
} catch (error) {
callback(new PluginError(PLUGIN_NAME, error, {fileName: file.path}));
}
})();
}, callback => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;

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

log(`${PLUGIN_NAME}:`, msg);
cb();
callback();
});
};

Expand Down
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand Down Expand Up @@ -38,18 +38,17 @@
"chalk": "^2.4.1",
"fancy-log": "^1.3.2",
"plugin-error": "^1.0.1",
"imagemin": "^6.0.0",
"imagemin": "^6.1.0",
"plur": "^3.0.1",
"pretty-bytes": "^5.1.0",
"through2-concurrent": "^2.0.0"
},
"devDependencies": {
"ava": "^0.25.0",
"get-stream": "^4.1.0",
"imagemin-pngquant": "^6.0.0",
"pify": "^4.0.1",
"xo": "^0.23.0",
"vinyl": "^2.2.0"
"ava": "^1.4.1",
"get-stream": "^5.1.0",
"imagemin-pngquant": "^7.0.0",
"vinyl": "^2.2.0",
"xo": "^0.24.0"
},
"optionalDependencies": {
"imagemin-gifsicle": "^6.0.1",
Expand Down
15 changes: 2 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@
*Issues with the output should be reported on the [`imagemin` issue tracker](https://github.com/imagemin/imagemin/issues).*

---

<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>

---


## Install

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

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

### imagemin([plugins], [options])
### imagemin(plugins?, options?)

Unsupported files are ignored.

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

#### options

Type: `Object`
Type: `object`

##### verbose

Expand All @@ -124,8 +118,3 @@ Enabling this will log info on every image passed to `gulp-imagemin`:
gulp-imagemin: ✔ image1.png (already optimized)
gulp-imagemin: ✔ image2.png (saved 91 B - 0.4%)
```


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
22 changes: 11 additions & 11 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import {promisify} from 'util';
import fs from 'fs';
import path from 'path';
import imageminPngquant from 'imagemin-pngquant';
import pify from 'pify';
import Vinyl from 'vinyl';
import getStream from 'get-stream';
import test from 'ava';
import m from '.';
import gulpImagemin from '.';

const fsP = pify(fs);
const readFile = promisify(fs.readFile);

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

stream.end(new Vinyl({
path: path.join(__dirname, file),
contents: buf
contents: buffer
}));

return {buf, stream};
return {buffer, stream};
};

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

t.true(file[0].contents.length < buf.length);
t.true(file[0].contents.length < buffer.length);
});

test('use custom plugins', async t => {
Expand All @@ -44,7 +44,7 @@ test('use custom svgo settings', async t => {
pretty: true
}
};
const {stream} = await createFixture([m.svgo(svgoOpts)], 'fixture-svg-logo.svg');
const {stream} = await createFixture([gulpImagemin.svgo(svgoOpts)], 'fixture-svg-logo.svg');
const compareStream = (await createFixture(null, 'fixture-svg-logo.svg')).stream;
const file = await getStream.array(stream);
const compareFile = await getStream.array(compareStream);
Expand All @@ -53,7 +53,7 @@ test('use custom svgo settings', async t => {
});

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

Expand Down

0 comments on commit 6e091ed

Please sign in to comment.