From 6c9389b328a3b75019d8d81dff4e4b3f184cf942 Mon Sep 17 00:00:00 2001 From: Ludovico Fischer Date: Wed, 3 Sep 2014 04:32:53 +0200 Subject: [PATCH] Initial commit. --- .gitignore | 2 ++ LICENSE | 17 ++++++++++++++ README.md | 19 ++++++++++++++++ lib/index.js | 35 +++++++++++++++++++++++++++++ package.json | 24 ++++++++++++++++++++ test/fixtures/basic/src/index.html | 8 +++++++ test/fixtures/pdf/src/document.pdf | Bin 0 -> 15 bytes test/fixtures/pdf/src/style.css | 1 + test/index.js | 28 +++++++++++++++++++++++ 9 files changed, 134 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 lib/index.js create mode 100644 package.json create mode 100644 test/fixtures/basic/src/index.html create mode 100644 test/fixtures/pdf/src/document.pdf create mode 100644 test/fixtures/pdf/src/style.css create mode 100644 test/index.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0452acb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +**/build \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7b6eba3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,17 @@ +The MIT License (MIT) +Copyright (c) 2014 Ludovico Fischer +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d4da1d2 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# metalsmith-gzip + +A [Metalsmith](http://metalsmith.io) plugin that lets you create gzipped versions of your build files. This is useful if you are hosting your website on Amazon S3, where it is not possible to set up compression on the server. + +## Usage + +`metalsmith-gzip` will produce a gzipped version of all files in your build whose extension matches the following regular expression + + /\.[html|css|js|json|xml|svg|txt]/ + +This list is loosely based on the [HTML5 Boilerplate server configuration](https://github.com/h5bp/server-configs-apache). + +You will then need to set up an upload script yourself that puts the gzipped versions of the files on S3 or whatever hosting provider you prefer, and take care that the files are served with the correct Content-encoding. + + At the moment, `metalsmith-gzip` does not take any configuration options. + +## Acknowledgements + +The functionality was inspired by the [Middleman gzip extension](http://middlemanapp.com/advanced/file-size-optimization/). diff --git a/lib/index.js b/lib/index.js new file mode 100644 index 0000000..72ebb64 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,35 @@ +var zlib = require('zlib'), + extname = require('path').extname, + each = require('async').each, + clone = require('lodash.clone'); + + +module.exports = plugin; + +function plugin() { + + var compressible = /\.[html|css|js|json|xml|svg|txt]/; + + return function(files, metalsmith, done) { + + each(Object.keys(files), function(file, done) { + + if (compressible.test(file)) { + var data = files[file]; + zlib.gzip(data.contents, function(err, buffer) { + if (!!err) { + done(err); + } + + var compressedFile = file + '.gz'; + files[compressedFile] = clone(data); + files[compressedFile].contents = buffer; + done(); + }); + + } else { + done(); + } + }, done); + }; +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..f8db0d1 --- /dev/null +++ b/package.json @@ -0,0 +1,24 @@ +{ + "name": "metalsmith-gzip", + "version": "0.1.0", + "author": "Ludovico Fischer", + "license": "MIT", + "description": "A Metalsmith plugin to compress build files with gzip.", + "keywords": [ + "metalsmith", + "plugin" + ], + "main": "lib/index.js", + "dependencies": { + "async": "~0.9.0", + "lodash.clone": "~2.4.1" + }, + "devDependencies": { + "tape": "~2.14.0", + "metalsmith": "~0.10.0" + }, + "repository": { + "type": "git", + "url": "https://github.com/ludovicofischer/metalsmith-gzip" + } +} diff --git a/test/fixtures/basic/src/index.html b/test/fixtures/basic/src/index.html new file mode 100644 index 0000000..606d713 --- /dev/null +++ b/test/fixtures/basic/src/index.html @@ -0,0 +1,8 @@ + + + +Hello + +Hello, world! + + diff --git a/test/fixtures/pdf/src/document.pdf b/test/fixtures/pdf/src/document.pdf new file mode 100644 index 0000000000000000000000000000000000000000..789c544745a9af78c10a493812b0725d15a71732 GIT binary patch literal 15 WcmZ=u%uZDZaB)*e%gjmD;{pIBOay8G literal 0 HcmV?d00001 diff --git a/test/fixtures/pdf/src/style.css b/test/fixtures/pdf/src/style.css new file mode 100644 index 0000000..fb3a7cb --- /dev/null +++ b/test/fixtures/pdf/src/style.css @@ -0,0 +1 @@ +/* Not much style here. */ diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..36896a5 --- /dev/null +++ b/test/index.js @@ -0,0 +1,28 @@ +var test = require('tape'); +var compress = require('..'); +var Metalsmith = require('metalsmith'); + +test('HTML is compressed', function(t) { + t.plan(3); + var metalsmith = Metalsmith('test/fixtures/basic'); + metalsmith + .use(compress()) + .build(function(err, files) { + t.error(err, 'No build errors'); + t.ok(files['index.html.gz'], 'The compressed file is created'); + t.ok(files['index.html'], 'The original file is conserved'); + }); +}); + +test('PDF is skipped', function(t) { + t.plan(3); + var metalsmith = Metalsmith('test/fixtures/pdf'); + metalsmith + .use(compress()) + .build(function(err, files) { + t.error(err, 'No build errors'); + t.notOk(files['document.pdf.gz'], 'No compressed file is created'); + t.ok(files['document.pdf'], 'The original file is conserved'); + }); +}); +