Reputation: 1
I'm using handlebars templating engine on my app, which mainly aims at desktop browsers.
Currently I distribute all my templates in HTML and compile them in the browser. I want to apply precompiling to my templates to make my app faster, but things didn't work as I thought.
I run commands like this to precompile and minimize my templates:
handlebars -m example.handlebars -f example.js
Then I've got the example.js
with the file size doubled!
I do cut down the consume of browsers without compiling templates and the runtime package of handlebars is much smaller. But the file size of my templates costs me more than what I've got.
So did I make anything wrong when precompiling my templates?
Or did I misunderstand the meaning of precompiling templates?
Upvotes: 0
Views: 582
Reputation: 47117
There will always be an overhead in size when precompiling templates, since expressions like this:
<div>{{values.abc}}</div>
Will be compiled to something like:
function program(data) {
return '<div>' + program2(data) + '</div>';
}
function program2(data) {
return data.values ? data.values.abc ? data.values.abc : '' : '';
}
But here size shouldn't be a major concern. If it is you should consider dynamically loading the templates when they are required, instead of sending everything to the client at once.
Upvotes: 2