Skip to content

Faster startup (1/4): Lazy command loading#7010

Draft
gonzaloriestra wants to merge 1 commit intomainfrom
faster-startup
Draft

Faster startup (1/4): Lazy command loading#7010
gonzaloriestra wants to merge 1 commit intomainfrom
faster-startup

Conversation

@gonzaloriestra
Copy link
Copy Markdown
Contributor

@gonzaloriestra gonzaloriestra commented Mar 13, 2026

WHY are these changes introduced?

The CLI requires too much time to start working, it feels slow

WHAT is this pull request doing?

Splits the monolithic index.ts (which eagerly imports all 106 commands and their dependency trees) into a lightweight bootstrap.ts entry point and a lazy command-registry.ts. Commands are loaded on-demand via a LazyCommandLoader passed to ShopifyConfig.runCommand().

  • Separate bootstrap from index.ts: bootstrap.ts sets up global proxy, signal handlers, and calls runCLI — it does NOT import any command modules
  • Lazy command registry: command-registry.ts maps command IDs to dynamic import() expressions, so only the invoked command's module is loaded
  • Custom runCommand override in ShopifyConfig: when a LazyCommandLoader is set, runCommand bypasses oclif's default cmd.load() (which would pull in the entire index.ts) and directly runs the lazily-loaded command class
  • Individual hook files: hooks are moved to separate files (hooks/app-init.ts, hooks/did-you-mean.ts, etc.) so oclif loads them independently instead of through index.ts
  • Package exports for hooks: @shopify/app exports hooks via subpath entries (./hooks/public-metadata, ./hooks/sensitive-metadata) so they can be imported individually
  • Deferred error-handler import: errorHandler is imported lazily in the catch block of launchCLI, removing it from the critical startup path

Startup time: 1840ms → 710ms (61% faster)

How to test your changes?

  • shopify version / shopify help return correct output
  • shopify app dev and other commands work normally
  • All tests pass

Measuring impact

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

@gonzaloriestra
Copy link
Copy Markdown
Contributor Author

/snapit

Copy link
Copy Markdown
Contributor Author

gonzaloriestra commented Mar 13, 2026

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions

This comment was marked as outdated.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 13, 2026

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 82.24% 15165/18439
🟡 Branches 74.54% 7461/10010
🟢 Functions 81.35% 3808/4681
🟢 Lines 82.62% 14339/17355

Test suite run success

4000 tests passing in 1531 suites.

Report generated by 🧪jest coverage report action from f3f181b

@gonzaloriestra
Copy link
Copy Markdown
Contributor Author

/snapit

@github-actions
Copy link
Copy Markdown
Contributor

🫰✨ Thanks @gonzaloriestra! Your snapshot has been published to npm.

Test the snapshot by installing your package globally:

npm i -g --@shopify:registry=https://registry.npmjs.org @shopify/cli@0.0.0-snapshot-20260313155002

Caution

After installing, validate the version by running shopify version in your terminal.
If the versions don't match, you might have multiple global instances installed.
Use which shopify to find out which one you are running and uninstall it.

@gonzaloriestra gonzaloriestra force-pushed the faster-startup branch 2 times, most recently from a818a13 to 62c6045 Compare March 13, 2026 16:56
@gonzaloriestra gonzaloriestra mentioned this pull request Mar 16, 2026
5 tasks
@gonzaloriestra gonzaloriestra changed the title Faster startup Faster startup (1/4): Lazy command loading Mar 19, 2026
@gonzaloriestra gonzaloriestra force-pushed the faster-startup branch 9 times, most recently from 118f6ce to d5aae16 Compare March 25, 2026 12:30
@gonzaloriestra gonzaloriestra force-pushed the faster-startup branch 4 times, most recently from 0b7096a to da82414 Compare March 30, 2026 13:37
Split the monolithic index.ts (which eagerly imports all 106 commands and
their dependency trees) into a lightweight bootstrap.ts entry point and
a lazy command-registry.ts. Commands are loaded on-demand via a
LazyCommandLoader passed to ShopifyConfig.runCommand().

Hooks are moved to individual files so oclif loads them independently
instead of through index.ts. Token utilities extracted from
TokenizedText.tsx to break circular import chains.

Startup time: 1840ms → 710ms (61% faster)
@github-actions
Copy link
Copy Markdown
Contributor

Differences in type declarations

We detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:

  • Some seemingly private modules might be re-exported through public modules.
  • If the branch is behind main you might see odd diffs, rebase main into this branch.

New type declarations

packages/cli-kit/dist/public/node/custom-oclif-loader.d.ts
import { Command, Config } from '@oclif/core';
import { Options } from '@oclif/core/interfaces';
/**
 * Optional lazy command loader function.
 * If set, ShopifyConfig will use it to load individual commands on demand
 * instead of importing the entire COMMANDS module (which triggers loading all packages).
 */
export type LazyCommandLoader = (id: string) => Promise<typeof Command | undefined>;
export declare class ShopifyConfig extends Config {
    private lazyCommandLoader?;
    constructor(options: Options);
    /**
     * Set a lazy command loader that will be used to load individual command classes on demand,
     * bypassing the default oclif behavior of importing the entire COMMANDS module.
     *
     * @param loader - The lazy command loader function.
     */
    setLazyCommandLoader(loader: LazyCommandLoader): void;
    /**
     * Override runCommand to use lazy loading when available.
     * Instead of calling cmd.load() which triggers loading ALL commands via index.js,
     * we directly import only the needed command module.
     *
     * @param id - The command ID to run.
     * @param argv - The arguments to pass to the command.
     * @param cachedCommand - An optional cached command loadable.
     * @returns The command result.
     */
    runCommand<T = unknown>(id: string, argv?: string[], cachedCommand?: Command.Loadable | null): Promise<T>;
    customPriority(commands: Command.Loadable[]): Command.Loadable | undefined;
}

Existing type declarations

packages/cli-kit/dist/public/node/cli-launcher.d.ts
@@ -1,6 +1,8 @@
+import type { LazyCommandLoader } from './custom-oclif-loader.js';
 interface Options {
     moduleURL: string;
     argv?: string[];
+    lazyCommandLoader?: LazyCommandLoader;
 }
 /**
  * Launches the CLI.
packages/cli-kit/dist/public/node/cli.d.ts
@@ -1,3 +1,4 @@
+import type { LazyCommandLoader } from './custom-oclif-loader.js';
 /**
  * IMPORTANT NOTE: Imports in this module are dynamic to ensure that "setupEnvironmentVariables" can dynamically
  * set the DEBUG environment variable before the 'debug' package sets up its configuration when modules
@@ -7,6 +8,8 @@ interface RunCLIOptions {
     /** The value of import.meta.url of the CLI executable module */
     moduleURL: string;
     development: boolean;
+    /** Optional lazy command loader for on-demand command loading */
+    lazyCommandLoader?: LazyCommandLoader;
 }
 /**
  * A function that abstracts away setting up the environment and running
@@ -17,6 +20,7 @@ export declare function runCLI(options: RunCLIOptions & {
     runInCreateMode?: boolean;
 }, launchCLI?: (options: {
     moduleURL: string;
+    lazyCommandLoader?: LazyCommandLoader;
 }) => Promise<void>, argv?: string[], env?: NodeJS.ProcessEnv, versions?: NodeJS.ProcessVersions): Promise<void>;
 /**
  * A function for create-x CLIs that automatically runs the "init" command.
@@ -38,5 +42,5 @@ export declare const jsonFlag: {
 /**
  * Clear the CLI cache, used to store some API responses and handle notifications status
  */
-export declare function clearCache(): void;
+export declare function clearCache(): Promise<void>;
 export {};
\ No newline at end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant