02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School

Developing in ES2016 Today with Babel

Tutorial: Developing in ES2016 Today with Babel

In this tutorial, you’ll learn how to configure Babel and Webpack to start developing with modern JavaScript (ES6/ES2015) features while ensuring compatibility with older browsers. Babel acts as a transpiler, converting your ES6+ code into ES5 for seamless execution in all environments.


What You Will Learn

  1. Setting up Babel with the ES2015 preset.
  2. Configuring Webpack to use Babel for JavaScript files.
  3. Exploring new ES6 features like template literals and default parameters.

Configuring Babel and Webpack

  1. Install Required Packages
    In your project directory, install Babel and Webpack dependencies:

    npm install --save-dev @babel/preset-env babel-loader webpack webpack-cli
    
    • @babel/preset-env: Converts ES6+ to ES5.
    • babel-loader: Integrates Babel with Webpack.
  2. Update Webpack Configuration
    Edit webpack.config.js to configure Babel:

    module.exports = {
      module: {
        rules: [
          {
            test: /\.js$/, // Apply Babel to all .js files
            exclude: /node_modules/, // Exclude dependencies
            use: {
              loader: 'babel-loader',
              options: {
                presets: ['@babel/preset-env'], // Use the ES2015 preset
                cacheDirectory: true // Cache for faster builds
              }
            }
          }
        ]
      }
    };
    
  3. Verify Babel Installation
    Ensure .babelrc or babel.config.json exists with the following configuration:

    {
      "presets": ["@babel/preset-env"]
    }
    
  4. Restart Webpack Development Server
    After saving your configurations, restart your development server:

    npm start
    

Exploring ES6 Features

1. Default Parameters

Default values for function parameters simplify code:

function greet(from = "Webpack", to = "You") {
  console.log(`Hello from ${from} to ${to}`);
}
greet(); // Output: Hello from Webpack to You
greet("Babel", "React"); // Output: Hello from Babel to React

2. Template Literals

Use backticks for multiline strings and embedded expressions:

const user = "Developer";
console.log(`Welcome, ${user}!`); // Output: Welcome, Developer!

3. Arrow Functions

Simplify function expressions:

const add = (a, b) => a + b;
console.log(add(2, 3)); // Output: 5

4. Let and Const

Declare block-scoped variables:

let count = 0;
const MAX_COUNT = 10;

Testing the Setup

After restarting the Webpack server, test the new features in your JavaScript files. Babel automatically transpiles the ES6 code to ES5.

  • Check the browser's developer tools to confirm the output.
  • Verify compatibility by inspecting the generated ES5 code in the Webpack bundle.

Summary

In this tutorial, you configured Babel and Webpack to enable modern JavaScript development. You explored essential ES6 features and learned how Babel ensures compatibility with older browsers by transpiling code.

Continue learning advanced features and building modern web applications in the next lesson!

Video Transcript

00:00In the last setup and the last lecture,
00:03we set up the React environment to automatically refresh
00:07with the help of the webpac Development server
00:11in this lecture will continue down the path of enabling
00:14ES six to run on our server, enabling us to start working
00:19with the newest things already today.
00:21Obviously we're gonna need NPM for that.
00:24We're gonna need webpac for that.
00:25And we're gonna need Babel, which will be our transcriber
00:28that will transcribe whatever we're typing
00:31into the JavaScript version
00:33that will run on the browser ES five.
00:36So let's jump right into it.
00:39Now that we have a server that automatically updates,
00:42the next thing that we need is to make sure
00:44that our JavaScript
00:45that is rendered on the browser is gonna be ES five
00:48and not e es six.
00:49Really the terminologies are, you know,
00:51when we're looking at it, basically there's a application
00:54or a module that is called Babel.
00:56And Babel enables you using plugins to basically
01:01transcribe your text from your JavaScript
01:04before it's rendered from whatever version it is to ES five.
01:08So really you could say, okay, for example, ES six
01:10or ES 2015, that's what we're working in.
01:14And later we're gonna also add React
01:16and maybe LU notes of later stage versions of JavaScript,
01:20even more advanced JavaScript versions.
01:22And through these plugins, you basically enable that
01:26wrapping and we're gonna do that through web pack.
01:29So let's go ahead and do it.
01:30And what we're gonna do is we're gonna go into our terminal,
01:32and I'm gonna have to stop my server here for a second
01:34because I need to install more packages
01:37and I'm gonna go to NPM install.
01:39And again, I wanna save it into my dev environment.
01:42And I'm gonna ask to install two packages.
01:45One is Babel preset e es 2015,
01:49which is the preset that will enable me to work
01:53with the E ES six or ES 2015 already now.
01:56And it'll transcribe it down to E ES five.
01:59And the next one is Babble Loader,
02:01which is basically a loader, a loader module
02:05that is used by web pack.
02:07So let's go ahead. We're going to start the installation
02:09process and as things are installing,
02:12let's go into our editor, into our config web pack config
02:16and go right after that dev server.
02:18It doesn't really matter if it's right
02:19after, you could do it anywhere really
02:21there, but call it module.
02:23And in your module we're gonna send inside
02:25of here basically the loaders that web pack expects.
02:30And really right now we only have one loader,
02:32so let's add in that loader.
02:34So I'm just gonna send inside here an
02:36object, just one object.
02:38And the first parameter is really gonna be the test
02:41that is gonna decide does this file need
02:44to use this loader or not.
02:46Now everything here is, uh, we send in, uh, RegX,
02:50but as we send this regex in this case,
02:52what I wanna do is I really wanna send in JS files.
02:55So to do that, I'm just gonna wrap it around here.
02:58And this is the way you would write it in J in RegX
03:01that we want any file that is extension ends with js.
03:05Any file that extends with with js, we want
03:08to call in our babble loader.
03:10So to do that, we're gonna call the loader
03:13and we're gonna ask for the babble loader.
03:15The next thing that we're gonna do is I'm gonna set
03:17inside a query, which will be the parameters
03:20that we send into Babel,
03:21because Babel by default doesn't do anything
03:23and we have to tell it what to do.
03:25So in this case, I'm gonna tell it that we wanted
03:27to cache the directory
03:30because we don't want it to need to redo it over
03:33and over the Babel library elements.
03:36And the next thing,
03:37after we cash our directory, I also wanna make sure
03:40that it knows what presets it's going to use.
03:42In this case, we only lo we only
03:46have one preset that we are in the process
03:48of loading right now, which is the e ES 2015.
03:51Now the presets are always,
03:52and let me just double check if it's
03:54already done loading or not.
03:55Yeah, it's done loading. Uh, the presets are always,
03:57you just use the last name right here in the pre,
04:00there's quite a few different presets where
04:02that Babo loader itself is really this loader
04:05that we're telling, um, we're telling Webpac to use
04:08and webpac is basically gonna find all JS files
04:11and then convert them from ES 2015
04:15to e es five.
04:17So if we go ahead and say this, let's just check
04:19what else do we have to do here?
04:21Generally speaking, I think that's all we need.
04:23All that's left for us to do is restart our
04:25server to make it work.
04:27But before we do that, let's go ahead
04:28and put here some ES six stuff that our e es 2015
04:32that are theoretically not available right now in the
04:36current version of JavaScript.
04:37So if I tape you, for example, function
04:39and I'm gonna function
04:41and I have your function called, let's say from,
04:44and two it has, it says hello, it is gonna go
04:47to my console dot log.
04:49And traditionally in JavaScript to be able to output,
04:52for example, a message that says Hello
04:57from, and then I would have to space out
05:00and use the variable
05:02and then I would have to go back in there, um, to,
05:07and then again, I would have to go out
05:09and I would have to use the two variable.
05:12And so, and what if I also wanted a dot, which I'd have
05:14to also again extend it and then put that dot there.
05:18And if I go ahead and I make sure that I call my hey
05:23and I save things up and I go start up my server
05:26'cause I stopped it.
05:28So NPM start,
05:30and once it's done starting our server,
05:32we could go into our browser
05:34and we should see in our, um, output
05:37that we have your hello from undefined to undefined.
05:41Why? Because we didn't actually send
05:43anything into that function.
05:44So what would we have to do?
05:46Let's say in this scenario, if I wanted
05:48to have a default here, we're using ES five, right?
05:51And we would have to create here a from equals if from, um,
05:56doesn't exist, then I'm gonna put here from
06:01web pack or let's say from Ben, right?
06:04I'm the one that's saying hello.
06:06And to R two we would say R two to be two
06:09or you.
06:12In other words, what we have here is basically I'm checking
06:15if this variable exists or that variable exists.
06:19If that came back, then it's never gonna go through that.
06:21If that came back, it's never gonna go through that.
06:24Now a problem
06:25with this is if the variables are not even there,
06:27but we're assuming they're there, they're just empty.
06:29So in this case, what's gonna happen is if I go back into my
06:31application now we should see from Bend to you beautiful.
06:35But in the newest version of JavaScript, we don't even have
06:37to do that because we could literally go right into our from
06:40and just set my from to be from Ben
06:43and my two to give it a default value to be you
06:46and I could get rid of these lines
06:48and I don't need them anymore.
06:50They will automatically go through
06:52and if I wanted to change
06:54the anything, I would just change it.
06:55So it's from John and the second parameter,
06:58I'm not changing, so it'll be from John to you.
07:00Okay? So that's basically a default where it,
07:02it only takes the default if no value is sent in.
07:06Now, beyond that, in E, another lovely thing in E ES six is
07:10that I don't have to use all these pluses.
07:13Instead of these pluses, I'm gonna go around
07:15and put a dollar sign, curly bracket, curly bracket,
07:19close it, basically setting my variable.
07:22Let's do it again also to this two.
07:24And there's one more exception right now
07:26and let me just curly bracket, curly bracket open, close
07:31with that dollar sign.
07:32Now this is not gonna work straight on,
07:34although I think in the real version of uh, ES six,
07:37whenever it's gonna come out, it
07:38should actually run as well.
07:40Not a hundred percent sure if it will or not,
07:42but right now you'll see that it's only coming out
07:43as this string currently at this point in time for you
07:46to actually get it to work, you can't send
07:48that double quote or regular quote.
07:50Instead of that, you can use the quote, the quote
07:53that is right next to your numbers.
07:55So right, 1, 2, 3, 4, 5.
07:56So right to the left of it, you'll have there a quote
08:00and if you wrap it around in this weird quote,
08:02the not normal quote, and you go back into your application
08:05and click on refresh, it's going to know
08:07that it should expect to get these interesting variables
08:10inside and only then those variables would work.
08:14So that's basically ES six.
08:16Now what's beautiful about this application
08:18that we've just created, if I just go into my application
08:21here and click your on refresh and look into my index js
08:26and search for that, um, hey function.
08:29So lemme just search you for function.
08:31Hey, you'll see that it automatically transcribed it
08:34and made it e es six friendly.
08:36So you can see all this stuff is now built the way
08:38that it would work, and ES five automatically transcribing
08:42it down from E ES six.
08:44Now if you want to play around with these stuff,
08:46you could just go and, and include it into the
08:48links of this chapter.
08:50You could literally just go
08:51and play around with some of the E es six features
08:53and see them in action, making sure
08:55that you're configuring which
08:56of the features do you want currently
08:58to have in your browser.
08:59So for example, in our case, if I just created those
09:03variable, uh, let me get, let me create that function name.
09:07Name two equals my name.
09:11Let's just say just for the sake of the most silly example,
09:14you'll see that it will automatically show you
09:16that conversion, which is doing Otto on the
09:19fly on the other side.
09:21So that's it. That
09:22Is the basics of how you configure
09:24and set a babble to be able to work with e ES six.
09:28And this lecture we learned how to create a transcribe
09:31or how to work with the Babel transcriber with Webpac
09:35enabling us now to start developing an ES six In the next
09:39lecture, we'll continue down this path
09:41and actually start using some E ES six NJSX.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!