Galerie und tage
This commit is contained in:
173
node_modules/gulp-imagemin/index.js
generated
vendored
173
node_modules/gulp-imagemin/index.js
generated
vendored
@@ -1,124 +1,91 @@
|
||||
const path = require('path');
|
||||
const log = require('fancy-log');
|
||||
const PluginError = require('plugin-error');
|
||||
const through = require('through2-concurrent');
|
||||
const prettyBytes = require('pretty-bytes');
|
||||
const chalk = require('chalk');
|
||||
const imagemin = require('imagemin');
|
||||
const plur = require('plur');
|
||||
'use strict';
|
||||
var path = require('path');
|
||||
var gutil = require('gulp-util');
|
||||
var through = require('through2-concurrent');
|
||||
var assign = require('object-assign');
|
||||
var prettyBytes = require('pretty-bytes');
|
||||
var chalk = require('chalk');
|
||||
var Imagemin = require('imagemin');
|
||||
var plur = require('plur');
|
||||
|
||||
const PLUGIN_NAME = 'gulp-imagemin';
|
||||
const defaultPlugins = ['gifsicle', 'jpegtran', 'optipng', 'svgo'];
|
||||
module.exports = function (opts) {
|
||||
opts = assign({
|
||||
// TODO: remove this when gulp get's a real logger with levels
|
||||
verbose: process.argv.indexOf('--verbose') !== -1
|
||||
}, opts);
|
||||
|
||||
const loadPlugin = (plugin, ...args) => {
|
||||
try {
|
||||
return require(`imagemin-${plugin}`)(...args);
|
||||
} catch (error) {
|
||||
log(`${PLUGIN_NAME}: Couldn't load default plugin "${plugin}"`);
|
||||
}
|
||||
};
|
||||
var totalBytes = 0;
|
||||
var totalSavedBytes = 0;
|
||||
var totalFiles = 0;
|
||||
var validExts = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
|
||||
|
||||
const exposePlugin = plugin => (...args) => loadPlugin(plugin, ...args);
|
||||
|
||||
const getDefaultPlugins = () =>
|
||||
defaultPlugins.reduce((plugins, plugin) => {
|
||||
const instance = loadPlugin(plugin);
|
||||
|
||||
if (!instance) {
|
||||
return plugins;
|
||||
}
|
||||
|
||||
return plugins.concat(instance);
|
||||
}, []);
|
||||
|
||||
module.exports = (plugins, options) => {
|
||||
if (typeof plugins === 'object' && !Array.isArray(plugins)) {
|
||||
options = plugins;
|
||||
plugins = null;
|
||||
}
|
||||
|
||||
options = {
|
||||
// TODO: Remove this when Gulp gets a real logger with levels
|
||||
silent: process.argv.includes('--silent'),
|
||||
verbose: process.argv.includes('--verbose'),
|
||||
...options
|
||||
};
|
||||
|
||||
const validExtensions = ['.jpg', '.jpeg', '.png', '.gif', '.svg'];
|
||||
|
||||
let totalBytes = 0;
|
||||
let totalSavedBytes = 0;
|
||||
let totalFiles = 0;
|
||||
|
||||
return through.obj({
|
||||
maxConcurrency: 8
|
||||
}, (file, encoding, callback) => {
|
||||
return through.obj(function (file, enc, cb) {
|
||||
if (file.isNull()) {
|
||||
callback(null, file);
|
||||
cb(null, file);
|
||||
return;
|
||||
}
|
||||
|
||||
if (file.isStream()) {
|
||||
callback(new PluginError(PLUGIN_NAME, 'Streaming not supported'));
|
||||
cb(new gutil.PluginError('gulp-imagemin', 'Streaming not supported'));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!validExtensions.includes(path.extname(file.path).toLowerCase())) {
|
||||
if (options.verbose) {
|
||||
log(`${PLUGIN_NAME}: Skipping unsupported image ${chalk.blue(file.relative)}`);
|
||||
if (validExts.indexOf(path.extname(file.path).toLowerCase()) === -1) {
|
||||
if (opts.verbose) {
|
||||
gutil.log('gulp-imagemin: Skipping unsupported image ' + chalk.blue(file.relative));
|
||||
}
|
||||
|
||||
callback(null, file);
|
||||
cb(null, file);
|
||||
return;
|
||||
}
|
||||
|
||||
const localPlugins = plugins || getDefaultPlugins();
|
||||
var imagemin = new Imagemin()
|
||||
.src(file.contents)
|
||||
.use(Imagemin.gifsicle({interlaced: opts.interlaced}))
|
||||
.use(Imagemin.jpegtran({progressive: opts.progressive}))
|
||||
.use(Imagemin.optipng({optimizationLevel: opts.optimizationLevel}))
|
||||
.use(Imagemin.svgo({
|
||||
plugins: opts.svgoPlugins || [],
|
||||
multipass: opts.multipass
|
||||
}));
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
const data = await imagemin.buffer(file.contents, {
|
||||
plugins: localPlugins
|
||||
});
|
||||
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';
|
||||
|
||||
if (saved > 0) {
|
||||
totalBytes += originalSize;
|
||||
totalSavedBytes += saved;
|
||||
totalFiles++;
|
||||
}
|
||||
|
||||
if (options.verbose) {
|
||||
log(`${PLUGIN_NAME}:`, chalk.green('✔ ') + file.relative + chalk.gray(` (${msg})`));
|
||||
}
|
||||
|
||||
file.contents = data;
|
||||
callback(null, file);
|
||||
} catch (error) {
|
||||
callback(new PluginError(PLUGIN_NAME, error, {fileName: file.path}));
|
||||
}
|
||||
})();
|
||||
}, callback => {
|
||||
if (!options.silent) {
|
||||
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$/, '')}%)`);
|
||||
}
|
||||
|
||||
log(`${PLUGIN_NAME}:`, msg);
|
||||
if (opts.use) {
|
||||
opts.use.forEach(imagemin.use.bind(imagemin));
|
||||
}
|
||||
|
||||
callback();
|
||||
imagemin.run(function (err, files) {
|
||||
if (err) {
|
||||
cb(new gutil.PluginError('gulp-imagemin:', err, {fileName: file.path}));
|
||||
return;
|
||||
}
|
||||
|
||||
var originalSize = file.contents.length;
|
||||
var optimizedSize = files[0].contents.length;
|
||||
var saved = originalSize - optimizedSize;
|
||||
var percent = originalSize > 0 ? (saved / originalSize) * 100 : 0;
|
||||
var savedMsg = 'saved ' + prettyBytes(saved) + ' - ' + percent.toFixed(1).replace(/\.0$/, '') + '%';
|
||||
var 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 = files[0].contents;
|
||||
cb(null, file);
|
||||
});
|
||||
}, function (cb) {
|
||||
var percent = totalBytes > 0 ? (totalSavedBytes / totalBytes) * 100 : 0;
|
||||
var 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();
|
||||
});
|
||||
};
|
||||
|
||||
module.exports.gifsicle = exposePlugin('gifsicle');
|
||||
module.exports.jpegtran = exposePlugin('jpegtran');
|
||||
module.exports.optipng = exposePlugin('optipng');
|
||||
module.exports.svgo = exposePlugin('svgo');
|
||||
|
||||
Reference in New Issue
Block a user