mattacular / grunt-handlebars-compiler

Grunt.js task to precompile Handlebars.js templates with the same options as the CLI utility
MIT License
12 stars 12 forks source link

Compiled templates are wrapped in a single require statement #4

Closed tfarnsworth closed 10 years ago

tfarnsworth commented 10 years ago

Hi! I'm using this plugin with the exportAMD option. It worked great for a single template, but when compiling multiple templates into a single file, I ran into issues. The compiled file was wrapping each template in it's own require statement. So it looked something like:

define(['handlebars'], function (Handlebars) {
    templates['templateOne'] = template(function (Handlebars,depth0,helpers,partials,data) {
        ...
    }
}
define(['handlebars'], function (Handlebars) {
    templates['templateTwo'] = template(function (Handlebars,depth0,helpers,partials,data) {
        ...
    }
}

I believe the issue is caused by the modules not having names but being in the same file, which doesn't play nicely in require. Since all the templates just need to require Handlebars, I rearranged the code a bit so that it generates something more like:

define(['handlebars'], function (Handlebars) {
    templates['templateOne'] = template(function (Handlebars,depth0,helpers,partials,data) {
        ...
    }
    templates['templateTwo'] = template(function (Handlebars,depth0,helpers,partials,data) {
        ...
    }
}

This works perfectly in my app, and I've updated the AMD tests to use multiple files. I would love to hear your thoughts, though!

mattacular commented 10 years ago

Thanks for the PR. At a glance, it looks good. I'll review more carefully and it will probably make the next release.

mattacular commented 10 years ago

I had to make some changes so I made a new PR on my end -

https://github.com/mattacular/grunt-handlebars-compiler/pull/5

Thanks!

tfarnsworth commented 10 years ago

Awesome, looks great. Thanks!