Bundle JavaScript Files with a Zero Config Setup using Webpack 4

Paul McBride
InstructorPaul McBride
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 5 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.

egghead
egghead
~ 32 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today