Bundle JavaScript Files with a Zero Config Setup using Webpack 4

Paul McBride
InstructorPaul McBride

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 4 years ago

Version 4 of Webpack has a great new "zero config" mode which allows you bundle your JavaScript with, you guessed it, zero config. In this lesson you'll learn how to install Webpack and use it to bundle your code.

Instructor: [00:01] To get started, we need to install Webpack and Webpack-cli. We want to see if these two are dev dependencies.

[00:15] Next, let's take a look at the editor. In the package.json file for this project, I'm going to create a build command, which simply runs Webpack. If we switch back over to the console and we run that command by typing npm run build, that will bundle our JavaScript for us. As you can see, we're getting a warning here to say that we haven't specified the mode option, but we'll get back to that.

[00:41] Let's take a look back at the editor to see what's happened. By default, Webpack will look inside the source directory at an index.js. Here, it will crawl the dependency tree and bundle everything up into a single JavaScript file. It then saves that JavaScript file into dist/min.js.

[01:02] Here, we can see all of our minified and amplified javascripts. Let's include this JavaScript in our index.html and make sure everything works. If we click this button, we can see we're getting the results we expect.

[01:27] Finally, let's take a look at the console again and we'll figure out how to solve this warning. Webpack is telling us we need to tell it which mode to run in. We can do that by having a look at the package.json and providing it as an argument to this command.

[01:44] By default, Webpack runs in production mode, which is why we get the amplified and minified code, but in development mode, it doesn't do that. Let's run this command. If we change back to the editor, we can see that everything still works as it did before. If we check our code, it's now being bundled using the development settings.