{"id":13717037,"url":"https://github.com/syntax-tree/estree-util-build-jsx","last_synced_at":"2025-04-19T11:52:03.265Z","repository":{"id":40249110,"uuid":"323859609","full_name":"syntax-tree/estree-util-build-jsx","owner":"syntax-tree","description":"Transform JSX in estrees to function calls (for react, preact, and most hyperscript interfaces)","archived":false,"fork":false,"pushed_at":"2024-08-23T11:45:10.000Z","size":134,"stargazers_count":22,"open_issues_count":0,"forks_count":3,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-13T03:53:45.673Z","etag":null,"topics":["ecmascript","estree","hyperscript","javascript","jsx","preact","react"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/syntax-tree.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"unifiedjs","open_collective":"unified"}},"created_at":"2020-12-23T09:30:43.000Z","updated_at":"2024-08-29T14:53:34.000Z","dependencies_parsed_at":"2024-01-14T22:13:43.401Z","dependency_job_id":null,"html_url":"https://github.com/syntax-tree/estree-util-build-jsx","commit_stats":{"total_commits":50,"total_committers":4,"mean_commits":12.5,"dds":0.06000000000000005,"last_synced_commit":"fade9da3545c0c398b1593e2f66fa51f1ce6ba33"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-build-jsx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-build-jsx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-build-jsx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/syntax-tree%2Festree-util-build-jsx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/syntax-tree","download_url":"https://codeload.github.com/syntax-tree/estree-util-build-jsx/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249688029,"owners_count":21311156,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ecmascript","estree","hyperscript","javascript","jsx","preact","react"],"created_at":"2024-08-03T00:01:17.180Z","updated_at":"2025-04-19T11:52:03.242Z","avatar_url":"https://github.com/syntax-tree.png","language":"JavaScript","readme":"# estree-util-build-jsx\n\n[![Build][build-badge]][build]\n[![Coverage][coverage-badge]][coverage]\n[![Downloads][downloads-badge]][downloads]\n[![Size][size-badge]][size]\n[![Sponsors][sponsors-badge]][collective]\n[![Backers][backers-badge]][collective]\n[![Chat][chat-badge]][chat]\n\n[estree][] utility to turn JSX into function calls: `\u003cx /\u003e` -\u003e `h('x')`!\n\n## Contents\n\n* [What is this?](#what-is-this)\n* [When should I use this?](#when-should-i-use-this)\n* [Install](#install)\n* [Use](#use)\n* [API](#api)\n  * [`buildJsx(tree[, options])`](#buildjsxtree-options)\n  * [`Options`](#options)\n  * [`Runtime`](#runtime-1)\n* [Examples](#examples)\n  * [Example: use with Acorn](#example-use-with-acorn)\n* [Types](#types)\n* [Compatibility](#compatibility)\n* [Related](#related)\n* [Security](#security)\n* [Contribute](#contribute)\n* [License](#license)\n\n## What is this?\n\nThis package is a utility that takes an [estree][] (JavaScript) syntax tree as\ninput that contains embedded JSX nodes (elements, fragments) and turns them into\nfunction calls.\n\n## When should I use this?\n\nIf you already have a tree and only need to compile JSX away, use this.\nIf you have code, use something like [SWC][] or [esbuild][] instead.\n\n## Install\n\nThis package is [ESM only][esm].\nIn Node.js (version 16+), install with [npm][]:\n\n```sh\nnpm install estree-util-build-jsx\n```\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```js\nimport {buildJsx} from 'https://esm.sh/estree-util-build-jsx@3'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import {buildJsx} from 'https://esm.sh/estree-util-build-jsx@3?bundle'\n\u003c/script\u003e\n```\n\n## Use\n\nSay we have the following `example.jsx`:\n\n```js\nimport x from 'xastscript'\n\nconsole.log(\n  \u003calbum id={123}\u003e\n    \u003cname\u003eBorn in the U.S.A.\u003c/name\u003e\n    \u003cartist\u003eBruce Springsteen\u003c/artist\u003e\n    \u003creleasedate date=\"1984-04-06\"\u003eApril 6, 1984\u003c/releasedate\u003e\n  \u003c/album\u003e\n)\n\nconsole.log(\n  \u003c\u003e\n    {1 + 1}\n    \u003cself-closing /\u003e\n    \u003cx name key=\"value\" key={expression} {...spread} /\u003e\n  \u003c/\u003e\n)\n```\n\n…and next to it a module `example.js`:\n\n```js\nimport fs from 'node:fs/promises'\nimport jsx from 'acorn-jsx'\nimport {fromJs} from 'esast-util-from-js'\nimport {buildJsx} from 'estree-util-build-jsx'\nimport {toJs} from 'estree-util-to-js'\n\nconst doc = String(await fs.readFile('example.jsx'))\n\nconst tree = fromJs(doc, {module: true, plugins: [jsx()]})\n\nbuildJsx(tree, {pragma: 'x', pragmaFrag: 'null'})\n\nconsole.log(toJs(tree).value)\n```\n\n…now running `node example.js` yields:\n\n```js\nimport x from \"xastscript\";\nconsole.log(x(\"album\", {\n  id: 123\n}, x(\"name\", null, \"Born in the U.S.A.\"), x(\"artist\", null, \"Bruce Springsteen\"), x(\"releasedate\", {\n  date: \"1984-04-06\"\n}, \"April 6, 1984\")));\nconsole.log(x(null, null, 1 + 1, x(\"self-closing\"), x(\"x\", Object.assign({\n  name: true,\n  key: \"value\",\n  key: expression\n}, spread))));\n```\n\n## API\n\nThis package exports the identifier [`buildJsx`][api-build-jsx].\nThere is no default export.\n\n### `buildJsx(tree[, options])`\n\nTurn JSX in `tree` into function calls: `\u003cx /\u003e` -\u003e `h('x')`!\n\n###### Algorithm\n\nIn almost all cases, this utility is the same as the Babel plugin, except that\nthey work on slightly different syntax trees.\n\nSome differences:\n\n* no pure annotations things\n* `this` is not a component: `\u003cthis\u003e` -\u003e `h('this')`, not `h(this)`\n* namespaces are supported: `\u003ca:b c:d\u003e` -\u003e `h('a:b', {'c:d': true})`,\n  which throws by default in Babel or can be turned on with `throwIfNamespace`\n* no `useSpread`, `useBuiltIns`, or `filter` options\n\n###### Parameters\n\n* `tree` ([`Node`][node])\n  — tree to transform (typically [`Program`][program])\n* `options` ([`Options`][api-options], optional)\n  — configuration\n\n###### Returns\n\nNothing (`undefined`).\n\n### `Options`\n\nConfiguration (TypeScript type).\n\n\u003e 👉 **Note**: you can also configure `runtime`, `importSource`, `pragma`, and\n\u003e `pragmaFrag` from within files through comments.\n\n##### Fields\n\n###### `runtime`\n\nChoose the [runtime][jsx-runtime] ([`Runtime`][api-runtime], default: `'classic'`).\n\nComment form: `@jsxRuntime theRuntime`.\n\n###### `importSource`\n\nPlace to import `jsx`, `jsxs`, `jsxDEV`, and `Fragment` from, when the\neffective runtime is automatic (`string`, default: `'react'`).\n\nComment form: `@jsxImportSource theSource`.\n\n\u003e 👉 **Note**: `/jsx-runtime` or `/jsx-dev-runtime` is appended to this provided\n\u003e source.\n\u003e In CJS, that can resolve to a file (as in `theSource/jsx-runtime.js`), but for\n\u003e ESM an export map needs to be set up to point to files:\n\u003e\n\u003e ```js\n\u003e // …\n\u003e \"exports\": {\n\u003e   // …\n\u003e   \"./jsx-runtime\": \"./path/to/jsx-runtime.js\",\n\u003e   \"./jsx-dev-runtime\": \"./path/to/jsx-runtime.js\"\n\u003e   // …\n\u003e ```\n\n###### `pragma`\n\nIdentifier or member expression to call when the effective runtime is classic\n(`string`, default: `'React.createElement'`).\n\nComment form: `@jsx identifier`.\n\n###### `pragmaFrag`\n\nIdentifier or member expression to use as a symbol for fragments when the\neffective runtime is classic (`string`, default: `'React.Fragment'`).\n\nComment form: `@jsxFrag identifier`.\n\n###### `development`\n\nWhen in the automatic runtime, whether to import `theSource/jsx-dev-runtime.js`,\nuse `jsxDEV`, and pass location info when available (`boolean`, default: `false`).\n\nThis helps debugging but adds a lot of code that you don’t want in production.\n\n###### `filePath`\n\nFile path to the original source file (`string`, example: `'path/to/file.js'`).\nPassed in location info to `jsxDEV` when using the automatic runtime with\n`development: true`.\n\n### `Runtime`\n\nHow to transform JSX (TypeScript type).\n\n###### Type\n\n```ts\ntype Runtime = 'automatic' | 'classic'\n```\n\n## Examples\n\n### Example: use with Acorn\n\nTo support configuration from comments in Acorn, those comments have to be in\nthe program.\nThis is done by [`espree`][espree] but not automatically by [`acorn`][acorn]:\n\n```js\nimport {Parser} from 'acorn'\nimport jsx from 'acorn-jsx'\n\nconst doc = '' // To do: get `doc` somehow.\n\nconst comments = []\nconst tree = Parser.extend(jsx()).parse(doc, {onComment: comments})\ntree.comments = comments\n```\n\n## Types\n\nThis package is fully typed with [TypeScript][].\nIt exports the additional type [`Options`][api-options] and\n[`Runtime`][api-runtime].\n\n## Compatibility\n\nProjects maintained by the unified collective are compatible with maintained\nversions of Node.js.\n\nWhen we cut a new major release, we drop support for unmaintained versions of\nNode.\nThis means we try to keep the current release line, `estree-util-build-jsx@^3`,\ncompatible with Node.js 16.\n\n## Related\n\n* [`syntax-tree/hast-util-to-estree`](https://github.com/syntax-tree/hast-util-to-estree)\n  — turn [hast](https://github.com/syntax-tree/hast) (HTML) to [estree][]\n  JSX\n* [`coderaiser/estree-to-babel`](https://github.com/coderaiser/estree-to-babel)\n  — turn [estree][] to Babel trees\n\n## Security\n\nThis package is safe.\n\n## Contribute\n\nSee [`contributing.md` in `syntax-tree/.github`][contributing] for ways to get\nstarted.\nSee [`support.md`][support] for ways to get help.\n\nThis project has a [code of conduct][coc].\nBy interacting with this repository, organization, or community you agree to\nabide by its terms.\n\n## License\n\n[MIT][license] © [Titus Wormer][author]\n\n\u003c!-- Definitions --\u003e\n\n[build-badge]: https://github.com/syntax-tree/estree-util-build-jsx/workflows/main/badge.svg\n\n[build]: https://github.com/syntax-tree/estree-util-build-jsx/actions\n\n[coverage-badge]: https://img.shields.io/codecov/c/github/syntax-tree/estree-util-build-jsx.svg\n\n[coverage]: https://codecov.io/github/syntax-tree/estree-util-build-jsx\n\n[downloads-badge]: https://img.shields.io/npm/dm/estree-util-build-jsx.svg\n\n[downloads]: https://www.npmjs.com/package/estree-util-build-jsx\n\n[size-badge]: https://img.shields.io/badge/dynamic/json?label=minzipped%20size\u0026query=$.size.compressedSize\u0026url=https://deno.bundlejs.com/?q=estree-util-build-jsx\n\n[size]: https://bundlejs.com/?q=estree-util-build-jsx\n\n[sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg\n\n[backers-badge]: https://opencollective.com/unified/backers/badge.svg\n\n[collective]: https://opencollective.com/unified\n\n[chat-badge]: https://img.shields.io/badge/chat-discussions-success.svg\n\n[chat]: https://github.com/syntax-tree/unist/discussions\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[npm]: https://docs.npmjs.com/cli/install\n\n[esmsh]: https://esm.sh\n\n[license]: license\n\n[author]: https://wooorm.com\n\n[typescript]: https://www.typescriptlang.org\n\n[contributing]: https://github.com/syntax-tree/.github/blob/main/contributing.md\n\n[support]: https://github.com/syntax-tree/.github/blob/main/support.md\n\n[coc]: https://github.com/syntax-tree/.github/blob/main/code-of-conduct.md\n\n[acorn]: https://github.com/acornjs/acorn\n\n[estree]: https://github.com/estree/estree\n\n[espree]: https://github.com/eslint/espree\n\n[node]: https://github.com/estree/estree/blob/master/es5.md#node-objects\n\n[program]: https://github.com/estree/estree/blob/master/es5.md#programs\n\n[jsx-runtime]: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html\n\n[swc]: https://swc.rs\n\n[esbuild]: https://esbuild.github.io\n\n[api-build-jsx]: #buildjsxtree-options\n\n[api-options]: #options\n\n[api-runtime]: #runtime-1\n","funding_links":["https://github.com/sponsors/unifiedjs","https://opencollective.com/unified"],"categories":["esast utilities"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Festree-util-build-jsx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsyntax-tree%2Festree-util-build-jsx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsyntax-tree%2Festree-util-build-jsx/lists"}