davis / plugin-hbs

Handlebars template loader plugin for SystemJS
MIT License
21 stars 9 forks source link

Added automatic discovery mechanism for partials #10

Open BennettJames opened 8 years ago

BennettJames commented 8 years ago

Alright, managed to piece together something that is pretty tidy compared to what I originally outlined in #9 and seems to work fine in browser & server-side.

Basic explanation of what goes on here: before translating a template, it parses the template into an AST. This syntax tree is then recursively searched for all partials, which are then added as a dependency to the currently-parsed template. Each dependency is required and registered as a partial, and the precompiled template is exported (as before). The exact same process is applied to each partial.


A few other notes:

davis commented 8 years ago

Hey Bennett, sorry I just saw this. Did you manage to get any progress on the way it travels the AST? Thanks!

BennettJames commented 7 years ago

Been a while since I thought about this, but I didn't look too far into better ways to traverse the AST. It's pretty similar to what hbsfy, the browserify plugin for handlebars, was doing at the time. I think that means it's probably fairly safe, though that code has since been updated to include some other special cases - I might want to downport those to this patch.

I don't know if there are any better options. There's a grunt plugin that also has to do partial-trawling, but at a glance it just relies on the abilities to traverse a directory from the server to get this done (we don't have the same luxury when operating in the browser). I'd certainly understand if you didn't want something this loose getting into the codebase, but I don't know where else to look to figure out how to integrate this in a cleaner fashion. Could always see if the handlebars project maintainers have any advice, I suppose.

P.S. I updated this to deal with module types, but it's completely untested. If there's still any interest in getting this merged I can try to verify that it works.

davis commented 7 years ago

Hey Bennett! It's been a while since I've written an app in Handlebars, I've added you as a collaborator. Feel free to merge if the code is sane!

BennettJames commented 7 years ago

Cool, sounds good. I'll try to figure out a decent way to test these changes and hopefully get it merged sometime this weekend.