Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Archived
Published
3 min read

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

How to setup Webpack +2.0 from scratch in 2017

Webpack is another evolution in the JavaScript tooling ecosystem

If you’re feeling intimidated by Webpack, don’t worry; you’re not alone. Webpack is intimidating.

Webpack config.js

What exactly is a proxyTable, anyway? Why do we need so many files just to compile our code? Don’t worry—we’ll start from the very beginning.

Webpack is the next step in the evolution of front-end tooling. First, there was Grunt, then came Gulp, and now we have Webpack.

When you have a more complex and resource-heavy project, Webpack is definitely the way to go. For example, you might need to compile JavaScript, SCSS, import files, hot-reload without affecting the current state, bundle production builds, and more.

Webpack to the Rescue

If you want to become a well-rounded frontend engineer, knowing Webpack is essential. I’ll try to make this as easy as possible.

Let’s Get Started!

Things You’ll Need:

  • Basic knowledge of Node.js
  • NPM (Node Package Manager)
  • Basic command line/terminal skills
  • A text editor (any will do)
  • A Unix-like operating system (Mac or Linux)
  • Install Node.js

Setting Up Webpack

With Node.js and NPM installed, we’re ready to set up Webpack. Start by creating a new project folder and navigate to it in your terminal. Initialize a new Node.js project with:

Terminal window
npm init -y

This will create a package.json file, which is the basic configuration file for your project.

Now, let’s create the necessary files: index.html, webpack.config.js, and index.js. Your project structure should look something like this:

my-project/
├── index.html
├── webpack.config.js
└── index.js

Install Webpack Dependencies

We need to install Webpack and Webpack Dev Server, which will serve our files via HTTP.

Terminal window
npm install --save-dev webpack webpack-dev-server

Configuring Webpack

Open webpack.config.js and start by requiring Webpack:

const path = require('path');
module.exports = {
entry: './index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
mode: 'development',
};

This basic configuration tells Webpack where to find the entry file (index.js) and where to output the bundled file (bundle.js).

Connecting Webpack to HTML

In index.html, include the bundled JavaScript file by adding a <script> tag:

<script src="bundle.js" />

Running Webpack

Before you can run Webpack, install it globally:

Terminal window
npm install -g webpack webpack-cli

Now, you can run Webpack with:

Terminal window
webpack

Webpack will generate a bundle.js file in the dist directory. Open index.html in your browser, and you’ll see the result of your first Webpack project.

How Do We Watch for Changes?

Currently, we have to manually re-run the compile task every time we make a change. That’s tedious and time-consuming. Luckily, there’s an easy fix!

Terminal window
webpack --watch

This command will automatically recompile your project whenever changes are detected, saving you time and effort.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
4 min read

The What, Why, and How of Using a Skeleton Loading Screen

Skeleton loading screens enhance user experience and make your app feel faster

Nov 12, 2020
Read article
Webdev
4 min read

Self-Taught Developer's Guide to Thriving in Tech

How to turn your non-traditional background into your biggest asset

Sep 28, 2024
Read article
Webdev
3 min read

CSS @supports: Write Future-Proof CSS

Detect CSS feature support and provide smart fallbacks with @supports

Dec 6, 2024
Read article
Webdev
3 min read

CSS Supports Nesting Now

CSS nesting is finally supported in all major browsers. Write cleaner, organized stylesheets without Sass or Less

Dec 6, 2024
Read article
Webdev
6 min read

Inside the CSS Engine: CSSOM Explained

A deep dive into how browsers parse and manipulate CSS, its impact on web performance, and why it matters

Oct 25, 2024
Read article
Webdev
4 min read

Remove Unnecessary NPM Packages with eslint-plugin-depend

We don't need packages to handle basic JavaScript tasks

Aug 13, 2024
Read article
Webdev
7 min read

Tips for Reducing Cyclomatic Complexity

Cyclomatic complexity is like counting how many ways a car can go. More options make it harder to drive because you have to make more decisions, which can lead to confusion.

Sep 10, 2024
Read article
Webdev
3 min read

CSS content-visibility: The Web Performance Boost You Might Be Missing

The content-visibility CSS property delays rendering an element, including layout and painting, until it is needed

Dec 5, 2024
Read article
Webdev
3 min read

The HTML Native Search Element

The search HTML element is a container that represents the parts of the web page with search functionality

Dec 2, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/easy-guide-for-webpack-2-0-from-scratch. It was written by a human and polished using grammar tools for clarity.