Gathering detailed insights and metrics for estree-util-build-jsx
Gathering detailed insights and metrics for estree-util-build-jsx
Gathering detailed insights and metrics for estree-util-build-jsx
Gathering detailed insights and metrics for estree-util-build-jsx
estree-util-to-js
estree (and esast) utility to serialize to JavaScript
hast-util-to-estree
hast utility to transform to estree (JavaScript AST) JSX
estree-util-is-identifier-name
Check if something can be an ecmascript (javascript) identifier name
unist-util-position-from-estree
unist utility to get a position from an estree node
Transform JSX in estrees to function calls (for react, preact, and most hyperscript interfaces)
npm install estree-util-build-jsx
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
22 Stars
73 Commits
3 Forks
9 Watching
1 Branches
11 Contributors
Updated on 29 Aug 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
1.3%
270,560
Compared to previous day
Last week
5.3%
1,447,054
Compared to previous week
Last month
13.8%
6,043,333
Compared to previous month
Last year
89%
54,701,304
Compared to previous year
estree utility to turn JSX into function calls: <x />
-> h('x')
!
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.
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.
This package is ESM only. In Node.js (version 16+), install with npm:
1npm install estree-util-build-jsx
In Deno with esm.sh
:
1import {buildJsx} from 'https://esm.sh/estree-util-build-jsx@3'
In browsers with esm.sh
:
1<script type="module"> 2 import {buildJsx} from 'https://esm.sh/estree-util-build-jsx@3?bundle' 3</script>
Say we have the following example.jsx
:
1import x from 'xastscript' 2 3console.log( 4 <album id={123}> 5 <name>Born in the U.S.A.</name> 6 <artist>Bruce Springsteen</artist> 7 <releasedate date="1984-04-06">April 6, 1984</releasedate> 8 </album> 9) 10 11console.log( 12 <> 13 {1 + 1} 14 <self-closing /> 15 <x name key="value" key={expression} {...spread} /> 16 </> 17)
…and next to it a module example.js
:
1import fs from 'node:fs/promises' 2import jsx from 'acorn-jsx' 3import {fromJs} from 'esast-util-from-js' 4import {buildJsx} from 'estree-util-build-jsx' 5import {toJs} from 'estree-util-to-js' 6 7const doc = String(await fs.readFile('example.jsx')) 8 9const tree = fromJs(doc, {module: true, plugins: [jsx()]}) 10 11buildJsx(tree, {pragma: 'x', pragmaFrag: 'null'}) 12 13console.log(toJs(tree).value)
…now running node example.js
yields:
1import x from "xastscript"; 2console.log(x("album", { 3 id: 123 4}, x("name", null, "Born in the U.S.A."), x("artist", null, "Bruce Springsteen"), x("releasedate", { 5 date: "1984-04-06" 6}, "April 6, 1984"))); 7console.log(x(null, null, 1 + 1, x("self-closing"), x("x", Object.assign({ 8 name: true, 9 key: "value", 10 key: expression 11}, spread))));
This package exports the identifier buildJsx
.
There is no default export.
buildJsx(tree[, options])
Turn JSX in tree
into function calls: <x />
-> h('x')
!
In almost all cases, this utility is the same as the Babel plugin, except that they work on slightly different syntax trees.
Some differences:
this
is not a component: <this>
-> h('this')
, not h(this)
<a:b c:d>
-> h('a:b', {'c:d': true})
,
which throws by default in Babel or can be turned on with throwIfNamespace
useSpread
, useBuiltIns
, or filter
optionsNothing (undefined
).
Options
Configuration (TypeScript type).
👉 Note: you can also configure
runtime
,importSource
,pragma
, andpragmaFrag
from within files through comments.
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 intheSource/jsx-runtime.js
), but for ESM an export map needs to be set up to point to files:1// … 2"exports": { 3 // … 4 "./jsx-runtime": "./path/to/jsx-runtime.js", 5 "./jsx-dev-runtime": "./path/to/jsx-runtime.js" 6 // …
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).
1type Runtime = 'automatic' | 'classic'
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
:
1import {Parser} from 'acorn' 2import jsx from 'acorn-jsx' 3 4const doc = '' // To do: get `doc` somehow. 5 6const comments = [] 7const tree = Parser.extend(jsx()).parse(doc, {onComment: comments}) 8tree.comments = comments
This package is fully typed with TypeScript.
It exports the additional type Options
and
Runtime
.
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 16.
syntax-tree/hast-util-to-estree
— turn hast (HTML) to estree
JSXcoderaiser/estree-to-babel
— turn estree to Babel treesThis package is safe.
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.
No vulnerabilities found.
No security vulnerabilities found.