Helper function to build react jsx
npx @tessl/cli install tessl/npm-babel-helper-builder-react-jsx@6.26.0Babel Helper Builder React JSX is a Babel helper function that creates a visitor for transforming JSX elements into JavaScript function calls. It provides a customizable JSX transformation pipeline with support for attribute processing, spread operators, and pre/post processing hooks.
npm install babel-helper-builder-react-jsximport buildReactJSX from "babel-helper-builder-react-jsx";For CommonJS:
const buildReactJSX = require("babel-helper-builder-react-jsx");import buildReactJSX from "babel-helper-builder-react-jsx";
// Create a Babel visitor for JSX transformation
const visitor = buildReactJSX({
pre: function (state) {
// Called before building element attributes
console.log("Processing element:", state.tagName);
},
post: function (state) {
// Called after building element attributes
console.log("Finished processing:", state.tagName);
}
});
// Use in a Babel plugin
export default function myPlugin() {
return {
visitor: buildReactJSX()
};
}Creates a Babel visitor object that transforms JSX elements into JavaScript function calls.
/**
* Creates a Babel visitor for transforming JSX elements
* @param opts - Configuration options with optional pre/post hooks (can be undefined)
* @returns Babel visitor object with JSX transformation logic
*/
function buildReactJSX(opts?: Options): Visitor;
interface Options {
/** Function called before building element attributes */
pre?: (state: ElementState, file: any) => void;
/** Function called after building element attributes */
post?: (state: ElementState, file: any) => void;
}
interface ElementState {
/** The tag node from the AST */
tagExpr: Object;
/** Raw string tag name */
tagName: string;
/** Array of call arguments for the transformed element */
args: Array<Object>;
/** Optional call property to override the returned call expression */
call?: Object;
/** The callee for the function call (set by pre/post hooks) */
callee?: Object;
}
interface Visitor {
/** Handles JSX namespaced names (throws error as not supported) */
JSXNamespacedName: (path: any) => never;
/** Main visitor method for transforming JSX elements */
JSXElement: {
exit: (path: any, file: any) => void;
};
}The ElementState object tracks the transformation state of each JSX element during processing.
interface ElementState {
/** The tag node from the AST */
tagExpr: Object;
/** Raw string tag name extracted from the tag expression */
tagName: string;
/** Array of call arguments being built for the transformed element */
args: Array<Object>;
/** Optional call property that can be set to override the call expression returned */
call?: Object;
/** The callee for the function call (set by pre/post hooks) */
callee?: Object;
}The helper supports comprehensive JSX-to-JavaScript transformation:
The helper provides clear error handling:
// Throws for unsupported JSX namespaced names
JSXNamespacedName(path) {
throw path.buildCodeFrameError("Namespace tags are not supported. ReactJSX is not XML.");
}
// Validates useBuiltIns option type
if (typeof useBuiltIns !== "boolean") {
throw new Error("transform-react-jsx currently only accepts a boolean option for useBuiltIns (defaults to false)");
}import buildReactJSX from "babel-helper-builder-react-jsx";
// Simple visitor without options (opts can be undefined)
const visitor = buildReactJSX();
// The visitor will transform:
// <div className="container">Hello World</div>
// Into function calls (exact output depends on configuration)import buildReactJSX from "babel-helper-builder-react-jsx";
const visitor = buildReactJSX({
pre: function (state, file) {
// Modify element processing before attributes are built
if (state.tagName === "CustomComponent") {
// Custom logic for specific components
console.log("Processing custom component");
}
},
post: function (state, file) {
// Set the callee to customize the function call
state.callee = t.memberExpression(
t.identifier("React"),
t.identifier("createElement")
);
}
});import buildReactJSX from "babel-helper-builder-react-jsx";
export default function myReactPlugin() {
return {
name: "my-react-transform",
visitor: buildReactJSX({
pre: function (state, file) {
// Set up the callee for React.createElement
state.callee = t.memberExpression(
t.identifier("React"),
t.identifier("createElement")
);
}
})
};
}