Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@salomvary/handlebars-to-jsx
Advanced tools
Converts Handlebars template to JSX-component. Uses Glimmer VM to parse Handlebars code to AST and Babel to create JSX AST and generate code.
# via NPM
npm install handlebars-to-jsx
# or Yarn
yarn add handlebars-to-jsx
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
options
OptionalisComponent
Optionaltrue
. Should return JSX code wrapped as a function component.isModule
Optionalfalse
. Should return generated code exported as default.includeImport
Optionalfalse
. 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>
This package comes with a command line utility:
$ handlebars-to-jsx --help
Usage: handlebars-to-jsx [path] [options]
Path must be a Handlebars file otherwise it will be read from stdin.
Options:
-c --component Should return JSX code wrapped as a function component.
-m --module Should return generated code exported as default.
-i --include-import Should return generated code with React import at the top.
-h --help Print this help text and exit.
Example usage:
$ npx handlebars-to-jsx test.hbs -c
props => <React.Fragment>{props.qux}
</React.Fragment>;
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>;
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);
MIT licensed
FAQs
Converts Handlebars template to React component
We found that @salomvary/handlebars-to-jsx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.