Package Exports
- htmlnano
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (htmlnano) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
PostHTML Minifier
Modular HTML minifier, built on top of the PostHTML. Inspired by cssnano.
Usage
Gulp
npm install --save-dev gulp-htmlnano
var gulp = require('gulp');
var htmlnano = require('gulp-htmlnano');
var options = {
removeComments: false
};
gulp.task('default', function() {
return gulp
.src('./index.html')
.pipe(htmlnano(options))
.pipe(gulp.dest('./build'));
});
Javascript
var htmlnano = require('htmlnano');
var options = {
removeEmptyAttributes: false, // Disable the module "removeEmptyAttributes"
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
};
htmlnano.process(html, options).then(function (result) {
// result.html is minified
});
PostHTML
Just add htmlnano
as the last plugin:
var posthtml = require('posthtml');
var options = {
removeComments: false, // Disable the module "removeComments"
collapseWhitespace: 'conservative' // Pass options to the module "collapseWhitespace"
};
posthtml([
/* other PostHTML plugins */
require('htmlnano')(options)
]).process(html).then(function (result) {
// result.html is minified
});
Modules
By default all modules are enabled. You can disable some of them by passing module name with false
in the plugin options (like in the usage example above).
collapseWhitespace
Collapses redundant white spaces (including new lines). It doesn’t affect white spaces in the elements <style>
, <textarea>
, <script>
, and <pre>
.
Options:
all
— collapses all redundant white spaces (default)conservative
— collapses all redundant white spaces to 1 space
Source:
<div>
hello world!
<style>div { color: red; } </style>
</div>
Minified (with all
):
<div>hello world!<style>div { color: red; } </style></div>
Minified (with conservative
):
<div> hello world! <style>div { color: red; } </style> </div>
This module can have side effects.
<i>hello</i> <i>world</i>
after minification will be rendered as helloworld
.
To prevent that use conservative
option.
removeComments
Options:
safe
– removes all HTML comments except<!--noindex--><!--/noindex-->
(default)all
— removes all HTML comments
Source:
<div><!-- test --></div>
Minified:
<div></div>
removeEmptyAttributes
Removes empty safe-to-remove attributes.
Source:
<img src="foo.jpg" alt="" style="">
Minified:
<img src="foo.jpg" alt="">
minifyCss
Minifies CSS with cssnano inside <style>
tags and style
attributes.
Options: See the documentation of cssnano. For example you can keep outdated vendor prefixes:
htmlnano.process(html, {
minifyCss: {
autoprefixer: false
}
});
Source:
<div>
<style>
h1 {
margin: 10px 10px 10px 10px;
color: #ff0000;
}
</style>
</div>
Minified:
<div>
<style>h1{margin:10px;color:red}</style>
</div>
minifyJs
Minifies JS with UglifyJS2 inside <script>
tags.
Options: See the API documentation of UglifyJS2
Source:
<div>
<script>
/* comment */
var foo = function () {
};
</script>
</div>
Minified:
<div>
<script>var foob=function(){};</script>
</div>
custom
It's also possible to pass custom modules in the minifier.
As a function:
var options = {
custom: function (tree, options) {
// Some minification
return tree;
}
};
Or as a list of functions:
var options = {
custom: [
function (tree, options) {
// Some minification
return tree;
},
function (tree, options) {
// Some other minification
return tree;
}
]
};
options
is an object with all options that were passed to the plugin.
Contribute
Since the minifier is modular, it's very easy to add new modules:
Create a ES6-file inside
lib/modules/
with a function that does some minification. For example you can checklib/modules/example.es6
.Add the module in the modules array. The modules are applied from top to bottom. So you can choose the order for your module.
Create a JS-file inside
test/modules/
with some unit-tests.Describe your module in the section "Modules".
Send me a pull request.
Other types of contribution (bug fixes, documentation improves, etc) are also welcome! Would like to contribute, but don't have any ideas what to do? Check out our issues.