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

Loaders and Plugins are two of the most important concepts to understand inside Webpack. Loaders handle file-specific processing like looking for all CSS files and running them through a SASS processor. Plugins are add-ons that carry out specific tasks like code splitting or enabling offline capabilities through the use of ServiceWorkers.

Get Unlimited Access Now

Kent C Dodds: There are two other really important concepts with how webpack accomplishes what it's doing. The first is loaders, and so, if I wanted to write ES6 or TypeScript, then I would have a loader. So in this case, Babel, or the TypeScript loader. There are six TypeScript loaders. I don't use TypeScript but I know that one of them is starting to become king.

I think it's like Awesome TypeScript loader or something like that. But yeah, so we say okay, webpack I want you to take all my JavaScript files and pipe them through this Babel loader. You don't use the word pipe, I shouldn't use that word because it's like gulp word.

Speaker 2: Stream.
Kent C Dodds: Yeah, like stream it, yeah, you basically just load it, load it through this Babel loader and then the Babel loader will spit out JavaScript at the end. And then webpack will take that and resolve all the dependencies and create the bundle for you, same thing works with CSS.

This is the CSS Modules logo. It doesn't have to be like CSS Modules, but webpack enables CSS Modules and it's awesome. You should look into it later, we're not going to talk about it today. So you take your CSS and you say, hey webpack, I want you to send that through the PostCSS loader or the SaaS loader or whatever, whatever you want to author your CSS in.

And it will spit out CSS for you and then webpack will take that, resolve all the dependencies, and then bundle it for you in your bundle. Yeah?
Speaker 3: So if I am targeting ES6, I don't need to run through Babel, right?
Kent C Dodds: Yes, that's right.
Speaker 3: But this is only if I'm targeting today's browsers.

Kent C Dodds: That's right, or ancient browsers because most of today's browsers are pretty well compliant with ES6. Yeah, because Edge is almost 100% and stuff. So good question, so yeah, and even if you are targeting today's browsers, there are likely features like async await maybe or some ES2016 features that you want to use.

because we all know when you just want to use the exponentiation operator and yeah.
Kent C Dodds: It's like 2, it saves you a couple characters of math.pwo but anyway.
Kent C Dodds: But it does include includes, array includes and so that's kind of nice. Of course, that's poly fillable, but anyway the point is that you're probably always going to be transpiling your JavaScript whether it's from TypeScript or using Babel.

Just because there will always be features that are really nice to be using and it's kind of a given that you're going to be using a toolchain anyway. May as well just stick Babel in there and then so, images of the same thing. So remember that everything is a module, so you can say, hey take all my image modules and load them through a couple of webpack loaders.

There's URLLoader or a file-loader and turn it into a Base64 string that I can use in my application. Or if it's too big, then I just want a URL for wherever that image is going to be and webpack will give you a URL and make sure that the image is accessible at that URL.

So yeah, that's the whole idea behind loaders. There are hundreds of loaders and you can chain loaders together. So you could say, okay, I want you to take my JavaScript and first load it through the eslint loader to lint all my code and then pass the result of that.

Which, actually, nothing has changed as a result of that loader. Pass that onto the Babel loader and then Babel will transpile it, and then that's the end of the chain and then webpack will just use that. Or we'll look at chaining a little bit with our CSS and I'll explain a little bit more about how the CSS loader works and the style loader because they're a little bit on the unique side.

But yeah, chaining loaders, that's really common. And loaders are how you take each file and do something to it before webpack takes over and does some magic to the resolution and bundling stuff. Any questions about loaders?
Kent C Dodds: Okay, this is either really good and you're all getting it, or you're shy.

Yeah, we'll get a good cadence. Nothing from online, so keep going. So plugin side of things, yeah, plugins do everything else, so there are a bunch of random words and stuff on there. We'll talk about some of these plugins but the CommonsChunkPlugin, we'll use this to take things that we want to have in a separate bundle altogether.

So that when there are changes to our code that separate bundle doesn't need to be changed and the browser can keep that cache. Lodash, anybody use Lodash here? Yeah, awesome, and if you're at home you can raise your hand and your wife's like, what the? But yeah, so Lodash has a really cool plugin that will really shrink the size of Lodash.

Lodash is an enormous library and it's enormously valuable but yeah, it's really big and so there's a really cool plugin to shrink it considerably. Also, a Babel plugin as well to enable you to use tree-shaking which we'll talk about later too, it's really cool. There's a GZip plugin, which we won't use but it's interesting.

We won't use it because we're deploying to a service that GZips for us. The PrefetchPlugin, we actually won't use that either because the Service Workers plugin that we're going to use will prefetch all of our chunks for us. It's going to be so cool!
Kent C Dodds: Hang on with me toward the end.

When I realized it was doing this, I was like what! Yeah, I'm really excited to show you that one, yeah.
Speaker 5: There was a question if we'll be seeing an example of using webpack on CSS and images.
Kent C Dodds: Yes.
Speaker 5: Perfect.
Kent C Dodds: Well, on CSS there won't be any images yet but we will be doing CSS.

Talk to me after, whoever that was, and I can show you an example of that. It's fairly straightforward. You kind of write everything the same way you normally would and you just add another loader. Good question. ExtractTextWebpackPlugin, we'll talk about that. There's even a plugin to automatically upload your bundle to S3, which that was kind of cool.

The DedupePlugin is pretty awesome because NPM kind of gets a bunch of flack for having a whole bunch of little modules and then you install these little modules and then you have duplications or something. So the npm-dedupe command will kind of fix that problem and npm3 actually dedupes your dependencies anyway.

But to take it even a step further, let's say you had two npm modules that copy pasted some code, like some JavaScript file. The DedupePlugin would be able to tell that those two files are the same and dedupe them. So it would say okay, these are the same, I'll only use one.

So I've seen that shrink my build a little bit as well. So yeah, plugins are pretty much responsible for optimizing your resulting bundle and then doing stuff with the resulting assets as well.

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