Generating Handlebars from a React Abstract Syntax

As a proof of concept, I'm trying to generate Handlebars code from a simple React component. For example, I'd like to transform something like this.

import React from 'react';

const HelloWorld = ({ name, items }) => {
  return (
     Hello {name}!
       {, i) => (
         <li key={i}> {item} </li>

export default HelloWorld;

To something like this.

 Hello {{ name }}!
  {{#each items}}
    <li key="{{@index}}">{{ this }}</li>

My original idea was to use babel, typescript or another AST generator to generate an AST and then traverse it building out the handlebars output. Starting with the default export, traversing the nodes and outputting the return JSX Statements into a resultant string. Has anybody seen something similar where a handlebars template is generated programmatically?

Read more here:

Content Attribution

This content was originally published by George Kemp at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: