npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

handlebars-to-jsx

v0.1.5

Published

Converts Handlebars template to React component

Downloads

1,262

Readme

Handlebars to JSX NPM Build Status

Converts Handlebars template to JSX-component. Uses Glimmer VM to parse Handlebars code to AST and Babel to create JSX AST and generate code.

Installation

# via NPM
npm install handlebars-to-jsx

# or Yarn
yarn add handlebars-to-jsx

Usage

The package only has one method compile. You can import it the following way:

import { compile } from 'handlebars-to-jsx'

The method has the following syntax:

compile(input[, options])
  • input
    The Handlebars template which should be converted to JSX code.
  • options Optional
    Options is optional and can have the following properties:
    • isComponent Optional
      The default is true. Should return JSX code wrapped as a function component.
    • isModule Optional
      The default is false. Should return generated code exported as default.
    • includeImport Optional
      The default is false. Should return generated code with React import at the top. Requires isModule to be true.

Use it for simply converting Handlebars template to JSX code:

compile('<div>{{variable}}</div>')

// Result code
// props => <div>{props.variable}</div>

By default the compile function returns a function component. You can convert Handlebars templates to JSX without wrapping them as arrow functions. In this variant, props is not added to the path of variables.

compile('<div>{{variable}}</div>', { isComponent: false })

// Result
// <div>{variable}</div>

Also, you can have the component exported as default:

compile('<div>{{variable}}</div>', { isModule: true })

// Result
// export default props => <div>{props.variable}</div>

Also, react can be imported:

compile('<div>{{variable}}</div>', { includeImport: true, isModule: true })

// Result
// import React from "react";
// export default props => <div>{props.variable}</div>

Code formatting

The output code is created from an AST tree, so it's unformatted by default. You can use tools like Prettier to format the code:

import { compile } from 'handlebars-to-jsx'
import prettier from 'prettier'

// The Handlebars input
const hbsCode = '<div>{{#each list}}<span>{{item}}</span>{{/each}}</div>'

const jsxCode = compile(hbsCode, { isComponent: false })
// <div>{list.map((item, i) => <span key={i}>{item.item}</span>)}</div>;

prettier.format(jsxCode, { parser: 'babylon' })
// <div>
//   {list.map((item, i) => (
//     <span key={i}>{item.item}</span>
//   ))}
// </div>;

Transpilation

If you want to have code lower than ES6, or you want to have the React source JS code without JSX, you can use babel:

import { compile } from 'handlebars-to-jsx'
import babel from '@babel/core'
import pluginTransformReactJSX from '@babel/plugin-transform-react-jsx'

// The Handlebars input
const hbsCode = '<div>{{variable}}</div>'

const jsxCode = compile(hbsCode, { isComponent: false })
// <div>{variable}</div>;

const { code } = babel.transform(jsxCode, {
  plugins: [pluginTransformReactJSX]
})
// React.createElement("div", null, variable);

License

MIT licensed