Dynamic Import Exercise

Steve Kinney

Steve Kinney


Check out a free preview of the full Vite course

The "Dynamic Import Exercise" Lesson is part of the full, Vite course featured in this preview video. Here's what you'd learn in this lesson:

Steve challenges the students to dynamically load another JavaScript file called "banner.js" when a certain condition is met (e.g., when the counter goes below zero or above ten). Students are encouraged to experiment with dynamically loading the "banner.js" file and observe how it gets split into separate bundles during the build process.


Transcript from the "Dynamic Import Exercise" Lesson

>> All right, so I got a challenge for you, which is we can choose to do this asynchronously or not. In counter, right? It would be great to make an additional file, let's call it banner.js, right? And banner.js, maybe when the counter goes below zero over ten, something along those lines, we might choose to then dynamically load another bundle, right?

And show it on, your counter's below zero, above ten, so on and so forth. If you're like, I have not had to write vanilla JavaScript in a while and I forgot how one manipulates the DOM. Don't worry, I already prepared for that fact. Marcus is gonna shake his head because he probably believe that you should all know how to use vanilla JavaScript, but I know that, I don't.

[LAUGH] So if you needed inspiration for the sample code, something like this, there is a div with the id content and you could literally set it to the text. Clearly, the nice part is you don't necessarily have to worry about styling it because we have not talked about CSS yet.

We're simply gonna try to dynamically load another JavaScript file on the condition that it goes, whatever you choose. I think I chose in this example below 0 or you can go above 10, whatever makes you happiest. Then we want to then, in the same way we dynamically loaded that counter before, we wanna then go load one more bundle.

And then we'll kinda see how that gets split out as well. And we'll get a chance to play with that, and then see the different ways that that might work. Kinda get warmed up, get loose, make sure everything installs the way we think it's supposed to install on our setups work before we get into a deeper dive into the squirrely bits.

Cool. There was a little solution hiding down there for the eagle-eyed amongst us, but let's do it together. So we can go ahead and like I showed you a little bit of a cheat sheet here because this is not. This is about configuring our build tools, not about.

We're gonna talk about how to build stuff, not actually build it. It's gonna be great. So we'll make a banner.js. That seems like a good name for the file. And I can do export const addBanner. No one asked me yet, but if you're wondering, generally speaking, as they just, that's a lot.

Let's do document.querySelector, and we'll do that #content. I almost always use export const instead of export default. One is cuz for stuff like React for instance, if you give the thing a name first, like IntelliSense is better about doing the auto importing. And then two, it's easier to just add more things.

But the nice part is on those dynamic imports, if you export default then it's still called default, and then you've got to rename it to whatever you want. And so if I export the constant, I get the right name. But if you wanna export default, go for it, I just choose not to.

And then we'll do .textContent = text, right? Something super simple. And what we're gonna do to the counter is we're gonna say we do this in the render where we've got the current count. And we'll set that, and we'll say, if the count, do we want to do less than 0 or greater than 10?

What makes us happy? Let's do less than 0, all right. We'll go with that import, and in this example, I called it banner.js. And then we've got that addBanner in there. Is that what it says? Let's see. Yep, we've got addBanner, IntelliSense for the win. And then now we have that file.

I can say addBanner and we'll say, You're below zero. And we know how that plays. And what I want you to see as we do this is when the file gets imported, right? So that's what we're gonna watch for here. So go ahead, we've got hello from index.js.

And let's go ahead and let's give it a shot, where I'm gonna pull up the Network tab here. And we should see it in that case where if we increment, file's not loaded. I want all things. And let's clear it real quick. Increment, and then as soon as we decrement to below zero, then it's dynamically loaded when it's needed, and we pull in all of the assets, right?

And it's from that initial, from that counter.js. And we get that all for free. And as we go to build it, you'll see that that is all kind of taken care for us. We'll actually do it from in here. I've got that preview server going, but we'll do npm run build.

And now you'll see that it breaks out into three files. So if you do need to, if you're like, hey, we talked about what is the upper limit for the bundle. If you are like, I do wanna start splitting this out, and I wanna have some opinions on when it happens and why it happens, right?

Those dynamic imports will basically take care of everything else for you. Just to kind of illustrate the difference here, let's go ahead and we'll do it at the top of the file like we would normally do. And like I said before, if you're using a router or something like that, a lot of this is being taken care of for you by, whether it's your routing framework of choice is effectively doing this under the hood.

But this is how it works and how if you need to have opinions about it in your build process put stuff out it all works. So now if I pull it up here and I do an npm run build, you can see I only got two files now.

Cuz it knew that, hey, I'm not going. Now, ES modules out of the box would try to do two requests and in development it will theoretically try to have those things in place. But to cut down on the number of requests, it will bundle this all together into one file.

Cuz it's like, if you instantly need this file, right? It will kinda be put together in one chunk. So you get some control out of that, but not necessarily from configuring Vite or anything along those lines, from just writing your code with asynchronous fetching will do the trick.

And everything else is effectively taken care of for you at that point.

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