WIP new image analizis module in Phantomas

This commit is contained in:
Gaël Métais 2023-08-14 01:54:17 +02:00
parent 8e941233a0
commit c308850eb3
7 changed files with 117 additions and 61 deletions

View file

@ -736,30 +736,82 @@ var policies = {
"hasOffenders": true,
"unit": 'bytes'
},
"imageOptimization": {
"tool": "redownload",
"imagesNotOptimized": {
"tool": "phantomas",
"label": "Image optimization",
"message": "<p>This metric measures the number of bytes that could be saved by optimizing images.</p><p>Image optimization is generally one of the easiest way to reduce a page weight, and as a result, the page load time. Don't use Photoshop or other image editing tools, they're not very good for optimization. Use specialized tools such as <a href=\"https://kraken.io/\" target=\"_blank\">Kraken.io</a> or the excellent <a href=\"https://imageoptim.com/\" target=\"_blank\">ImageOptim</a> on Mac. For SVG images, you can use <a href=\"https://jakearchibald.github.io/svgomg/\" target=\"_blank\">SVGOMG</a>.</p><p>The tools in use in YellowLabTools are not set to their maximum optimization power (JPEG quality 85), so you might be able to compress even more!</p>",
"isOkThreshold": 20480,
"isOkThreshold": 2048,
"isBadThreshold": 204800,
"isAbnormalThreshold": 307200,
"hasOffenders": true,
"unit": 'bytes'
"unit": 'bytes',
"valueTransformFn": function(offenders) {
let totalGain = 0;
offenders.forEach((offender) => {
offender.gain = offender.fileSize - offender.newFileSize;
totalGain += offender.gain;
});
return totalGain;
},
"offendersTransformFn": function(offenders) {
return offenders;
}
},
"oldImageFormats": {
"imagesOldFormat": {
"tool": "phantomas",
"label": "Old image formats",
"message": "<p>Measures the number of bytes that could be saved by converting images to newer and more efficient formats. The best image format is generally AVIF and the second best is WebP.</p><p>Be careful, you need to provide fallback images for old browsers and search engine bots.</p>",
"isOkThreshold": 30720,
"message": "<p>This metric goes further than \"Image optimization\". Measures the number of bytes that could be saved by converting images to newer and more efficient formats. The best image format is generally AVIF and the second best is WebP.</p><p>Be careful, you need to provide fallback images for old browsers and search engine bots.</p>",
"isOkThreshold": 2048,
"isBadThreshold": 307200,
"isAbnormalThreshold": 512000,
"hasOffenders": true,
"unit": 'bytes'
"unit": 'bytes',
"valueTransformFn": function(offenders) {
let totalGain = 0;
offenders.forEach((offender) => {
offender.gain = offender.fileSize - offender.newFileSize;
totalGain += offender.gain;
});
return totalGain;
},
"offendersTransformFn": function(offenders) {
return offenders;
}
},
"imagesTooLarge": {
"tool": "redownload",
"imagesScaledDown": {
"tool": "phantomas",
"label": "Oversized images",
"message": "<p>This is the number of images with a width >1200px on mobile, >1800px on tablet, >2400 on desktop, >3200px on HD desktop. Try reducing their size.</p><p>Please ignore if the file is used as a sprite.</p>",
"message": "<p>This rule compares the number of pixels in a loaded images to the number of physical pixels it is displayed on. Then it estimates the number of KB that could be saved by serving it with the correct dimensions.</p><p>Of course, it is hard to serve perfect images for all screens. For this reason, this rule is quite permissive.</p>",
"isOkThreshold": 2048,
"isBadThreshold": 307200,
"isAbnormalThreshold": 512000,
"hasOffenders": true,
"unit": 'bytes',
"valueTransformFn": function(offenders) {
let totalGain = 0;
offenders.forEach((offender) => {
offender.gain = offender.fileSize - offender.newFileSize;
totalGain += offender.gain;
});
return totalGain;
},
"offendersTransformFn": function(offenders) {
return offenders;
}
},
"imagesExcessiveDensity": {
"tool": "phantomas",
"label": "Excessive image density",
"message": "<p>Devices with very high pixel density screen (such as 3x or 4x) are programmed to load high density images. This is the normal behavior, however the human eye barely sees the difference over 2x. This metric alerts you if an image density is > 2.2x.</p><p>There is currently no browser functionnality to prevent the issue (for this reason its impact on global score is low). But you can build your own clever solution!</p>",
"isOkThreshold": 0,
"isBadThreshold": 10,
"isAbnormalThreshold": 20,
"hasOffenders": true
},
"imagesWithIncorrectSizesParam": {
"tool": "phantomas",
"label": "Incorrect sizes parameter",
"message": "<p>When using an adaptative image with a <i>srcset</i> attribute and <i>w</i> values, it is important to correctly set the <i>sizes</i> attribute. Otherwise, the browser might pick the wrong image in the <i>srcset</i>.</p><p>The <a href=\”https://chrome.google.com/webstore/detail/responsive-image-linter/mnddginionlghpblkimpdalcecpnbjln\" target=\"_blank\">Responsive Image Linter</a> extension for Chrome can help you further.</p>",
"isOkThreshold": 0,
"isBadThreshold": 5,
"isAbnormalThreshold": 10,

View file

@ -1,24 +1,37 @@
{
"globalScore": {
"pageWeight": 2,
"images": 2,
"domComplexity": 1,
"javascriptComplexity": 2,
"badJavascript": 2,
"jQuery": 0.5,
"cssComplexity": 0.5,
"badCSS": 1,
"fonts": 1,
"serverConfig": 1
},
"categories": {
"pageWeight": {
"label": "Page weight",
"label": "Network",
"policies": {
"totalWeight": 5,
"imageOptimization": 2,
"oldImageFormats": 2,
"imagesTooLarge": 1,
"compression": 2,
"fileMinification": 2
}
},
"requests": {
"label": "Requests",
"policies": {
"totalRequests": 2,
"domains": 3,
"notFound": 2,
"fileMinification": 2,
"identicalFiles": 2,
"emptyRequests": 3,
"notFound": 2,
"domains": 3
}
},
"images": {
"label": "Images",
"policies": {
"imagesNotOptimized": 2,
"imagesOldFormat": 2,
"imagesScaledDown": 2,
"imagesExcessiveDensity": 0.25,
"imagesWithIncorrectSizesParam": 1,
"lazyLoadableImagesBelowTheFold": 2,
"hiddenImages": 1
}
@ -102,17 +115,5 @@
"cachingTooShort": 1
}
}
},
"globalScore": {
"pageWeight": 3,
"requests": 2,
"domComplexity": 2,
"javascriptComplexity": 2,
"badJavascript": 2,
"jQuery": 0.5,
"cssComplexity": 0.5,
"badCSS": 1,
"fonts": 1,
"serverConfig": 1
}
}

View file

@ -58,6 +58,13 @@ var RulesChecker = function() {
offenders = data.toolsResults[policy.tool].offenders[metricName];
}
// It is possible to declare a transformation function for the main metric value.
// The function should
if (policy.valueTransformFn) {
rule.value = policy.valueTransformFn(offenders);
}
var offendersObj = {};
// It is possible to declare a transformation function for the offenders.

View file

@ -47,6 +47,7 @@ var PhantomasWrapper = function() {
// Mandatory
'analyze-css': true,
'analyze-images': true,
'ignoreSslErrors': true, // until Phantomas 2.1
'ignore-ssl-errors': true // for Phantomas >= 2.2
};
@ -80,6 +81,12 @@ var PhantomasWrapper = function() {
offenders: results.getAllOffenders()
};
// Special rules here
if (task.options.device !== 'phone') {
delete json.metrics.imagesExcessiveDensity;
delete json.offenders.imagesExcessiveDensity;
}
deferred.resolve(json);
}).
catch(res => {

View file

@ -14,13 +14,13 @@ var async = require('async');
var request = require('request');
var md5 = require('md5');
var imageOptimizer = require('./imageOptimizer');
//var imageOptimizer = require('./imageOptimizer');
var fileMinifier = require('./fileMinifier');
var gzipCompressor = require('./gzipCompressor');
var brotliCompressor = require('./brotliCompressor');
var contentTypeChecker = require('./contentTypeChecker');
var fontAnalyzer = require('./fontAnalyzer');
var imageDimensions = require('./imageDimensions');
//var imageDimensions = require('./imageDimensions');
var Redownload = function() {
@ -76,9 +76,9 @@ var Redownload = function() {
.then(contentTypeChecker.checkContentType)
.then(imageOptimizer.optimizeImage)
//.then(imageOptimizer.optimizeImage)
.then(imageDimensions.getDimensions)
//.then(imageDimensions.getDimensions)
.then(fileMinifier.minifyFile)
@ -151,12 +151,12 @@ var Redownload = function() {
});
// Image compression
offenders.imageOptimization = listImagesNotOptimized(results);
metrics.imageOptimization = offenders.imageOptimization.totalGain;
//offenders.imageOptimization = listImagesNotOptimized(results);
//metrics.imageOptimization = offenders.imageOptimization.totalGain;
// Image width
offenders.imagesTooLarge = listImagesTooLarge(results, data.params.options.device);
metrics.imagesTooLarge = offenders.imagesTooLarge.length;
//offenders.imagesTooLarge = listImagesTooLarge(results, data.params.options.device);
//metrics.imagesTooLarge = offenders.imagesTooLarge.length;
// File minification
offenders.fileMinification = listFilesNotMinified(results);
@ -341,7 +341,7 @@ var Redownload = function() {
}
function listImagesNotOptimized(requests) {
/*function listImagesNotOptimized(requests) {
var results = {
totalGain: 0,
images: []
@ -396,9 +396,9 @@ var Redownload = function() {
}
});
return results;
}
}*/
function listImagesTooLarge(requests, device) {
/*function listImagesTooLarge(requests, device) {
var results = [];
requests.forEach(function(req) {
@ -423,7 +423,7 @@ var Redownload = function() {
});
return results;
}
}*/
function listFilesNotMinified(requests) {

View file

@ -40,7 +40,7 @@
"md5": "2.3.0",
"meow": "5.0.0",
"parse-color": "1.0.0",
"phantomas": "2.8.0",
"phantomas": "gmetais/phantomas#analyze-image",
"q": "1.5.1",
"request": "2.88.2",
"sharp": "0.32.3",

View file

@ -82,17 +82,6 @@ describe('redownload', function() {
data.toolsResults.redownload.offenders.totalWeight.byType.image.requests.length.should.equal(2);
data.toolsResults.redownload.offenders.totalWeight.byType.other.requests.length.should.equal(1);
data.toolsResults.redownload.offenders.should.have.a.property('imageOptimization');
data.toolsResults.redownload.offenders.imageOptimization.totalGain.should.be.above(0);
data.toolsResults.redownload.offenders.imageOptimization.images.length.should.equal(2);
data.toolsResults.redownload.offenders.should.have.a.property('oldImageFormats');
data.toolsResults.redownload.offenders.oldImageFormats.totalGain.should.be.above(0);
data.toolsResults.redownload.offenders.oldImageFormats.images.length.should.equal(1);
data.toolsResults.redownload.offenders.should.have.a.property('imagesTooLarge');
data.toolsResults.redownload.offenders.imagesTooLarge.length.should.equal(0);
data.toolsResults.redownload.offenders.should.have.a.property('compression');
data.toolsResults.redownload.offenders.compression.totalGain.should.be.above(0);
data.toolsResults.redownload.offenders.compression.files.length.should.equal(5);