Skip to content

Commit

Permalink
ES2015ify and remove support for Node.js versions older than v4 (#379)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevva authored and sindresorhus committed Jun 18, 2017
1 parent 1f245ac commit 65ff84e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 77 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ node_js:
- '8'
- '6'
- '4'
- '0.12'
- '0.10'
2 changes: 0 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ environment:
- nodejs_version: '8'
- nodejs_version: '6'
- nodejs_version: '4'
- nodejs_version: '0.12'
- nodejs_version: '0.10'
install:
- ps: Install-Product node $env:nodejs_version
- set CI=true
Expand Down
2 changes: 1 addition & 1 deletion gruntfile.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';
module.exports = function (grunt) {
module.exports = grunt => {
grunt.initConfig({
imagemin: {
dist: {
Expand Down
22 changes: 14 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"url": "http://gruntjs.com/"
},
"engines": {
"node": ">=0.10.0"
"node": ">=4"
},
"scripts": {
"test": "xo && grunt && ava && del-cli tmp"
Expand All @@ -30,18 +30,24 @@
"svg"
],
"dependencies": {
"async": "^1.5.2",
"chalk": "^1.0.0",
"gulp-rename": "^1.2.0",
"imagemin": "^4.0.0",
"pretty-bytes": "^3.0.1"
"imagemin": "^5.3.1",
"p-map": "^1.1.1",
"plur": "^2.1.2",
"pretty-bytes": "^4.0.2"
},
"devDependencies": {
"ava": "^0.17.0",
"del-cli": "^0.2.1",
"ava": "*",
"del-cli": "^1.0.0",
"grunt": "^1.0.0",
"grunt-cli": "^1.2.0",
"xo": "^0.16.0"
"xo": "*"
},
"optionalDependencies": {
"imagemin-gifsicle": "^5.0.0",
"imagemin-jpegtran": "^5.0.0",
"imagemin-optipng": "^5.1.0",
"imagemin-svgo": "^5.1.0"
},
"appveyor_id": "7w491e6edsuanreu"
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ grunt.initConfig({
options: {
optimizationLevel: 3,
svgoPlugins: [{removeViewBox: false}],
use: [mozjpeg()]
use: [mozjpeg()] // Example plugin usage
},
files: {
'dist/img.png': 'src/img.png',
Expand Down
126 changes: 63 additions & 63 deletions tasks/imagemin.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
'use strict';
var fs = require('fs');
var os = require('os');
var path = require('path');
var async = require('async');
var chalk = require('chalk');
var prettyBytes = require('pretty-bytes');
var Imagemin = require('imagemin');
var rename = require('gulp-rename');
const os = require('os');
const chalk = require('chalk');
const imagemin = require('imagemin');
const plur = require('plur');
const prettyBytes = require('pretty-bytes');
const pMap = require('p-map');

module.exports = function (grunt) {
const defaultPlugins = ['gifsicle', 'jpegtran', 'optipng', 'svgo'];

const loadPlugin = (grunt, plugin, opts) => {
try {
return require(`imagemin-${plugin}`).apply(null, opts);
} catch (err) {
grunt.warn(`Couldn't load default plugin "${plugin}"`);
}
};

const getDefaultPlugins = (grunt, opts) => defaultPlugins.reduce((plugins, plugin) => {
const instance = loadPlugin(grunt, plugin, opts);

if (!instance) {
return plugins;
}

return plugins.concat(instance);
}, []);

module.exports = grunt => {
grunt.registerMultiTask('imagemin', 'Minify PNG, JPEG, GIF and SVG images', function () {
var done = this.async();
var files = this.files;
var totalSaved = 0;
var options = this.options({
const done = this.async();
const options = this.options({
interlaced: true,
optimizationLevel: 3,
progressive: true
});

async.eachLimit(files, os.cpus().length, function (file, next) {
var msg;
var imagemin = new Imagemin()
.src(file.src[0])
.dest(path.dirname(file.dest))
.use(Imagemin.jpegtran(options))
.use(Imagemin.gifsicle(options))
.use(Imagemin.optipng(options))
.use(Imagemin.svgo({plugins: options.svgoPlugins || []}));

if (options.use) {
options.use.forEach(imagemin.use.bind(imagemin));
}
if (Array.isArray(options.svgoPlugins)) {
options.plugins = options.svgoPlugins;
}

if (path.basename(file.src[0]) !== path.basename(file.dest)) {
imagemin.use(rename(path.basename(file.dest)));
}
const plugins = options.use || getDefaultPlugins(grunt, options);

fs.stat(file.src[0], function (err, stats) {
if (err) {
grunt.warn(err + ' in file ' + file.src[0]);
return next();
}
let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;

imagemin.run(function (err, data) {
if (err) {
grunt.warn(err + ' in file ' + file.src[0]);
return next();
}
const processFile = file => Promise.resolve(grunt.file.read(file.src[0], {encoding: null}))
.then(buf => Promise.all([imagemin.buffer(buf, {plugins}), buf]))
.then(res => {
const optimizedBuf = res[0];
const originalBuf = res[1];
const originalSize = originalBuf.length;
const optimizedSize = optimizedBuf.length;
const saved = originalSize - optimizedSize;
const percent = originalSize > 0 ? (saved / originalSize) * 100 : 0;
const savedMsg = `saved ${prettyBytes(saved)} - ${percent.toFixed(1).replace(/\.0$/, '')}%`;
const msg = saved > 0 ? savedMsg : 'already optimized';

var origSize = stats.size;
var diffSize = origSize - ((data[0].contents && data[0].contents.length) || 0);
if (saved > 0) {
totalBytes += originalSize;
totalSavedBytes += saved;
totalFiles++;
}

totalSaved += diffSize;
grunt.file.write(file.dest, optimizedBuf);
grunt.verbose.writeln(chalk.green('✔ ') + file.src[0] + chalk.gray(` (${msg})`));
})
.catch(err => {
grunt.warn(`${err} in file ${file.src[0]}`);
});

if (diffSize < 10) {
msg = 'already optimized';
} else {
msg = [
'saved ' + prettyBytes(diffSize) + ' -',
(diffSize / origSize * 100).toFixed() + '%'
].join(' ');
}
pMap(this.files, processFile, {concurrency: os.cpus().length}).then(() => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;

grunt.verbose.writeln(chalk.green('✔ ') + file.src[0] + chalk.gray(' (' + msg + ')'));
process.nextTick(next);
});
});
}, function (err) {
if (err) {
grunt.warn(err);
if (totalFiles > 0) {
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}

var msg = [
'Minified ' + files.length,
files.length === 1 ? 'image' : 'images',
chalk.gray('(saved ' + prettyBytes(totalSaved) + ')')
].join(' ');

grunt.log.writeln(msg);
done();
});
Expand Down

0 comments on commit 65ff84e

Please sign in to comment.