innet-jsx
This utils help to convert
jsx
into js
with innet rules.Install
npmnpm i -g innet-jsx
You can use
npx innet-jsx
instead of innet-jsx
without installation.Usage
You can useNode.js
exports or CLI
for conversion of jsx
into js
.CLI
Runinnet-jsx
with -v
option to see innet-jsx
version you have.
innet-jsx -v
Run
innet-jsx
with -h
option to see help information.
innet-jsx -h
Arguments
Run the CLI with input file path you want to convert.innet-jsx test.jsx
You will get js
file with the same name, around itIf you want to set another output file path, add the path after the input one.
innet-jsx test.jsx custom.js
Options
Use-m
option to add map file.
innet-jsx test.jsx -m
You will get test.js.mapUse
-w
option to watch the input file
innet-jsx test.jsx -w
Node.js
You can install it local to the projectnpm i -D innet-jsx
# or
yarn add -D innet-jsx
Transform
Usetransform
to convert jsx
string code to js
.
```typescript jsx
import transform from 'innet-jsx'console.log(transform('<></>'))
*You will get an object with 2 fields: code (contains result of transformation) and map (contains map data)*
You can pass to `transform` an object with the next fields: `code`, `map` (optional).
```typescript jsx
import transform from 'innet-jsx'
console.log(transform({
code: '<></>',
map: '...',
}))
If you want to get Abstract Syntax Tree (AST), use
parse
function.
```typescript jsx
import { parse } from 'innet-jsx'console.log(parse('<></>'))
## Rules
A fragment converts to an array
```typescript jsx
<></> // []
<>1</> // ['1']
<>{1}</> // [1]
<>{1}{2}</> // [1,2]
<>{1} {2}</> // [1,' ',2]
<> {1} {2} </> // [1,' ',2]
<>
{1}
{2}
</> // [1,' ',2]
An element converts to an object ```typescript jsx
// {type:'div'}
// {type:'div'}
Props of an element contains in
Children of an element contains in `children` field
```typescript jsx
<div>1</div> // {type:'div',children:['1']}
<div>{2}</div> // {type:'div',children:[2]}
<div><span /></div>
// {type:'div',children:[{type:'span'}]}
Props of an element contains in
props
field
```typescript jsx
// {type:'div',props:{id:'test'}}
// {type:'div',props:{id:"test1",class:"test2"}}
const test = 1;
const test = 1;
test: {test}</>} />
// {type:'div',props:{children:'test: ',test}}
```