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 continues walking through the commons chunking exercise. He troubleshoots some issues that arise with the test scripts by conditionally chunking based on the environment. He then spends some time answering a few audience questions.

Get Unlimited Access Now

Kent C Dodds: This is not crazy important. It's just good to know. If I were to try and run the tests now, going to have a bit of a problem.
Kent C Dodds: And that is webpackJsonp is not defined, that's because pretty much just the way that karma is loading our stuff into the browser.

It's not loading the code that's necessary for this commons chunking to work. So, for example, actually, if I just remove that. The vendor file, and try to load. Where did my stuff go? Right here. Gets disorganized. Try to load this. I'm going to get the same error, Jsonp is not defined, and that's because it's the vendor chunk that holds the Webpack runtime now.

So, no longer is the Webpack runtime living in app. So basically, like I said before, when you say comments chunk, I want to put all the common stuff in the vendor file. One of the common things is the Webpack runtime. So, it puts all the Webpack runtime in the vendor file.

And so, without loading the vendor file, then I'm going to have a problem, and in our test, we're not loading the vendor file. But if you stop and ask yourself do I really need to have comments tracking in my test? The answer is absolutely no. And so, you can, this is one of the reasons why I created this Webpack plugin, or common config details.

because now you can just say if prod, then we're going to have this. So, it will only be defined and prod. One other problem that we run into though is if you have one item in the plugins array that's undefined, then Webpack is going to cry like a baby, and so, there is another util here called remove empty that you can use.

You just wrap your plugins array and remove empty, and it will remove any empty plugins that you have in that array, so that way, you can use these utilities, like if prod and such. And then the test should hopefully work.
Kent C Dodds: because now it's no longer chunking it in the test environment, just for broad, boom.

Kent C Dodds: Everyone having fun?
Kent C Dodds: I'm thinking maybe a little bit, in a little bit we'll maybe do some air squats, but I don't know.
Speaker 2: What do you mean by empty plugins?
Kent C Dodds: Empty plugins, good question. So, when I use ifProd, this function will take two arguments. One of them is, well, here, let me actually rewrite this.

Using or not using the configure tells. And so, let's env.prod, if it is, then let's just stick this right in here, otherwise undefined. That's essentially what it's doing. So, by, and I don't know about you, but this is why I like having the utils, because I think this doesn't look as nice.

But yes, so, when it when env.prod is false, then you're going to wind up with undefined, and Webpack will cry. And so, you need to remove empty before you send it to plugins. And if you want to, you can run, just to see what the end result of everything is, you could run npm run build:dev, and then pass along env.debug, because we added that earlier.

And you'll see the output and the plugins, you'll see the progress plugin But you won't see the CommonsChunkPlugin, because I was doing the dev mode. Alternatively, here, let me get rid of this stuff. Alternatively, if I did, oops.
Kent C Dodds: Prod, or just regular build, then you're going to see the CommonsChunkPlugin.

Right there.
Kent C Dodds: Fun stuff, any other questions about what's going on here? Okay, well, a lot of questions, sorry
Speaker 2: Those first two, you covered those, so it's just that last one.
Kent C Dodds: Okay, so how do you, I'm not sure I understand, Martin. Let's see.
Kent C Dodds: Yeah, so generally, you would probably want to put d3 and react in here, and by generally, I mean yes, you would do that.

I'm not sure why I didn't do that for this x, or for this portion, but yeah, you would. You could do that and try it out and it should work, yeah.
Speaker 2: Wouldn't you have lost, because like the d3 in the reactor is only used for the pie chart thing?

Kent C Dodds: Yeah, so you could potentially lose some of that I'm not sure how it was, how it would respond to that, maybe you would make a special chunk for that or something. It's pretty magical. So yeah, you could give it a shot and look at it, but I'm pretty sure it should work as you would expect, I would expect it would, maybe not, no.

Something to look at, good question. Other questions?
Speaker 3: So yeah, so in the vendor, you just list all the imports that you want to have quick into the vendor. And okay, so that's explicit, everything that's not in there goes into the app?
Kent C Dodds: Yeah.
Speaker 3: Because that's-
Kent C Dodds: Yep, and you can have all kinds of entries in here, and there are actually several ways to accomplish commons chunking with the CommonsChunkPlugin.

It's got kind of a big API that you can go look in the doc set later. But yeah, this is probably the simplest, even though it's not very simple. And how do you test if karma doesn't load vendor JS as reactors in there? So, yeah, good question, as far as testing is concerned.

So, if you remember, this app module still actually does have, well, actually, this is totally irrelevant. Entry doesn't matter at all in the testing. In karma config, what matters is what you specify as your files. And so, it's going to take each individual file and resolve all of its dependencies totally outside of the idea of entry, or what we're putting in vendor.

Because we added this if prod stuff, it's also not going to care about their common strength again either. It just resolves everything for us, and in testing that, that's just fine. And we trust our tools to be able to do everything else for us, and then we have end to end test of make sure that the tools work too, so.

Kent C Dodds: Cool, so, any other questions, okay.
Speaker 2: Did you answer that one about testing of Carmen, Carmen doesn't load the banner JS.
Kent C Dodds: Yeah, just answered that one.
Speaker 2: And then what about Billy's one right above there, that one popped in?
Kent C Dodds: Okay, Billy is asking the CommonsChunk, and chunks are evaluated by Webpack.

How do I determine that one module is common? So, you would explicitly decide that yourself and put that in vendor. There are ways to specify, hey, Webpack, I don't know what things are common. But just, and this would apply in a situation where you have multiple apps that you're bundling for.

But you'd say hey, take all of these apps. Whatever's coming between them, make it come and chunks for those. And yeah, so that's totally possible. That's actually what we're doing at PayPal, where we have multiple apps. In this app, we're mostly just doing this to get our vendor away from our app, our app code.

So, again, this is kind of a feature that has a couple different faces. It depends on your needs for the app.

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