Freak613 / 1more

MIT License
49 stars 1 forks source link

Templates without parsing. #1

Closed mrjjwright closed 3 years ago

mrjjwright commented 3 years ago

Strange question: is it possible to create templates without the parsing of template literal string, just in plain JS? Love your stuff, excited to try it out.

Freak613 commented 3 years ago

Thanks! As of now it's not possible. Parsed template is runtime entity, it has hot callbacks for reference discovery and updates. Theoretically it could be possible to return:

const templateNode = [...values];
templateNode.p = runtimeTemplateInstance;
return templateNode;

But it will slip through type checks as plain array.

It is possible to add such option if there is valid usecase. Compiler overhead is around 1kB to download, compilation itself should have low runtime pressure, so it should sustain workload to some degree.

Freak613 commented 3 years ago

However I think opt-out of compilation will have more consequences. Template cache implementation is similar to lit-html / hyperhtml, it relies on template string array equality and there will be no option to integrate both static and dynamic parsing inside one runtime.

Freak613 commented 3 years ago

But it could be added as separate type, like component type. But I'm currently hesitant on adding more types to not complicate type checking and fine with restrictions the system currently has.

mrjjwright commented 3 years ago

It’s no big deal. I am building a reactive low code editor and I use Sinuous currently. It has a template function that also parses template literals. The problem is that I am building these programmatically and I was looking for a way to generate quick templates without having to generate html template literals but the more I think about it it’s not that hard from AST. Thanks for the quick response!

Freak613 commented 3 years ago

This library implementation has pretty low overhead of generating html objects, it just returns arguments back. And from testing I've done it's pretty equal to returning pure array or object of values themselves and, to some limits, it should not be a problem.