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

Setting up Babel 6 the high performing way

Setting Up Babel 6 the High-Performing Way

In this lecture, we delve into configuring Babel 6 with Webpack to optimize React applications by transcribing modern JavaScript (ES6) and JSX into ES5 for compatibility with browsers.


Key Concepts Discussed:

  1. What is Babel?

    • Babel is a JavaScript transcriber that converts ES6+ and JSX code into ES5 to ensure compatibility with older browsers.
  2. Integrating Babel with Webpack:

    • Webpack uses loaders to process files during the build process. Babel Loader is used to integrate Babel with Webpack.
  3. Required Packages:

    • babel-loader: Connects Webpack with Babel.
    • Presets for specific transcriptions:
      • @babel/preset-env for ES6 to ES5 conversion.
      • @babel/preset-react for JSX to JavaScript.
  4. Basic Webpack Configuration:

    • Define entry and output points in webpack.config.js.
    • Set up Babel as a loader to transpile JavaScript files.
    • Exclude unnecessary files (e.g., node_modules) from transpilation.

Hands-on Steps:

  1. Install Babel Loader and Presets:

    npm install --save-dev babel-loader @babel/preset-env @babel/preset-react
    
  2. Update Webpack Configuration:
    Add the following to webpack.config.js:

    module.exports = {
        entry: './src/client.js', // Starting file
        output: {
            path: __dirname + '/dist',
            filename: 'bundle.js',
        },
        module: {
            rules: [
                {
                    test: /\.js$/, // Process all .js files
                    exclude: /node_modules/, // Exclude dependencies
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: ['@babel/preset-env', '@babel/preset-react'], // ES6 and JSX
                        },
                    },
                },
            ],
        },
    };
    
  3. Add Babel Configuration:
    Alternatively, include Babel presets in package.json:

    "babel": {
        "presets": ["@babel/preset-env", "@babel/preset-react"]
    }
    
  4. Run the Development Server:
    Add a script to package.json for starting Webpack Dev Server:

    "scripts": {
        "start": "webpack-dev-server --open"
    }
    

    Start the server:

    npm start
    

2024 Updates & Best Practices:

  1. Move to Babel 7:

    • In 2024, Babel 7+ is the standard. Update packages accordingly:
      • Replace babel-loader with the latest version.
      • Use @babel/preset-env and @babel/preset-react for modern configurations.
  2. TypeScript Integration:

    • For better type safety, consider using Babel with TypeScript (@babel/preset-typescript).
  3. Modern Alternatives:

    • Vite.js and Parcel are faster and simpler alternatives to Webpack for modern React projects.
  4. ES Modules:

    • Leverage native ES Modules (import/export) for bundling where possible, reducing the need for Babel in newer environments.
  5. Improved Polyfill Management:

    • Use tools like core-js selectively to add only the required polyfills, optimizing bundle size.

With this configuration, your React applications are now ready to leverage ES6 and JSX while maintaining compatibility with browsers. Next, we’ll explore performance enhancements with ES6 constants.

Video Transcript

00:00In the last lecture, we started configuring Webpac
00:04and talked about the differences between dependencies
00:06and dev dependencies.
00:08It started for us to continue setting up Webpac,
00:11and for that we're gonna use Babel six to transcribe all
00:15of our code, which will be an E es six into ES five.
00:18So Babel is a transcriber,
00:20and for Webpac to be able to work with it,
00:22we're gonna look at how to load what we need
00:25to load in Babel, how to configure it with Webpac.
00:28And although there are multiple ways to do it,
00:30we're gonna show you the Webpac way
00:32in our development environment currently,
00:34and as we progress throughout the title, we're all
00:37to change it and show you the different configurations
00:39and when are they best used.
00:41So with that said, let's see how to set up
00:43and configure Babble and Webpack together.
00:47Alrightyy, let's start configuring Webpack.
00:49And to do that, I'm gonna go into the Webpack config file,
00:53webpack config dot js file to be more accurate.
00:57Now in node js, if you're unfamiliar, the way
01:01that files usually work
01:03whenever you're sending files from one place
01:05to another is using modules.
01:08Now, modules in are derived out of common js,
01:12and they're built basically by defining a module exports.
01:17Now, by the way, I'm using NPM with quite a late version.
01:20So it's uh, six plus a version that is relatively up
01:23to relatively up to date, more technically speaking, um,
01:26on user using node version, uh, 6.3 something.
01:31Now in six point something, there's already
01:34some rudimentary support to ES six.
01:37There's not full support to E es six,
01:40but there's partial support to ES six already built in.
01:43So I could already start using E es six things directly
01:46inside of here, but I'm still going
01:48to use the common GS structure, which basically says that,
01:51um, I, I have your, uh, module
01:53and I'm exporting whatever's content within here,
01:55which is basically this object, which then webpac,
01:59the dev server will be able to fetch this,
02:01or Webpac will be able to, to fetch this
02:03and create the process that it needs.
02:05Now in the documentations of webpac, which is
02:07beyond the scope of this title,
02:10you'll find all the information of why, what is needed.
02:13But in this case, I need to send you an entry point,
02:15which this entry point is gonna be the starting file
02:18of our client js.
02:20Now, because we're working in our client js using imports
02:24and using all these new features
02:25and external files,
02:27what's actually gonna happen is Webpac needs to go
02:30through each file
02:31and concatenate, well combine it all together into
02:34a singular file.
02:36And what I'm starting with is basically saying
02:38to start off from our source file, which is called client
02:42js, and fetch all the files that it needs
02:44and bundle them up as you bundle them up,
02:47then put them into this output.
02:49In our case, I'm gonna say the path of that output.
02:52I wanna set it to be, um, virtually in the current folder
02:57that I'm in and my file name, I'm gonna go ahead
03:00and set you're a file name, which I'll call index js.
03:03Really technically I could set you're the path
03:05to be basically our root path, which basically will say,
03:08Hey, I want your output to be whatever that file is
03:11to output on our server in the index js file.
03:15Now, technically inside of our public file in our html,
03:19we're already loading up this index js.
03:22So assuming that this file will be loaded up, we could
03:26reference that route directory or
03:27because we're in the roots of we're good,
03:29but it would, it should save it
03:31and be load it up into our browser.
03:34So for the basic constructs of of webpac,
03:37this is really all we need.
03:38We set an entry point and we set an output, but
03:41because we're also gonna be leveraging
03:43and using the webpac dev server to build a server for us,
03:47because in our first sample initially,
03:49we're not gonna have our own server
03:50that we're gonna configuring.
03:52So we're just gonna add temporarily into this dev,
03:56into this configuration,
03:58which will remove in a later chapter.
04:00I'm gonna go ahead and say a few things.
04:01First of all, I'm gonna say it that I want it to be in line,
04:04which means that whenever we make changes
04:06to our client code, anything in our entry,
04:09then it will recompile it and refresh it for our browser.
04:12And I'm gonna go ahead and set our content base
04:15because it's gonna be our core server.
04:17I'm gonna go ahead and say your public folder is gonna be
04:21our public folder here.
04:22So anything that's inside
04:23of the public folder will be visibly available in the root
04:27of the directory.
04:28So in this case, if I go to HT index at html,
04:31it would find that file.
04:33Last, but not least, at least I need to also set a port
04:36for our developer environment.
04:38And once I do that, okay, I set up myself,
04:41my basic configurations for my server as well.
04:45Now, technically speaking, this is really all I need
04:47to be able to start up a dev server,
04:50although it's not really gonna work really well if I go
04:53ahead and save this and go into my, uh, package.
04:57Do JSO I'm gonna go ahead here
04:58and just add another script here.
05:00And I'm gonna just gonna call this script,
05:03I'm gonna call this, um, start, which is a, a a script
05:06that will enable us to start up our, our server in our case.
05:12And for that, I'm just gonna go ahead
05:13and call the webpac dev server
05:15and let me just grab the name to be accurately.
05:18So I want to grab this module
05:20and basically tell it to initiate itself
05:22to start up our server.
05:24Now, once I've done this, if I go into my terminal,
05:28and let me go ahead here
05:29and I'll just type NPM start,
05:31what it's actually gonna do is it's gonna fetch this command
05:34and call the webpac dev server, which is one of the modules
05:37that are we've installed in our dev dependencies,
05:40which should then call by default, our web pack config,
05:44fetch out all that information
05:46and start up our server on Port 3000.
05:49Now, it shouldn't work
05:50because there's a few things that we still need to do
05:52that are not configured correctly.
05:55In this case, it's telling us that our dev server,
05:58i i is incorrect, which means
06:00that we have your a different error is that inside
06:03of our Webpack config.
06:04I didn't put here a comma, I i I created invalid JavaScript.
06:08And if I go ahead here and just give it another shot,
06:11then our server should start up,
06:14but our content should not run.
06:16And you're gonna see an un unexpected token
06:18unexpected information.
06:20Part of this unexpected information is directly assigned
06:24to the fact that we're using a lot of modern features
06:27and a lot of elements
06:28that are not supported natively by browsers.
06:31So we're automatically generating an error in our JavaScript
06:34is not good because we really need to modify our JavaScript
06:38and make it e es six enabled.
06:41Now that's exactly what we're gonna do in this lecture
06:45and I'm gonna go ahead and stop this
06:46and clear everything out.
06:48But I'm gonna go ahead and I need to install a few things.
06:51So I'm gonna go ahead and NPM install and I'm gonna go ahead
06:54and save to Dev because this is part
06:56of the development process that we need to install.
07:00Now the first thing that we want
07:01to install is a transcriber, which is called Babble Loader,
07:05which hopefully you've worked
07:06with already previously if you've been working with React.
07:10So I'm gonna go ahead
07:11and you might have been working
07:13with an older version of React.
07:14So let me just walk you through this.
07:16I'm loading up a babble loader.
07:18Now, loaders generally,
07:20whenever you see anything that says loader are associated
07:23with webpac normally.
07:25And what this says, this is a connector for webpac
07:28to enable it to, to create, um, transcribes
07:32of Babel, which basically convert different versions
07:35of JavaScript into ES five JavaScript.
07:38So lemme go ahead and, uh, in Babel six,
07:41which is the version that we're, we're gonna be working
07:43with, we need to define presets
07:45and literally tell Babel what is it going
07:48to actually transcribe.
07:49So one is we're gonna transcribe from e ES 2015,
07:52which is e es six into ES five.
07:55And we're also gonna transcribe from
07:57React related JavaScript,
07:59which is namely JSX into native ES five.
08:03Okay? So those are the core library elements that we need
08:07to be able to activate our Babel.
08:09And I'm gonna go ahead here and install this.
08:11And as this process is being installed,
08:14let's go ahead into our JS package.
08:17And what I wanna do is I want to not Jason, sorry,
08:19into our web config.
08:20And what I want to do is I want to go ahead
08:22and add our first loader up.
08:24Now, these loaders,
08:25their main role is basically gonna be mul multiple.
08:29One is that we want to basically make sure
08:31that any JavaScript file that is being used
08:33as it's being loaded,
08:35that it will automatically be transcribed into basically
08:40a version that is ES five compatible.
08:43To do that, I'm gonna go ahead and load up a few loaders.
08:46First of all, we're gonna set here the module parameter,
08:50and for our mount module parameter, I'm gonna go ahead
08:52and add the loaders.
08:53This is, it's a part of that built in documentation,
08:56which expects basically an array
08:58of different loaders that could be used.
09:00And literally in this area of the loaders,
09:02we're gonna be putting all of the things
09:04that we're gonna be putting in for optimization as well.
09:07Although right now what we're doing is we're making sure
09:09that we could work with E ES six,
09:11which is all our code is ES six and also JSX.
09:15And we're basically enabling it.
09:16So let's go ahead and enable it.
09:18And the process is quite straightforward.
09:20There are three parameters that we really want to define
09:24or could define more accurately and normally would define.
09:27The first one is gonna be what do we want?
09:29What type of files do we want to be able to do this,
09:32use this loader on?
09:33And in this case, what I wanna do is I basically want
09:36anything that is a JavaScript file, anything
09:38that is a JS file to run through our loader.
09:41Now, what loader do we want to use?
09:43Well, the loader that we want to use is the Babel loader.
09:47Now the baba loader is literally defined using
09:51the word Babel.
09:52Now if you're wor wondering why Babel,
09:55it's basically like babble,
09:57not babble, babble a bale loader.
09:58Why? Because just a minute ago,
10:00we've installed into our dev dependencies, the bale loader,
10:03and as we said, loaders that are used for uh,
10:06web pack are usually end, end with the name dash loader.
10:10So in this case, what I'm basically doing is I'm telling my
10:12loader to use the babble loader,
10:14but I could only type, I could type only
10:15babble, which we'll do as well.
10:17Uh, webpac is smart enough to realize that what,
10:19that's what we actually mean.
10:21Last but not least, I'm also gonna exclude from packaging
10:25anything that is a node module
10:26because we don't need, it takes too long for us.
10:29And technically speaking,
10:31it's just not needed, not warranted.
10:33And what I'm gonna do is go ahead
10:34and just exclude anything that is a node module
10:38and anything that isn't a node module
10:39that is basically not going to touch it
10:41because really in our case, we only need to transcribe
10:44anything that's actually code that we created.
10:47So in this case, anything that's sitting in
10:49that folder means it's a code that we didn't create
10:51so we don't have to worry about transcribing it.
10:53Now, last but not least, on contrary to earlier versions
10:57of Babel earlier than version six, we didn't have
11:00to explicitly tell Babel what should it be using it.
11:03Since then, we do now there is a public,
11:06there is a config file that we could put to default
11:09for Babel, but in our case, I'm just gonna go ahead
11:11and set a configuration within our package, do js ON,
11:14and just right here underneath our main,
11:16but really doesn't matter where as long as it's one
11:18of the root elements inside of our config.
11:20I'm just gonna go ahead and define a new babble parameter.
11:23And I'm gonna go ahead and,
11:25and define inside of this parameter the values
11:27that I want Babel to be aware of.
11:30So the, the values are basically the only thing I care about
11:33in my case is just presets.
11:35And I'm gonna go ahead for my presets.
11:37I'm literally just gonna send your an array.
11:39And in this array I'm just gonna go ahead
11:41and I'm gonna list out the presets that I'm using.
11:44In this case, I'm using the ES six, the ES six preset,
11:48which is called E ES 2015.
11:50And I'm using the JSX preset, which is called React.
11:54And let me just make sure that my code is valid.
11:56I just deleted that comma out.
11:59And once I deleted that comma out, let's go ahead
12:02and try to start our server.
12:03And I'm gonna call it NPM start.
12:05And you'll see that our server this time around will run
12:08and will start even though our code is still incorrect.
12:11And part of it is
12:12because we're still doing a few things
12:14that are just not acceptable.
12:16And among them, we're dynamically loading CSS
12:21and SAS that we basically don't really
12:26have support for right now.
12:27And we need to add that support, which is exactly
12:29what we're gonna do in the next lecture.
12:35In this lecture, we configured Babel to run
12:38with Webpac as a loader.
12:41In the next lecture, we're gonna talk more about a little
12:43bit of performance with the introduction
12:46of ees six constants to show you
12:48how Babel transcribes our code down into ES five.

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!