Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel command line not transpiling import statements - React

I have the following JSX file.

import React from "react";

export default class Code extends React.Component {
    render() {
        return <div></div>
    }
}

React.render(<Code />, document.getElementById('code'));

I'm running Babel from the command line using the following

babel --plugins transform-react-jsx app.jsx -d dist

I get the following output

import React from "react";
export default class Code extends React.Component {
  render() {
    return React.createElement("div", null);
  }

}
React.render(React.createElement(Code, null), document.getElementById('code'));

This obviously does not run in the browser since import and export are not valid. What settings do I need to specify for Babel to transfrom es6 imports?

Here is my .babelrc file

{
    "plugins": [
        [
            "transform-react-jsx",
            {
                "pragma": "dom" // default pragma is React.createElement
            },
        ]
    ]
}

Thanks

like image 486
Arun Avatar asked Apr 12 '26 15:04

Arun


1 Answers

What about using preset-react instead which will also include transform-react-jsx plugin.

  1. Install the preset module npm install --save-dev @babel/preset-react
  2. Add the presets to your .babelrc file, i.e.

    { "presets": ["@babel/preset-react"] }

  3. Run your babel script babel app.jsx -d dist

If you don't want to add .babelrc file just run the script as so after installing the preset module babel --presets @babel/preset-react app.jsx -d dist

More information on the preset-react here

like image 142
Van Avatar answered Apr 15 '26 08:04

Van



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!