I have been given the task of transforming ES2019 compiled React code back to JSX. The client lost their original files and I put my hand up to complete the task. Unfortunately, there are a few hundred files and as I go one by one transforming them, I thought there must be a way to compile them in one go to JSX.
For example a snippet of a file needed to transform may be...
import _extends from "@babel/runtime/helpers/extends";
import React from 'react';
import { Item } from './Item';
const Container = props => {
return /*#__PURE__*/React.createElement("div", _extends({}, props, {
component: props.component || Item,
}));
};
export default Container;
And I want to transform to...
import React from 'react';
import { Item } from './Item';
const Container = (props) => {
return <div component={props.component || Item} />;
};
export default Container;
I know I can transform JSX to JS but how can I reverse this across hundreds of files?
There're several ways to convert HTML Strings to JSX with React. One way is to put the string in between curly braces in our component. Another way is to put the string in an array with other strings or JSX code. Finally, we can use the dangerouslySetInnerHTML prop render an HTML string as HTML in our component.
What's a JSX Transform? Browsers don't understand JSX out of the box, so most React users rely on a compiler like Babel or TypeScript to transform JSX code into regular JavaScript. Many preconfigured toolkits like Create React App or Next. js also include a JSX transform under the hood.
Babel can convert JSX syntax! Check out our React preset to get started. Use it together with the babel-sublime package to bring syntax highlighting to a whole new level.
React, ES6, JavaScript, EJS, and Python are the most popular alternatives and competitors to JSX.
Instead of transforming JSX to React.createElement, the new JSX transform automatically imports special functions from those new entry points in the React package and calls them. Let’s say that your source code looks like this:
Upgrading to the new transform is completely optional, but it has a few benefits: With the new transform, you can use JSX without importing React. Depending on your setup, its compiled output may slightly improve the bundle size. It will enable future improvements that reduce the number of concepts you need to learn React.
To solve these issues, React 17 introduces two new entry points to the React package that are intended to only be used by compilers like Babel and TypeScript. Instead of transforming JSX to React.createElement, the new JSX transform automatically imports special functions from those new entry points in the React package and calls them.
Because the new JSX transform will automatically import the necessary react/jsx-runtime functions, React will no longer need to be in scope when you use JSX. This might lead to unused React imports in your code.
There is apparently a babel plugin to do exactly what you ask. -> babel-js-to-jsx
Babel 6 plugin to convert from desugared React.DOM CallExpressions -> the equivalent JSX. Currently used to migrate to ES6+ from other compile to JS languages.
It can be used as a plugin:
require("babel-core").transform(code, {
plugins: ["babel-transform-js-to-jsx", "syntax-jsx"],
}).code
Or from the command line for composition with other tools:
npm install babel-transform-js-to-jsx
cat example.ls | lsc -cb --no-header | js-to-jsx | esformatter -c format.json
babel-js-to-jsx Documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With