18.8 Dynamic import(): Lazy Loading Modules
Now, let’s talk about getting lazy. And I mean that in the best possible way. Why should your user’s browser have to download, parse, and compile every single module your application might need the moment the page loads? It shouldn’t. That’s a great way to make your app feel like it’s running on a potato connected by two cans and a string. Enter import(). This isn’t the static import statement you put at the top of your file. This is a function—a function that returns a promise. And it is the single most powerful tool you have for lazy-loading modules. It lets you say, “Hey, I might need this chunk of code later, but only fetch it and get it ready when I actually ask for it.” This is the cornerstone of code-splitting, and it will make your performance metrics sing.