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

Configuring the Webpack Development Server

Transcript Converted into Tutorial (Markdown Format)

# Configuring the Webpack Development Server  

In this tutorial, we’ll configure the Webpack Development Server to enable **hot module replacement**. This setup allows your browser to refresh automatically whenever you make code changes, saving you time and improving your development workflow.  

---

## **What You’ll Learn**  
By the end of this tutorial, you’ll know how to:  
- Add and configure the Webpack Development Server.  
- Set up entry and output points in Webpack.  
- Enable real-time browser updates for your React applications.  

---

## **Prerequisites**  
Before you begin:  
1. Ensure Node.js and NPM are installed.  
   - Check by running:  
     ```bash
     node -v  
     npm -v  
  1. You should already have a basic Webpack configuration in place.

Steps to Configure the Webpack Development Server

Step 1: Install the Webpack Development Server

Install the server as a development dependency:

npm install --save-dev webpack-dev-server  

Step 2: Update the Webpack Configuration File

Modify your webpack.config.js file to include the development server setup:

const path = require('path');

module.exports = {
    entry: './src/client.js',  // The entry point of your app
    output: {
        path: path.resolve(__dirname, 'public'),  // Output directory
        filename: 'index.js',  // Output file
    },
    mode: 'development',
    devServer: {
        contentBase: path.resolve(__dirname, 'public'),  // Base folder for the server
        port: 3000,  // Port number for the server
        inline: true,  // Enable automatic refresh
    },
};

Step 3: Create Required Files

  1. Create an entry file:

    mkdir src  
    touch src/client.js  
    
  2. Add a basic script in client.js:

    console.log('Hello from Webpack!');
    
  3. Ensure your public/index.html includes the output JavaScript file:

    <script src="index.js"></script>
    

Step 4: Add a Script to Start the Server

To simplify running the server, add a start script to your package.json:

"scripts": {
    "start": "webpack serve"
}

Now, you can start the server with:

npm start  

Step 5: Test the Setup

  1. Run the server:
    npm start  
    
  2. Open your browser and navigate to http://localhost:3000.
  3. Open the browser’s developer console to verify the message from client.js.

Enabling Hot Module Replacement

Hot module replacement ensures that any changes in your code are reflected immediately:

  1. Update webpack.config.js:
    devServer: {
        contentBase: path.resolve(__dirname, 'public'),
        port: 3000,
        inline: true,
        hot: true,  // Enable HMR
    },
    
  2. Restart the server and test by modifying client.js. Save the changes and watch the browser update without a manual refresh!

Key Takeaways

  • The Webpack Development Server simplifies React development by automating browser refreshes.
  • Setting up entry/output points in Webpack organizes your code for scalable projects.
  • Hot Module Replacement (HMR) significantly enhances productivity during development.

Next Steps

In the next tutorial, we’ll integrate Babel to enable ES6 and ES2016 support in your React applications.


Resources


This structured tutorial guides users step by step through setting up the Webpack Development Server, making it beginner-friendly while incorporating advanced tips like Hot Module Replacement. Let me know if further refinements are needed!

Video Transcript

00:00In the last lecture we configured our web pack
00:03environment, but we want to continue configuring it
00:06as we include into it the web pack development server.
00:10So through a few configurations of web pack
00:13and the inclusion of the web pack development server,
00:16it will enable us to create a hot refresh,
00:19which basically means
00:20that anytime we change something in our code,
00:23automatically our code will compile or or prepare itself
00:27and then re remove that need for us to refresh
00:31and the browser and reconfigure our server.
00:34So we'll automatically refresh
00:36so our browser will automatically give us the content
00:38that was updated, which will save us a lot of time.
00:41So let's jump right into it.
00:44So model module express is not a E ES
00:48six standard.
00:49It is actually a standard that it comes out of common js.
00:53If you've heard of that package, which was endorsed by
00:56no js, we're gonna see
00:58how you do this in ees six in a later video.
01:01But generally speaking, I just want you to know
01:03what this is when you see it.
01:04What this is, is basically says is
01:06whenever you're loading in external files, such as this case
01:09where we're loading up our web pack
01:11or web pack is loading this file to gain access to
01:15configurations it looks for
01:17or it only has access to whatever was exported, anything
01:20that is placed up above here
01:22or below that's not exported, you will just not,
01:24it won't have access to.
01:26So literally all this is, it basically says is anything
01:28that's here is globally
01:29or publicly available to anyone that loads this file.
01:33Now there's a few different configurations that we want
01:35to put in here and the first is our entry point.
01:38And our entry point is basically the starting file
01:41for our client side JavaScript application that we want
01:44to package up for Webpack.
01:46And in this case where it's gonna be inside of our source.
01:48And just so it's very clear, I'm gonna call it client dot j
01:51and we'll create that file in a moment.
01:53And we also want to give a direction of the output itself
01:57so we know where our starting point is.
01:59And this file might load up a few different files,
02:01which it will throughout this chapter as well
02:03and in general throughout our course.
02:05And we want to set it and say, okay,
02:07what path do we want to put it in?
02:09In this case, just to keep things simple,
02:11I'm gonna put it into our public folder
02:13and we're gonna also drop in a lot
02:15of other things into that public folder.
02:18And I'm gonna set the file name
02:19that while our final file name will be called
02:22and I'll just call it app js.
02:24So that's gonna be our application
02:25or you know what, let's keep it close to the index file,
02:29let's call it index js.
02:31Okay, so now that we've said that that is the basics,
02:33there's a lot of other things that we probably want
02:35to configure and set, but even
02:36before that, let's just make sure that, make sure,
02:38and I kind of like in the magic of internet, I went ahead
02:42and I kind of like already set it
02:44while you weren't noticing.
02:45Um, oh, that's not that. Let me find the right folder.
02:49Here we go. So, so basically
02:51what I did is I grabbed from our starter file,
02:53our public while you didn't notice.
02:56So now we have all of our basic HTML files.
02:59So let's go ahead and also create that script, file
03:02that client, that script.
03:03So I'm just gonna go jump into my terminal for a second
03:06and I'm gonna ask to touch our source,
03:13uh, client js.
03:14So we'll have that file
03:15and let's just put something very simple inside there.
03:18So I'm just gonna go into that file in my editor
03:20and I'm just gonna do a console log
03:23and we're gonna log in, um, hello from webpac
03:29and let me save this now even
03:30before we're done with this, now that we have what's our
03:32HTML file, just go into that HT ML file, scroll all the way
03:37to the bottom of the page
03:38and make sure that if you don't have it already, make sure
03:41to add your index js.
03:43I already did that before
03:46'cause I was replacing all the JavaScripts,
03:47so it's expecting to find a index js in it.
03:50Perfect. So now that it has all that configured,
03:52let's go ahead and configure our
03:54server to configure our server.
03:57I'm gonna do it exactly in the same place inside
03:59of my web pack config.
04:00And I'm gonna continue
04:01and set oso the dev server configurations
04:05and the dev server configurations.
04:06We're gonna set three different configurations.
04:08The first one is what folder is our base folder,
04:12our content folder.
04:13And in this case, we already know what it is.
04:15So our base folder is gonna be basically the public folder.
04:18That's gonna be our route directory or our web route.
04:24The next thing that we're gonna do is we're gonna set a po
04:26uh, port and I'm gonna set the port to be 3000.
04:29But you really could set it to any number
04:30that is not being used currently by your system.
04:33Usually 3000 is not used.
04:36And I wanna do one more thing
04:37before we wrap it up is I want to go
04:39and I'm gonna set inline true.
04:41Now, by setting this variable, it's gonna make sure
04:44that our server will refresh
04:46and recompile anytime we change something in our files
04:49and oats, a refresh our browser.
04:51Crazy cool stuff. So let's go ahead and save that
04:54and to be able to run our server and check it out.
04:56Now that we have our web pack configured
04:59and our web pack dev server ossa installed both of them,
05:02it's a bit of a pain.
05:03If you wanted to actually access them directly from
05:06terminal, you would have to type their, um, node js modules
05:11and you'd have to look for web pack and dev
05:15and server and then dot bin probably or bin
05:20and then uh, web pack, dev server
05:23or so on and so forth and so forth.
05:25But it's not really worth it because
05:26what you wanna do is you want,
05:28and the easiest way to work around with things is by,
05:31by the way, don't do it until the installation is complete,
05:33because while the installation is going on,
05:36your web pack will refresh.
05:37So make sure that you go into the file and it's fresh
05:40and you see that your two installations are created.
05:43Now notice by default in our script,
05:45we have your attest, we don't need that.
05:47So we could delete it. And instead of
05:49that we're gonna put our own script.
05:50And our own script that we want
05:52to put in is incredibly simple.
05:53We're gonna type your start and the value is
05:56Just gonna be web dev server.
05:58Now because this app, this is living within,
06:02it's living within the server, it's living within node js,
06:05it's living within all these access, it has access
06:07to all the node modules that are installed.
06:10And in this case it gives me access.
06:11I don't have to type all these long paths.
06:13I could just literally just create these short
06:15scripts commands.
06:17Once I have the short script command,
06:19that's really all I need to, to be able to trigger it
06:22because it's called start.
06:24I'm just gonna type your NPM start
06:26and it's literally gonna go there
06:28and look at the configurations.
06:30It's gonna compile the files.
06:31If everything worked okay, that's it. Our server is running.
06:35And if now I go into my browser,
06:37and by the way, this is the, this is the, the,
06:40the file that we're working with.
06:41And you could get it at the source
06:43files inside of this file.
06:45It's included in our links to this chapter.
06:48But if we open up a new and go into local hosts
06:52and we go into the path 3000, not 3001,
06:56you'll see that our server is running.
06:58Not only that, if I go here
07:00and I click into inspect my element, we'll see inside
07:03of our console that we have your hello from Webpac.
07:06Moreover than that, if we go
07:07and we make any changes inside of Webpac,
07:10so if I go into my client and I say hello from Webpac 2.0
07:15and I go ahead and save it all, that's all I had to do.
07:18If I go back into my browser, it's going
07:20to refresh and re-render.
07:22And you could see that uh, there was some sort
07:24of a interruption, but you were still getting hello from
07:26Webpac 2.0.
07:28And literally that's all we had to do.
07:30'cause every time that we save, we go back,
07:32it's gonna locate, locate it, and it's gonna refresh it.
07:34So that's it. That is the basics.
07:36Basics of how to get set up
07:39and running with Webpac with hot refreshes.
07:42Now obviously this is gonna become incredibly useful
07:44throughout this title 'cause we won't have
07:45to click refresh each time we make some changes.
07:49In this lecture, we learned how
07:51to create a hot refresh using the Webpac development server.
07:56In the next lecture, we'll continue down this path
07:59and learn how to integrate E es six
08:01or ES 2016 already today, or the help of Babel.

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!