-
Notifications
You must be signed in to change notification settings - Fork 202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optionally remove whitespace and HTML comments when compiled #49
Comments
Interesting idea. Though it could prove to be extremely difficult to get right. gzip handles alot here. Any ideas on how it would work? |
Before I started using this plugin, I used a grunt task to compile all Handlebars templates into a I stripped out the whitespace and HTML comments using the following code. /* Inside a grunt task */
var templateFunction =
require("handlebars").precompile(file.read(filepath));
// Remove unwanted line ending characters \r\n or \n from compiled templates
templateFunction = templateFunction.replace(/\s*(\\r\\n|\\n)+\s*/gm, '');
// Remove unwanted HTML comments from compiled templates
templateFunction = templateFunction.replace(/<!--[^>]*-->/gm, ''); |
Right but this changes the rendering of the markup, doesn't it? templateFunction = templateFunction.replace(/\s*(\\r\\n|\\n)+\s*/gm, ''); Wouldn't we want it to be: templateFunction = templateFunction.replace(/\s*(\\r\\n|\\n)+\s*/gm, ' '); Things like Rating:
<span class="rating">4</span>
<span class="delimiter">/</span>
<span class="max-rating">5</span> Is an example I've personally run into recently. When you strip whitespace, there are no spaces between the output You'd also want to handle tabs. I'd love to see like 10 templates with the whitespace stripped and 10 templates with it all there and see the gzipped difference. In my testing in my app, I didn't implement this because it added unpredictability of spacing, but it only saved us 40 bytes in a 3 KB response after gzip. |
For your first point, if I need that space, I put it within the span. The size difference between inclusion and exclusion of comments is extremely minimal when gzip'd versions are compared. Totally agree! I have a requirement that certain comments must exist in every file. Comments such as: <!-- Copyright that is huge... --> I've seen "poorly" written jQuery plugins that fail |
I'd love a pull request with an option for each of these - including no space and one space replaces. Specifically, it'd be like 3 if-statements here: https://github.com/SlexAxton/require-handlebars-plugin/blob/master/hbs.js#L380 That's the output function string like you were modifying in grunt. People have added an Seem good? |
I'm also worried about this, if added, please set it as |
Closing. Optional patch welcome. |
Relevant Handlebars.js issue: handlebars-lang/handlebars.js#336 |
I propose an option,
removeWhitespace
, to remove whitespace characters including new line characters (\n
or\r
or\r\n
) and HTML comments<!-- -->
.These characters take up space within the compiled template strings.
The text was updated successfully, but these errors were encountered: