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

Adding Support for ES6 and JSX with BabelJS

In modern JavaScript and React development, writing in ES6 and JSX is essential for building scalable and maintainable applications. However, not all browsers natively support these features. In this tutorial, we’ll configure BabelJS with Webpack to transcribe ES6 and JSX into ES5, ensuring broader compatibility. By the end of this session, your environment will be fully prepared for React development.


Steps to Add ES6 and JSX Support with BabelJS

1. What is BabelJS?

BabelJS is a JavaScript transpiler that converts newer JavaScript syntax (ES6 and beyond) into ES5, which is supported by most browsers. Babel also supports JSX, a React-specific syntax that simplifies component development.

To enhance compatibility with older browsers, Babel can use polyfills, which fill in missing functionality for browsers that don’t fully support ES5.


2. Install BabelJS and Required Packages

Run the following command to install Babel and its required dependencies:

npm install --save-dev babel-loader @babel/core @babel/preset-env @babel/preset-react @babel/polyfill  

Package Breakdown:

  • babel-loader: Integrates Babel with Webpack.
  • @babel/core: The core Babel library.
  • @babel/preset-env: Transpiles ES6+ syntax to ES5.
  • @babel/preset-react: Adds support for JSX syntax.
  • @babel/polyfill: Provides missing functionality for older browsers.

3. Configure Babel in package.json

Add a babel section to your package.json file:

"babel": {  
  "presets": ["@babel/preset-env", "@babel/preset-react"]  
}  

This configuration tells Babel to use the preset-env for ES6 and preset-react for JSX.


4. Update Webpack Configuration

Edit your webpack.config.js file to include Babel:

module.exports = {  
  // Existing configurations  
  module: {  
    rules: [  
      {  
        test: /\.js$/, // Targets JavaScript files  
        exclude: /node_modules/, // Skips transpiling node_modules  
        use: {  
          loader: 'babel-loader' // Transpiles JS files using Babel  
        }  
      }  
    ]  
  },  
  entry: ['@babel/polyfill', './src/index.js'], // Includes the polyfill  
  // Other configurations  
};  

Key Points:

  • test: /\.js$/: Ensures only .js files are processed by Babel.
  • exclude: /node_modules/: Skips unnecessary files in the node_modules folder.
  • @babel/polyfill: Ensures older browsers can run your application.

5. Verify the Setup

To test the configuration:

  1. Create a new file in your src/ directory (e.g., src/index.js).
  2. Add ES6+ and JSX code:
const App = () => {  
  const message = "Hello, ES6 and JSX!";  
  return <h1>{message}</h1>;  
};  

console.log(App());  
  1. Run the Webpack development server:
npm start  
  1. Open your browser and check the console to ensure the output is correctly transpiled.

Key Takeaways

  • BabelJS enables modern JavaScript and JSX to run on older browsers by transpiling them to ES5.
  • Using Webpack with Babel loader automates the transpilation process.
  • Polyfills enhance compatibility for browsers that don’t fully support ES5.

In the next tutorial, we’ll create our first React component, using the ES6 and JSX features now available in our environment.

Video Transcript

00:00We are almost ready to start doing react things.
00:03But before we do that, we wanna set up ES six and JSX.
00:06And to do that we're gonna use Babel.
00:08Babel is a transcriber that enables you to transcribe a lot
00:12of different versions of JavaScript
00:13and features down into ES five.
00:16ES five is really very common on most modern browsers,
00:19which is basically the common version.
00:21And the year 2016,
00:23while ES six is probably gonna be the common version in the
00:26year 2017 or 18.
00:29Yet from now until then, we wanna make sure
00:31that people will be able to run our site.
00:33So what we're gonna do is we're gonna code in ES six,
00:35but we're gonna use babble with the help of loaders
00:38that are gonna be plugged into Webpack
00:41to basically transcribe it down to ES five.
00:43Now you might be thinking there are browsers
00:45that don't even support ES five, and that is true.
00:48So to be able to solve that problem,
00:49we're gonna use a babble polyfill,
00:51which is basically a filler that
00:54fixes some browser ca compatibility
00:56and extends the features of JavaScript on browsers
01:00that don't have all the JavaScript features.
01:01Now, it's not gonna be a holistic complete support,
01:04but it's gonna add support to older browsers,
01:06making more browsers capable
01:08of watching your site even if they're older.
01:11With that said, let's see it. Let's set it up.
01:15Although there are a lot of ways to build React components,
01:18the most common way is with ES six currently,
01:22which is the latest approved version of JavaScript
01:25that is not fully supported in browsers and ota.
01:29Lemme just, I'm breaking out of the server
01:30'cause I want to go ahead and install a few packages
01:32as we talk about what they are.
01:34So first of all, the type of packages
01:36that we're gonna be installing are gonna be
01:38for the development itself
01:40and really gonna be used by Webpac and not by anything else.
01:43So it's gonna package and and prepare everything for us.
01:47And the packages that we're gonna be using is gonna be
01:50babble packages.
01:51And the first one is gonna be the bale loader.
01:54That by the way, anytime you see dash loader 99.9%
01:58of the time, it means it's a web package library
02:01that basically enables you to load things up using Webpack.
02:06Now bale generally when you see that relates
02:09to converting code from one version to another,
02:13mainly in JavaScript.
02:14Now, the two type of things that we wanna convert,
02:17'cause we wanna be, we wanna code in E ES six is we wanna
02:20convert it down to ES five.
02:22Now Babel does that automatically, as long as we define it
02:26and we let it do so.
02:27So when we're installing ba,
02:28it doesn't really do anything on its own.
02:31Then we have to go and add Babel preset.
02:33And in this case we want to go to E ES six,
02:35which is ES 2015.
02:38I know it's kind of weird because the names are currently
02:40changing, but E ES six is the name
02:42and the next version will be E ES seven.
02:45The next package that we're going
02:46to wanna install is gonna be Babel preset.
02:50And this preset is gonna take JSX, which is a language
02:54that has been created or you could say en encouraged
02:57or modified by
02:59Facebook and used within React.
03:01And literally what that does is basically converts some,
03:05some of your code in React to be a lot more easy
03:09for uh, development.
03:10And beyond that oto, it enables us to type in JSX,
03:15which you're gonna look at
03:16or literally in this lecture as well.
03:18So again, we're loading Babel Loader itself,
03:21we're installing that, which is gonna be used with Webpac,
03:24and then we're defining the presets that Babel is going
03:27to use or installing them E ES 2015 and React.
03:31Last but not least, I'm also going ahead,
03:33I'm gonna install a poly fill of Babel.
03:37Now what that does is basically for browsers that are older
03:41that don't support ES five,
03:43because all of this stuff is transcribed down into ES five,
03:47what actually is gonna happen is
03:49whenever we add the poly fill as well,
03:51it will add a little bit of extra stuff into the JavaScript,
03:54making sure that browsers that are kind of support ES five
03:58but don't fully will have greater support of it.
04:01So let's go ahead and install it.
04:03And as it's being installed, I wanna go through the process
04:06of setting up and configuring Babel as well.
04:09So to do that, I'm gonna go into my package JSO
04:13and you can find any empty space really in your root area
04:17of your J-J-S-O-N.
04:18And I want you to add Babel, by the way,
04:20there's a few different ways
04:22of doing this even in an external file,
04:24but just for the sake of keeping things organized here, and
04:28because we don't have that much going on here, I'm gonna put
04:31that all inside of our JSO, our package, JSON,
04:35and I'm gonna add the presets,
04:37and let me just put this in a double quote
04:40and I'm gonna go ahead and set this as an array.
04:43Now this array will basically list out all the presets
04:47that were in the process of installing,
04:48which the first one was ES 2015, which is ES six.
04:53And the next one is React, which will give us support
04:56for JSX.
04:58So there we go, we could save that.
04:59And that's all we have to do from the configurations
05:01of Babel, but we're not done yet
05:03because Babel needs to also be called and triggered to run.
05:07To do that, I'm gonna go inside of my web pack
05:09because web pack is gonna be doing that.
05:12And in my web pack configuration,
05:14what I wanna do is I wanna go inside
05:17and actually add inside another module that it's going
05:21to actually run.
05:23So to do that again, I'm gonna add another object inside
05:28of our export and I'm gonna add the module object.
05:32And in that module object, I'm gonna go ahead
05:36and add here a few loaders,
05:38but in this case really only one loader.
05:40But here I would put all the different loaders
05:43that we are gonna add to Web Pack.
05:45Now, what are loaders?
05:47So generally speaking, a loader
05:49and web pack is a package that is then
05:52whenever we're grabbing these files from j,
05:54the Client Digest or any other place,
05:57It will basically run through and do something to them.
06:00In this case, what I wanna do is I want to define a test
06:03to basically tell Webpac whenever it approaches a file,
06:06whenever it recognizes that specific file
06:09or specific rule in that file,
06:11then go ahead and do something.
06:13In this case, I want to go to anything that is a JS file,
06:16and this is just a j uh, a a um,
06:19a RegX rule that I've put here.
06:21And this is a very simple rule that all it says
06:23that it's a JS file.
06:25Now, I also wanna exclude anything that lives inside
06:29of node modules
06:30because our node modules don't need to be converted.
06:34So if we're using a node module, then we don't want it
06:37to be converted as well.
06:39So I'm putting your Rex really,
06:40but a very simple Rex that literally just says
06:43exclude node modules.
06:45So we're not gonna be, um,
06:47transcribing anything inside of that folder.
06:50Last but not least, we want tell our loader
06:52where does it want, what, what's gonna be doing,
06:55what will we do in that scenario?
06:57Now, as I told you that anything that ends
06:59with the word loader, and lemme just double check
07:01that we're done loading
07:02and we are, if I go back into my package,
07:05and by the way, one thing that I did
07:07that was a massive mistake is in the process
07:09as things were installing, I installed my Babel presets.
07:14And because I've done that, I overrode the file change
07:17that was actually happening in parallel
07:19as it was being installed, but no big harm done.
07:22I'm just gonna run it again.
07:23Hopefully it'll take a little,
07:25it will be a little bit faster.
07:26But in that process, I could again talk about
07:28that Babel loader, that dash loader,
07:31and that's really what I'm gonna be using for our loaders
07:34inside of Webpack.
07:35And in this case I want to use Babbel loader, but
07:38because it is assumed
07:40and is automatic that it's a loader, you don't have
07:42to w write here loader Webpack will know
07:45that when you type your babbel, it will search
07:47for the loader.
07:48So all that's left for me to do is go ahead and save this
07:52and while it's installing,
07:53because we made that mistake, oh, and it's done.
07:55Beautiful. So let me just double check
07:57that our package has been updated
08:00and we can definitely see in our dev dependencies we have
08:02more dev dependencies, which is beautiful.
08:05So last item that's left for us to do
08:08before we could test things out is to basically make sure
08:12that we're O2, including our Babel polyfill.
08:15Now, because our ba Babel polyfill is not meant
08:18to run on specific files, it's actually meant
08:21to just be added into our JavaScript.
08:24I'm gonna go into my entries
08:25and instead of having here a string,
08:28I'm gonna send in an array
08:30and set up our first parameter to be our Babel polyfill.
08:34And I'm just gonna go ahead and type your Babel polyfill
08:38and what will happen, it's gonna add
08:39that into our final outcome.
08:41So it's gonna grab this file and anything that it calls
08:44and grab this file and anything that it calls.
08:47So if I go ahead and save this
08:48and start up my server again by,
08:51by typing NPM start and
08:54It runs once our server will run.
08:56If we go into our index file, first
08:58of all, it's gonna be massive.
08:59It's gonna be a lot bigger
09:00because it's gonna include also the polyfills of,
09:04of, uh, of Babel.
09:05And it's also gonna include our entry.
09:07And you can see this is never ending in oso, the code
09:10of webpac that will restart things.
09:12And again, these files are not gonna be the final files you
09:15use in production, but these are your
09:17development environment files.
09:19So once you're ready to production,
09:20you would work in a little bit different way,
09:23but you can see that everything here refreshed,
09:25everything here is working.
09:27So now we have everything we need from a setup
09:29and configuration because now we support.
09:32And by the way, I wanna show this to you.
09:34If I go inside of my client here,
09:36I'm just gonna create here a function.
09:39And let's just go ahead
09:40and use something that is new to E ES six.
09:43And I'm gonna set here just a let
09:45and let is a little bit different than a variable.
09:48It's very similar. We'll talk about it further
09:50shortly, but it's not the same.
09:52And in any case, it's not supported in ES five.
09:55So I'm gonna go ahead and just set it as let and save it.
09:58And now with our configuration with our server running,
10:02if I go back into my application year
10:04and automatically refresh,
10:05we'll see hello 2.0, be beyond that.
10:07If I go ahead and search here for that hello 2.0,
10:12you'll notice that a Avar is here instead of our lab
10:16because in ES five lets don't exist.
10:18So they were automatically transcribed and replaced,
10:21and now we're ready and configured
10:23because now we have ES six
10:25and JSX support JSX.
10:28And we'll take a look at what that actually even is.
10:30In the next lecture when we create our very,
10:33very first component in React.
10:36In this lecture, we set up Babel loaders
10:40to be loaded into webpac, to enable us to type ES six
10:44and JSX,
10:45and have it transcribed down to ES five
10:47as we're adding Polyfills to them.
10:50In the next lecture, it will be finally time for us
10:52to create our first React component.

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!