Webpack 4 Fundamentals

Q&A and Closing Remarks

Sean Larkin

Sean Larkin

Webpack 4 Fundamentals

Check out a free preview of the full Webpack 4 Fundamentals course

The "Q&A and Closing Remarks" Lesson is part of the full, Webpack 4 Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

After taking questions from students about caching, lazy-loading with code splitting, and web performance, Sean wraps up Webpack Fundamentals course.


Transcript from the "Q&A and Closing Remarks" Lesson

>> Sean Larkin: Yeah, go ahead, this is the perfect time. Cuz really I think this kinda rounds out, now, you should, by default at least, have everything, understanding how to use a loader. Understanding how to apply it, understanding where it is composed to isolate behaviors in your configurations. How really to use ESM and even common JS if you have to.

But I mean hopefully you feel a lot more confident about what Webpack is that it's doing and how it works. Tomorrow, before we learn about plugins, I have a special deep dive section which actually talks about, what is Webpack doing under the hood, specifically? But yeah, I think, rounded out wise, this is a good place to stop in terms of content, what were the other questions?

>> Speaker 2: If you split your JavaScript files into main, vendor and manifest, as per the docs on caching. And your vendor files getting too big, what are some of the step you'd take to reduce the file size?
>> Sean Larkin: That guide that we talk about there, I don't think it's been upgraded to Webpack 4 yet.

And we'll talk about caching tomorrow as well. But If you're still on Webpack 3, I'll just say remove the comments chunks plugin. Basically caching is way less important than being able to minimize the amount of JavaScript you have on your initial download. So you can still split out the manifest, that's valuable.

When they say manifest it's really the run time, the bootstrap code. But I would say focus more on actually splitting your code with like the dynamic import statement
>> Sean Larkin: Instead of trying to force or synthetically create this spender bundle. Because at the end of the day, caching really only solves the time it takes for the network to retrieve an asset.

But the number one cost of a page loading slowly is the amount of JavaScript you have to parse evaluate and execute. So you don't get any winds there by creating these vendor bundles. So I would say like long answer short, turn the caching off, turn the caching features off.

And then just focus on trying to asynchronously load code that you don't need upfront.
>> Sean Larkin: Any other questions?
>> Speaker 3: Is there a lazy load plugin you'd recommend?
>> Sean Larkin: So lazy loading is code splitting in Webpack, and we have a plan to clear up these. So I'll give you a preview of what it looks like.

So let's say for example, we don't need this footer code. We'll make it as a scenario, and so it's kind of contrived here, but I'm just in our dev server. But let's just say, I would only want to load the footer when somebody clicks on that button, right?

So the first question you would ask is, well, if I'm going to load the footer only when the button's clicked, why would I load it up front, right? So this was the main reason Webpack was created, was to be able to support this case of lazy loading or code splitting.

So what we can do is we can just remove that footer and instead we just need to basically say, button.addEventListener, click, and now we have the event. So this call back is only gonna trigger when this event happens. And instead of having this footer statement, we can actually say something like const loadFooter.

So there's two ways to do it. I'd like to obstruct it out because it's easier to understand.
>> Sean Larkin: So this is called the Dynamic Important Statement. Webpack supports it out of the box, if you're using Babble you have to add another plugin to support the syntax. But typescript can handle it, so you don't need to do anything extra.

But stand alone without Babble or Typescript Webpack can read this and it handles the statement. So I would just pass the exact same, the footer. And I think what I'll do, let's see. Was that a default export?
>> Sean Larkin: No, it's not. Okay, so I'm just gonna say m, no, this is fine.

So all I've done is I've changed what was import footer from footer, right? So that's where we started, and I changed it to actually be this. Now, what we can do here is we will probably move this code, this document.appendChild-
>> Sean Larkin: Up inside of, is that the right way to do it?

Yeah, it is. Okay, I would add it inside this event listener, right? We'll need that eventually. But all we're doing is we're taking, we're actually gonna call this inside the event. So loadFooter and it returns a promise. So Webpack converts this statement into a promise itself that lazy loads a separate file.

So what I'm doing here is just saying I get the module and then I think it's probably end.footer.
>> Sean Larkin: We'll pull it up inside of the dev server here in a second.
>> Sean Larkin: So let's take a look and see if it worked. I usually console log or set a break point to make sure I know what it looks like.

>> Sean Larkin: All right, so we don't have a footer, and we do have a footer. So you can see, if we look at our bundles now, we've actually created a separate file, right? It's a completely separate bundle. And let's see.
>> Sean Larkin: We'll just load it again. So only when, so now, we're not incurring any of that code that's only needed until that user actually does something, right?

Or until some sort of asynchronous process happens. And so in this case it's a call back. So only when that happen does this code actually load. This is what's called, when we say code splitting or any Webpack maintenance to this code splitting, this is the process that we're referring to.

And so you don't need a plug in, it's actually just imperative as part of the code you write. That's what makes it so, I guess, I think it's so expressive and really powerful. Because literally all we've changed is just, we took the static import out, which would force you to have that code upfront, into a function that returns a dynamic import.

And so we just call that, and then we grab the export we need which is .footer, and we can just use it the exact same way.
>> Sean Larkin: Yep?
>> Speaker 4: Any tree shaking benefit over exporting individual functions instead of the class?
>> Sean Larkin: So this I'll definitely talk about tomorrow.

We have the first part of the day is all about web performance and the constraints and the limitations. So I'll give you the-
>> Speaker 4: Let's just defer it to tomorrow?
>> Sean Larkin: Yeah, I'll cover it tomorrow but I'll give the short answer now and we'll dive into it more tomorrow.

The short answer is, yes, methods on classes cannot be tree shaken even if they are not used. So I typically tend, when I'm writing for a web scenario, you can kinda see everything I write is a function, right? I'm either writing functions or just individual exports of values.

The reason why is because those can actually be statically tree shaken. So I usually tell people, I know the object-oriented folks are like, why? But, I usually say, either have classes with very small amount of methods where you can incur a little overhead of something that doesn't get used.

Or just switch to using a function. When you think about scope and object oriented, like modules are your scope. People used to create classes to scope things, but now you have this beautiful primitive that's literally a module. And so you can get dependency injection just using ESM. You could export, you don't even need a custom dependency injection system because you could have singletons or new instances any time using importing.

So the short answer is yes. You would be better off exporting individual functions and values and primitives, versus exporting a whole class.
>> Speaker 5: I just have one quick question if that's okay.
>> Sean Larkin: Yeah.
>> Speaker 5: So it's the same problem as npm in general, but, how do you go about finding good plugins versus bad plugins?

I mean is there a curated list or is there like-
>> Sean Larkin: Yeah.
>> Speaker 5: How would you go about it?
>> Sean Larkin: So that's a great question. So really we have a couple resources. Well, one, Webpack-Contrib hosts, we go out of our way to ensure we at some time eventually own a really popular third party plugin.

There are very few that I know of besides what the Chrome team manages. And that's for like their service worker stuff or like fire base or whatever or like Angular and theres. So we essentially house, Webpack-contrib is 90 repositories of loaders, and plugins, and some utility stuff. But we own most of them, almost all of them.

And so I would just say the best.
>> Sean Larkin: I would say the best way is just to look and see, kind of like, how maintained is it? What version of Webpack is in their tests? Is it 4 or is it like 1? A good example is I was looking up a loader, I was trying to find a loader.

So I was GLSL is for using 3js and I was like GLS loader. And I stumbled across this webpack-glsl-loader of course, you're like, there's 40 over them. So I think with the one I stumbled across that I really liked was the Webpack 1. But it definitely wasn't maintained, so much that like a PR that I've been trying to submit has not been, like, even resolved.

This one is pretty old too, right? If you look at it.
>> Sean Larkin: Yeah, and it's using like the old she-bang in here, too. To go grab raw-loader. So I think the best way is like glslfy, this is probably an old browserify plugin that got converted to a loader too.

So really the best way is kinda just like the same way you would approach anything else, how much is it contributed? How active are its commits? And it may just be, it's a finished module. There's some out there that are like that, but you can always tweet us and be like, Sean, is this legit or not?

And if you have issues, like we actively try to reach out the maintainers themselves, we're like hey, can we take this over, do you mind? Like we would love to get this off your back, and then it's like, we can curate it, standardize it, and make it consistent like the rest of them.

>> Sean Larkin: That would be probably the best way to find out.
>> Sean Larkin: Otherwise thank you guys for joining Day 1 and I will see you tomorrow.
>> Speaker 2: [APPLAUSE]
>> Sean Larkin: That's a wrap. [LAUGH]

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now