Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 6c9389b
Showing
9 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
**/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!doctype html> | ||
|
||
<html> | ||
<head><title>Hello</title></head> | ||
<body> | ||
Hello, world! | ||
</body> | ||
</html> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/* Not much style here. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
|