Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/infernojs/babel-plugin-inferno
Transforms JSX to InfernoJS vNodes
https://github.com/infernojs/babel-plugin-inferno
inferno jsx
Last synced: 26 days ago
JSON representation
Transforms JSX to InfernoJS vNodes
- Host: GitHub
- URL: https://github.com/infernojs/babel-plugin-inferno
- Owner: infernojs
- License: mit
- Created: 2015-10-03T13:24:25.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-08-31T14:32:05.000Z (2 months ago)
- Last Synced: 2024-09-27T19:41:03.502Z (about 1 month ago)
- Topics: inferno, jsx
- Language: JavaScript
- Homepage:
- Size: 847 KB
- Stars: 79
- Watchers: 14
- Forks: 26
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# InfernoJS Babel Plugin
> Plugin for babel 6+ to enable JSX for Inferno
This plugin transforms JSX code in your projects to [Inferno](https://github.com/trueadm/inferno) compatible virtual DOM.
It is recommended to use this plugin for compiling JSX for inferno. It is different to other JSX plugins, because it outputs highly optimized inferno specific `createVNode` calls. This plugin also checks children shape during compilation stage to reduce overhead from runtime application.## How to install
```bash
npm i --save-dev babel-plugin-inferno
```## How to use
Add the plugin to your `package.json` and update the plugin section in your `.babelrc` file. Or if your Babel settings are located inside the `package.json` - update the plugin section there.
It's important that you also include the `babel-plugin-syntax-jsx`plugin.
Example on a `.babelrc` file that will work with Inferno:
Make sure inferno plugin is added before babel module transformers
```js
{
"presets": [ "es2015" ],
"plugins": [["babel-plugin-inferno", {"imports": true}]]
}
```## Examples
```js
// Render a simple div
, container);
Inferno.render(// Render a div with text
Inferno.render(Hello world, container);// Render a div with a boolean attribute
, container);
Inferno.render(```
## Fragments
All of the following syntaxes are **reserved** for createFragment call
```js
<>
Foo
Bar
>
Foo
Bar
Foo
Bar```
React.Fragment is also compiled to inferno createFragment call to ease project migration to Inferno https://github.com/infernojs/babel-plugin-inferno/issues/56.
## Special flags
This plugin provides few special compile time flags that can be used to optimize an inferno application.
```js
- Children is rendered as pure text
// ChildFlags:
- Children is another vNode (Element or Component)
- Children is always array without keys
- Children is array of vNodes having unique keys
- This attribute is used for defining children shpae runtime. See inferno-vnode-flags (ChildFlags) for possibe values// Functional flags
- This flag tells inferno to always remove and add the node. It can be used to replace key={Math.random()}
```Flag called `noNormalize` has been removed in v4, and is replaced by `$HasVNodeChildren`
## Options
Change in v4:
#### Imports (boolean)
babel-plugin-inferno will automatically import the required methods from inferno library.
There is no need to import inferno in every single JSX file. Only import the inferno specific code required by the application.example:
```js
import {render} from 'inferno'; // Just import what you need, (render in this case)// The plugin will automatically import, createVNode
render(1, document.getElementById('root'));
```You need to have support for ES6 modules for this to work. If you are using legacy build system or outdated version of webpack, you can revert this change by using `imports: false`
```js
{
"presets": [ "es2015" ],
"plugins": [["inferno", {
"imports": false
}]]
}
```#### Pragma
Each method that is used from inferno can be replaced by custom name.
``` pragma ``` (string) defaults to createVNode.
``` pragmaCreateComponentVNode ``` (string) defaults to createComponentVNode.
``` pragmaNormalizeProps ``` (string) defaults to normalizeProps.
``` pragmaTextVNode ``` (string) defaults to createTextVNode.``` pragmaFragmentVNode ``` (string) defaults to createFragment.
```js
{
"presets": [ "es2015" ],
"plugins": [["inferno", {
"imports": true,
"pragma": "",
"pragmaCreateComponentVNode": "",
"pragmaNormalizeProps": "",
"pragmaTextVNode": ""
}]]
}
```### Troubleshoot
You can verify `babel-plugin-inferno` is used by looking at the compiled output.
This plugin does not generate calls to `createElement` or `h`, but instead it uses low level InfernoJS API
`createVNode`, `createComponentVNode`, `createFragment` etc. If you see your JSX being transpiled into `createElement` calls
its good indication that your babel configuration is not correct.