├── .gitignore ├── LICENSE ├── package.json ├── README.md ├── test ├── input.txt └── test.js └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | test/* 3 | !test/input.txt 4 | !test/test.js 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 Larry Davis 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-zopfli", 3 | "version": "0.0.2", 4 | "description": "Zopfli compressor for gulp", 5 | "keywords": [ 6 | "gulpplugin", 7 | "zopflie", 8 | "compress", 9 | "deflate", 10 | "gzip", 11 | "gulp" 12 | ], 13 | "homepage": "https://github.com/romeovs/gulp-zopfli", 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/romeo/gulp-zopfli.git" 17 | }, 18 | "author": { 19 | "name": "Romeo Van Snick", 20 | "email": "romeovs@gmail.com" 21 | }, 22 | "scripts": { 23 | "test": "cd test && mocha -R spec" 24 | }, 25 | "contributors": [ 26 | "TruongSinh Tran-Nguyen ", 27 | "Jeremy Stuckey " 28 | ], 29 | "dependencies": { 30 | "event-stream": "~3.0.16", 31 | "clone": "~0.1.11", 32 | "node-zopfli": "~1.0.3" 33 | }, 34 | "devDependencies": { 35 | "gulp": "~3.4.x", 36 | "q": "~1.0.0", 37 | "mocha": "~1.13.0", 38 | "should": "~2.0.2" 39 | }, 40 | "main": "index.js", 41 | "licenses": [ 42 | { 43 | "type": "MIT", 44 | "url": "http://github.com/jstuckey/gulp-gzip/raw/master/LICENSE" 45 | } 46 | ], 47 | "readmeFilename": "README.md", 48 | "bugs": { 49 | "url": "https://github.com/romeovs/gulp-zopfli/issues", 50 | "email": "romeovs@gmail.com" 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | gulp-zopfli 2 | =========== 3 | 4 | a [Zopfli](http://en.wikipedia.org/wiki/Zopfli) plugin for [gulp](https://github.com/wearefractal/gulp), 5 | based on [node-zopfli](https://npmjs.org/package/node-zopfli). 6 | 7 | ## Install 8 | 9 | ``` 10 | npm install --save-dev gulp-zopfli 11 | ``` 12 | 13 | ## Options 14 | 15 | You can pass an options array to the zopfli object. These options are 16 | passed on to `node-zopfli` library, except for one: the `format` options is 17 | used to pick a compression format. The possible formats are: `"deflate"`, `"zlib"` or `"gzip"`. 18 | 19 | The defaults options are: 20 | ```javascript 21 | { 22 | format: "gzip", 23 | verbose: false, 24 | verbose_more: false, 25 | numiterations: 15, 26 | blocksplitting: true, 27 | blocksplittinglast: false, 28 | blocksplittingmax: 15 29 | }; 30 | ``` 31 | 32 | ## Examples 33 | 34 | ```javascript 35 | var gulp = require("gulp"); 36 | var zopfli = require("gulp-zopfli"); 37 | 38 | gulp.task("compress", function() { 39 | gulp.src("./dev/scripts/*.js") 40 | .pipe(zopfli()) 41 | .pipe(gulp.dest("./public/scripts")); 42 | }); 43 | 44 | gulp.task("default", function() { 45 | gulp.run("compress"); 46 | }); 47 | ``` 48 | Since the plugin is based largely on it, 49 | if you're already using [gulp-gzip](https://github.com/jstuckey/gulp-gzip), 50 | switching to `zopfli` should be as easy as replacing 51 | 52 | ```javascript 53 | var gzip = require("gulp-gzip"); 54 | ``` 55 | by 56 | ```javascript 57 | var gzip = require("gulp-zopfli"); 58 | ``` 59 | 60 | 61 | Credit 62 | ====== 63 | 64 | This plugin was based largely on [gulp-gzip](https://github.com/jstuckey/gulp-gzip), 65 | I basically just replaced `gzip` by `zopfli` so all credit is due to Jeremy. 66 | -------------------------------------------------------------------------------- /test/input.txt: -------------------------------------------------------------------------------- 1 | The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*jshint bitwise:true, camelcase:true, curly:true, eqeqeq:true, forin:true, immed:true, latedef:true, 2 | newcap:true, noarg:true, noempty:true, nonew:true, plusplus:true, regexp:true, undef:true, 3 | unused:true, strict:true, trailing:true, indent:2, quotmark:single, esnext:true, node:true */ 4 | var es = require('event-stream'); 5 | var clone = require('clone'); 6 | var zopfli = require('node-zopfli'); 7 | 8 | const PLUGIN_NAME = 'gulp-zopfli'; 9 | 10 | /** 11 | * 12 | * @param {Object} opts 13 | * @param {String} opts.format gzip || deflate || zlib 14 | * @param {Number} opts.thresholdRatio if compression ratio does not reach the threshold, 15 | * does not compress, default to 0 16 | * @param {String} opts.thresholdBehavior if compression ratio does not reach the threshold, 17 | * "original" to output original file, or 18 | * "blank" (default) to output nothing 19 | * @returns {Stream} 20 | */ 21 | module.exports = function(opts) { 22 | 'use strict'; 23 | // default options 24 | var options = opts || {}; 25 | var format = options.format || 'gzip'; 26 | 27 | var ext; 28 | 29 | if(format === 'gzip') { 30 | ext = '.gz'; 31 | } else if(format === 'deflate') { 32 | ext = '.deflate'; 33 | } else if(format === 'zlib') { 34 | ext = '.zz'; 35 | } 36 | 37 | // delete format option 38 | if(options.format !== null) { 39 | delete options.format; 40 | } 41 | 42 | var compress = function(file, callback) { 43 | 44 | // pass along empty files 45 | if(file.isNull()) { 46 | return callback(null, file); 47 | } 48 | 49 | // clone file and append the extension 50 | var newFile = clone(file); 51 | newFile.path += ext; 52 | newFile.shortened += ext; 53 | 54 | // Check if file is a buffer or a stream 55 | if(file.isBuffer()) { 56 | 57 | // File contents is a buffer 58 | var zcb = function(err, buffer) { 59 | if(!err) { 60 | if(options.thresholdRatio > file.contents.length / buffer.length) { 61 | if(options.thresholdBehavior === 'original'){ 62 | // output original file 63 | return callback(null, file); 64 | } 65 | // output nothing 66 | return callback(); 67 | } 68 | newFile.contents = buffer; 69 | // output compressed file 70 | return callback(null, newFile); 71 | } 72 | callback(err, null); 73 | }; 74 | 75 | if(format === 'gzip') { 76 | zopfli.gzip(file.contents, options, zcb); 77 | } else if(format === 'deflate') { 78 | zopfli.deflate(file.contents, options, zcb); 79 | } else if(format === 'zlib') { 80 | zopfli.zlib(file.contents, options, zcb); 81 | } 82 | } else if(file.isStream()) { 83 | 84 | // File contains a stream 85 | 86 | var z; 87 | if(format === 'gzip') { 88 | z = zopfli.createGzip(options); 89 | } else if(format === 'deflate') { 90 | z = zopfli.createDeflate(options); 91 | } else if(format === 'zlib') { 92 | z = zopfli.createZlib(options); 93 | } 94 | newFile.contents = file.contents 95 | .pipe(z); 96 | // .pipe(es.through()); 97 | callback(null, newFile); 98 | } 99 | }; 100 | 101 | return es.map(compress); 102 | }; 103 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | /*jshint bitwise:true, camelcase:true, curly:true, eqeqeq:true, forin:true, immed:true, latedef:true, 2 | newcap:true, noarg:true, noempty:true, nonew:true, plusplus:true, regexp:true, undef:true, 3 | unused:true, strict:true, trailing:true, indent:2, quotmark:single, esnext:true, node:true */ 4 | /*global describe, it, afterEach*/ 5 | 'use strict'; 6 | var zopfli = require('../'); 7 | var exec = require('child_process').exec; 8 | var q = require('q'); 9 | var zlib = require('zlib'); 10 | var should = require('should'); 11 | var gulp = require('gulp'); 12 | var es = require('event-stream'); 13 | var fs = require('fs'); 14 | var fsPromise = {}; 15 | fsPromise.stat = q.denodeify(fs.stat); 16 | fsPromise.exists = function(path){ 17 | var d = q.defer(); 18 | fs.exists(path, function(v){ 19 | d.resolve(v); 20 | }); 21 | return d.promise; 22 | }; 23 | var Stream = require('stream'); 24 | 25 | require('mocha'); 26 | 27 | describe('gulp-zopfli', function() { 28 | describe('compress', function() { 29 | afterEach(function(done){ 30 | exec('rm -rf *.txt.gz && rm -rf *.txt.gz &&rm -rf *.txt.zz && rm -rf *.txt.deflate && rm -rf computed/*', done); 31 | }); 32 | it('should append gz to the file extension', function(done) { 33 | gulp.src('*.txt') 34 | .pipe(zopfli()) 35 | .pipe(es.map(function(file, callback) { 36 | // Check if file properties end with .gz 37 | file.path.should.endWith('gz'); 38 | file.shortened.should.endWith('gz'); 39 | callback(null, null); 40 | done(); 41 | })); 42 | }); 43 | 44 | it('should return file contents as a buffer in buffer mode', function(done) { 45 | gulp.src('*.txt') 46 | .pipe(zopfli()) 47 | .pipe(es.map(function(file, callback) { 48 | // Check if file contents is a Buffer object 49 | file.contents.should.be.instanceof(Buffer); // should.have.type didn't work 50 | callback(null, null); 51 | done(); 52 | })); 53 | }); 54 | 55 | it('should return file contents as a stream in stream mode', function(done) { 56 | gulp.src('*.txt', {buffer: false}) 57 | .pipe(zopfli()) 58 | .pipe(es.map(function(file, callback) { 59 | // Check if file contents is a Buffer object 60 | file.contents.should.be.instanceof(Stream); // should.have.type didn't work 61 | callback(null, null); 62 | done(); 63 | })); 64 | }); 65 | 66 | it('should create a .gz file in buffer mode', function(done) { 67 | var outStream = gulp.dest('./'); 68 | 69 | // Capture close event of the write stream so we know when gulp.dest finishes 70 | outStream.on('close', function() { 71 | // Get the compressed file 72 | fs.readFile('./input.txt.gz', function(err, file) { 73 | 74 | // Check if the file was found 75 | should.not.exist(err); 76 | should.exist(file); 77 | file.should.not.be.empty; 78 | 79 | done(); 80 | }); 81 | }); 82 | gulp.src('*.txt') 83 | .pipe(zopfli()) 84 | .pipe(outStream); 85 | }); 86 | 87 | it('should create a .gz file in stream mode', function(done) { 88 | 89 | var outStream = gulp.dest('./'); 90 | 91 | // Capture close event of the write stream so we know when gulp.dest finishes 92 | outStream.on('close', function() { 93 | // Get the compressed file 94 | fs.readFile('./input.txt.gz', function(err, file) { 95 | 96 | // console.log(util.inspect(file)); 97 | 98 | // Check if the file was found 99 | should.not.exist(err); 100 | should.exist(file); 101 | file.should.not.be.empty; 102 | 103 | done(); 104 | }); 105 | }); 106 | gulp.src('*.txt', {buffer: false}) 107 | .pipe(zopfli()) 108 | .pipe(outStream); 109 | }); 110 | 111 | it('should match uncompressed file with original file (gzip)', function(done) { 112 | 113 | var outStream = gulp.dest('./'); 114 | 115 | // Capture close event of the write stream so we know when gulp.dest finishes 116 | outStream.on('close', function() { 117 | 118 | // Get the compressed file 119 | fs.readFile('./input.txt.gz', function(err, file) { 120 | // Uncompress the file 121 | zlib.gunzip(file, function(err, uncompressedFileBuffer) { 122 | // Convert buffer to utf8 string 123 | var uncompressedFile = uncompressedFileBuffer.toString('utf8', 0, uncompressedFileBuffer.length); 124 | // console.log(util.inspect(uncompressedFile)); 125 | 126 | // Get original file as utf8 string 127 | fs.readFile('./input.txt', {encoding: 'utf8'}, function(err, originalFile) { 128 | // console.log(util.inspect(originalFile)); 129 | 130 | // Compare the original file to the uncompressed .gz file 131 | originalFile.should.equal(uncompressedFile); 132 | 133 | done(); 134 | }); 135 | }); 136 | }); 137 | }); 138 | gulp.src('*.txt') 139 | .pipe(zopfli()) 140 | .pipe(outStream); 141 | }); 142 | 143 | it('should match uncompressed file with original file (zlib)', function(done) { 144 | 145 | var outStream = gulp.dest('./'); 146 | 147 | // Capture close event of the write stream so we know when gulp.dest finishes 148 | outStream.on('close', function() { 149 | 150 | // Get the compressed file 151 | fs.readFile('./input.txt.zz', function(err, file) { 152 | // Uncompress the file 153 | zlib.unzip(file, function(err, uncompressedFileBuffer) { 154 | // Convert buffer to utf8 string 155 | var uncompressedFile = uncompressedFileBuffer.toString('utf8', 0, uncompressedFileBuffer.length); 156 | // console.log(util.inspect(uncompressedFile)); 157 | 158 | // Get original file as utf8 string 159 | fs.readFile('./input.txt', {encoding: 'utf8'}, function(err, originalFile) { 160 | // console.log(util.inspect(originalFile)); 161 | 162 | // Compare the original file to the uncompressed .gz file 163 | originalFile.should.equal(uncompressedFile); 164 | 165 | done(); 166 | }); 167 | }); 168 | }); 169 | }); 170 | gulp.src('*.txt') 171 | .pipe(zopfli({format: 'zlib'})) 172 | .pipe(outStream); 173 | }); 174 | 175 | it('should match uncompressed file with original file (deflate)', function(done) { 176 | 177 | var outStream = gulp.dest('./'); 178 | 179 | // Capture close event of the write stream so we know when gulp.dest finishes 180 | outStream.on('close', function() { 181 | 182 | // Get the compressed file 183 | fs.readFile('./input.txt.deflate', function(err, file) { 184 | // Uncompress the file 185 | zlib.inflateRaw(file, function(err, uncompressedFileBuffer) { 186 | // Convert buffer to utf8 string 187 | var uncompressedFile = uncompressedFileBuffer.toString('utf8', 0, uncompressedFileBuffer.length); 188 | // console.log(util.inspect(uncompressedFile)); 189 | 190 | // Get original file as utf8 string 191 | fs.readFile('./input.txt', {encoding: 'utf8'}, function(err, originalFile) { 192 | 193 | // Compare the original file to the uncompressed .gz file 194 | originalFile.should.equal(uncompressedFile); 195 | 196 | done(); 197 | }); 198 | }); 199 | }); 200 | }); 201 | gulp.src('*.txt') 202 | .pipe(zopfli({format: 'deflate'})) 203 | .pipe(outStream); 204 | }); 205 | 206 | it('should match uncompressed file with original file in stream mode', function(done) { 207 | 208 | var outStream = gulp.dest('./', {buffer: false}); 209 | 210 | // Capture close event of the write stream so we know when gulp.dest finishes 211 | outStream.on('close', function() { 212 | 213 | // Get the compressed file 214 | fs.readFile('./input.txt.gz', function(err, file) { 215 | // Uncompress the file 216 | zlib.gunzip(file, function(err, uncompressedFileBuffer) { 217 | // Convert buffer to utf8 string 218 | var uncompressedFile = uncompressedFileBuffer.toString('utf8', 0, uncompressedFileBuffer.length); 219 | // console.log(util.inspect(uncompressedFile)); 220 | 221 | // Get original file as utf8 string 222 | fs.readFile('./input.txt', {encoding: 'utf8'}, function(err, originalFile) { 223 | if(err) { 224 | throw err; 225 | } 226 | // Compare the original file to the uncompressed .gz file 227 | originalFile.should.equal(uncompressedFile); 228 | 229 | done(); 230 | }); 231 | }); 232 | }); 233 | }); 234 | gulp.src('*.txt', {buffer: false}) 235 | .pipe(zopfli()) 236 | .pipe(outStream); 237 | }); 238 | 239 | describe('compressed file is larger than original', function(){ 240 | it('does not output if does not reach threshold', function(done) { 241 | var outStream = gulp.dest('computed'); 242 | var ratio = 1; 243 | outStream.on('close', function() { 244 | fsPromise.exists('computed/condensed.txt.gz') 245 | .then(function(compressedFile){ 246 | compressedFile.should.be.false; 247 | done(); 248 | }) 249 | .catch(done) 250 | ; 251 | }); 252 | gulp.src('original/*.txt') 253 | .pipe(zopfli({thresholdRatio: ratio})) 254 | .pipe(outStream); 255 | }); 256 | it('outputs original if does reaches threshold', function(done) { 257 | var outStream = gulp.dest('computed'); 258 | var ratio = 1; 259 | outStream.on('close', function() { 260 | fsPromise.exists('computed/condensed.txt') 261 | .then(function(compressedFile){ 262 | compressedFile.should.be.true; 263 | done(); 264 | }) 265 | .catch(done) 266 | ; 267 | }); 268 | gulp.src('original/*.txt') 269 | .pipe(zopfli({ 270 | thresholdRatio: ratio, 271 | thresholdBehavior: 'original' 272 | })) 273 | .pipe(outStream); 274 | }); 275 | it('outputs compress if reaches threshold', function(done) { 276 | var outStream = gulp.dest('computed'); 277 | var ratio = 0.9; 278 | outStream.on('close', function() { 279 | fsPromise.exists('computed/condensed.txt.gz') 280 | .then(function(compressedFile){ 281 | compressedFile.should.be.true; 282 | done(); 283 | }) 284 | .catch(done) 285 | ; 286 | }); 287 | gulp.src('original/*.txt') 288 | .pipe(zopfli({thresholdRatio: ratio})) 289 | .pipe(outStream); 290 | }); 291 | it('should not output compressed file if it is larger than uncompressed in stream mode'); 292 | 293 | }); 294 | }); 295 | }); 296 | --------------------------------------------------------------------------------