So you wrote your entry and output point on your Webpack config file. What do you do next?
The next step is to create a development environment with Webpack, and add hot reloading.
That way it’s easy, and fast to develop your React application.
Under the hood, Webpack dev server is a mini Node.js Express server.
It uses a library called SockJS to emulate a web socket.
The Node.js server listens to when files were changes and triggers events to react accordingly.
Webpack dev server is also a separate package that needs to get install via NPM.
Webpack dev server should only be used for development only.
Now that you know Webpack dev server may help you quickly develop a React application, let’s break down the steps.
The first step I will take is to create a directory called, public.
The public directory is responsible for holding any CSS stylesheets, image assets, and of course, your skeleton HTML markup.
Inside the public directory, I will add a file called, index.html.
This HTML has 1 simple job, and that is to serve our React app and attach it to a div element with an ID of root.
Later in this guide, I will tell Webpack to use this HTML file for development and to automatically inject a script tag element at the bottom of this file.
That script tag will be the entry point to the React application.
For the moment I’m going to install the minimal dependencies to get a React dev server running.
List of dependencies I’m installing:
The next important part of this code is the script that was added.
When you’re going to run npm start on your terminal.
npm start will run the command client webpack-dev-server and tell it to look for the webpack config.
Go ahead and run npm install to install these dependencies.
The next step is to work on the webpack config file.
In the root of the project I will create a file named webpack.config.js.
First I’m going to add some helpful variables and functions.
On the first couple lines of code you’ll notice that I’m importing modules that were not defined in the package.json file.
That’s because Webpack utilizes Node.js to run. And the module path, and fs are native Node.js modules.
On line 6, I’m utilizing the filesystem module to fetch me an absolute path of the current working directory of the project.
On line 9, I’m creating a variable that is assigned to a function. This function is responsible to return a valid path.
It will be helpful when we need to define path values on our webpack configuration.
Speaking of, the Webpack configurations is nothing but an object that will needs to get export.
This step is a required otherwise Webpack will throw an error.
mode – this property accepts 3 values, development, production, and none.
Certain modules, and Webpack optimization tools require specific modes. But in this case I defaulted to development mode.
The value on this configuration file is pointing to an index.js file that will be under a directory called src.
output – Tells Webpack how to output the bundle.
In the configuration above, I’m telling Webpack to output the bundle file under a the path static/js and name the file bundle.js.
We don’t need to create any directories, Webpack will do that for us.
These configurations are responsible to tell webpack-dev-server what host, port number, and what content base to use.
Let’s breakdown the configuration:
contentBase – This property tells Webpack what static file it should serve. In this case we want to serve our index.html file that was created in the public directory.
compress – This enables gzip compression for everything that is being served.
hot – This property enables hot reloading. Whenever you modify a file, the browser will automatically refresh with the new updates.
This property requires the HotModuleReplacementPlugin. I did not have to add it too the package.json file, because webpack-dev-server adds that for us.
host – By default it uses localhost as the host name but you’re more than welcome to use whatever you want.
port – Specifies what port number to listen too. For this tutorial it will be port 3000.
So the browser URL will be localhost:3000.
publicPath – Defines what the browser path will look like when serving public assets.
At this point you can run npm start, and it will serve the static HTML file.
But we’re trying to develop a React application with it, so let’s tell it to embed the main source file of our app.
P.S. I will be creating the main React app source as the final step.
In the first step of this guide, I imported a module called html-webpack-plugin.
That node module will help Webpack stitch the main React source file, and the index HTML file together.
In the example above I’m telling html-webpack-plugin what template to use, and passing the inject property as true.
And that’s it! Let’s create the React app now.
The first step is to create a directory called src in the root of the project.
And inside the directory, src, create a file named index.js.
The code sample above is some basic React code that creates an H1 element that says “Hello World”.
And React DOM is attaching to the element with an ID of root.
The first step we need to take is to install a few more dev dependencies. I’m going to run the following command.
npm i –save-dev babel-loader babel-preset-react-app @babel/core
Let’s do a quick break down of what I’ve just installed.
babel-loader – This module is the middleware that let’s us use babel core and Webpack together.
It’s built and maintained by the Create React App team.
Now we must add to the Webpack configuration.
The first step I need to make is to create an environment variable called NODE_ENV.
This is required for the babel preset I’m about to use.
In this example, I’m assigning it the value of development on line 4.
This value may either be development or production.
The next step is to update the Webpack configuration object.
Right above the plugin property, I’ve added a property called module.
Why would we want to do this?
Let’s do one final breakdown for what’s going on in the rules object.
test – I’m telling Webpack to execute this rule only on files ending on .js or .jsx.
exclude – Don’t apply this rule to any files in node_modules.
include – Apply this rule to any JS or JSX files inside the directory, src.
loader – If the conditions above are met, use the babel-loader.
options.presets – Allow JSX syntax to be supported.
Now that all of the steps above have been completed you may run npm start to create your local dev environment, and begin developing your React application.
If you’d like to see the source code, click the link below.
Don’t forget to give it a star if your liked this Webpack tutorial.