18
18
* [ Use] ( #use )
19
19
* [ API] ( #api )
20
20
* [ ` buildJsx(tree, options?) ` ] ( #buildjsxtree-options )
21
+ * [ ` Options ` ] ( #options )
22
+ * [ ` Runtime ` ] ( #runtime )
21
23
* [ Examples] ( #examples )
22
24
* [ Example: use with Acorn] ( #example-use-with-acorn )
23
- * [ Algorithm] ( #algorithm )
24
25
* [ Types] ( #types )
25
26
* [ Compatibility] ( #compatibility )
26
27
* [ Related] ( #related )
@@ -37,12 +38,12 @@ function calls.
37
38
## When should I use this?
38
39
39
40
If you already have a tree and only need to compile JSX away, use this.
40
- If you have code, using something like [ SWC] [ ] or [ esbuild] [ ] instead.
41
+ If you have code, use something like [ SWC] [ ] or [ esbuild] [ ] instead.
41
42
42
43
## Install
43
44
44
45
This package is [ ESM only] [ esm ] .
45
- In Node.js (version 14.14+, 16.0+), install with [ npm] [ ] :
46
+ In Node.js (version 14.14+ and 16.0+), install with [ npm] [ ] :
46
47
47
48
``` sh
48
49
npm install estree-util-build-jsx
@@ -90,27 +91,24 @@ console.log(
90
91
91
92
``` js
92
93
import fs from ' node:fs/promises'
93
- import {Parser } from ' acorn'
94
- import jsx from ' acorn-jsx'
95
- import {generate } from ' astring'
94
+ import {fromJs } from ' esast-util-from-js'
96
95
import {buildJsx } from ' estree-util-build-jsx'
96
+ import {toJs } from ' estree-util-to-js'
97
+ import jsx from ' acorn-jsx'
97
98
98
99
const doc = String (await fs .readFile (' example.jsx' ))
99
100
100
- const tree = Parser .extend (jsx ()).parse (doc, {
101
- sourceType: ' module' ,
102
- ecmaVersion: 2022
103
- })
101
+ const tree = fromJs (doc, {module: true , plugins: [jsx ()]})
104
102
105
103
buildJsx (tree, {pragma: ' x' , pragmaFrag: ' null' })
106
104
107
- console .log (generate (tree))
105
+ console .log (toJs (tree). value )
108
106
```
109
107
110
108
…now running ` node example.js ` yields:
111
109
112
110
``` js
113
- import x from ' xastscript' ;
111
+ import x from " xastscript" ;
114
112
console .log (x (" album" , {
115
113
id: 123
116
114
}, x (" name" , null , " Born in the U.S.A." ), x (" artist" , null , " Bruce Springsteen" ), x (" releasedate" , {
@@ -125,31 +123,53 @@ console.log(x(null, null, 1 + 1, x("self-closing"), x("x", Object.assign({
125
123
126
124
## API
127
125
128
- This package exports the identifier ` buildJsx ` .
126
+ This package exports the identifier [ ` buildJsx ` ] [ build-jsx ] .
129
127
There is no default export.
130
128
131
129
### ` buildJsx(tree, options?) `
132
130
133
- Turn JSX in ` tree ` ([ ` Program ` ] [ program ] ) into function calls:
134
- ` <x /> ` -> ` h('x') ` !
131
+ Turn JSX in ` tree ` into function calls: ` <x /> ` -> ` h('x') ` !
132
+
133
+ ###### Algorithm
134
+
135
+ In almost all cases, this utility is the same as the Babel plugin, except that
136
+ they work on slightly different syntax trees.
137
+
138
+ Some differences:
139
+
140
+ * no pure annotations things
141
+ * ` this ` is not a component: ` <this> ` -> ` h('this') ` , not ` h(this) `
142
+ * namespaces are supported: ` <a:b c:d> ` -> ` h('a:b', {'c:d': true}) ` ,
143
+ which throws by default in Babel or can be turned on with ` throwIfNamespace `
144
+ * no ` useSpread ` , ` useBuiltIns ` , or ` filter ` options
135
145
136
- ##### ` options `
146
+ ###### Parameters
137
147
138
- Configuration (optional).
148
+ * ` tree ` ([ ` Node ` ] [ node ] )
149
+ — tree to transform (typically [ ` Program ` ] [ program ] )
150
+ * ` options ` ([ ` Options ` ] [ options ] , optional)
151
+ — configuration
152
+
153
+ ###### Returns
154
+
155
+ Given, modified, ` tree ` ([ ` Node ` ] [ node ] ).
156
+
157
+ ### ` Options `
158
+
159
+ Configuration (TypeScript type).
139
160
140
161
> 👉 ** Note** : you can also configure ` runtime ` , ` importSource ` , ` pragma ` , and
141
162
> ` pragmaFrag ` from within files through comments.
142
163
143
164
###### ` options.runtime `
144
165
145
- Choose the [ runtime] [ ]
146
- (` string ` , ` 'automatic' ` or ` 'classic' ` , default: ` 'classic' ` ).
166
+ Choose the [ runtime] [ jsx-runtime ] ([ ` Runtime ` ] [ runtime ] , default: ` 'classic' ` ).
147
167
148
168
Comment form: ` @jsxRuntime theRuntime ` .
149
169
150
170
###### ` options.importSource `
151
171
152
- Place to import ` jsx ` , ` jsxs ` , ` jsxDEV ` , and/or ` Fragment ` from, when the
172
+ Place to import ` jsx ` , ` jsxs ` , ` jsxDEV ` , and ` Fragment ` from, when the
153
173
effective runtime is automatic (` string ` , default: ` 'react' ` ).
154
174
155
175
Comment form: ` @jsxImportSource theSource ` .
@@ -184,20 +204,26 @@ Comment form: `@jsxFrag identifier`.
184
204
185
205
###### ` options .development `
186
206
187
- Import ` jsxDEV` from ` theSource/ jsx- dev- runtime .js ` and add location info on
188
- where a component originated from (` boolean` , default: ` false ` ).
207
+ When in the automatic runtime, whether to import ` theSource/ jsx- dev- runtime .js ` ,
208
+ use ` jsxDEV` , and pass location info when available (` boolean` , default: ` false ` ).
209
+
189
210
This helps debugging but adds a lot of code that you don’t want in production.
190
- Only used in the automatic runtime.
191
211
192
212
###### ` options .filePath `
193
213
194
214
File path to the original source file (` string` , example: ` ' path/to/file.js' ` ).
195
- Used in the location info when using the automatic runtime with
215
+ Passed in location info to ` jsxDEV ` when using the automatic runtime with
196
216
` development: true ` .
197
217
198
- ##### Returns
218
+ ### ` Runtime `
199
219
200
- The given ` tree` (` Node ` ).
220
+ How to transform JSX (TypeScript type).
221
+
222
+ ###### Type
223
+
224
+ ` ` ` ts
225
+ type Runtime = ' automatic' | ' classic'
226
+ ` ` `
201
227
202
228
## Examples
203
229
@@ -218,19 +244,6 @@ const tree = Parser.extend(jsx()).parse(doc, {onComment: comments})
218
244
tree .comments = comments
219
245
` ` `
220
246
221
- ## Algorithm
222
-
223
- In almost all cases, this utility is the same as the Babel plugin, except that
224
- they work on slightly different syntax trees.
225
-
226
- Some differences:
227
-
228
- * no pure annotations things
229
- * ` this ` is not a component: ` < this > ` -> ` h (' this' )` , not ` h (this )`
230
- * namespaces are supported: ` < a: b c: d> ` -> ` h (' a:b' , {' c:d' : true })` ,
231
- which throws by default in Babel or can be turned on with ` throwIfNamespace`
232
- * no ` useSpread` , ` useBuiltIns` , or ` filter` options
233
-
234
247
## Types
235
248
236
249
This package is fully typed with [TypeScript][].
@@ -321,10 +334,18 @@ abide by its terms.
321
334
322
335
[espree]: https://github.com/eslint/espree
323
336
337
+ [node]: https://github.com/estree/estree/blob/master/es5.md#node-objects
338
+
324
339
[program]: https://github.com/estree/estree/blob/master/es5.md#programs
325
340
326
- [runtime]: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
341
+ [jsx- runtime]: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html
327
342
328
343
[swc]: https://swc.rs
329
344
330
345
[esbuild]: https://esbuild.github.io
346
+
347
+ [build-jsx]: #buildjsxtree-options
348
+
349
+ [options]: #options
350
+
351
+ [runtime]: #runtime
0 commit comments