Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d09a486
webpack 1.0
sokra Dec 17, 2013
07eb496
Remove unneeded module.exports
shama May 5, 2014
bc50dcb
Merge pull request #1 from shama/patch-1
sokra May 6, 2014
a13a699
fixes #3
sokra Nov 11, 2014
b8e2546
0.1.2
sokra Nov 11, 2014
64523c6
fixed #5
sokra Apr 8, 2015
9a6a85a
0.2.0
sokra Apr 8, 2015
8d75f0e
Permit {path} and {query} in asset name
codechaotic Aug 12, 2015
31747c4
Add missing semicolon
codechaotic Aug 12, 2015
350b192
Add zopfli option
rufman Aug 19, 2015
5e45990
Expose zopfli compression options and add documentation
rufman Aug 20, 2015
ec8897a
Fix copy paste error
rufman Sep 8, 2015
e0fd30d
Merge pull request #9 from codechaotic/master
sokra Jan 21, 2016
9c75ecc
Merge pull request #10 from rufman/master
sokra Jan 21, 2016
d4d96d2
fixed #12, fixed #13
sokra Jan 23, 2016
9e66cfa
some breaking changes
sokra Jan 23, 2016
3f06d1b
0.3.0
sokra Jan 23, 2016
173ace9
Update index.js
Icehunter Feb 5, 2016
ee8e701
instance var instead of static
Icehunter Feb 11, 2016
5d3b3a3
Merge pull request #18 from Icehunter/master
sokra Mar 26, 2016
e428e53
0.3.1
sokra Mar 26, 2016
402e942
Update node-zopfli version
robertbachmann Sep 13, 2016
7e55907
Merge pull request #34 from robertbachmann/update_node_zopfli
Sep 24, 2016
f534167
chore(license): update to JSF standard license
joshwiens Feb 21, 2017
66ec99a
docs(readme): updates for JSF maintainers
joshwiens Feb 21, 2017
a4fc534
Merge pull request #42 from webpack-contrib/d3viant0ne-JSFMaintainers
joshwiens Feb 21, 2017
8e5937e
Merge pull request #41 from webpack-contrib/d3viant0ne-JSFLicense
joshwiens Feb 21, 2017
2b01c04
Use emit event instead of this-compilation
ijpiantanida Mar 2, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

Copyright JS Foundation and other contributors

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.
83 changes: 74 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
# compression plugin for webpack
[![npm][npm]][npm-url]
[![deps][deps]][deps-url]
[![chat][chat]][chat-url]

## Usage
<div align="center">
<!-- replace with accurate logo e.g from https://worldvectorlogo.com/ -->
<a href="https://github.com/webpack/webpack">
<img width="200" height="200" vspace="" hspace="25"
src="https://cdn.rawgit.com/webpack/media/e7485eb2/logo/icon.svg">
</a>
<h1>Compression Plugin</h1>
<p>Compression plugin for Webpack.<p>
</div>

<h2 align="center">Install</h2>

```bash
npm i -D compression-webpack-plugin
```

<h2 align="center">Usage</h2>

``` javascript
var CompressionPlugin = require("compression-webpack-plugin");
module.exports = {
plugins: [
new CompressionPlugin({
asset: "{file}.gz",
asset: "[path].gz[query]",
algorithm: "gzip",
regExp: /\.js$|\.html$/,
test: /\.js$|\.html$/,
threshold: 10240,
minRatio: 0.8
})
Expand All @@ -19,12 +37,59 @@ module.exports = {

Arguments:

* `asset`: The target asset name. `{file}` is replaced with the original asset. Defaults to `"{file}.gz"`.
* `algorithm`: Can be a `function(buf, callback)` or a string. For a string the algorithm is tacken from `zlib`. Defaults to `"gzip"`.
* `regExp`: All assets matching this RegExp are processed. Defaults to every asset.
* `asset`: The target asset name. `[file]` is replaced with the original asset. `[path]` is replaced with the path of the original asset and `[query]` with the query. Defaults to `"[path].gz[query]"`.
* `algorithm`: Can be a `function(buf, callback)` or a string. For a string the algorithm is taken from `zlib` (or zopfli for `zopfli`). Defaults to `"gzip"`.
* `test`: All assets matching this RegExp are processed. Defaults to every asset.
* `threshold`: Only assets bigger than this size are processed. In bytes. Defaults to `0`.
* `minRatio`: Only assets that compress better that this ratio are processed. Defaults to `0.8`.

## License
Option Arguments for Zopfli (see [node-zopfli](https://github.com/pierreinglebert/node-zopfli#options) doc for details):
* verbose: Default: false,
* verbose_more: Default: false,
* numiterations: Default: 15,
* blocksplitting: Default: true,
* blocksplittinglast: Default: false,
* blocksplittingmax: Default: 15

<h2 align="center">Maintainers</h2>

<table>
<tbody>
<tr>
<td align="center">
<img width="150" height="150"
src="https://avatars3.githubusercontent.com/u/166921?v=3&s=150">
</br>
<a href="https://github.com/bebraw">Juho Vepsäläinen</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars2.githubusercontent.com/u/8420490?v=3&s=150">
</br>
<a href="https://github.com/d3viant0ne">Joshua Wiens</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars3.githubusercontent.com/u/533616?v=3&s=150">
</br>
<a href="https://github.com/SpaceK33z">Kees Kluskens</a>
</td>
<td align="center">
<img width="150" height="150"
src="https://avatars3.githubusercontent.com/u/3408176?v=3&s=150">
</br>
<a href="https://github.com/TheLarkInn">Sean Larkin</a>
</td>
</tr>
<tbody>
</table>


[npm]: https://img.shields.io/npm/v/webpack-loader-seed.svg
[npm-url]: https://npmjs.com/package/webpack-loader-seed

[deps]: https://david-dm.org/webpack-contrib/webpack-loader-seed.svg
[deps-url]: https://david-dm.org/webpack-contrib/webpack-loader-seed

MIT (http://www.opensource.org/licenses/mit-license.php)
[chat]: https://img.shields.io/badge/gitter-webpack%2Fwebpack-brightgreen.svg
[chat-url]: https://gitter.im/webpack/webpack
108 changes: 68 additions & 40 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,85 @@
Author Tobias Koppers @sokra
*/
var async = require("async");
var url = require('url');

var Source = require("webpack/lib/Source");

function RawSource(value) {
Source.call(this);
this._value = value;
}
module.exports = RawSource;

RawSource.prototype = Object.create(Source.prototype);
RawSource.prototype._bake = function() {
return {
source: this._value
};
};
var RawSource = require("webpack-sources/lib/RawSource");

function CompressionPlugin(options) {
options = options || {};
this.asset = options.asset || "{file}.gz";
this.asset = options.asset || "[path].gz[query]";
this.algorithm = options.algorithm || "gzip";
this.compressionOptions = {};
if(typeof this.algorithm === "string") {
var zlib = require("zlib");
this.algorithm = zlib[this.algorithm];
if(!this.algorithm) throw new Error("Algorithm not found in zlib");
this.algorithm = this.algorithm.bind(zlib);
if (this.algorithm === "zopfli") {
try {
var zopfli = require("node-zopfli");
} catch(err) {
throw new Error("node-zopfli not found");
}
this.compressionOptions = {
verbose: options.hasOwnProperty('verbose') ? options.verbose : false,
verbose_more: options.hasOwnProperty('verbose_more') ? options.verbose_more : false,
numiterations: options.numiterations ? options.numiterations : 15,
blocksplitting: options.hasOwnProperty('blocksplitting') ? options.blocksplitting : true,
blocksplittinglast: options.hasOwnProperty('blocksplittinglast') ? options.blocksplittinglast : false,
blocksplittingmax: options.blocksplittingmax ? options.blocksplittingmax : 15
};
this.algorithm = function (content, options, fn) {
zopfli.gzip(content, options, fn);
};
} else {
var zlib = require("zlib");
this.algorithm = zlib[this.algorithm];
if(!this.algorithm) throw new Error("Algorithm not found in zlib");
this.compressionOptions = {
level: options.level || 9,
flush: options.flush,
chunkSize: options.chunkSize,
windowBits: options.windowBits,
memLevel: options.memLevel,
strategy: options.strategy,
dictionary: options.dictionary
};
}
}
this.regExp = options.regExp;
this.test = options.test || options.regExp;
this.threshold = options.threshold || 0;
this.minRatio = options.minRatio || 0.8;
}
module.exports = CompressionPlugin;

CompressionPlugin.prototype.apply = function(compiler) {
compiler.plugin("compilation", function(compilation) {
compilation.plugin("optimize-assets", function(assets, callback) {
async.forEach(Object.keys(assets), function(file, callback) {
if(this.regExp && !this.regExp.test(file)) return callback();
var asset = assets[file];
var content = asset.source();
if(!Buffer.isBuffer(content))
content = new Buffer(content, "utf-8");
var originalSize = content.length;
if(originalSize < this.threshold) return callback();
this.algorithm(content, function(err, result) {
if(err) return callback(err);
if(result.length / originalSize > this.minRatio) return callback();
var newFile = this.asset.replace(/\{file\}/g, file);
assets[newFile] = new RawSource(result);
callback();
}.bind(this));
}.bind(this), callback);
}.bind(this));
compiler.plugin("emit", function(compilation, callback) {
var assets = compilation.assets;
async.forEach(Object.keys(assets), function(file, callback) {
if(Array.isArray(this.test)) {
if(this.test.every(function(t) {
return !t.test(file);
})) return callback();
} else if(this.test && !this.test.test(file))
return callback();
var asset = assets[file];
var content = asset.source();
if(!Buffer.isBuffer(content))
content = new Buffer(content, "utf-8");
var originalSize = content.length;
if(originalSize < this.threshold) return callback();
this.algorithm(content, this.compressionOptions, function(err, result) {
if(err) return callback(err);
if(result.length / originalSize > this.minRatio) return callback();
var parse = url.parse(file);
var sub = {
file: file,
path: parse.pathname,
query: parse.query || ""
};
var newFile = this.asset.replace(/\[(file|path|query)\]/g, function(p0,p1) {
return sub[p1];
});
assets[newFile] = new RawSource(result);
callback();
}.bind(this));
}.bind(this), callback);
}.bind(this));
};
};
45 changes: 23 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
{
"name": "compression-webpack-plugin",
"version": "0.1.0",
"author": "Tobias Koppers @sokra",
"description": "Prepare compressed versions of assets to serve them with Content-Encoding",
"peerDependencies": {
"webpack": ">=0.11 <0.12"
},
"dependencies": {
"async": "0.2.x"
},
"homepage": "http://github.com/webpack/compression-webpack-plugin",
"repository": {
"type": "git",
"url": "http://github.com/webpack/compression-webpack-plugin.git"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
]
}
"name": "compression-webpack-plugin",
"version": "0.3.2",
"author": "Tobias Koppers @sokra",
"description": "Prepare compressed versions of assets to serve them with Content-Encoding",
"dependencies": {
"async": "0.2.x",
"webpack-sources": "^0.1.0"
},
"optionalDependencies": {
"node-zopfli": "^2.0.0"
},
"homepage": "http://github.com/webpack/compression-webpack-plugin",
"repository": {
"type": "git",
"url": "http://github.com/webpack/compression-webpack-plugin.git"
},
"licenses": [
{
"type": "MIT",
"url": "http://www.opensource.org/licenses/mit-license.php"
}
]
}