Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
ludofischer committed Sep 3, 2014
0 parents commit 6c9389b
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
**/build
17 changes: 17 additions & 0 deletions 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.
19 changes: 19 additions & 0 deletions 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/).
35 changes: 35 additions & 0 deletions 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);
};
}
24 changes: 24 additions & 0 deletions 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"
}
}
8 changes: 8 additions & 0 deletions test/fixtures/basic/src/index.html
@@ -0,0 +1,8 @@
<!doctype html>

<html>
<head><title>Hello</title></head>
<body>
Hello, world!
</body>
</html>
Binary file added test/fixtures/pdf/src/document.pdf
Binary file not shown.
1 change: 1 addition & 0 deletions test/fixtures/pdf/src/style.css
@@ -0,0 +1 @@
/* Not much style here. */
28 changes: 28 additions & 0 deletions 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');
});
});

0 comments on commit 6c9389b

Please sign in to comment.