diff --git a/lib/metadata/policies.js b/lib/metadata/policies.js index 3157fb1..ead2c2e 100644 --- a/lib/metadata/policies.js +++ b/lib/metadata/policies.js @@ -54,7 +54,7 @@ var policies = { "isBadThreshold": 2000, "isAbnormalThreshold": 4000, "hasOffenders": false, - "unit": 'ms' + "unit": "ms" }, "DOMaccesses": { "tool": "domAccessAgregator", @@ -734,22 +734,96 @@ var policies = { "isBadThreshold": 3145728, "isAbnormalThreshold": 5242880, "hasOffenders": true, - "unit": 'bytes' + "unit": "bytes" }, - "imageOptimization": { - "tool": "redownload", + "imagesNotOptimized": { + "tool": "phantomas", "label": "Image optimization", - "message": "

This metric measures the number of bytes that could be saved by optimizing images.

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 Kraken.io or the excellent ImageOptim on Mac. For SVG images, you can use SVGOMG.

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!

", - "isOkThreshold": 20480, + "message": "

This metric measures the number of bytes that could be saved by optimizing images.

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 Kraken.io or the excellent ImageOptim on Mac. For SVG images, you can use SVGOMG.

The tools in use in YellowLabTools are not set to their maximum optimization power, so you might be able to compress even more!

", + "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; + } }, - "imagesTooLarge": { - "tool": "redownload", + "imagesOldFormat": { + "tool": "phantomas", + "label": "Old image formats", + "message": "

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.

Be careful, you need to provide fallback images for old browsers and search engine bots.

", + "isOkThreshold": 2048, + "isBadThreshold": 512000, + "isAbnormalThreshold": 819200, + "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; + } + }, + "imagesScaledDown": { + "tool": "phantomas", "label": "Oversized images", - "message": "

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.

Please ignore if the file is used as a sprite.

", + "message": "

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.

Of course, it is hard to serve perfect images for all screens. For this reason, this rule is quite permissive.

", + "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": "

This metric measures the number of bytes that could be saved by going further than just resizing images.

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.

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!

", + "isOkThreshold": 102400, + "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; + } + }, + "imagesWithIncorrectSizesParam": { + "tool": "phantomas", + "label": "Incorrect sizes parameter", + "message": "

When using an adaptative image with a srcset attribute and w values, it is important to correctly set the sizes attribute. Otherwise, the browser might pick the wrong image in the srcset.

The Responsive Image Linter extension for Chrome can help you further.

", "isOkThreshold": 0, "isBadThreshold": 5, "isAbnormalThreshold": 10, @@ -763,7 +837,7 @@ var policies = { "isBadThreshold": 204800, "isAbnormalThreshold": 409600, "hasOffenders": true, - "unit": 'bytes' + "unit": "bytes" }, "fileMinification": { "tool": "redownload", @@ -773,12 +847,12 @@ var policies = { "isBadThreshold": 61440, "isAbnormalThreshold": 122880, "hasOffenders": true, - "unit": 'bytes' + "unit": "bytes" }, "totalRequests": { "tool": "redownload", "label": "Requests number", - "message": "

Each request slows down the page loading, especially on the protocol HTTP/1, but also a little on HTTP/2.

There are several technics to reduce their number:

", + "message": "

Each request slows down the page loading, especially on the protocol HTTP/1, but also a little on HTTP/2 or 3.

There are several technics to reduce their number:

", "isOkThreshold": 80, "isBadThreshold": 240, "isAbnormalThreshold": 320, @@ -869,7 +943,7 @@ var policies = { "isOkThreshold": 0, "isBadThreshold": 102400, "isAbnormalThreshold": 204800, - "unit": 'bytes', + "unit": "bytes", "hasOffenders": true, "offendersTransformFn": function(offenders) { return offenders; @@ -895,7 +969,7 @@ var policies = { "isBadThreshold": 51200, "isAbnormalThreshold": 122880, "hasOffenders": true, - "unit": 'bytes' + "unit": "bytes" }, "oldHttpProtocol": { "label": "HTTP protocols", diff --git a/lib/metadata/scoreProfileGeneric.json b/lib/metadata/scoreProfileGeneric.json index 8bff9e4..5019a70 100644 --- a/lib/metadata/scoreProfileGeneric.json +++ b/lib/metadata/scoreProfileGeneric.json @@ -1,23 +1,38 @@ { + "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, - "imagesTooLarge": 1, - "compression": 2, - "fileMinification": 2 - } - }, - "requests": { - "label": "Requests", - "policies": { "totalRequests": 2, "domains": 3, - "notFound": 2, + "compression": 2, + "fileMinification": 2, "identicalFiles": 2, "emptyRequests": 3, + "notFound": 2 + } + }, + "images": { + "label": "Images", + "policies": { + "imagesNotOptimized": 2, + "imagesOldFormat": 1, + "imagesScaledDown": 2, + "imagesExcessiveDensity": 0.25, + "imagesWithIncorrectSizesParam": 1, "lazyLoadableImagesBelowTheFold": 2, "hiddenImages": 1 } @@ -101,17 +116,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 } } \ No newline at end of file diff --git a/lib/rulesChecker.js b/lib/rulesChecker.js index eaffd6e..f5f7824 100644 --- a/lib/rulesChecker.js +++ b/lib/rulesChecker.js @@ -57,9 +57,16 @@ var RulesChecker = function() { data.toolsResults[policy.tool].offenders[metricName]) { 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. // The function should take an array of strings as single parameter and return a string. if (policy.offendersTransformFn) { diff --git a/lib/tools/phantomas/phantomasWrapper.js b/lib/tools/phantomas/phantomasWrapper.js index e71039e..ac2ac31 100644 --- a/lib/tools/phantomas/phantomasWrapper.js +++ b/lib/tools/phantomas/phantomasWrapper.js @@ -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 => { diff --git a/lib/tools/redownload/redownload.js b/lib/tools/redownload/redownload.js index 803b0dd..c386b63 100644 --- a/lib/tools/redownload/redownload.js +++ b/lib/tools/redownload/redownload.js @@ -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) { diff --git a/package.json b/package.json index 07e5917..e0e3ac3 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "yellowlabtools": "./bin/cli.js" }, "engines": { - "node": ">= 16.0" + "node": ">= 18.0" }, "main": "./lib/index.js", "dependencies": { @@ -49,9 +49,10 @@ "md5": "2.3.0", "meow": "5.0.0", "parse-color": "1.0.0", - "phantomas": "2.8.0", + "phantomas": "2.10.0", "q": "1.5.1", "request": "2.88.2", + "sharp": "0.32.6", "ttf2woff2": "5.0.0", "uglify-js": "3.17.4", "woff-tools": "0.1.0" diff --git a/test/core/redownloadTest.js b/test/core/redownloadTest.js index 0c02dae..b5d858e 100644 --- a/test/core/redownloadTest.js +++ b/test/core/redownloadTest.js @@ -82,13 +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('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);