Skip to content

Commit

Permalink
Use a constant for the plugin name (sindresorhus#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
ulivz authored and sindresorhus committed Sep 22, 2017
1 parent ac05168 commit efc4148
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ const prettyBytes = require('pretty-bytes');
const chalk = require('chalk');
const imagemin = require('imagemin');
const plur = require('plur');
const PLUGIN_NAME = 'gulp-imagemin';

const defaultPlugins = ['gifsicle', 'jpegtran', 'optipng', 'svgo'];

const loadPlugin = (plugin, args) => {
try {
return require(`imagemin-${plugin}`).apply(null, args);
} catch (err) {
gutil.log(`gulp-imagemin: Couldn't load default plugin "${plugin}"`);
gutil.log(`${PLUGIN_NAME}: Couldn't load default plugin "${plugin}"`);
}
};

Expand Down Expand Up @@ -60,13 +61,13 @@ module.exports = (plugins, opts) => {
}

if (file.isStream()) {
cb(new gutil.PluginError('gulp-imagemin', 'Streaming not supported'));
cb(new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
return;
}

if (validExts.indexOf(path.extname(file.path).toLowerCase()) === -1) {
if (opts.verbose) {
gutil.log(`gulp-imagemin: Skipping unsupported image ${chalk.blue(file.relative)}`);
gutil.log(`${PLUGIN_NAME}: Skipping unsupported image ${chalk.blue(file.relative)}`);
}

cb(null, file);
Expand All @@ -91,15 +92,15 @@ module.exports = (plugins, opts) => {
}

if (opts.verbose) {
gutil.log('gulp-imagemin:', chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
gutil.log(PLUGIN_NAME + ':', chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
}

file.contents = data;
cb(null, file);
})
.catch(err => {
// TODO: remove this setImmediate when gulp 4 is targeted
setImmediate(cb, new gutil.PluginError('gulp-imagemin', err, {fileName: file.path}));
setImmediate(cb, new gutil.PluginError(PLUGIN_NAME, err, {fileName: file.path}));
});
}, cb => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
Expand All @@ -109,7 +110,7 @@ module.exports = (plugins, opts) => {
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}

gutil.log('gulp-imagemin:', msg);
gutil.log(PLUGIN_NAME + ':', msg);
cb();
});
};
Expand Down

0 comments on commit efc4148

Please sign in to comment.