BeetleJuice BeetleJuice - 1 month ago 14x
HTML Question

gulp-htmlmin fails on valid document: workaround or abandon plugin?

I'm trying to minify my HTML. I've just discovered and started using the


My gulp task...

return gulp.src(appDev+'test.html')
.pipe(htmlmin({collapseWhitespace: true}))

fails when applied to this valid HTML, or any document with the

<div> < back </div>

The error is:

Error: Parse Error: < back </div>
at new HTMLParser (html-minifier\src\htmlparser.js:236:13)

I can think of two solutions:

  • Replace
    in all my templates. Doing so manually won't be fun, but that's life. Any idea how to do it in a gulp task?

  • Ditch this plugin in search for one that can parse and minify my templates. Any suggestions?

Guess I'm wondering how someone more experienced at building for deployment (I'm new) would handle this.


I'm afraid you're going to have to change them to &lt;. The html-minifier team has specifically stated they won't support bare <s.

You want do this anyway, both to not trip up parsers and to protect against certain XSS attacks. See

for more info.

The good news is any text editor worth its coding salt supports project-wide or at least multi-file search and replace. Assuming all your HTML <tags> don't have whitespace after the <, you should be able to just replace "< " with "&lt; ".