The full video and many others like it are all available as part of our Frontend Masters subscription.

Kent C Dodds

Kent C. Dodds works at PayPal as a full stack JavaScript engineer. He host JavaScript Air and co-hosts React30 podcasts. Kent is an Egghead.io instructor and Google Developer Expert.

Kent C Dodds

PayPal

Kent begins by making a case for Webpack. He explains how it replaces task runners like Grunt or Gulp. Webpack also allows developers to explicitly declare dependencies making the bundling of modules into static assets much easier.

Get Unlimited Access Now

Kent C Dodds: So WebPack 101, why does this thing exist? Has anybody seen a project that looks like this before, where you have script tags, a ton of script tags? This is actually one of my old projects. It is a nightmare, to have to manage all of these script tags.

And part of the reason that this is such a terrible thing is because these scripts are running in order and so you need to have them sorted in the way that they need to be so that the app actually loads. If there's a dependency between one script and another, you need to make sure that the dependent scripts loads after.

So yeah, this is one of the problems that Webpack solves, for sure. Lots of you in the pre-event survey mentioned that you're using Grunt or Gulp print now and you want to compare Webpack with that or you want to see how they can work together or see how you can avoid it all together.

So with Webpack, I started using Webpack just over a year and a half ago. And I went from nothing to Grunt to Gulp to Webpack. And ever since I started using Webpack, I haven't needed Grunt or Gulp at all. And the reason for that is if you try to think of, what are some of the things that you do with Grunt or Gulp, why are those things useful?

Well, you're going to concatenate your JavaScript and minify it. You're going to do the same to your CSS. There are various things that you're trying to do to generate optimized assets for your web application. Right, so you have your JavaScript, your CSS, even fonts, HTML, images, SPGs, everything, all these assets for your application.

The reason that with Webpack you don't need these task runners anymore is because Webpack takes care of all of this for you. And what the way that it accomplishes this is by turning everything into a module. You just think conceptually that, yes JavaScript is a module like each file you can think of as a module pretty easily.

But CSS webpack considers CSS to be a module just like a JavaScript file. So not only that, but it also enables you to use common JS, AMD or yes, six imports. So these different module styles to make your dependencies explicit. And that is really a powerful concept as well because now it's not a matter of loading the right scripts in the right order, but you just say this is my main file and then it has dependencies.

And all you care about is that main file. And then Webpack will fix it all up for you and give you one file that you just load into the browser. And then you can minify that in and optimize that for delivery for your application. But it takes it a step further, like I said, to CSS where CSS are modules and now you can take the CSS modules and make those dependencies explicit.

And same with fonts and with HTML and images and spgs, everything is a module. And that's one of the most important pieces of Webpack. Do I have any questions?
Speaker 2: Typescript.
Kent C Dodds: Yes, a typescript would be a loader that would transpire down into JavaScript. Good question, yeah.
Speaker 3: How are you running the E to E tests?

Kent C Dodds: End to end, E to E tests, those run kind of the same way that you would with anything else. End to end tests shouldn't really care about how you build your application or how it's bundled. End to end tests just say load up this application. However it's built, that's irrelevant.

Just load up the application. Start inputting forms, and as far as integration and unit tests, we'll definitely cover that in this workshop. Great question. Other questions? Anything from the chat? Okay, cool. Okay, sweet, so, yeah. So that's the basic idea of the most fundamental piece of Webpack is you have all these assets for your web application.

They are all modules, you take those modules, make the dependencies between them explicit through various module syntaxes. So CSS, there's a module syntax for that, and JavaScript, obviously, common JS, AMD, or ES6 modules. So you'll make all those dependencies explicit. Then Webpack will resolve all those dependencies and spit out for you the static assets that you use for your web application.

This is a very fundamental piece of Webpack, so I want to make sure everybody understands that. And it's a little bit different if you were using Gulp or something. All of these different things would be like totally separate. You'd say, Gulp, go get me all my css files. I'm going to do all this stuff.

There's no notion of dependencies between css and JavaScript. But with Webpack there is and it's awesome.

Ready to take your code to the next level?

Intense courses with world-class teachers and unlimited access to our growing library of videos for the great price of $39 per month.

Get Unlimited Access Now