In the previous chapter, Platform Guard, we built a security system to ensure our feature only runs on supported computers. We established safety.
Now, we are going to focus on speed.
Imagine a master mechanic who owns a massive truck filled with thousands of specialty tools.
In software, Lazy Module Loading is that second approach.
The code for our desktop feature might be "heavy" (containing complex logic or UI libraries). If we load it every time the application starts, the user waits longer, even if they never use the command. We want to keep the application lightweight and only fetch the "heavy tools" when the user specifically asks for them.
To understand how we achieve this, we need to look at how we import files in JavaScript.
This is what you usually see at the very top of a file.
// โ This loads the code immediately when the app starts!
import { HeavyFeature } from './heavy-feature.js';
When the computer sees this, it stops everything to read heavy-feature.js before it continues.
This is a special function that we can call inside our code.
// โ
This only loads the code when this line is executed
const myFeature = await import('./heavy-feature.js');
The import() function tells the system: "Go find this file now, read it, and come back to me when you are done."
Let's look at how we implemented this in our command configuration from Chapter 1: Command Configuration.
We didn't put the import at the top of index.ts. Instead, we put it inside a property called load.
// --- File: index.ts ---
const desktop = {
// ... name, description, guard logic ...
// The Lazy Loader:
load: () => import('./desktop.js'),
} satisfies Command
Explanation:
load is a function.import('./desktop.js').() => ...), it does not run immediately.desktop.js?
The file ./desktop.js is the "heavy tool." It contains the actual code that draws the user interface and talks to the operating system. We will write the contents of this file in the next chapter.
For now, just know that desktop.js must have a default export. This is the entry point the loader is looking for.
// --- File: desktop.js (Preview) ---
export default function DesktopCommand() {
// This is the heavy logic!
// It only runs after the dynamic import finishes.
return "I have arrived!";
}
What happens when a user actually triggers the command? Here is the sequence of events.
load function but does not execute it. desktop.js stays on the hard drive, keeping memory usage low.load() function we defined.import('./desktop.js') command runs. The system reads the file from the disk.You might be wondering: "What happens while the computer is reading the file from the disk? Does the app freeze?"
No. The import() function returns something called a Promise.
Think of a Promise like a restaurant pager.
Our system handles this pager automatically. It shows a small loading spinner if the file takes a long time to read, ensuring the user always knows what is happening.
By using Lazy Module Loading, we have kept our application fast and responsive. We are acting like the smart mechanic: keeping our heavy tools organized and only bringing them out when the job requires them.
So far, we have:
Now, the system has successfully fetched the desktop.js file. But what code goes inside that file? How do we actually show something to the user?
In the next chapter, we will build the visual interface that users interact with.
Next Chapter: Visual Command Handler
Generated by Code IQ