Skip to content

Commit 662b974

Browse files
committed
Initial commit
0 parents  commit 662b974

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# compression plugin for webpack
2+
3+
## Usage
4+
5+
``` javascript
6+
var CompressionPlugin = require("compression-webpack-plugin");
7+
module.exports = {
8+
plugins: [
9+
new CompressionPlugin({
10+
asset: "{file}.gz",
11+
algorithm: "gzip",
12+
regExp: /\.js$|\.html$/,
13+
threshold: 10240,
14+
minRatio: 0.8
15+
})
16+
]
17+
}
18+
```
19+
20+
Arguments:
21+
22+
* `asset`: The target asset name. `{file}` is replaced with the original asset. Defaults to `"{file}.gz"`.
23+
* `algorithm`: Can be a `function(buf, callback)` or a string. For a string the algorithm is tacken from `zlib`. Defaults to `"gzip"`.
24+
* `regExp`: All assets matching this RegExp are processed. Defaults to every asset.
25+
* `threshold`: Only assets bigger than this size are processed. In bytes. Defaults to `0`.
26+
* `minRatio`: Only assets that compress better that this ratio are processed. Defaults to `0.8`.
27+
28+
## License
29+
30+
MIT (http://www.opensource.org/licenses/mit-license.php)

index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var async = require("async");
6+
7+
var Source = require("webpack/lib/Source");
8+
9+
function RawSource(value) {
10+
Source.call(this);
11+
this._value = value;
12+
}
13+
module.exports = RawSource;
14+
15+
RawSource.prototype = Object.create(Source.prototype);
16+
RawSource.prototype._bake = function() {
17+
return {
18+
source: this._value
19+
};
20+
};
21+
22+
function CompressionPlugin(options) {
23+
options = options || {};
24+
this.asset = options.asset || "{file}.gz";
25+
this.algorithm = options.algorithm || "gzip";
26+
if(typeof this.algorithm === "string") {
27+
var zlib = require("zlib");
28+
this.algorithm = zlib[this.algorithm];
29+
if(!this.algorithm) throw new Error("Algorithm not found in zlib");
30+
this.algorithm = this.algorithm.bind(zlib);
31+
}
32+
this.regExp = options.regExp;
33+
this.threshold = options.threshold || 0;
34+
this.minRatio = options.minRatio || 0.8;
35+
}
36+
module.exports = CompressionPlugin;
37+
38+
CompressionPlugin.prototype.apply = function(compiler) {
39+
compiler.plugin("compilation", function(compilation) {
40+
compilation.plugin("optimize-assets", function(assets, callback) {
41+
async.forEach(Object.keys(assets), function(file, callback) {
42+
if(this.regExp && !this.regExp.test(file)) return callback();
43+
var asset = assets[file];
44+
var content = asset.source();
45+
if(!Buffer.isBuffer(content))
46+
content = new Buffer(content, "utf-8");
47+
var originalSize = content.length;
48+
if(originalSize < this.threshold) return callback();
49+
this.algorithm(content, function(err, result) {
50+
if(err) return callback(err);
51+
if(result.length / originalSize > this.minRatio) return callback();
52+
var newFile = this.asset.replace(/\{file\}/g, file);
53+
assets[newFile] = new RawSource(result);
54+
callback();
55+
}.bind(this));
56+
}.bind(this), callback);
57+
}.bind(this));
58+
}.bind(this));
59+
};

package.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "compression-webpack-plugin",
3+
"version": "0.1.0",
4+
"author": "Tobias Koppers @sokra",
5+
"description": "Prepare compressed versions of assets to serve them with Content-Encoding",
6+
"peerDependencies": {
7+
"webpack": ">=0.11 <0.12"
8+
},
9+
"dependencies": {
10+
"async": "0.2.x"
11+
},
12+
"homepage": "http://github.com/webpack/compression-webpack-plugin",
13+
"repository": {
14+
"type": "git",
15+
"url": "http://github.com/webpack/compression-webpack-plugin.git"
16+
},
17+
"licenses": [
18+
{
19+
"type": "MIT",
20+
"url": "http://www.opensource.org/licenses/mit-license.php"
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)