1.0.3 • Published 6 months ago

react-jsx-compiler v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

jsx-compiler

This work is in very large parts based upon the work of the DataFormsJS project and their jsx-loader!

Here their compiler is merely re-package to be available standalone.

Installation

Very simple:

npm install jsx-compiler

Compile to Component

You can compile a JSX script to a React component:

import Compiler from 'jsx-compiler'

const script = `
    return (
        <>
        {list.map(item => (
            <div key={item}>
                {item} {p.b}
            </div>))}
        </>
)`;
const ctx = {
list: ["a", "b", "c"],
};

const compiler = new Compiler();
const Component = compiler.compileToComponent(ctx, script, "p");

return <Component b="hihi" />;

This <Component/> will behave as expected.

Compile to String

You can compile a JSX script to JavaScript:

import Compiler from 'jsx-compiler'

const script = `
    return <div>huhu</div>
`

const compiler = new Compiler();
return compiler.compileToString(script);

Here, return React.createElement("div", null, "huhu") will be returned.

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago