
JSX IR Runtime
Runtime for rendering JSX-IR. Runtime does not renders JSX-IR, otherwise it provides common interfaces for Renderer to be implemented.
Installation
npm install jsx-runtime
Usage
This package does not really intended to be used by the end user, instead users should install Renderers which depends on this package.
Runtime 1 main and most important method, registers an Renderer to the runtime:
jsx.register(rendererName, rendererConfig);
Simple Renderer may look like this:
var jsx = require('../');
var renderer = jsx.register('jsx-unwrap', {
tags: {
'*': {
enter: function(tag, props) {
return {
tag: tag,
props: props || null,
children: []
};
},
leave: function(parent, tag) {
if (!parent.children.length) {
parent.children = null;
}
return parent;
},
child: function(child, parent) {
parent.children.push(child);
return parent;
}
}
}
});
module.exports = renderer;
https://github.com/jsx-ir/jsx-runtime/blob/master/tests/_unwrap-renderer.js
This simple Renderer from tests just unwraps incoming data back to JSX-IR so it could be easy testable. Renderers could be used in many different ways, starting from generating simple string-markup to complex output of framework objects, bypassing string/dom parsing and initializing directly with passed values.
API
Definition in TypeScript format
interface Runtime {
register(name: string, config: RendererConfig): Renderer;
}
interface RendererConfig {
before(element: JSX.Element): JSX.Element;
after(element: any): any;
tags: {
'*': TagHandler;
[tag: string]: TagHandler;
};
}
interface TagHandler {
props?: (props: JSX.Props, tag: string) => any;
child?: (child: JSX.Child, parent: any, tag: string) => any;
enter: (tag: string, props: any) => any;
leave?: (element: any, tag: string) => any;
children?: (children: JSX.Children, parent: any, tag: string) => any[];
custom?: (fn: Function, props: JSX.Props, children: JSX.Children, tag: string) => any;
}
interface Renderer {
render(element: JSX.Element);
render(fn: Function, props?: JSX.Props, children?: JSX.Children);
render(tag: string, props?: JSX.Props, children?: JSX.Children);
}
License
MIT