|
@@ -13,6 +13,7 @@ var async = require('async');
|
|
var request = require('request');
|
|
var request = require('request');
|
|
|
|
|
|
var imageOptimizer = require('./imageOptimizer');
|
|
var imageOptimizer = require('./imageOptimizer');
|
|
|
|
+var fileMinifier = require('./fileMinifier');
|
|
|
|
|
|
|
|
|
|
var WeightChecker = function() {
|
|
var WeightChecker = function() {
|
|
@@ -23,6 +24,8 @@ var WeightChecker = function() {
|
|
|
|
|
|
// This function will re-download every asset and check if it could be optimized
|
|
// This function will re-download every asset and check if it could be optimized
|
|
function recheckAllFiles(data) {
|
|
function recheckAllFiles(data) {
|
|
|
|
+ var startTime = Date.now();
|
|
|
|
+ debug('Redownload started');
|
|
var deferred = Q.defer();
|
|
var deferred = Q.defer();
|
|
|
|
|
|
var requestsList = JSON.parse(data.toolsResults.phantomas.offenders.requestsList);
|
|
var requestsList = JSON.parse(data.toolsResults.phantomas.offenders.requestsList);
|
|
@@ -35,7 +38,9 @@ var WeightChecker = function() {
|
|
|
|
|
|
redownloadEntry(entry)
|
|
redownloadEntry(entry)
|
|
|
|
|
|
- .then(imageOptimizer.recompressIfImage)
|
|
|
|
|
|
+ .then(imageOptimizer.optimizeImage)
|
|
|
|
+
|
|
|
|
+ .then(fileMinifier.minifyFile)
|
|
|
|
|
|
.then(function(newEntry) {
|
|
.then(function(newEntry) {
|
|
callback(null, newEntry);
|
|
callback(null, newEntry);
|
|
@@ -49,11 +54,20 @@ var WeightChecker = function() {
|
|
|
|
|
|
// Lanch all redownload functions and wait for completion
|
|
// Lanch all redownload functions and wait for completion
|
|
async.parallelLimit(redownloadList, MAX_PARALLEL_DOWNLOADS, function(err, results) {
|
|
async.parallelLimit(redownloadList, MAX_PARALLEL_DOWNLOADS, function(err, results) {
|
|
|
|
+
|
|
if (err) {
|
|
if (err) {
|
|
debug(err);
|
|
debug(err);
|
|
deferred.reject(err);
|
|
deferred.reject(err);
|
|
} else {
|
|
} else {
|
|
|
|
+
|
|
debug('All files checked');
|
|
debug('All files checked');
|
|
|
|
+ endTime = Date.now();
|
|
|
|
+ debug('Redownload took %d ms', endTime - startTime);
|
|
|
|
+
|
|
|
|
+ // Remove unwanted requests (redirections, about:blank)
|
|
|
|
+ results = results.filter(function(result) {
|
|
|
|
+ return (result !== null && result.weightCheck && result.weightCheck.bodySize > 0);
|
|
|
|
+ });
|
|
|
|
|
|
var metrics = {};
|
|
var metrics = {};
|
|
var offenders = {};
|
|
var offenders = {};
|
|
@@ -67,6 +81,11 @@ var WeightChecker = function() {
|
|
offenders.imageOptimization = listImageNotOptimized(results);
|
|
offenders.imageOptimization = listImageNotOptimized(results);
|
|
metrics.imageOptimization = offenders.imageOptimization.totalGain;
|
|
metrics.imageOptimization = offenders.imageOptimization.totalGain;
|
|
|
|
|
|
|
|
+ // File minification
|
|
|
|
+ offenders.fileMinification = listFilesNotMinified(results);
|
|
|
|
+ metrics.fileMinification = offenders.fileMinification.totalGain;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
|
|
data.toolsResults.weightChecker = {
|
|
data.toolsResults.weightChecker = {
|
|
metrics: metrics,
|
|
metrics: metrics,
|
|
@@ -144,14 +163,14 @@ var WeightChecker = function() {
|
|
};
|
|
};
|
|
|
|
|
|
requests.forEach(function(req) {
|
|
requests.forEach(function(req) {
|
|
- if (req.weightCheck.bodySize && req.weightCheck.isOptimized === false) {
|
|
|
|
- var gain = req.weightCheck.bodySize - req.weightCheck.optimized;
|
|
|
|
|
|
+ if (req.weightCheck.uncompressedSize && req.weightCheck.isOptimized === false) {
|
|
|
|
+ var gain = req.weightCheck.uncompressedSize - req.weightCheck.optimized;
|
|
|
|
|
|
results.totalGain += gain;
|
|
results.totalGain += gain;
|
|
|
|
|
|
results.images.push({
|
|
results.images.push({
|
|
url: req.url,
|
|
url: req.url,
|
|
- original: req.weightCheck.bodySize,
|
|
|
|
|
|
+ original: req.weightCheck.uncompressedSize,
|
|
optimized: req.weightCheck.optimized,
|
|
optimized: req.weightCheck.optimized,
|
|
lossless: req.weightCheck.lossless,
|
|
lossless: req.weightCheck.lossless,
|
|
lossy: req.weightCheck.lossy,
|
|
lossy: req.weightCheck.lossy,
|
|
@@ -164,10 +183,35 @@ var WeightChecker = function() {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ function listFilesNotMinified(requests) {
|
|
|
|
+ var results = {
|
|
|
|
+ totalGain: 0,
|
|
|
|
+ files: []
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ requests.forEach(function(req) {
|
|
|
|
+ if (req.weightCheck.uncompressedSize && req.weightCheck.isMinified === false) {
|
|
|
|
+ var gain = req.weightCheck.uncompressedSize - req.weightCheck.minified;
|
|
|
|
+
|
|
|
|
+ results.totalGain += gain;
|
|
|
|
+
|
|
|
|
+ results.files.push({
|
|
|
|
+ url: req.url,
|
|
|
|
+ original: req.weightCheck.uncompressedSize,
|
|
|
|
+ minified: req.weightCheck.minified,
|
|
|
|
+ gain: gain
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ return results;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
function redownloadEntry(entry) {
|
|
function redownloadEntry(entry) {
|
|
var deferred = Q.defer();
|
|
var deferred = Q.defer();
|
|
|
|
|
|
- function onError(message) {
|
|
|
|
|
|
+ function downloadError(message) {
|
|
debug('Could not download %s Error: %s', entry.url, message);
|
|
debug('Could not download %s Error: %s', entry.url, message);
|
|
entry.weightCheck = {
|
|
entry.weightCheck = {
|
|
message: message
|
|
message: message
|
|
@@ -175,18 +219,32 @@ var WeightChecker = function() {
|
|
deferred.resolve(entry);
|
|
deferred.resolve(entry);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Not downloaded again but will be counted in totalWeight
|
|
|
|
+ function notDownloadableFile(message) {
|
|
|
|
+ entry.weightCheck = {
|
|
|
|
+ message: message
|
|
|
|
+ };
|
|
|
|
+ deferred.resolve(entry);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Not counted in totalWeight
|
|
|
|
+ function unwantedFile(message) {
|
|
|
|
+ debug(message);
|
|
|
|
+ deferred.resolve(entry);
|
|
|
|
+ }
|
|
|
|
+
|
|
if (entry.method !== 'GET') {
|
|
if (entry.method !== 'GET') {
|
|
- onError('only downloading GET');
|
|
|
|
|
|
+ notDownloadableFile('only downloading GET');
|
|
return deferred.promise;
|
|
return deferred.promise;
|
|
}
|
|
}
|
|
|
|
|
|
if (entry.status !== 200) {
|
|
if (entry.status !== 200) {
|
|
- onError('only downloading requests with status code 200');
|
|
|
|
|
|
+ unwantedFile('only downloading requests with status code 200');
|
|
return deferred.promise;
|
|
return deferred.promise;
|
|
}
|
|
}
|
|
|
|
|
|
if (entry.url === 'about:blank') {
|
|
if (entry.url === 'about:blank') {
|
|
- onError('not downloading about:blank');
|
|
|
|
|
|
+ unwantedFile('not downloading about:blank');
|
|
return deferred.promise;
|
|
return deferred.promise;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -202,15 +260,14 @@ var WeightChecker = function() {
|
|
url: entry.url,
|
|
url: entry.url,
|
|
headers: reqHeaders,
|
|
headers: reqHeaders,
|
|
timeout: REQUEST_TIMEOUT
|
|
timeout: REQUEST_TIMEOUT
|
|
- //encoding: (entry.contentType === 'image/jpeg' || entry.contentType === 'image/png') ? 'binary' : null
|
|
|
|
};
|
|
};
|
|
|
|
|
|
download(requestOptions, entry.contentType, function(error, result) {
|
|
download(requestOptions, entry.contentType, function(error, result) {
|
|
if (error) {
|
|
if (error) {
|
|
if (error.code === 'ETIMEDOUT') {
|
|
if (error.code === 'ETIMEDOUT') {
|
|
- onError('timeout after ' + REQUEST_TIMEOUT + 'ms');
|
|
|
|
|
|
+ downloadError('timeout after ' + REQUEST_TIMEOUT + 'ms');
|
|
} else {
|
|
} else {
|
|
- onError('error while downloading: ' + error.code);
|
|
|
|
|
|
+ downloadError('error while downloading: ' + error.code);
|
|
}
|
|
}
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -332,7 +389,6 @@ var WeightChecker = function() {
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
return {
|
|
return {
|
|
recheckAllFiles: recheckAllFiles,
|
|
recheckAllFiles: recheckAllFiles,
|
|
listRequestWeight: listRequestWeight,
|
|
listRequestWeight: listRequestWeight,
|