Welcome back! In the previous chapter, Command Declaration, we created a "Passport" for our tool. We told the application who we are (name, aliases) without dragging along all our heavy luggage (the code logic).
Now, we need to answer the question: How does the application actually pick up that luggage when it needs it?
This brings us to Lazy Module Loading.
To understand this concept, let's look at a central use case: Optimizing Startup Speed.
Imagine you go to a restaurant. You sit down and open the menu.
No. That would be wasteful, expensive, and the kitchen would be a chaotic mess. Instead, the chef waits. They only cook the steak after you specifically order it.
Lazy Module Loading works the exact same way:
index.ts from Chapter 1) instantly.allowed-tools.
Let's look at the index.ts file again. We briefly touched on the load property in the last chapter. Now, let's break it down.
Here is the specific line that handles the magic:
// --- File: index.ts ---
const permissions = {
// ... name, aliases, etc.
// This is our "Order Button"
load: () => import('./permissions.js'),
} satisfies Command
() =>
Notice we didn't write load: import(...). We wrote load: () => import(...).
This is a wrapper. It tells the computer: "Don't do this yet! Keep this instruction in your pocket. Only execute it when I call your function."
If we removed the () =>, the application would load the file immediately at startup, defeating the whole purpose!
import()
This is a standard JavaScript feature. Unlike static imports (which happen at the top of a file), import() loads a file on demand.
./permissions.js).A Promise is like that buzzing pager restaurants give you. It doesn't give you the food immediately; it guarantees that eventually the food (code) will be ready.
So, where does this point to? It points to ./permissions.js. This is where our actual logic lives.
While we haven't written the complex logic yet, let's create the basic structure so the load function has something to grab.
// --- File: permissions.js ---
// This is the file that gets loaded "Lazily"
export default function PermissionsTool() {
console.log("The heavy code is now loaded!");
return "UI Logic goes here";
}
Explanation:
load function.What actually happens inside the Application Core when a user types a command?
Let's visualize the "Restaurant Order" process.
To truly understand this, let's look at a simplified version of the code running inside the Application Core that processes your command.
The core finds your command metadata, sees the load function, and executes it using await.
// --- Application Core Logic ---
async function runCommand(userQuery) {
// 1. Find the passport (from Chapter 1)
const cmdDef = registry.find(c => c.aliases.includes(userQuery));
// 2. COOK THE FOOD! Call the load function and wait
const loadedModule = await cmdDef.load();
// 3. Serve the food. Access the 'default' export.
const toolLogic = loadedModule.default;
// 4. Run the tool
toolLogic();
}
Explanation:
permissions object.await cmdDef.load(): This creates the "pause." The app waits here for a millisecond while the file is fetched from the disk.loadedModule.default: This grabs the function we exported in permissions.js.In this chapter, we learned about Lazy Module Loading.
load: () => import(...) to load code only when requested.
Now that the application has successfully loaded our heavy code file (permissions.js), we need to decide what that code actually does. We want to display an interactive interface to the user.
To do that, we need to learn about rendering. Let's move on to Chapter 3: Local JSX Command Handler.
Generated by Code IQ