|
@@ -15,6 +15,7 @@ var request = require('request');
|
|
var md5 = require('md5');
|
|
var md5 = require('md5');
|
|
|
|
|
|
var imageOptimizer = require('./imageOptimizer');
|
|
var imageOptimizer = require('./imageOptimizer');
|
|
|
|
+var imageReformater = require('./imageReformater');
|
|
var fileMinifier = require('./fileMinifier');
|
|
var fileMinifier = require('./fileMinifier');
|
|
var gzipCompressor = require('./gzipCompressor');
|
|
var gzipCompressor = require('./gzipCompressor');
|
|
var brotliCompressor = require('./brotliCompressor');
|
|
var brotliCompressor = require('./brotliCompressor');
|
|
@@ -78,6 +79,10 @@ var Redownload = function() {
|
|
|
|
|
|
.then(imageOptimizer.optimizeImage)
|
|
.then(imageOptimizer.optimizeImage)
|
|
|
|
|
|
|
|
+ .then(function(entry) {
|
|
|
|
+ return Q(imageReformater.reformatImage(entry));
|
|
|
|
+ })
|
|
|
|
+
|
|
.then(imageDimensions.getDimensions)
|
|
.then(imageDimensions.getDimensions)
|
|
|
|
|
|
.then(fileMinifier.minifyFile)
|
|
.then(fileMinifier.minifyFile)
|
|
@@ -91,7 +96,7 @@ var Redownload = function() {
|
|
})
|
|
})
|
|
|
|
|
|
.then(function(newEntry) {
|
|
.then(function(newEntry) {
|
|
- debug('File %s - Redownloaded, optimized, minified, compressed, analyzed: done', entry.url);
|
|
|
|
|
|
+ debug('File %s - Redownloaded, optimized, reformated, minified, compressed, analyzed: done', entry.url);
|
|
|
|
|
|
// For the progress bar
|
|
// For the progress bar
|
|
doneCount ++;
|
|
doneCount ++;
|
|
@@ -154,6 +159,10 @@ var Redownload = function() {
|
|
offenders.imageOptimization = listImagesNotOptimized(results);
|
|
offenders.imageOptimization = listImagesNotOptimized(results);
|
|
metrics.imageOptimization = offenders.imageOptimization.totalGain;
|
|
metrics.imageOptimization = offenders.imageOptimization.totalGain;
|
|
|
|
|
|
|
|
+ // Old image formats
|
|
|
|
+ offenders.oldImageFormats = listImagesWithOldFormats(results);
|
|
|
|
+ metrics.oldImageFormats = offenders.oldImageFormats.totalGain;
|
|
|
|
+
|
|
// Image width
|
|
// Image width
|
|
offenders.imagesTooLarge = listImagesTooLarge(results, data.params.options.device);
|
|
offenders.imagesTooLarge = listImagesTooLarge(results, data.params.options.device);
|
|
metrics.imagesTooLarge = offenders.imagesTooLarge.length;
|
|
metrics.imagesTooLarge = offenders.imagesTooLarge.length;
|
|
@@ -398,6 +407,67 @@ var Redownload = function() {
|
|
return results;
|
|
return results;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ function listImagesWithOldFormats(requests) {
|
|
|
|
+ var results = {
|
|
|
|
+ totalGain: 0,
|
|
|
|
+ images: []
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ requests.forEach(function(req) {
|
|
|
|
+
|
|
|
|
+ if (req.weightCheck.bodySize > 0 &&
|
|
|
|
+ imageReformater.entryTypeCanBeReformated(req) &&
|
|
|
|
+ (req.weightCheck.webpSize > 0 || req.weightCheck.avifSize > 0)) {
|
|
|
|
+
|
|
|
|
+ var image = {
|
|
|
|
+ url: req.url,
|
|
|
|
+ originalWeigth: req.weightCheck.bodySize,
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ switch (req.contentType) {
|
|
|
|
+ case 'image/jpeg':
|
|
|
|
+ image.originalFormat = 'JPEG';
|
|
|
|
+ break;
|
|
|
|
+ case 'image/png':
|
|
|
|
+ image.originalFormat = 'PNG';
|
|
|
|
+ break;
|
|
|
|
+ case 'image/gif':
|
|
|
|
+ image.originalFormat = 'GIF';
|
|
|
|
+ break;
|
|
|
|
+ case 'image/webp':
|
|
|
|
+ image.originalFormat = 'WebP';
|
|
|
|
+ break;
|
|
|
|
+ case 'image/avif':
|
|
|
|
+ image.originalFormat = 'AVIF';
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (req.weightCheck.webpSize) {
|
|
|
|
+ image.webpSize = req.weightCheck.webpSize;
|
|
|
|
+ image.webpGain = req.weightCheck.bodySize - req.weightCheck.webpSize;
|
|
|
|
+
|
|
|
|
+ image.bestFormat = 'WebP';
|
|
|
|
+ image.maxGain = image.webpGain;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (req.weightCheck.avifSize) {
|
|
|
|
+ image.avifSize = req.weightCheck.avifSize;
|
|
|
|
+ image.avifGain = req.weightCheck.bodySize - req.weightCheck.avifSize;
|
|
|
|
+
|
|
|
|
+ if (!req.weightCheck.webpSize || req.weightCheck.webpSize > req.weightCheck.avifSize) {
|
|
|
|
+ image.bestFormat = 'AVIF';
|
|
|
|
+ image.maxGain = image.avifGain;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ results.totalGain += image.maxGain;
|
|
|
|
+ results.images.push(image);
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return results;
|
|
|
|
+ }
|
|
|
|
+
|
|
function listImagesTooLarge(requests, device) {
|
|
function listImagesTooLarge(requests, device) {
|
|
var results = [];
|
|
var results = [];
|
|
|
|
|
|
@@ -819,9 +889,9 @@ var Redownload = function() {
|
|
|
|
|
|
debug('Downloading %s', entry.url);
|
|
debug('Downloading %s', entry.url);
|
|
|
|
|
|
- // Always add compression and webp headers before sending, in case the server listens to them
|
|
|
|
|
|
+ // Always add compression and webp/avif headers before sending, in case the server listens to them
|
|
var reqHeaders = [];
|
|
var reqHeaders = [];
|
|
- reqHeaders['Accept'] = '*/*,image/webp';
|
|
|
|
|
|
+ reqHeaders['Accept'] = '*/*,image/webp,image/avif';
|
|
reqHeaders['Accept-Encoding'] = 'gzip, deflate, br';
|
|
reqHeaders['Accept-Encoding'] = 'gzip, deflate, br';
|
|
reqHeaders['Connection'] = 'keep-alive';
|
|
reqHeaders['Connection'] = 'keep-alive';
|
|
reqHeaders['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36';
|
|
reqHeaders['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36';
|