(plugins, opts) => {
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
opts = plugins;
plugins = null;
}
opts = Object.assign({
// TODO: remove this when gulp get's a real logger with levels
verbose: process.argv.indexOf('--verbose') !== -1
}, opts);
const validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;
return through.obj({
maxConcurrency: 8
}, (file, enc, cb) => {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-imagemin', '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)}`);
}
cb(null, file);
return;
}
const use = plugins || getDefaultPlugins();
imagemin.buffer(file.contents, {use})
.then(data => {
const originalSize = file.contents.length;
const optimizedSize = data.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';
totalBytes += originalSize;
totalSavedBytes += saved;
totalFiles++;
if (opts.verbose) {
gutil.log('gulp-imagemin:', chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
}
file.contents = data;
cb(null, file); // eslint-disable-line promise/no-callback-in-promise
})
.catch(err => {
// TODO: remove this setImmediate when gulp 4 is targeted
setImmediate(cb, new gutil.PluginError('gulp-imagemin', err, {fileName: file.path}));
});
}, cb => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
if (totalFiles > 0) {
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}
gutil.log('gulp-imagemin:', msg);
cb();
});
}n/a
gifsicle = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
```
### Custom plugin options
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
...(plugins, opts) => {
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
opts = plugins;
plugins = null;
}
opts = Object.assign({
// TODO: remove this when gulp get's a real logger with levels
verbose: process.argv.indexOf('--verbose') !== -1
}, opts);
const validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;
return through.obj({
maxConcurrency: 8
}, (file, enc, cb) => {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-imagemin', '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)}`);
}
cb(null, file);
return;
}
const use = plugins || getDefaultPlugins();
imagemin.buffer(file.contents, {use})
.then(data => {
const originalSize = file.contents.length;
const optimizedSize = data.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';
totalBytes += originalSize;
totalSavedBytes += saved;
totalFiles++;
if (opts.verbose) {
gutil.log('gulp-imagemin:', chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
}
file.contents = data;
cb(null, file); // eslint-disable-line promise/no-callback-in-promise
})
.catch(err => {
// TODO: remove this setImmediate when gulp 4 is targeted
setImmediate(cb, new gutil.PluginError('gulp-imagemin', err, {fileName: file.path}));
});
}, cb => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
if (totalFiles > 0) {
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}
gutil.log('gulp-imagemin:', msg);
cb();
});
}n/a
jpegtran = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
### Custom plugin options
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
...optipng = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
### Custom plugin options
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
...svgo = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
```js
...(plugins, opts) => {
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
opts = plugins;
plugins = null;
}
opts = Object.assign({
// TODO: remove this when gulp get's a real logger with levels
verbose: process.argv.indexOf('--verbose') !== -1
}, opts);
const validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
let totalBytes = 0;
let totalSavedBytes = 0;
let totalFiles = 0;
return through.obj({
maxConcurrency: 8
}, (file, enc, cb) => {
if (file.isNull()) {
cb(null, file);
return;
}
if (file.isStream()) {
cb(new gutil.PluginError('gulp-imagemin', '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)}`);
}
cb(null, file);
return;
}
const use = plugins || getDefaultPlugins();
imagemin.buffer(file.contents, {use})
.then(data => {
const originalSize = file.contents.length;
const optimizedSize = data.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';
totalBytes += originalSize;
totalSavedBytes += saved;
totalFiles++;
if (opts.verbose) {
gutil.log('gulp-imagemin:', chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
}
file.contents = data;
cb(null, file); // eslint-disable-line promise/no-callback-in-promise
})
.catch(err => {
// TODO: remove this setImmediate when gulp 4 is targeted
setImmediate(cb, new gutil.PluginError('gulp-imagemin', err, {fileName: file.path}));
});
}, cb => {
const percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
let msg = `Minified ${totalFiles} ${plur('image', totalFiles)}`;
if (totalFiles > 0) {
msg += chalk.gray(` (saved ${prettyBytes(totalSavedBytes)} - ${percent.toFixed(1).replace(/\.0$/, '')}%)`);
}
gutil.log('gulp-imagemin:', msg);
cb();
});
}n/a
gifsicle = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
```
### Custom plugin options
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
...jpegtran = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
### Custom plugin options
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
...optipng = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
### Custom plugin options
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
...svgo = function () {
const args = [].slice.call(arguments);
return loadPlugin(plugin, args);
}...
```js
…
.pipe(imagemin([
imagemin.gifsicle({interlaced: true}),
imagemin.jpegtran({progressive: true}),
imagemin.optipng({optimizationLevel: 5}),
imagemin.svgo({plugins: [{removeViewBox: true}]})
]))
…
```
Note that you may come across an older, implicit syntax. In versions < 3, the same was written like this:
```js
...