reactjs - How to convert react JSX files to simple JavaScript file [offline transformation] - Stack Overflow
JSX to ES6 converter(online preferably)
reactjs - React.js - convert .jsx to .js - Stack Overflow
node.js - Transform JSX to JS using babel - Stack Overflow
If you are using babel-cli you need to use a plugin to transform jsx syntax to js syntax that the browser understands. See the following link: http://babeljs.io/docs/plugins/transform-react-jsx/ You'll need to install babel-plugin-transform-react-jsx plugin and use it in a .babelrc file like that:
{
"plugins": ["transform-react-jsx"]
}
Then your babel command should work.
Try babel --presets react JSX_Files --watch --out-dir public/javascripts (s on the end of preset).
Hello dev-devs!
I am looking for an online JSX to ES6x converter for a project. I have come across jscodeshit but I am trying to get the gist of it. Any help is fully appreciated.
Thank you
if you are only using babel to transform jsx to js, this is what you need :
installation
- install
babel-clias global (optional)sudo npm install -g babel-cli - install
babel-preset-es2015(optional. if your code using es6 standard)npm install babel-preset-es2015 - install
babel-preset-react(required)
configuration
in your root of project, add this file .babelrc and write this to it
{
"presets": ["es2015", "react"]
}
or
{
"presets": ["react"]
}
run
babel source_dir -d target_dir
As of 2023 if you are using babel to transform jsx to js, this is what you will need to do:
Install Packages
Install the following globally on your machine:
npm install -g @babel/cli
npm install -g @babel/core
npm install -g @babel/preset-react
or install the following as a project dependency (my personal preference):
npm install --save-dev @babel/cli
npm install --save-dev @babel/core
npm install --save-dev @babel/preset-react
Create Configuration File
Create a .babelrc config file at the root of your project with the following setting as a bare minimum:
{
"presets": ["@babel/preset-react"]
}
You should review the babel docs if you have a more complicated use case and determine what additional settings you may need to add. You can find the settings for compiling jsx to js on the @babel/preset-react page.
Compile
You can compile any jsx file into js by running the following command in your terminal:
npx babel [jsx-file-here.jsx] -d [output-directory]
If your jsx file imports anything please keep the following in mind:
- This will not import any imported code. You will need to also compile any files that are imported. You can then use your code as a browser module: see next 2 points.
- JS imports are widely supported now (see also) but you will need to add
type="module"to the script tag in your HTML. - Remember you must include the file extension
.jsin your import statements still! No browser will intuptret import Foo from './bar' asimport Foo from './bar.js. - If you do not want to do the previous you can use something like rollup.js to bundle all the files into a single script.
Without knowing the rest of your project setup, I can't tell how the current app.js file is getting created, but you're right: what you need is to transpile the code via Babel. This can be done two ways in development: on the dev server (recommended), or in the dev browser using standalone Babel.
On the Dev Server (Recommended)
React's recommended way to start with React is using a tool called Create React App. Since you said you're new to React, this will take care of Babel behind the scenes and give you a development server (so you don't need WAMP, only Node and NPM), and a build process to create the assets like app.js to deliver to the production server.
If you don't have time to immediately learn Babel and Webpack, I'd recommend using Create React App first.
In the Dev Browser
If you don't want to introduce a new tool, and you just want to transpile JSX with minimal configuration, you can do this in the browser via the instructions. This used to be demonstrated in the React.js tutorials until they switched to recommending Create React App. The instructions for installing "in the browser" require two things: adding babel.min.js to your <head>, and adding the type="text/babel" attribute to your .jsx file (working code example below). However, the documentation gives this advice:
Compiling in the browser has a fairly limited use case, so if you are working on a production site you should be precompiling your scripts server-side. See setup build systems for more information.
Your HTML and JavaScript should look as follows (I've omitted a lot of your code for brevity):
index.html
<!doctype html>
<head>
<script src=src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.0/react-dom.js"></script>
</head>
<body>
<h1>Browser in JSX Demo</h1>
<div id="appRoot">If you're seeing this, it's not working.</div>
<script type="text/babel" src="app.jsx"></script>
</body>
</html>
app.jsx
class App extends React.Component {
render() {
return <div>It works!</div>;
}
}
const el = document.getElementById('appRoot');
ReactDOM.render(<App />, el);
Please note that the extensions used in the script src attribute and the JavaScript file must match. You can use .jsx or .js, but they must match. I don't know how your server is working its magic, but that's beyond the scope of the question, and is not expected behavior by default.
You can use Webpack to achieve what you want.
You can download every dependencies like this (if you have npm installed):
npm install --save-dev webpack
npm install --save-dev webpack-dev-server
npm install --save-dev loader-utils html-webpack-plugin extract-text-webpack-plugin
npm install --save-dev babel-core babel-loader babel-register
npm install --save-dev babel-preset-es2015 babel-preset-react
Then you can create the file webpack.config.js at the root of your folder with this code inside:
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
export default () => ({
entry: [
path.join(__dirname, 'src/index.jsx'),
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './src/index.html'
}),
]
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel',
options: {
babelrc: false,
presets: [
['es2015', { modules: false }],
'react',
],
}
}
]
},
]
},
});
And in your package.json, add those lines:
{
...
"scripts": {
"dev": "webpack-dev-server",
"build": "webpack"
}
...
}
You will now be able to run npm run dev for development and npm run build to compile the code ready for a production server.
You can init npm in your project if it's not already done like this npm init
You can also follow this guide: Setting Up A React Workflow with Babel and Webpack 2
Webpack is basically a tool that takes every .jsx file and convert it to a .js file. In the code above, I use index.jsx as the main file. Every react component that will be imported in this file will be automatically computed.