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

estree-util-build-jsx

v3.0.1

Published

Transform JSX in estrees to function calls (for react, preact, and most hyperscript interfaces)

Downloads

5,602,782

Readme

estree-util-build-jsx

Build Coverage Downloads Size Sponsors Backers Chat

estree utility to turn JSX into function calls: <x /> -> h('x')!

Contents

What is this?

This package is a utility that takes an estree (JavaScript) syntax tree as input that contains embedded JSX nodes (elements, fragments) and turns them into function calls.

When should I use this?

If you already have a tree and only need to compile JSX away, use this. If you have code, use something like SWC or esbuild instead.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install estree-util-build-jsx

In Deno with esm.sh:

import {buildJsx} from 'https://esm.sh/estree-util-build-jsx@3'

In browsers with esm.sh:

<script type="module">
  import {buildJsx} from 'https://esm.sh/estree-util-build-jsx@3?bundle'
</script>

Use

Say we have the following example.jsx:

import x from 'xastscript'

console.log(
  <album id={123}>
    <name>Born in the U.S.A.</name>
    <artist>Bruce Springsteen</artist>
    <releasedate date="1984-04-06">April 6, 1984</releasedate>
  </album>
)

console.log(
  <>
    {1 + 1}
    <self-closing />
    <x name key="value" key={expression} {...spread} />
  </>
)

…and next to it a module example.js:

import fs from 'node:fs/promises'
import jsx from 'acorn-jsx'
import {fromJs} from 'esast-util-from-js'
import {buildJsx} from 'estree-util-build-jsx'
import {toJs} from 'estree-util-to-js'

const doc = String(await fs.readFile('example.jsx'))

const tree = fromJs(doc, {module: true, plugins: [jsx()]})

buildJsx(tree, {pragma: 'x', pragmaFrag: 'null'})

console.log(toJs(tree).value)

…now running node example.js yields:

import x from "xastscript";
console.log(x("album", {
  id: 123
}, x("name", null, "Born in the U.S.A."), x("artist", null, "Bruce Springsteen"), x("releasedate", {
  date: "1984-04-06"
}, "April 6, 1984")));
console.log(x(null, null, 1 + 1, x("self-closing"), x("x", Object.assign({
  name: true,
  key: "value",
  key: expression
}, spread))));

API

This package exports the identifier buildJsx. There is no default export.

buildJsx(tree[, options])

Turn JSX in tree into function calls: <x /> -> h('x')!

Algorithm

In almost all cases, this utility is the same as the Babel plugin, except that they work on slightly different syntax trees.

Some differences:

  • no pure annotations things
  • this is not a component: <this> -> h('this'), not h(this)
  • namespaces are supported: <a:b c:d> -> h('a:b', {'c:d': true}), which throws by default in Babel or can be turned on with throwIfNamespace
  • no useSpread, useBuiltIns, or filter options
Parameters
  • tree (Node) — tree to transform (typically Program)
  • options (Options, optional) — configuration
Returns

Nothing (undefined).

Options

Configuration (TypeScript type).

👉 Note: you can also configure runtime, importSource, pragma, and pragmaFrag from within files through comments.

Fields
runtime

Choose the runtime (Runtime, default: 'classic').

Comment form: @jsxRuntime theRuntime.

importSource

Place to import jsx, jsxs, jsxDEV, and Fragment from, when the effective runtime is automatic (string, default: 'react').

Comment form: @jsxImportSource theSource.

👉 Note: /jsx-runtime or /jsx-dev-runtime is appended to this provided source. In CJS, that can resolve to a file (as in theSource/jsx-runtime.js), but for ESM an export map needs to be set up to point to files:

// …
"exports": {
  // …
  "./jsx-runtime": "./path/to/jsx-runtime.js",
  "./jsx-dev-runtime": "./path/to/jsx-runtime.js"
  // …
pragma

Identifier or member expression to call when the effective runtime is classic (string, default: 'React.createElement').

Comment form: @jsx identifier.

pragmaFrag

Identifier or member expression to use as a symbol for fragments when the effective runtime is classic (string, default: 'React.Fragment').

Comment form: @jsxFrag identifier.

development

When in the automatic runtime, whether to import theSource/jsx-dev-runtime.js, use jsxDEV, and pass location info when available (boolean, default: false).

This helps debugging but adds a lot of code that you don’t want in production.

filePath

File path to the original source file (string, example: 'path/to/file.js'). Passed in location info to jsxDEV when using the automatic runtime with development: true.

Runtime

How to transform JSX (TypeScript type).

Type
type Runtime = 'automatic' | 'classic'

Examples

Example: use with Acorn

To support configuration from comments in Acorn, those comments have to be in the program. This is done by espree but not automatically by acorn:

import {Parser} from 'acorn'
import jsx from 'acorn-jsx'

const doc = '' // To do: get `doc` somehow.

const comments = []
const tree = Parser.extend(jsx()).parse(doc, {onComment: comments})
tree.comments = comments

Types

This package is fully typed with TypeScript. It exports the additional type Options and Runtime.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, estree-util-build-jsx@^3, compatible with Node.js 166.

Related

Security

This package is safe.

Contribute

See contributing.md in syntax-tree/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer