2
votes

I'm trying to setup a watchfile in webstorm.

I want to precompile one file into my main templates.js aggregate file.

Is this possible?

If I use /path/to/hello.tpl -f templates.js option it just overwrites all my other templates in the templates.js file.

If I use /path/to/templates > templates.js that means I have to precompile every single template in my filewatcher when I modify just one template.

Edit:

If you pass a directory, and redirect its output to a file, you can get all precompiled templates in one file. handlebars -e tpl ./templates/ > ./js/templates.js

This will precompile all ./templates/*.tpl files and save them to ./js/templates.js which you can then include in your page.

Here's some more info about the technique: http://berzniz.com/post/24743062344/handling-handlebars-js-like-a-pro

2
I have a doubt, usually I used a one to one mapping template and js. However you're having a different approach like many to one templates and js. Can you please share some tutorials/ blogs to get a deep understanding. - Praveen
Thank you, I marked as fav since started learning handlebar.js. Looks like I haven't enough done. I will delete my answer, within a few minutes. - Praveen

2 Answers

1
votes

There is a nodejs plugin which exactly does what you want and more. Please try this out and see if it suits you.

https://github.com/cif/handlebar-rider

0
votes

My solution to this was to slightly modify the handlebars module itself so in node_modules\handlebars\bin\handlebars find the processTemplate function and change

if (extension.test(path) || fs.statSync(path).isDirectory()) {
    processTemplate(path, root || template);
  }

to

if (extension.test(path) || /\.hbr$/.test(path)|| fs.statSync(path).isDirectory()) {
    processTemplate(path, root || template);
  }

which then takes all your template files (that I gave an hbr extension) and puts them in the file specified by the -f flag.