als-render-jsx
is a JavaScript library that transforms JSX-like code into plain JavaScript. It is designed to mimic JSX syntax while enabling runtime evaluation and rendering in both Node.js and browser environments.
- By default,
als-render-jsx
is intended to work with theals-component
library, seamlessly integrating JSX-like components into projects. - The library is highly customizable. You can override its two core static functions:
-
RenderJsx.componentFn
— to define how components are rendered. -
RenderJsx.buildAction
— to define how event handlers are processed.
-
- With these overrides, the library can become universal and adaptable to various use cases.
Install the library using npm:
npm i als-render-jsx
In a Node.js environment, import the library as follows:
const RenderJsx = require('als-render-jsx');
In the browser, include the library using a script tag:
Development Version
<script src="/node_modules/als-render-jsx/render-jsx.js"></script>
Minified Production Version
<script src="/node_modules/als-render-jsx/render-jsx.min.js"></script>
Use the RenderJsx.render
method to transform JSX-like code into plain JavaScript.
const component = /*js*/`class Some {
constructor(props,inner) {super(props,inner)}
render() {
return (<div>{this.inner}</div>);
}
}`;
const result = RenderJsx.render(component);
console.log(result);
class Some {
constructor(props,inner) {super(props,inner)}
render() {
return `<div>${this.inner}</div>`;
}
}
const component = /*js*/`class Parent {
constructor(props,inner) {super(props,inner)}
render() {
return (<div><Child>some inner</Child></div>);
}
}`;
const result = RenderJsx.render(component);
console.log(result);
class Parent {
constructor(props,inner) {super(props,inner)}
render() {
return `<div>${new Child({},'some inner').call()}</div>`;
}
}
class Input {
constructor(props) {super(props)}
render(props) {
return (<input {...props} />);
}
}
class Login {
constructor(props,inner) {super(props,inner)}
render() {
return (<div>
<Input {{ name:'email',placeholder:'Email',type:'email',required:true }}>
<Input {{ name:'password',placeholder:'password',type:'password',required:true }}>
</div>);
}
}
als-render-jsx
allows you to redefine two core functions to suit your needs.
This function defines how components are rendered. By default, it returns an initialization of another component.
RenderJsx.componentFn = function (isAwait, tagName, props, inner) {
return `${isAwait}(new ${tagName}(${props}, \`${inner}\`)).call()`;
};
- If a component includes the
async={true}
prop in the JSX, theisAwait
parameter becomestrue
.
You can customize the function to return a string representation for debugging:
RenderJsx.componentFn = function (isAwait, tagName, props, inner) {
return `Debug: { isAwait: ${isAwait}, tagName: ${tagName}, props: ${props}, inner: ${inner} }`;
};
This function processes event handlers and assigns listeners after the HTML is rendered.
RenderJsx.buildAction = function ([name, value]) {
const event = name.split('on')[1].toLowerCase();
value = `$${this.action('${event}', ${value})}`;
return [event, value];
};
Customize it to log all event assignments:
RenderJsx.buildAction = function ([name, value]) {
console.log(`Assigning event: ${name} with handler: ${value}`);
return [name, value];
};
const components = /*js*/`class Parent {
render() {
return (<div><Child prop="value">Hello</Child></div>);
}
}`;
const result = RenderJsx.render(components);
console.log(result);
class Parent {
render() {
return `<div>${(new Child({prop: "value"}, \`Hello\`)).call()}</div>`;
}
}
RenderJsx.componentFn = function (isAwait, tagName, props, inner) {
return `CustomComponent(${tagName}, ${props}, ${inner})`;
};
RenderJsx.buildAction = function ([name, value]) {
return [`custom-${name}`, `handle(${value})`];
};
const components = /*js*/`function App() {
return (<div onClick={handleClick}>Click Me</div>);
}`;
const result = RenderJsx.render(components);
console.log(result);
function App() {
return `<div custom-onClick="handle(handleClick)">Click Me</div>`;
}
This demonstrates how you can fully control the behavior of the library to match your specific requirements.