فهرست منبع

Rename isMinified into isOptimized

Gaël Métais 10 سال پیش
والد
کامیت
113665139a

+ 15 - 7
lib/tools/weightChecker/fileMinifier.js

@@ -39,8 +39,9 @@ var FileMinifier = function() {
                 debug('JS minification complete for %s', entry.url);
                 
                 if (gainIsEnough(fileSize, newFileSize)) {
-                    entry.weightCheck.minified = newFileSize;
-                    entry.weightCheck.isMinified = false;
+                    entry.weightCheck.bodyAfterOptimization = newFile;
+                    entry.weightCheck.optimized = newFileSize;
+                    entry.weightCheck.isOptimized = false;
                     debug('Filesize is %d bytes smaller (-%d%)', fileSize - newFileSize, Math.round((fileSize - newFileSize) * 100 / fileSize));
                 }
 
@@ -69,8 +70,9 @@ var FileMinifier = function() {
                 debug('CSS minification complete for %s', entry.url);
                 
                 if (gainIsEnough(fileSize, newFileSize)) {
-                    entry.weightCheck.minified = newFileSize;
-                    entry.weightCheck.isMinified = false;
+                    entry.weightCheck.bodyAfterOptimization = newFile;
+                    entry.weightCheck.optimized = newFileSize;
+                    entry.weightCheck.isOptimized = false;
                     debug('Filesize is %d bytes smaller (-%d%)', fileSize - newFileSize, Math.round((fileSize - newFileSize) * 100 / fileSize));
                 }
 
@@ -99,8 +101,9 @@ var FileMinifier = function() {
                 debug('HTML minification complete for %s', entry.url);
                 
                 if (gainIsEnough(fileSize, newFileSize)) {
-                    entry.weightCheck.minified = newFileSize;
-                    entry.weightCheck.isMinified = false;
+                    entry.weightCheck.bodyAfterOptimization = newFile;
+                    entry.weightCheck.optimized = newFileSize;
+                    entry.weightCheck.isOptimized = false;
                     debug('Filesize is %d bytes smaller (-%d%)', fileSize - newFileSize, Math.round((fileSize - newFileSize) * 100 / fileSize));
                 }
 
@@ -176,12 +179,17 @@ var FileMinifier = function() {
         return deferred.promise;
     }
 
+    function entryTypeCanBeMinified(entry) {
+        return entry.isJS || entry.isCSS || entry.isHTML;
+    }
+
     return {
         minifyFile: minifyFile,
         minifyJs: minifyJs,
         minifyCss: minifyCss,
         minifyHtml: minifyHtml,
-        gainIsEnough: gainIsEnough
+        gainIsEnough: gainIsEnough,
+        entryTypeCanBeMinified: entryTypeCanBeMinified
     };
 };
 

+ 17 - 14
lib/tools/weightChecker/gzipCompressor.js

@@ -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
     };
 };
 

+ 9 - 3
lib/tools/weightChecker/imageOptimizer.js

@@ -22,7 +22,7 @@ var ImageOptimizer = function() {
         debug('Let\'s try to optimize %s', entry.url);
         debug('Current file size is %d', fileSize);
 
-        if (isJpeg(entry)) {
+        if (isJPEG(entry)) {
             debug('File is a JPEG');
 
             // Starting softly with a lossless compression
@@ -129,6 +129,7 @@ var ImageOptimizer = function() {
                 debug('SVG lossless compression complete for %s', entry.url);
                 
                 if (gainIsEnough(fileSize, newFileSize)) {
+                    entry.weightCheck.bodyAfterOptimization = newFile;
                     entry.weightCheck.lossless = entry.weightCheck.optimized = newFileSize;
                     entry.weightCheck.isOptimized = false;
                     debug('Filesize is %d bytes smaller (-%d%)', fileSize - newFileSize, Math.round((fileSize - newFileSize) * 100 / fileSize));
@@ -157,7 +158,7 @@ var ImageOptimizer = function() {
         return (gain > 2048 || (ratio > 0.2 && gain > 100));
     }
 
-    function isJpeg(entry) {
+    function isJPEG(entry) {
         return entry.isImage && entry.contentType === 'image/jpeg';
     }
 
@@ -226,13 +227,18 @@ var ImageOptimizer = function() {
         return deferred.promise;
     }
 
+    function entryTypeCanBeOptimized(entry) {
+        return isJPEG(entry) || isPNG(entry) || isSVG(entry);
+    }
+
     return {
         optimizeImage: optimizeImage,
         compressJpegLosslessly: compressJpegLosslessly,
         compressJpegLossly: compressJpegLossly,
         compressPngLosslessly: compressPngLosslessly,
         compressSvgLosslessly: compressSvgLosslessly,
-        gainIsEnough: gainIsEnough
+        gainIsEnough: gainIsEnough,
+        entryTypeCanBeOptimized: entryTypeCanBeOptimized
     };
 };
 

+ 32 - 6
lib/tools/weightChecker/weightChecker.js

@@ -43,7 +43,7 @@ var WeightChecker = function() {
 
                 .then(fileMinifier.minifyFile)
 
-                .then(gzipCompressor.compress)
+                .then(gzipCompressor.compressFile)
 
                 .then(function(newEntry) {
                     callback(null, newEntry);
@@ -88,7 +88,9 @@ var WeightChecker = function() {
                 offenders.fileMinification = listFilesNotMinified(results);
                 metrics.fileMinification = offenders.fileMinification.totalGain;
 
-                
+                // Gzip compression
+                offenders.gzipCompression = listFilesNotGzipped(results);
+                metrics.gzipCompression = offenders.gzipCompression.totalGain;
 
                 data.toolsResults.weightChecker = {
                     metrics: metrics,
@@ -166,7 +168,7 @@ var WeightChecker = function() {
         };
 
         requests.forEach(function(req) {
-            if (req.weightCheck.uncompressedSize && req.weightCheck.isOptimized === false) {
+            if (req.weightCheck.uncompressedSize && imageOptimizer.entryTypeCanBeOptimized(req) && req.weightCheck.isOptimized === false) {
                 var gain = req.weightCheck.uncompressedSize - req.weightCheck.optimized;
 
                 results.totalGain += gain;
@@ -193,15 +195,39 @@ var WeightChecker = function() {
         };
 
         requests.forEach(function(req) {
-            if (req.weightCheck.uncompressedSize && req.weightCheck.isMinified === false) {
-                var gain = req.weightCheck.uncompressedSize - req.weightCheck.minified;
+            if (req.weightCheck.uncompressedSize && fileMinifier.entryTypeCanBeMinified(req) && req.weightCheck.isOptimized === false) {
+                var gain = req.weightCheck.uncompressedSize - req.weightCheck.optimized;
+
+                results.totalGain += gain;
+
+                results.files.push({
+                    url: req.url,
+                    original: req.weightCheck.uncompressedSize,
+                    optimized: req.weightCheck.optimized,
+                    gain: gain
+                });
+            }
+        });
+
+        return results;
+    }
+
+    function listFilesNotGzipped(requests) {
+        var results = {
+            totalGain: 0,
+            files: []
+        };
+
+        requests.forEach(function(req) {
+            if (req.weightCheck.uncompressedSize && req.weightCheck.isCompressed === false && req.weightCheck.afterCompression) {
+                var gain = req.weightCheck.uncompressedSize - req.weightCheck.afterCompression;
 
                 results.totalGain += gain;
 
                 results.files.push({
                     url: req.url,
                     original: req.weightCheck.uncompressedSize,
-                    minified: req.weightCheck.minified,
+                    gzipped: req.weightCheck.afterCompression,
                     gain: gain
                 });
             }

+ 13 - 9
test/core/fileMinifierTest.js

@@ -49,8 +49,9 @@ describe('fileMinifier', function() {
         fileMinifier.minifyFile(entry)
 
         .then(function(newEntry) {
-            newEntry.weightCheck.should.have.a.property('isMinified').that.equals(false);
-            newEntry.weightCheck.should.have.a.property('minified').that.is.below(fileSize);
+            newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
+            newEntry.weightCheck.should.have.a.property('optimized').that.is.below(fileSize);
+            newEntry.weightCheck.should.have.a.property('bodyAfterOptimization');
 
             done();
         })
@@ -62,7 +63,7 @@ describe('fileMinifier', function() {
 
     it('should fail minifying an already minified JS', function(done) {
         this.timeout(5000);
-        
+
         var fileContent = fs.readFileSync(path.resolve(__dirname, '../www/jquery1.8.3.js'));
         var fileSize = fileContent.length;
 
@@ -92,8 +93,9 @@ describe('fileMinifier', function() {
         fileMinifier.minifyFile(entry)
 
         .then(function(newEntry) {
-            newEntry.weightCheck.should.not.have.a.property('isMinified');
-            newEntry.weightCheck.should.not.have.a.property('minified');
+            newEntry.weightCheck.should.not.have.a.property('isOptimized');
+            newEntry.weightCheck.should.not.have.a.property('optimized');
+            newEntry.weightCheck.should.not.have.a.property('bodyAfterOptimization');
 
             done();
         })
@@ -133,8 +135,9 @@ describe('fileMinifier', function() {
         fileMinifier.minifyFile(entry)
 
         .then(function(newEntry) {
-            newEntry.weightCheck.should.not.have.a.property('isMinified');
-            newEntry.weightCheck.should.not.have.a.property('minified');
+            newEntry.weightCheck.should.not.have.a.property('isOptimized');
+            newEntry.weightCheck.should.not.have.a.property('optimized');
+            newEntry.weightCheck.should.not.have.a.property('bodyAfterOptimization');
 
             done();
         })
@@ -188,8 +191,9 @@ describe('fileMinifier', function() {
         fileMinifier.minifyFile(entry)
 
         .then(function(newEntry) {
-            newEntry.weightCheck.should.have.a.property('isMinified').that.equals(false);
-            newEntry.weightCheck.should.have.a.property('minified').that.is.below(fileSize);
+            newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
+            newEntry.weightCheck.should.have.a.property('optimized').that.is.below(fileSize);
+            newEntry.weightCheck.should.have.a.property('bodyAfterOptimization');
 
             done();
         })

+ 16 - 16
test/core/gzipCompressorTest.js

@@ -29,7 +29,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -65,14 +65,14 @@ describe('gzipCompressor', function() {
             contentLength: 999,
             weightCheck: {
                 body: fileContent.toString('utf8'),
-                bodyAfterMinification: minifiedContent.toString('utf8'),
+                bodyAfterOptimization: minifiedContent.toString('utf8'),
                 totalWeight: fileSize + 200,
                 headersSize: 200,
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: false,
-                minified: minifiedSize
+                isOptimized: false,
+                optimized: minifiedSize
             }
         };
 
@@ -109,14 +109,14 @@ describe('gzipCompressor', function() {
             contentLength: 999,
             weightCheck: {
                 body: fileContent.toString('utf8'),
-                bodyAfterMinification: minifiedContent.toString('utf8'),
+                bodyAfterOptimization: minifiedContent.toString('utf8'),
                 totalWeight: gzipedSize + 200,
                 headersSize: 200,
                 bodySize: gzipedSize,
                 isCompressed: true,
                 uncompressedSize: fileSize,
-                isMinified: false,
-                minified: minifiedSize
+                isOptimized: false,
+                optimized: minifiedSize
             }
         };
 
@@ -156,7 +156,7 @@ describe('gzipCompressor', function() {
                 bodySize: gzipedSize,
                 isCompressed: true,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -164,7 +164,7 @@ describe('gzipCompressor', function() {
 
         .then(function(newEntry) {
             newEntry.weightCheck.should.not.have.a.property('minified');
-            newEntry.weightCheck.should.not.have.a.property('bodyAfterMinification');
+            newEntry.weightCheck.should.not.have.a.property('bodyAfterOptimization');
             newEntry.weightCheck.should.not.have.a.property('afterCompression');
             newEntry.weightCheck.should.not.have.a.property('afterOptimizationAndCompression');
 
@@ -194,7 +194,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -230,7 +230,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -267,7 +267,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -303,7 +303,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -339,7 +339,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -376,7 +376,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 
@@ -413,7 +413,7 @@ describe('gzipCompressor', function() {
                 bodySize: fileSize,
                 isCompressed: false,
                 uncompressedSize: fileSize,
-                isMinified: true
+                isOptimized: true
             }
         };
 

+ 2 - 0
test/core/imageOptimizerTest.js

@@ -207,6 +207,8 @@ describe('imageOptimizer', function() {
         .then(function(newEntry) {
             newEntry.weightCheck.should.have.a.property('isOptimized').that.equals(false);
             newEntry.weightCheck.should.have.a.property('lossless').that.is.below(fileSize);
+            newEntry.weightCheck.should.have.a.property('optimized').that.equals(newEntry.weightCheck.lossless);
+            newEntry.weightCheck.should.have.a.property('bodyAfterOptimization');
 
             done();
         })

+ 7 - 1
test/core/weightCheckerTest.js

@@ -56,6 +56,7 @@ describe('weightChecker', function() {
                 },
                 status: 200,
                 isImage: true,
+                isSVG: true,
                 type: 'image',
                 contentType: 'image/svg+xml'
             },
@@ -127,6 +128,10 @@ describe('weightChecker', function() {
             data.toolsResults.weightChecker.offenders.imageOptimization.totalGain.should.be.above(0);
             data.toolsResults.weightChecker.offenders.imageOptimization.images.length.should.equal(2);
 
+            data.toolsResults.weightChecker.offenders.should.have.a.property('gzipCompression');
+            data.toolsResults.weightChecker.offenders.gzipCompression.totalGain.should.be.above(0);
+            data.toolsResults.weightChecker.offenders.gzipCompression.files.length.should.equal(4);
+
             data.toolsResults.weightChecker.offenders.should.have.a.property('fileMinification');
             data.toolsResults.weightChecker.offenders.fileMinification.totalGain.should.be.above(0);
             data.toolsResults.weightChecker.offenders.fileMinification.files.length.should.equal(2);
@@ -149,7 +154,8 @@ describe('weightChecker', function() {
                Accept: '*/*'
             },
             status: 200,
-            isJS: true
+            isJS: true,
+            type: 'js'
         };
 
         weightChecker.redownloadEntry(entry)