Strip HTML comments from Blaze templates in production

I’m currently using Blaze templates, and have lots of HTML comments, often for commenting out parts of code that I need to stash.

I noticed that in production, these comments aren’t stripped. Not only does that increase the bundle size, but it leaks development info into the final public product.

Is there an easy way to minify Blaze templates when they’re compiled for production? At the very least stripping out HTML comments, but if it also strips unnecessary whitespaces / lines / etc. that would be great as well.

Or else, if this isn’t an automatic part of the meteor build chain, is there a tool out there that I can use to pre-process my Blaze templates before building the final production bundle?

1 Like

Simply use a Blaze comment block to wrap the html one:

{{!--
<!--
Html content here
-->
--}}

Or just use only the Blaze comments markup.

Hi there,
I want to come back to this topic, we really need to strip comments out of the production HTML. I tried to create a html-minifier but only .js and .css files are supported. Is there any other solution?

Not exactly the same, but replacing them with comment tags will ensure they are removed when compiled.

@quape if you can’t use the Blaze comments (curly brackets) then what about a small isobuild compiler? Since striping comments won’t require an AST (afaik) you should be good using a simple regex.

Edit: I think the best would be to create a commit in the Blaze repo because this should be core functionality of Blaze

Edit edit: okay none of the build plugins (compiler, linter, minifier) will work with html and that’s a pity since there won’t be any way right now to “enhance” the toolchain with own transformations. We would have to implement this feature for Blaze.

Maybe someone of the core team can enlighten us, why it isobuild designed to prevent a second html compiler plugin?

1 Like

@jkuester Thanks for your help. Well I would also call it a security issue when normal HTML comments find their way in a production system. I would treat them like js comments and strip them before going productive. At least I should have a option for that or would be able to write my own minifier.

3 Likes