Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Bundle JavaScript Files with a Zero Config Setup using Webpack 4


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




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

    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.

    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.

    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.

    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.

    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.