|
@@ -15,7 +15,7 @@ var GzipCompressor = function() {
|
|
|
function gzipUncompressedFile(entry) {
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
- if (isCompressible(entry) && !entry.weightCheck.isCompressed) {
|
|
|
+ if (entryTypeCanBeGzipped(entry) && entry.weightCheck && !entry.weightCheck.isCompressed) {
|
|
|
debug('Compression missing, trying to gzip file %s', entry.url);
|
|
|
|
|
|
var uncompressedSize = entry.weightCheck.uncompressedSize;
|
|
@@ -33,10 +33,11 @@ var GzipCompressor = function() {
|
|
|
debug('File correctly gziped, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
|
|
|
entry.weightCheck.afterCompression = compressedSize;
|
|
|
- deferred.resolve(entry);
|
|
|
} else {
|
|
|
- debug('Gain is not enough, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
+ debug('Gzip gain is not enough, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
}
|
|
|
+
|
|
|
+ deferred.resolve(entry);
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
@@ -50,12 +51,12 @@ var GzipCompressor = function() {
|
|
|
function gzipOptimizedFile(entry) {
|
|
|
var deferred = Q.defer();
|
|
|
|
|
|
- if (isCompressible(entry) && !entry.weightCheck.isOptimized && !entry.weightCheck.isMinified) {
|
|
|
+ if (entryTypeCanBeGzipped(entry) && entry.weightCheck && entry.weightCheck.isOptimized === false) {
|
|
|
debug('Trying to gzip file after minification: %s', entry.url);
|
|
|
|
|
|
- var uncompressedSize = entry.weightCheck.optimized || entry.weightCheck.minified;
|
|
|
+ var uncompressedSize = entry.weightCheck.optimized;
|
|
|
|
|
|
- zlib.gzip(new Buffer(entry.weightCheck.bodyAfterMinification, 'utf8'), function(err, buffer) {
|
|
|
+ zlib.gzip(new Buffer(entry.weightCheck.bodyAfterOptimization, 'utf8'), function(err, buffer) {
|
|
|
if (err) {
|
|
|
debug('Could not compress minified file with gzip');
|
|
|
debug(err);
|
|
@@ -65,13 +66,14 @@ var GzipCompressor = function() {
|
|
|
var compressedSize = buffer.length;
|
|
|
|
|
|
if (gainIsEnough(uncompressedSize, compressedSize)) {
|
|
|
- debug('File correctly gziped, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
+ debug('Minified file correctly gziped, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
|
|
|
entry.weightCheck.afterOptimizationAndCompression = compressedSize;
|
|
|
- deferred.resolve(entry);
|
|
|
} else {
|
|
|
- debug('Gain is not enough, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
+ debug('Gzip gain is not enough on minified file, was %d and is now %d bytes', uncompressedSize, compressedSize);
|
|
|
}
|
|
|
+
|
|
|
+ deferred.resolve(entry);
|
|
|
}
|
|
|
});
|
|
|
} else {
|
|
@@ -81,10 +83,6 @@ var GzipCompressor = function() {
|
|
|
return deferred.promise;
|
|
|
}
|
|
|
|
|
|
- function isCompressible(entry) {
|
|
|
- return entry.isJS || entry.isCSS || entry.isHTML || entry.isJSON || entry.isSVG || entry.isTTF || entry.isXML || entry.isFavicon;
|
|
|
- }
|
|
|
-
|
|
|
// The gain is estimated of enough value if it's over 1KB or over 20%,
|
|
|
// but it's ignored if is below 100 bytes
|
|
|
function gainIsEnough(oldWeight, newWeight) {
|
|
@@ -93,8 +91,13 @@ var GzipCompressor = function() {
|
|
|
return (gain > 2048 || (ratio > 0.2 && gain > 100));
|
|
|
}
|
|
|
|
|
|
+ function entryTypeCanBeGzipped(entry) {
|
|
|
+ return entry.isJS || entry.isCSS || entry.isHTML || entry.isJSON || entry.isSVG || entry.isTTF || entry.isXML || entry.isFavicon;
|
|
|
+ }
|
|
|
+
|
|
|
return {
|
|
|
- compressFile: compressFile
|
|
|
+ compressFile: compressFile,
|
|
|
+ entryTypeCanBeGzipped: entryTypeCanBeGzipped
|
|
|
};
|
|
|
};
|
|
|
|