UNPKG

aroma-jsx-engine

Version:

A lightweight and flexible JSX/TSX engine for rendering React components in Node.js applications. Designed to work seamlessly with the [`aroma.js`](https://www.npmjs.com/package/aroma.js) framework, this package allows you to compile and render JSX/TSX co

29 lines (23 loc) 812 B
const babel = require('@babel/core'); function compile(code) { try { const isTypeScript = /(interface|type|as)\s+\w+/g.test(code); const presets = [ '@babel/preset-react', ...(isTypeScript ? ['@babel/preset-typescript'] : []), ]; const compiledCode = babel.transformSync(code, { presets, filename: isTypeScript ? 'component.tsx' : 'component.jsx', }).code; const wrappedCode = ` const React = require('react'); ${compiledCode} `; const renderFunction = eval(wrappedCode); return renderFunction; } catch (error) { throw new Error(`JSX/TSX compilation failed: ${error.message}`); } } module.exports = { compile };