From 6e091edf2d3b4c4c6b9aa51f2ee993b4b7dd3d74 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 30 May 2019 17:25:57 +0700 Subject: [PATCH] Require Node.js 8 --- .travis.yml | 2 +- index.js | 38 ++++++++++++++++++++------------------ package.json | 15 +++++++-------- readme.md | 15 ++------------- test.js | 22 +++++++++++----------- 5 files changed, 41 insertions(+), 51 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2ae9d62..f98fed0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: + - '12' - '10' - '8' - - '6' diff --git a/index.js b/index.js index 19e4b4b..530929e 100644 --- a/index.js +++ b/index.js @@ -37,12 +37,13 @@ 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; @@ -50,30 +51,31 @@ module.exports = (plugins, options) => { 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; @@ -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)}`; @@ -106,7 +108,7 @@ module.exports = (plugins, options) => { } log(`${PLUGIN_NAME}:`, msg); - cb(); + callback(); }); }; diff --git a/package.json b/package.json index 2316ee9..30e6a3e 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=6" + "node": ">=8" }, "scripts": { "test": "xo && ava" @@ -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", diff --git a/readme.md b/readme.md index 9c30230..96b4afc 100644 --- a/readme.md +++ b/readme.md @@ -4,12 +4,6 @@ *Issues with the output should be reported on the [`imagemin` issue tracker](https://github.com/imagemin/imagemin/issues).* ---- - -

🦄 Support my open-source work by buying this awesome video course:
Learn to build apps and APIs with Node.js by Wes Bos
Try his free JavaScript 30 course for a taste of what to expect & check out his ES6, React, Sublime courses.

- ---- - ## Install @@ -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. @@ -111,7 +105,7 @@ Default: `[imagemin.gifsicle(), imagemin.jpegtran(), imagemin.optipng(), imagemi #### options -Type: `Object` +Type: `object` ##### verbose @@ -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) diff --git a/test.js b/test.js index d7412e2..6618795 100644 --- a/test.js +++ b/test.js @@ -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 => { @@ -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); @@ -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);