Wednesday, 20 January 2021

Transform ES2019 to React JSX

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?



from Transform ES2019 to React JSX

No comments:

Post a Comment