html-compression-webpack-plugin
Advanced tools
Comparing version 0.1.0 to 0.2.0
65
index.js
@@ -9,2 +9,4 @@ /* | ||
var url = require('url'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
@@ -51,5 +53,11 @@ var RawSource = require("webpack-sources/lib/RawSource"); | ||
} | ||
this.test = options.test || options.regExp || /\.html$/ | ||
this.testHTML = options.testHTML || /\.html$/ | ||
this.test = options.test || /.*\.(js|css)$/i | ||
this.threshold = options.threshold || 0; | ||
this.minRatio = options.minRatio || 0.8; | ||
this.deleteOriginals = options.deleteOriginals || true | ||
this.assetsRelativeOutputDirectory = options.assetsRelativeOutputDirectory | ||
this.originalAssetsPaths = new Array(); | ||
if(!this.assetsRelativeOutputDirectory && this.deleteOriginals) | ||
throw new Error('Set relative output directory of assets when enabling deletion of original files'); | ||
} | ||
@@ -59,4 +67,5 @@ module.exports = HTMLCompressionPlugin; | ||
HTMLCompressionPlugin.prototype.apply = function(compiler) { | ||
compiler.plugin('emit', function(compilation, callback) { | ||
async.forEach(Object.keys(compilation.assets), function(file, callback) { | ||
compiler.plugin("this-compilation", function(compilation) { | ||
compilation.plugin('optimize-assets', function(assets, callback) { | ||
async.forEach(Object.keys(assets), function(file, callback) { | ||
if(Array.isArray(this.test)) { | ||
@@ -67,5 +76,4 @@ if(this.test.every(function(t) { | ||
} else if(this.test && !this.test.test(file)) | ||
return callback(); | ||
console.log(file + " is HTML!") | ||
var asset = compilation.assets[file]; | ||
return callback(); | ||
var asset = assets[file]; | ||
var content = asset.source(); | ||
@@ -88,7 +96,50 @@ if(!Buffer.isBuffer(content)) | ||
}); | ||
compilation.assets[newFile] = new RawSource(result); | ||
if(this.deleteOriginals === true){ | ||
this.originalAssetsPaths.push(path.join(__dirname, this.assetsRelativeOutputDirectory, file)); | ||
} | ||
assets[newFile] = new RawSource(result); | ||
callback(); | ||
}.bind(this)); | ||
}.bind(this), callback); | ||
}.bind(this)); | ||
}.bind(this)); | ||
compiler.plugin('emit', function(compilation, callback) { | ||
async.forEach(Object.keys(compilation.assets), function(file, callback) { | ||
if(Array.isArray(this.testHTML)) { | ||
if(this.testHTML.every(function(t) { | ||
return !t.test(file); | ||
})) return callback(); | ||
} else if(this.testHTML && !this.testHTML.test(file)) | ||
return callback(); | ||
var asset = compilation.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]; | ||
}); | ||
if(this.deleteOriginals === true){ | ||
this.originalAssetsPaths.push(path.join(__dirname, this.assetsRelativeOutputDirectory, file)); | ||
} | ||
compilation.assets[newFile] = new RawSource(result); | ||
callback(); | ||
}.bind(this)); | ||
}.bind(this), callback); | ||
}.bind(this)); | ||
compiler.plugin('done', function(stats) { | ||
for (var i in this.originalAssetsPaths) { | ||
fs.unlink(this.originalAssetsPaths[i]); | ||
} | ||
}.bind(this)); | ||
}; |
{ | ||
"name": "html-compression-webpack-plugin", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"author": "Tobias Koppers @sokra & Tom De Backer @TomDeBacker", | ||
@@ -5,0 +5,0 @@ "description": "Prepare compressed versions of assets to serve them with Content-Encoding.", |
@@ -1,6 +0,11 @@ | ||
# html compression plugin for webpack | ||
# HTML compression plugin for webpack | ||
## Info | ||
The `compression-webpack-plugin` didn't compress HTML files. This plugin aims to solve that. Also added an option to remove the original assets after compressing. | ||
## Usage | ||
Credits to @sokra for the code from compression-webpack-plugin (https://github.com/webpack/compression-webpack-plugin) | ||
Link to NPM: https://www.npmjs.com/package/html-compression-webpack-plugin | ||
@@ -12,8 +17,11 @@ ``` javascript | ||
new HTMLCompressionPlugin({ | ||
asset: "[path].gz[query]", | ||
algorithm: "gzip", | ||
test: /\.js$|\.html$/, | ||
threshold: 10240, | ||
minRatio: 0.8 | ||
}) | ||
testHTML: /\.html$/, | ||
test: /.*\.(css|js)$/i, | ||
deleteOriginals: true, | ||
assetsRelativeOutputDirectory: '../build/client/assets', | ||
asset: '[path].gz', | ||
algorithm: 'gzip', | ||
threshold: 0, | ||
minRatio: 0.0 | ||
}) | ||
] | ||
@@ -23,7 +31,11 @@ } | ||
Arguments: | ||
* `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]"`. | ||
* `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]"` | ||
* `assetsRelativeOutputDirectory`: The relative directory where your assets will be output to. Plugin will look in sub-folders for the original assets aswell. | ||
* `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 HTML asset. | ||
* `deleteOriginals`: All original assets which have been compressed will be deleted in the done phase. The stats after bundling will show the files emitted but they will be deleted. Defaults to `true` | ||
* `test`: All assets matching this RegExp are processed. Defaults to `/.*\.(js|css)$/i` | ||
* `testHTML`: All assets matching this RegExp are processed. This will happen in the emit phase. Defaults to `/\.html$/` | ||
* `threshold`: Only assets bigger than this size are processed. In bytes. Defaults to `0`. | ||
@@ -42,2 +54,2 @@ * `minRatio`: Only assets that compress better that this ratio are processed. Defaults to `0.8`. | ||
MIT (http://www.opensource.org/licenses/mit-license.php) | ||
MIT (http://www.opensource.org/licenses/mit-license.php) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8234
135
52
1