If you have been doing front-end development recently, the chances are that you would have heard about Webpack multiple times. In fact, most of the starter kits are built using Webpack Dev Server. The implementation and configuration are hidden by wrappers above it so that the developer continues working on the application and does not have to worry about how to serve the files. If you are unaware of Webpack, you can read more about it and the config file in my post which gives an Introduction to Webpack. Webpack Dev Server spins up a nodeJS server with some additional functionalities that help you as a developer working on your front end. So it provides us with all the features of Webpack plus a development server which serves our files over HTTP and functionalities such as watch mode, live reloading and hot module replacement which makes our lives easier as a developer.
A couple of things that should be kept in mind while using Webpack Dev Server:
Getting started is very easy. You need to follow steps similar to what Webpack requires for setup.
That is
The setup of this would look like this commit. You can add a console log statement in a javascript file to test if the javascript file is getting included in the index.html file. In the webpack.config.js file, you will then specify the entry and output points for Webpack. The config file would then look like:
module.exports = {
entry: './src/app.js',
output: {
filename: "bundle.js"
}
}
JavaScriptIf you have installed webpack and webpack-dev-server dependencies, place an index.html file in the root directory which has a script tag with source as bundle.js. After this, you should be run the command webpack-dev-server in a terminal window which is open in your project folder. When you then navigate to localhost:8080
on your browser, you should see the contents of your index.html file and the logged statement in the console window.
You can add some webpack dev server specific configuration to the config file to enhance the previous simple example. A separate object (with devServer as the key) is added in the webpack.config.js file for these. The commonly used settings are:
devServer: {
port: 9000,
compress: true,
stats: "errors-only",
open: true,
contentBase: path.resolve(__dirname, 'app'),
publicPath: '/dist'
}
JavaScriptThis post gives you an overview of Webpack Dev Server and what all can be achieved using minimal configuration. The final code can be viewed at this Github repository. If you want some more detailed tutorial on how to dynamically inject javascript and CSS in your HTML using webpack, or any other functionalities regarding it, let me know in the comment section, and I will write a post on that. Hope this post helped clear your understanding of Webpack Dev Server.
I am terrible at optimizing my keyboard layout for anything. But off lately, my little…
I recently switched completely to the Brave browser and have set ad blocking to aggressive…
I was preparing a slide deck for a hackathon and decided to put in a…
I have been using npx a lot lately, especially whenever I want to use a…
Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is…
While working on a project, I wanted to do an integrity check of a file…