ts-plugin-inferno
Typescript JSX transformer for InfernoJS.
This is a plugin for Typescript compiler that compiles Typescript JSX syntax ( TSX ) directly to Inferno API to avoid createElement method calls.
Inferno versions
Version 10 of the plugin compiles JSX for Inferno 10. It calls newVNode, newComponentVNode, newFragment and newTextVNode, and writes the flags of each vNode, including the shape of its children, as one number. For example <div>Hello</div> becomes newVNode(3, "div", null, "Hello"): HtmlElement (1) and HasTextChildren (2).
For Inferno 9 and older, use version 7 of the plugin. Inferno 10 numbers its vNode flags differently, so code compiled by version 7 has to be compiled again for Inferno 10.
From version 10 on, the major version of the plugin matches the major version of Inferno: use plugin 10.x with Inferno 10.x, plugin 11.x with Inferno 11.x, and so on. See the Inferno 10 migration guide.
Install
yarn add -D ts-plugin-inferno typescript
The compiled code imports inferno, so the project needs inferno 10 installed.
General usage
const transformInferno = require('ts-plugin-inferno').default
// Typescript compiler options
options: {
getCustomTransformers: () => ({
after: [transformInferno()],
}),
},
It's different depending on what bundler you're using. Please check the examples folder.
The plugin imports the Inferno functions that the compiled JSX calls (newVNode, newComponentVNode, ...) in the module format of the output.
A file without imports and exports is a script (unless moduleDetection is "force"), which TypeScript emits without a module wrapper, so the plugin requires them there with require("inferno"): an import declaration would turn the script into a module.
Options
transformInferno({
uselessFlags: 'warn'
})
uselessFlags
What to do about useless flags:
'warn'(default): print a warning withconsole.warn.'error': stop the build with an error that points at the flag. For example, CI can use it to keep useless flags out.'off': do nothing.
Any other value throws when the plugin is created, so a typo does not turn the check off silently. To use a different level in CI, set it where the plugin is created:
after: [transformInferno({
// Most CI services set CI=true
uselessFlags: process.env.CI ? 'error' : 'warn'
})],
Special flags
The plugin provides a few compile time flags that can be used to optimize an Inferno application:
// ChildFlags
<div $HasTextChildren /> - Children is rendered as pure text
<div $HasVNodeChildren /> - Children is another vNode (Element or Component)
<div $HasNonKeyedChildren /> - Children is always an array without keys
<div $HasKeyedChildren /> - Children is an array of vNodes having unique keys
<div $ChildFlag={expression} /> - Defines the children shape at runtime, see ChildFlags in inferno-vnode-flags
// Functional flags
<div $Flags={expression} /> - Replaces the vNode flags, see VNodeFlags in inferno-vnode-flags
$ChildFlag and $Flags need a value, a valueless one is a compile error.
$ReCreate has been removed in Inferno 10, and the plugin throws an error for it. To re-create an element, change its key instead, for example key={version}: Inferno unmounts the old element and mounts a new one when the key changes.
A $ChildFlag written as a ChildFlags number, e.g. $ChildFlag={16} (HasTextChildren), is packed into the flags like the other child flags. Any other $ChildFlag expression is only known at runtime, so that element is compiled to the deprecated createVNode (or createFragment), which converts the value.
$Flags={expression} replaces the flags of the element; the plugin still adds the child bit of the children, and HasInvalidChildren (16) for a component.
The flags the plugin writes, from VNodeFlags of inferno-vnode-flags 10:
| Flag | Value | Written for |
|---|---|---|
ComponentUnknown |
0 | components; Inferno sets the component type at runtime |
HtmlElement |
1 | elements |
HasTextChildren |
2 | one text child |
HasNonKeyedChildren |
4 | an array of children without keys |
HasVNodeChildren |
8 | one element or component child |
HasInvalidChildren |
16 | no children |
HasKeyedChildren |
32 | an array of children that all have a key |
SvgElement |
64 | SVG elements |
Fragment |
256 | Fragments |
InputElement |
512 | <input> |
TextareaElement |
2048 | <textarea> |
SelectElement |
4096 | <select> |
ContentEditable |
131072 | elements and components with a contentEditable prop |
Children that need normalizing, such as {expression}, get no child bit, e.g. <div>{a}</div> becomes newVNode(1, "div", null, a).
Invalid flags
When the JSX shows that the children cannot have the shape a child flag declares, the plugin throws an error that points at the flag. Inferno would otherwise render the children wrong or throw in development. For example:
// An array is not a single vNode
<div $HasVNodeChildren><a/><b/></div>
<div $HasVNodeChildren>{[a, b]}</div>
// Text is not a vNode, and an element is not text
<div $HasVNodeChildren>text</div>
<div $HasTextChildren><a/></div>
// One element is not an array, and keyed children need keys
<ul $HasNonKeyedChildren><li/></ul>
<ul $HasKeyedChildren><li key="1"/><li/></ul>
// Not a ChildFlags value
<div $ChildFlag={3}>{a}</div>
/project/src/App.tsx(1,6): $HasVNodeChildren needs one element or component child, but there are 2 children.
> 1 | <div $HasVNodeChildren><a/><b/></div>
| ^^^^^^^^^^^^^^^^^
Dynamic children such as {expression} are not checked, and neither are the children of components. Parentheses and type assertions do not hide a child, e.g. {<a/> as any} is an element. The uselessFlags option does not change this check.
Useless flags
Child flags are only needed for children whose shape the plugin cannot see, such as {expression} children or a children={expression} prop.
When the children are written as JSX, the plugin sets the child flags itself.
It warns about flags that cannot improve the output:
// The children are known at compile time: the plugin already compiles them with HasVNodeChildren
<div $HasVNodeChildren>
<h1>Hi</h1>
</div>
// Components get their children in props.children, so child flags do nothing
<Foo $HasKeyedChildren>{items}</Foo>
// Only one child flag applies. The order is $ChildFlag, $HasKeyedChildren, $HasNonKeyedChildren,
// $HasTextChildren, $HasVNodeChildren
<div $HasKeyedChildren $HasNonKeyedChildren>{items}</div>
// $Flags has no effect on Fragments
<Fragment $Flags={1}>{items}</Fragment>
A warning shows the file, line and column, the reason and the code around the flag:
ts-plugin-inferno: /project/src/App.tsx(3,10): $HasVNodeChildren is not needed: the children are known at compile time, so the plugin sets their child flags. Child flags only help with dynamic children such as {expression}.
1 | function App() {
2 | return (
> 3 | <div $HasVNodeChildren>
| ^^^^^^^^^^^^^^^^^
4 | <h1>Hi</h1>
5 | </div>
6 | );
With uselessFlags: 'error' the build fails at the first useless flag, with the same message and code.
The warnings do not change the compiled code. Remove the flags they point at, or set uselessFlags: 'off'.
Breaking change in v6.0.0
Since version v6.0.0 this plugin only supports Javascript runtime environment where Object.assign is available.
It's also highly recommended to set typescript settings as follows
compilerOptions.moduletoES2015orES6or higher.commpilerOptions.targettoES2015or higher
Usage with FuseBox
Look into the examples/fuse-box folder and the fuse.js file.
You could also try to build the project by running the following commands:
cd examples/fuse-box && npm install
npm run start:dev or npm run start:prod
Usage with webpack and ts-loader
Look into the examples/webpack folder and its webpack config.
You could also try to build the project by running the following commands:
cd examples/webpack && npm install
npm run build:prod or npm start
Testing
You can run the following command to test: npm test
Adding test cases
Write your test in a .tsx file and add it to tests/cases.
Compile with npm test and look into the tests/temp and verify.
Overwrite references by running the following command: npm run overwrite-references
Run npm test again to verify that all tests are passing.
Benchmarks
bench/ measures how much time and memory the plugin itself costs, separated from what TypeScript costs anyway.
It has no dependencies beyond the ones the tests use.
npm run bench # every case, about 5 minutes
npm run bench:quick # shorter runs, without the largest case
npm run bench:compare # working tree against HEAD; pass another ref with -- --baseline <ref>
npm run bench:profile # CPU and allocation profile of the plugin functions (case mixed-M)
node bench/run.js --help # filters, compiler options, rounds, output file
The plugin is built from src/ into bench/.cache/ for every run; a git baseline is extracted and built the same way,
so the two only differ by the plugin's code. --baseline also takes a directory with a built plugin, e.g. a copy of
the published package.
The cases are the same as in babel-plugin-inferno's benchmark, so the results of the two plugins compare: hand-written
components in bench/fixtures/ (a TodoMVC app, a dashboard, an SVG icon set, an article and a TSX form) and generated
modules from bench/generate.js: random but seeded component trees of 200, 2,000 and 20,000 JSX nodes
(mixed-S/M/L), a list of 2,000 keyed children (wide-M) and elements nested 200 deep (deep-M).
Credits
This is fork of awesome ts-transform-inferno!