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

jsx-to-hyperscript

v2.2.1

Published

A CLI command for converting JSX files into standard JS.

Downloads

14

Readme

jsx-to-hyperscript

A CLI command for converting JSX files into standard JS.

Usage

CLI

Usage:
    jsx-to-hyperscript <glob> [--write] [--factory='h'] [--flow]
    
Options:
    --write       When set, causes the file to be overwritten in-place.
    --leafsFirst  As a non-standard improvement, `h` calls are nested with arrow functions so that execution order is contextual. If you would like the original behavior, use this leafsFirst flag. See docs. Optional.
    --factory     By default 'h'. The factory function. Optional.
    --flow        If you use flowtype, removes type annotations.
// package.json
{
  "scripts": {
    "build": "jsx-to-hyperscript 'build/**/*.{js,mjs}' --write"
  }
}

Import

import convertJsx from 'jsx-to-hyperscript'

type options = {
  factory?: 'h',
  reverse?: false
}

convertJsx(fileContents[, options])

Examples

Input:

// Instances of React are transformed as well
import {h as React} from "grainbox"

const signs = [{ value: "+" }, { value: "-" }]

const renderSign = (props) => {
  return (
    <span {...props}>{props.value}</span>
  )
}

const Component = () => (<div></div>)
const test = (
  <div style={{color: 'orange'}}>
    <Component />
    <button
      onClick={() => {
        const el = <div/>
        console?.log(el)
      }}
      {...passProps}
    />
    {signs.map(renderSign)}
  </div>
)

Output:

// Instances of h are transformed as well
import {h as h} from "grainbox"

const signs = [{ value: "+" }, { value: "-" }]

const renderSign = (props) => {
  return h("span", props, [() => (props.value)])
}

const Component = () => h("div", null, [])
const test = h("div", { style: { color: "orange" } }, [
  () => (Component({})),
  () => h(
    "button",
    {
      onClick: () => {
        const el = h("div", null, [])
        console?.log(el)
      },
      ...passProps,
    },
    []
  ),
  () => (signs.map(renderSign)),
])

Fragments are supported, too:

// input
function renderRow() {
  return (
    <>
      <span/>
      <span/>
    </>
  )
}
// output
function renderRow() {
  return (
    [
      () => h('span', null, []),
      () => h('span', null, [])
    ]
  )
}

--leafsFirst

hyperscript is usually used like this:

h('div', {className: 'col'}, [
  h('div', null, [
    h('span')
  ])
])

However, this would cause the leafs to compute before the root. Instead, by using arrow functions, the call order can be flipped around so that elements are built from root to leaf:

h('div', {className: () => ('col')}, [
  () => h('div', null, [
    () => h('span')
  ])
])

This allows grainbox, along with some slight modifications to hyperscript, to build html with context. For example, when the span above is being created, it is possible to obtain a reference to the parent div. This is useful for setting up fine-grained updates.

Because context is gained in the latter, it is the default. If you need the former, you can set the --leafsFirst flag.