Introduction About React Babel:
Installation of React Babel:
The npm module for Babel is babel-core. And we will install babel-core in a way that is different from that we did React and React DOM. So instead of using the command npm install –save babel-core, we will be using the command npm install –save-dev babel-core. The reason for this is that we will only use Babel in a mode that is meant for development. When that React application is shipped into production, no transformations are required. Also, the –save-dev flag saves a module for npm for the purpose of the development version only. Just like we can write –save as -S, we can write –save-dev as -D. We can also install two other modules related to Babel namely babel-loader and the babel-preset-react.
Presets can configure Babel. Here, these preset transpile almost all of the web technologies. And these technologies are available to us that include css, jsx, js and so on. Here we will see how to make use of two presets that are react and env. Package.json or even cli can configure Babel. But we mostly prefer package.json to configure. Presets basically are like modules that help us in the transpiring of one language to another.
The next thing that we are going to do is add the above lines to our package.json file. After that, it will simply let the React Babel know that these particular presets are used to transpile.
Configuration of Webpack for Babel:
Now what we are going to do is configure the webpack for using it with Babel. For this, we will be using the babel-loader. Webpack provides us with valuable hooks that process the files. And for us developers, these new modules are extremely useful and easy to configure. The rules are simple and specify what we should do when a particular file is encountered. The text key specifies the file regex. And, we will use it to select all the files that have the extension .m and .js. But, it’ll exclude folders such as node_module and bower_components. This is because we don’t want to transpile the node_module files if we have specified the babel-loader already.
Making use of CSS Files:
For making use of Babel with CSS, we will be using the style-loader and css loader of Babel. The use key can also take an array that is composed of the objects or it might also be composed of strings. As a result of our loaders that are loaded from last to first, first, our fill will go to the css loader. As a result, it makes our css exclusive to one and only one component in which we import it. The loader for CSS will resolve all the imports in the files of CSS. After that, the style-loader will add these in the form of style tags on the head.
The presets and plugins of Babel are the details for configurations for Babel. We use these for the purpose of transpiling of the code. Babel supports a number of plugins. We can also use it individually as well if the environment and code that we will execute are known.
The Babel CLI introduces us to a number of features. These features basically include methods and objects. But we can’t use them for transpilation purposes.
Advantages of Using Babel:
- Babel is very versatile and we can use it with a gulp, webpack, react, typescript, flow, and so on. As a result, this makes it a very powerful tool that can be used for very big projects. Also, it makes the developer’s life much easier.
- It also works along with React JSX in terms of syntax. And, we can compile it very well in JSX form.
- Many plugins and polyfills support babel that makes it very easy for developers to work.
Disadvantages of Babel:
- Babel has the ability to change the syntax when it transpile. Thus, it’s much more difficult in coding. Also, it is even difficult to understand the code when the product releases after production.
- The transpiled code is most of the time much larger in size when compared to the original code.
- As the new features that will come in the Ecma script, it becomes more difficult to transpile. Hence, we need to use polyfill so that it can support all other browsers.
In this article, we learned a lot about React Babel and how we can install it. We have also discussed the configuration of React Babel and how we can implement it to use with React Webpack. But as detailed it might be, this was just an overview of it. We can make an innumerable number of changes in the configuration as we like. This is because it is highly customizable. We need to remember that we have to use the loader for each file that we will use during our project. Many more technologies are compatible with Babel and we can use them freely as per our requirements.