Skip to content

Instantly share code, notes, and snippets.

@LoyEgor
Last active April 23, 2017 04:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LoyEgor/860db9d14c6554e267b57431c8e58aca to your computer and use it in GitHub Desktop.
Save LoyEgor/860db9d14c6554e267b57431c8e58aca to your computer and use it in GitHub Desktop.
html compressin settings
// install
// npm i gulp-htmlmin gulp-replace
var htmlmin = require('gulp-htmlmin');
var replace = require('gulp-replace');
//compress html
gulp.task('htmlmin', function() {
return gulp.src('app/**/*.html')
.pipe(htmlmin({
//Extrem compression//
// collapseWhitespace: true,
// conservativeCollapse: true, //work with collapseWhitespace and save at least 1 space inside teg
// preserveLineBreaks: false, //make line break if collapseWhitespace true
collapseBooleanAttributes: true, //<input disabled="disabled"> to <input disabled>. problem in cases when css looks like: input[disabled="disabled"] { color: green }
decodeEntities: true,
minifyCSS: true,
minifyJS: true,
//removeAttributeQuotes: true, //remove "" and it may be too much
removeComments: true,
removeCommentsFromCDATA: true,
removeEmptyAttributes: true,
removeRedundantAttributes: true, //default attribute name-value get stripped. Example: <input type="text" /> convert to <input/> so css input[type="text"] won't work
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
sortAttributes: true, //improve gzip compression
sortClassName: true, //improve gzip compression
useShortDoctype: true
}))
//remove empty line from compressed html
.pipe(replace(/^\s*\r?\n/gm, ''))
//add min to js and css
.pipe(replace(/^(\s*<(link|script).*[^\/oldie](?!\.min).{4})\.(js|css)/gmi, "$1.min.$3"))
.pipe(gulp.dest('dist'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment