<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rafael Calhau</title>
    <description>The latest articles on DEV Community by Rafael Calhau (@calhaudev).</description>
    <link>https://dev.to/calhaudev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F148508%2F0d707a53-1a25-4b65-91bf-2896f7c53217.jpeg</url>
      <title>DEV Community: Rafael Calhau</title>
      <link>https://dev.to/calhaudev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/calhaudev"/>
    <language>en</language>
    <item>
      <title>Let's Talk About Babel: Have you ever stopped to understand it?</title>
      <dc:creator>Rafael Calhau</dc:creator>
      <pubDate>Tue, 09 Apr 2024 01:46:20 +0000</pubDate>
      <link>https://dev.to/calhaudev/lets-talk-babel-did-you-ever-stopped-to-understand-it-p8o</link>
      <guid>https://dev.to/calhaudev/lets-talk-babel-did-you-ever-stopped-to-understand-it-p8o</guid>
      <description>&lt;p&gt;Sometimes, my friends and coworkers ask me about Babel. I noticed that lots of developers who are just getting into JavaScript and checking out different frameworks have the same questions about what Babel is and how to use it in their projects.&lt;/p&gt;

&lt;p&gt;Ok, let's walk together and understand more about this tool that help us so much to delivery our work to the world.&lt;/p&gt;

&lt;p&gt;Babel is a JavaScript compiler that allows you to use the latest JavaScript syntax and features, known as ECMAScript (ES), while ensuring compatibility with older browsers and environments. Here in this post, we'll explore the basics of setting up Babel for a React project, including configuring Babel presets and integrating it with webpack, an another tool very popular and powerful.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before we begin, ensure you have the following installed Node.js and npm (Node Package Manager) on your machine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting Up Your Project
&lt;/h2&gt;

&lt;p&gt;Start by creating a new directory for your project and initializing a new npm project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;mkdir &lt;/span&gt;react-project
&lt;span class="nb"&gt;cd &lt;/span&gt;react-project
npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, install the necessary dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;react react-dom
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; @babel/core @babel/preset-env @babel/preset-react babel-loader webpack webpack-cli webpack-dev-server html-webpack-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, why are we installing so many packages? If you felt confused, let's understand what we are installing and to focus more about the Babel, right?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;react&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt;&lt;br&gt;
These are crucial for React development. &lt;code&gt;react&lt;/code&gt; is where you get all the React goodness for building components and managing state. &lt;code&gt;react-dom&lt;/code&gt; is specifically for rendering React components in the browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;@babel/core&lt;/code&gt;&lt;br&gt;
This is like the engine behind Babel. It's responsible for taking your modern JavaScript code and transforming it into something that older browsers can understand. When you're using the latest JavaScript features or JSX syntax in your React components, &lt;code&gt;@babel/core&lt;/code&gt; does the heavy lifting to make it all work across different browsers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;@babel/preset-env&lt;/code&gt;&lt;br&gt;
Consider it as your solution for ensuring JavaScript compatibility. It analyzes your code and automatically determines the transformations necessary to ensure it functions properly in the environments you specify. This means you can confidently write modern JavaScript without concerns about whether it'll function correctly in older browsers or Node.js versions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;@babel/preset-react&lt;/code&gt;&lt;br&gt;
This preset is specifically tailored for React. It takes care of converting JSX syntax into regular JavaScript function calls. Without this, your browsers would be scratching their heads at the JSX syntax you're using in your React components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;babel-loader&lt;/code&gt;&lt;br&gt;
Think of this as the bridge between Babel and webpack. babel-loader helps webpack understand how to use Babel to transpile your JavaScript files. It's like the translator that makes sure webpack knows how to handle JSX and all the modern JavaScript features you're using in your React project.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;webpack&lt;/code&gt; and &lt;code&gt;webpack-cli&lt;/code&gt;&lt;br&gt;
It's like your project's organizer. It bundles all your JavaScript, CSS, and other assets into neat packages that the browser can easily understand. webpack-cli is like webpack's personal assistant, helping you run webpack commands from your terminal with ease.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;webpack-dev-server&lt;/code&gt;&lt;br&gt;
It makes your life easier during development by providing live reloading and hot module replacement (HMR). It saves you from manually refreshing the browser every time you make a change in your code, which is super handy when you're knee-deep in React development.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;html-webpack-plugin&lt;/code&gt;&lt;br&gt;
Plugin to generate an HTML file for the bundled JavaScript to be injected into. This is useful for development and production modes to create a complete HTML file automatically.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; &lt;em&gt;Both Babel CLI and webpack CLI depend on their respective core packages (&lt;code&gt;@babel/core&lt;/code&gt; and &lt;code&gt;webpack&lt;/code&gt;) to perform their tasks. Babel CLI handles JavaScript transformation, while webpack CLI manages bundling and optimizing web assets.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Great! Now that we understand the purpose of each package, let's proceed with building our small project using React and Babel.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Babel
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;.babelrc&lt;/code&gt; file in the root of your project directory to configure Babel presets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;.babelrc&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"presets"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-env"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"@babel/preset-react"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration tells Babel to use &lt;code&gt;@babel/preset-env&lt;/code&gt; for handling modern JavaScript syntax, as we learned above and &lt;code&gt;@babel/preset-react&lt;/code&gt; for JSX transformation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring webpack
&lt;/h2&gt;

&lt;p&gt;Create a &lt;code&gt;webpack.config.js&lt;/code&gt; file in the root of your project directory to configure webpack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// webpack.config.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HtmlWebpackPlugin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;html-webpack-plugin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isDevelopment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isDevelopment&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bundle.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bundle.[contenthash].js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;clean&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// webpack 5 feature to clean the output directory before each build&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\.&lt;/span&gt;&lt;span class="sr"&gt;jsx&lt;/span&gt;&lt;span class="se"&gt;?&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;exclude&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sr"&gt;/node_modules/&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;use&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;babel-loader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;devServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;static&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="c1"&gt;// Serve from the 'dist' directory&lt;/span&gt;
      &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Enable Hot Module Replacement (HMR)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;extensions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.jsx&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="c1"&gt;// webpack 5 automatically resolves these file extensions&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;HtmlWebpackPlugin&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="na"&gt;template&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./public/index.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// Path to your HTML template&lt;/span&gt;
        &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;body&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool, this webpack configuration sets up a basic setup to bundle your React code using &lt;code&gt;babel-loader&lt;/code&gt; and serves the bundled files through &lt;code&gt;webpack-dev-server&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a React Component
&lt;/h2&gt;

&lt;p&gt;First, create a &lt;code&gt;src&lt;/code&gt; directory in the root of your project and add a new file index.js.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/index.js&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hello&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Creating the HTML Template
&lt;/h2&gt;

&lt;p&gt;Create an index.html file in the root of your project's public directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- public/index.html --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"UTF-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;My React App&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Running Your React App
&lt;/h2&gt;

&lt;p&gt;Add scripts to your package.json file to build and run your React app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="err"&gt;//&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;package.json&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"scripts"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"start"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack serve"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"dev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack serve --mode development"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"build"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"webpack --mode production"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can start your development server by running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Beautiful. Navigate to &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; in your browser to see your React app running!&lt;/p&gt;

&lt;p&gt;By now, you should have a clearer understanding of how Babel operates, its purpose, and how it integrates with webpack. Essentially, webpack communicates with Babel through the webpack plugin called "babel-loader". Think of this plugin as a bridge between the two, allowing webpack to seamlessly interact with Babel, ensuring your JavaScript code is transformed effectively during the bundling process.&lt;/p&gt;

&lt;h2&gt;
  
  
  A little more to consume
&lt;/h2&gt;

&lt;p&gt;Here are a few topics and concepts that will help you become more confident in understanding Babel better:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transpilation:&lt;/strong&gt; Babel is like a language translator for JavaScript. It helps convert modern JavaScript code into older versions that can run on older browsers or systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ECMAScript Versions:&lt;/strong&gt; Babel lets you use the latest JavaScript features, even if they're not supported everywhere. It's like having a sneak peek into the future of JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Babel Core:&lt;/strong&gt; Think of Babel Core as the engine under the hood. It's what does all the heavy lifting, like parsing and transforming your code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Plugins and Presets:&lt;/strong&gt; These are like tools in a toolbox. Plugins are individual tools for specific tasks, while presets are pre-packaged sets of tools tailored for different needs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Configuration:&lt;/strong&gt; Just like setting preferences in an app, configuring Babel lets you customize how it works for your project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration with Build Tools:&lt;/strong&gt; Babel plays well with other tools like Webpack or Gulp, making it easy to fit into your existing workflow.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Target Environments:&lt;/strong&gt; With Babel, you can make sure your code works everywhere you need it to, whether it's in a web browser or on a server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Polyfills:&lt;/strong&gt; Babel can automatically add missing pieces of code (polyfills) to make sure your code runs smoothly on older systems.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Debugging and Optimization:&lt;/strong&gt; Understanding how to tweak your Babel setup can help improve performance and make debugging easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Community and Ecosystem:&lt;/strong&gt; There's a whole community of developers using and contributing to Babel. It's like having a big group of friends who can help you out when you need it.&lt;/p&gt;

&lt;p&gt;Getting familiar with these topics will give you a solid foundation for mastering Babel and making the most of its capabilities in your projects.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>babel</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Simplifying Code Management with Monorepo</title>
      <dc:creator>Rafael Calhau</dc:creator>
      <pubDate>Tue, 17 Jan 2023 00:54:51 +0000</pubDate>
      <link>https://dev.to/calhaudev/simplifying-code-management-with-monorepo-5551</link>
      <guid>https://dev.to/calhaudev/simplifying-code-management-with-monorepo-5551</guid>
      <description>&lt;p&gt;First of all, what is monorepo? What is it used for? And how to use it? I gonna try to answer all these questions in this post.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a monorepo?
&lt;/h2&gt;

&lt;p&gt;Basically, monorepo is a version control strategy where you can store code from projects with a strong relationship between them in a single repository. For example, if you have a full-stack application, and you probably have your frontend, backend, and maybe also a mobile project that is part of your solution. A monorepo would hold all these projects inside a single repository. But what are the benefits of using a monorepo after all?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zqxubqakofs66y4iajo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9zqxubqakofs66y4iajo.png" alt="Monorepo Scheme" width="800" height="930"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Image: &lt;a href="https://monorepo.tools/#what-is-a-monorepo" rel="noopener noreferrer"&gt;https://monorepo.tools/#what-is-a-monorepo&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There needs to be more clarity about what, in fact, is a monorepo. As said before, monorepo holds distinct parts of your solution, and they share a well-defined relationship, even components that can be shared between these projects. Let’s pinpoint a few benefits of having a monorepo.&lt;/p&gt;

&lt;h2&gt;
  
  
  The benefits
&lt;/h2&gt;

&lt;h3&gt;
  
  
  A single version of each component
&lt;/h3&gt;

&lt;p&gt;With monorepo, developers can maintain a single version of each shared component, easily keeping track of component versions and distributing them between the projects. Monorepo allows for better version management of the codebase, making it easier to track and manage changes, and to ensure that different parts of the codebase are compatible with one another.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code sharing and collaboration
&lt;/h3&gt;

&lt;p&gt;When we have all of the code in one place, it's easier for developers like you and me to share and collaborate on code, and it's also easier to ensure that different parts of the codebase are compatible with one another. And you might be wondering, why it is easier to ensure that? It’s because all of the code is in one place and developers can more easily see how different parts of the codebase depend on each other.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cross-dependency management
&lt;/h3&gt;

&lt;p&gt;Monorepo allows for better management of dependencies between different parts of the codebase, making it easier to manage and maintain the codebase. In other words, it's about making sure that different modules, libraries, or components in a codebase work together seamlessly. The cross-dependency management can be a hard task on large codebases, and the monorepo strategy can help to simplify this by keeping all of the code for a project or organization in a single repository.&lt;/p&gt;

&lt;h3&gt;
  
  
  Development, testing, and deployment simplified
&lt;/h3&gt;

&lt;p&gt;In fact, developers can speed up the development process if they have a clear understanding of where each part of the project is, and how to use and maintain them, and this even makes it possible for other teams to help build and maintain new features for specific parts of the project. Additionally, using a monorepo can make it easier to automate tasks like testing and deployment, as all necessary code is located in one place.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, what it is used for, after all?
&lt;/h2&gt;

&lt;p&gt;Monorepo is used and is great for many different types of development:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Web and mobile development&lt;/li&gt;
&lt;li&gt;Backend and frontend development&lt;/li&gt;
&lt;li&gt;Infrastructure as a Code&lt;/li&gt;
&lt;li&gt;Machine Learning and Data Science&lt;/li&gt;
&lt;li&gt;Embedded systems&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also, it might be great for a few use-cases, like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Large, complex codebases that are difficult to manage with multiple repositories&lt;/li&gt;
&lt;li&gt;Organizations that have multiple teams working on different projects, but with shared dependencies&lt;/li&gt;
&lt;li&gt;Automating the build, test, and deployment processes&lt;/li&gt;
&lt;li&gt;Improving collaboration between teams&lt;/li&gt;
&lt;li&gt;Keeping track of the entire company's codebase&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Here a few tips for who is studying monorepos:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Start by understanding the basic concepts: Before diving into the implementation of a monorepo, it's important to understand the basic concepts, such as the benefits and trade-offs of using a monorepo, as well as the different tools and strategies that can be used to manage a monorepo.&lt;/li&gt;
&lt;li&gt;Get hands-on experience: Practice is key to understanding how monorepos work in practice. Try setting up a monorepo for a small personal project, or work on an open-source project that uses a monorepo. This will give you a better understanding of how to manage and work with a monorepo.&lt;/li&gt;
&lt;li&gt;Experiment with a small project: Try setting up a small JavaScript project using a monorepo tool, like Lerna or Yarn Workspaces. This will give you hands-on experience working with monorepos and will help you understand how to manage and work with code in a monorepo.&lt;/li&gt;
&lt;li&gt;Study best practices: Learn from others who have experience working with monorepos. Read case studies and best practices from companies that have successfully implemented monorepos. This will give you a better understanding of what works and what doesn't when it comes to monorepos.&lt;/li&gt;
&lt;li&gt;Join a community: Monorepos are becoming more popular, and there are many communities, forums, and groups dedicated to discussing monorepos and sharing tips and best practices. Joining these communities can be a great way to connect with others who are also studying monorepos, and to get answers to any questions you may have.&lt;/li&gt;
&lt;li&gt;Learn the trade-offs: Monorepos are not a silver bullet and have some trade-offs like longer build and test time and difficulties in scaling. Understand the trade-offs and when to use monorepos and when not.&lt;/li&gt;
&lt;li&gt;Learn about related technologies: Monorepo is not the only way to manage dependencies in javascript, Learn about other technologies like npm, yarn, webpack, and related concepts like dependency management, packaging, and building.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By following these tips, you'll be well on your way to understanding and working with monorepos in your development projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Take your first hands-On
&lt;/h2&gt;

&lt;p&gt;A good example of a Lerna monorepo project is the "lerna-demo" repository on GitHub. This project demonstrates how to use Lerna to manage a monorepo with multiple npm packages. The repository includes a couple of example packages, and shows how to use Lerna to manage dependencies, versioning, and publishing packages to npm.&lt;/p&gt;

&lt;p&gt;You can find the "getting-started-example" repository here: &lt;strong&gt;&lt;a href="https://github.com/lerna/getting-started-example" rel="noopener noreferrer"&gt;https://github.com/lerna/getting-started-example&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
This can be a good starting point to understand the basic functionality of Lerna and how to use it in your own project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Important to understand when not to use
&lt;/h2&gt;

&lt;p&gt;Overall, a monorepo can help to improve the organization and efficiency of a development team by making it easier to manage and share code, and also make it easier to keep track of the entire codebase. So will be scenarios that monorepo can effectively help your team / organization, and scenarios that it will not be necessary like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Small projects with just one or a few developers and no cross-dependency between different parts of the project. A single repository for a simple project may be overkill.&lt;/li&gt;
&lt;li&gt;Projects with different ownership or security requirements. So, the codebase is not available for all teams or developers.&lt;/li&gt;
&lt;li&gt;Projects with different languages, or different build systems.&lt;/li&gt;
&lt;li&gt;Monorepos can be hard to scale when the projects are owned by different teams with different release cycles as well.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's very important to evaluate the specific needs of your project or organization before deciding whether or not to use a monorepo. If it is not the best option for your specific use case, it's better to consider other alternatives.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>promptengineering</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
