Our Global Presence
Canada
57 Sherway St,
Stoney Creek, ON
L8J 0J3
India
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
USA
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
Babel is a JavaScript compiler that includes the ability to compile JSX into regular JavaScript.
Array.from
and built-ins like Promise
are only available in ES6+, but they can be used in older environments if a Babel polyfill is.@babel/register is often used in Node.js. It makes the babel run dynamically when the require
code is executed. Since it’s not popular way to run babel in React, we are not going to deal with @babel/register way.
Let’s Create sample project.
— Create a directory and package.json
mkdir test-babel-how cd test-babel-how npm init -y
— Install the necessary packages
npm i @babel/core @babel/cli @babel/plugin-transform-arrow-functions @babel/plugin-transform-template-literals @babel/preset-react
— Create a sample code
src
folder and create code.js
in the src
folder.test-babel-how | -- node_modules -- src | -- code.js -- package-lock.json -- package.json
const element = <div>babel test</div>; // 1 const text = `element type is ${element.type}`; // 2 const add = (a,b)> a + b; // 3
We are going to:
— run the following command in the terminal
npx babel src/code.js // 1 --presets=@babel/preset-react // 2 --plugins=@babel/plugin-transform-template-literals, // 3 @babel/plugin-transform-arrow-functions
src/code.js
.@babel/preset-react
.@babel/plugin-transform-template-literals
& @babel/plugin-transform-arrow-functions
.— Following output in the terminal
const element = /*#__PURE__*/React.createElement("div", null, "babel test" ); // 1 const text = "element type is ".concat(element.type); // 2 const add = (a, b) > a + b; // 3
concat
method.— install packages to use webpack
npm i webpack webpack-cli babel-loader
— create a babel.config.js
file
const presets = ['@babel/preset-react']; const plugins = [ '@babel/plugin-transform-template-literals', '@babel/plugin-transform-arrow-functions' ]; module.exports = { presets, plugins };
babel.config.js
to specify presets & plugins.— create a webpackge.config.js
file
const path = require('path'); module.exports = { entry: './src/code.js', // 1 output: { // 2 path: path.resolve(__dirname, 'dist'), filename: 'code.bundle.js', }, module: { // 3 rules: [{ test: /\.js$/, use: 'babel-loader'}], }, optimization: { minimizer: []} , // 4 }
dist/code.bundle.js
file.babel-loader
to handle JavaScript files.babel.config.js
file.— run webpack
npx webpack
— dist/code.bundle.js
is created with a following output:
/******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; // ... // ... /* 0 */ /***/ (function(module, exports) { const element = /*#__PURE__*/React.createElement("div", null, "babel test"); // 1 const text = "element type is ".concat(element.type); // 2 const add = (a, b) > a + b; // 3 /***/ }) /******/ ]);
// ...
part is webpack’s run time code.— create runBabel.js
file
const babel = require('@babel/core'); // 1 const fs = require('fs'); const filename = './src/code.js'; const source = fs.readFileSync(filename, 'utf8'); // 2 const presets = ['@babel/preset-react']; // 3 const plugins = [ '@babel/plugin-transform-template-literals', '@babel/plugin-transform-arrow-functions', ]; const { code } = babel.transformSync(source, { // 4 filename, presets, plugins, configFile: false, // 5 }); console.log(code); // 6
code.js
to compiletransformSync
function— run file
node runBabel.js
const element = /*#__PURE__*/React.createElement("div", null, "babel test" ); // 1 const text = "element type is ".concat(element.type); // 2 const add = (a, b) > a + b; // 3
For more information and to develop web application using React JS, Hire React Developer from us as we give you a high-quality product by utilizing all the latest tools and advanced technology. E-mail us any clock at – hello@hkinfosoft.com or Skype us: “hkinfosoft”. To develop any custom web apps using React JS, please visit our technology page.
Content Source:
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
57 Sherway St,
Stoney Creek, ON
L8J 0J3
606, Suvas Scala,
S P Ring Road, Nikol,
Ahmedabad 380049
1131 Baycrest Drive,
Wesley Chapel,
FL 33544
© 2024 — HK Infosoft. All Rights Reserved.
© 2024 — HK Infosoft. All Rights Reserved.
T&C | Privacy Policy | Sitemap