Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'imagemin' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
config = require(path.resolve(dataSource));
} catch (error) {
console.error(`Cannot require "config"\n${error}`); // eslint-disable-line no-console
process.exit(1); // eslint-disable-line no-process-exit
}
opts.plugin = config.plugins;
} else {
opts.plugin = requirePlugins(arrify(opts.plugin));
}
if (Buffer.isBuffer(input)) {
return imagemin
.buffer(input, {
plugins: opts.plugin
})
.then(buf => process.stdout.write(buf));
}
let spinner = null;
if (opts.verbose || opts.silent) {
spinner = ora();
if (opts.verbose) {
spinner.text = "Starting minifying images...";
spinner.start();
}
}
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
interlaced: true
}));
break;
case '.png':
console.log('Minifying PNG File: ' + file);
// OptiPNG options https://github.com/imagemin/imagemin#optipng
imagemin.src(file).dest(file).use(ImageMin.optipng({
optimizationLevel: 2
}));
break;
case '.jpg':
case '.jpeg':
console.log('Minifying JPEG File: ' + file);
// jpegTran options https://github.com/imagemin/imagemin#jpegtran
imagemin.src(file).dest(file).use(ImageMin.jpegtran({
progressive: true
}));
console.log('Minifying JPEG File: ' + file);
break;
default:
console.log('Encountered file with ' + ext + ' extension - not compressing.');
break;
}
}
case '.css':
console.log('Minifying CSS File: ' + file);
var source = fs.readFileSync(file, 'utf8');
var result = cssMinifier.minify(source);
fs.writeFileSync(file, result, 'utf8'); // overwrite the original unminified file
break;
// Image options https://github.com/imagemin/imagemin
case '.svg':
console.log('Minifying SVG File: ' + file);
// svgGo options https://github.com/imagemin/imagemin#svgo
imagemin.src(file).dest(file).use(ImageMin.svgo());
break;
case '.gif':
console.log('Minifying GIF File: ' + file);
// GifSicle options https://github.com/imagemin/imagemin#gifsicle
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
interlaced: true
}));
break;
case '.png':
console.log('Minifying PNG File: ' + file);
// OptiPNG options https://github.com/imagemin/imagemin#optipng
imagemin.src(file).dest(file).use(ImageMin.optipng({
optimizationLevel: 2
}));
break;
case '.jpg':
case '.jpeg':
console.log('Minifying JPEG File: ' + file);
// jpegTran options https://github.com/imagemin/imagemin#jpegtran
imagemin.src(file).dest(file).use(ImageMin.jpegtran({
progressive: true
case '.svg':
console.log('Minifying SVG File: ' + file);
// svgGo options https://github.com/imagemin/imagemin#svgo
imagemin.src(file).dest(file).use(ImageMin.svgo());
break;
case '.gif':
console.log('Minifying GIF File: ' + file);
// GifSicle options https://github.com/imagemin/imagemin#gifsicle
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
interlaced: true
}));
break;
case '.png':
console.log('Minifying PNG File: ' + file);
// OptiPNG options https://github.com/imagemin/imagemin#optipng
imagemin.src(file).dest(file).use(ImageMin.optipng({
optimizationLevel: 2
}));
break;
case '.jpg':
case '.jpeg':
console.log('Minifying JPEG File: ' + file);
// jpegTran options https://github.com/imagemin/imagemin#jpegtran
imagemin.src(file).dest(file).use(ImageMin.jpegtran({
progressive: true
}));
console.log('Minifying JPEG File: ' + file);
break;
default:
console.log('Encountered file with ' + ext + ' extension - not compressing.');
break;
}
},
fromString: true
});
fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
break;
case '.css':
console.log('Minifying CSS File: ' + file);
var source = fs.readFileSync(file, 'utf8');
var result = cssMinifier.minify(source);
fs.writeFileSync(file, result, 'utf8'); // overwrite the original unminified file
break;
// Image options https://github.com/imagemin/imagemin
case '.svg':
console.log('Minifying SVG File: ' + file);
// svgGo options https://github.com/imagemin/imagemin#svgo
imagemin.src(file).dest(file).use(ImageMin.svgo());
break;
case '.gif':
console.log('Minifying GIF File: ' + file);
// GifSicle options https://github.com/imagemin/imagemin#gifsicle
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
interlaced: true
}));
break;
case '.png':
console.log('Minifying PNG File: ' + file);
// OptiPNG options https://github.com/imagemin/imagemin#optipng
imagemin.src(file).dest(file).use(ImageMin.optipng({
optimizationLevel: 2
}));
break;
case '.jpg':
// default optimizers
if(options.gifsicle.enabled !== false)
plugins.push(require('imagemin-gifsicle')(options.gifsicle));
if(options.mozjpeg.enabled !== false)
plugins.push(require('imagemin-mozjpeg')(options.mozjpeg));
if(options.svgo.enabled !== false)
plugins.push(require('imagemin-svgo')(options.svgo));
if(options.pngquant.enabled !== false)
plugins.push(require('imagemin-pngquant')(options.pngquant));
if(options.optipng.enabled !== false)
plugins.push(require('imagemin-optipng')(options.optipng));
// optional optimizers
if(options.webp)
plugins.push(require('imagemin-webp')(options.webp));
imagemin
.buffer(content, {
plugins
})
.then(data => {
callback(null, data);
})
.catch(err => {
callback(err);
});
}
};
}
if (options.webp && ext === '.webp') {
imageminOption.plugins.push(webpmin({quality: options.webpQuality}));
}
// Add additional plugins.
if (options.pngquant && ext === '.png') { // Lossy compression.
imageminOption.plugins.push(pngquant());
}
if (options.jpegrecompress && ['.jpg', '.jpeg'].indexOf(ext) !== -1) {
imageminOption.plugins.push(jpegrecompress({quality: options.jpegrecompressQuality}));
}
return imagemin.buffer(buffer, imageminOption)
.then(function (newBuffer) {
let length = buffer.length;
if (newBuffer && length > newBuffer.length) {
let saved = ((length - newBuffer.length) / length * 100).toFixed(2);
log.log('%s(IMG): %s [ %s saved]', pkg.name, path, saved + '%');
route.set(path, newBuffer); // Update the route.
}
});
});
});
function instantiate(type)
{
var instance;
switch(type)
{
case "gif":
{
if (!gif) gif = new Imagemin().use( Imagemin.gifsicle({interlaced:true}) );
instance = gif;
break;
}
case "jpeg":
{
if (!jpg) jpg = new Imagemin().use( Imagemin.jpegtran({progressive:true}) );
instance = jpg;
break;
}
case "png":
{
if (!png) png = new Imagemin().use( Imagemin.optipng({optimizationLevel:7}) ); // 0-7
instance = png;
break;
}
default:
{
if (!svg)
{
svg = new Imagemin().use( Imagemin.svgo({plugins:[
{
collapseGroups: false,
function instantiate(type)
{
var instance;
switch(type)
{
case "gif":
{
if (!gif) gif = new Imagemin().use( Imagemin.gifsicle({interlaced:true}) );
instance = gif;
break;
}
case "jpeg":
{
if (!jpg) jpg = new Imagemin().use( Imagemin.jpegtran({progressive:true}) );
instance = jpg;
break;
}
case "png":
{
if (!png) png = new Imagemin().use( Imagemin.optipng({optimizationLevel:7}) ); // 0-7
instance = png;
break;
}
default:
{
case "gif":
{
if (!gif) gif = new Imagemin().use( Imagemin.gifsicle({interlaced:true}) );
instance = gif;
break;
}
case "jpeg":
{
if (!jpg) jpg = new Imagemin().use( Imagemin.jpegtran({progressive:true}) );
instance = jpg;
break;
}
case "png":
{
if (!png) png = new Imagemin().use( Imagemin.optipng({optimizationLevel:7}) ); // 0-7
instance = png;
break;
}
default:
{
if (!svg)
{
svg = new Imagemin().use( Imagemin.svgo({plugins:[
{
collapseGroups: false,
convertStyleToAttrs: false, // didn't support base64 strings within inline CSS (svgo v0.4.4)
moveGroupAttrsToElems: false
}]}) );
}
instance = svg;
}