Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
als-render-jsx
Advanced tools
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.
als-render-jsx
is intended to work with the als-component
library, seamlessly integrating JSX-like components into projects.RenderJsx.componentFn
— to define how components are rendered.RenderJsx.buildAction
— to define how event handlers are processed.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>);
}
}
componentFn
and buildAction
als-render-jsx
allows you to redefine two core functions to suit your needs.
RenderJsx.componentFn(isAwait, tagName, props, inner)
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()`;
};
async={true}
prop in the JSX, the isAwait
parameter becomes true
.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} }`;
};
RenderJsx.buildAction([name, value])
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>`;
}
}
componentFn
and buildAction
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.
FAQs
Transform JSX-like code into plain JavaScript
The npm package als-render-jsx receives a total of 6 weekly downloads. As such, als-render-jsx popularity was classified as not popular.
We found that als-render-jsx demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.