plugin-hbs icon indicating copy to clipboard operation
plugin-hbs copied to clipboard

Added automatic discovery mechanism for partials

Open BennettJames opened this issue 8 years ago • 4 comments

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:

  • The register-partials-as-dependencies system this uses can result in a partial being registered multiple times. I don't think this will ever be an issue, as re-registering the same template twice doesn't look like has any effect on Handlebars.
  • The way it travels the AST is really rough. It looks at every single value in every object in the returned AST object. I couldn't find much by ways of official documentation on how to deal with partial discovery, and this seems to work "well enough".
  • if you think this looks good, I should probably add some documentation to the readme to clarify how partials are handled.

BennettJames avatar Feb 08 '16 06:02 BennettJames

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

davis avatar Sep 06 '16 18:09 davis

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.

BennettJames avatar Jan 03 '17 03:01 BennettJames

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!

davis avatar Jan 19 '17 18:01 davis

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

BennettJames avatar Jan 19 '17 22:01 BennettJames