Skip to content

Faster startup: Inline app-init and hydrogen-init hooks#7053

Merged
gonzaloriestra merged 1 commit into
mainfrom
faster-startup-3
Apr 27, 2026
Merged

Faster startup: Inline app-init and hydrogen-init hooks#7053
gonzaloriestra merged 1 commit into
mainfrom
faster-startup-3

Conversation

@gonzaloriestra

@gonzaloriestra gonzaloriestra commented Mar 19, 2026

Copy link
Copy Markdown
Contributor

WHY are these changes introduced?

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

WHAT is this pull request doing?

Replaces the re-export hook stubs from PR 1 with inlined implementations that avoid loading heavy package dependency chains.

  • Inlined app-init.ts: lazily imports LocalStorage instead of pulling in the entire @shopify/app chain (~1s of imports). Uses Node's native crypto.randomUUID() instead of importing from cli-kit
  • Inlined hydrogen-init.ts: skips loading @shopify/cli-hydrogen entirely for non-hydrogen commands (~300ms+ saved). Only loads the module when options.id starts with hydrogen:

How to test your changes?

  • shopify app dev still initializes correctly (LocalStorage cleared, run ID set)
  • shopify hydrogen dev still runs hydrogen init hook
  • Non-hydrogen commands skip hydrogen module loading

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 commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

@github-actions

github-actions Bot commented Mar 30, 2026

Copy link
Copy Markdown
Contributor

Coverage report

St.
Category Percentage Covered / Total
🟢 Statements 82.24% 15165/18439
🟡 Branches 74.55% 7462/10010
🟢 Functions 81.33% 3808/4682
🟢 Lines 82.62% 14339/17355

Test suite run success

4000 tests passing in 1531 suites.

Report generated by 🧪jest coverage report action from 40f806b

@gonzaloriestra gonzaloriestra force-pushed the faster-startup-3 branch 2 times, most recently from 1c3373e to efa9dd4 Compare March 30, 2026 13:38
@gonzaloriestra gonzaloriestra changed the title Faster startup (3/4): Inline app-init and hydrogen-init hooks Faster startup: Inline app-init and hydrogen-init hooks Apr 6, 2026
@gonzaloriestra gonzaloriestra changed the base branch from faster-startup-2 to graphite-base/7053 April 7, 2026 11:18
@gonzaloriestra gonzaloriestra changed the base branch from graphite-base/7053 to faster-startup April 7, 2026 11:18
@gonzaloriestra gonzaloriestra force-pushed the faster-startup branch 2 times, most recently from 4dff85c to 7590cda Compare April 9, 2026 10:15
@gonzaloriestra gonzaloriestra force-pushed the faster-startup-3 branch 2 times, most recently from 14a02dd to 3d49ec6 Compare April 9, 2026 10:24
@gonzaloriestra gonzaloriestra force-pushed the faster-startup branch 2 times, most recently from 5ede813 to db63872 Compare April 13, 2026 12:32
@gonzaloriestra gonzaloriestra marked this pull request as ready for review April 14, 2026 09:04
@gonzaloriestra gonzaloriestra requested a review from a team as a code owner April 14, 2026 09:04
@gonzaloriestra gonzaloriestra force-pushed the faster-startup-3 branch 2 times, most recently from 4bffb74 to a4c95cc Compare April 14, 2026 09:26
Replace re-exports from @shopify/app and @shopify/cli-hydrogen with
inlined versions. app-init now lazily imports LocalStorage instead of
pulling the entire @shopify/app chain (~1s). hydrogen-init skips loading
@shopify/cli-hydrogen entirely for non-hydrogen commands (~300ms).

Startup time: 710ms → 530ms (-25%)

Made-with: Cursor
@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';
/**
 * 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>;
/**
 * Subclass of oclif's Config that loads command classes on demand for faster CLI startup.
 */
export declare class ShopifyConfig extends Config {
    private lazyCommandLoader?;
    /**
     * 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>;
}

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

Base automatically changed from faster-startup to main April 27, 2026 11:34
@gonzaloriestra gonzaloriestra added this pull request to the merge queue Apr 27, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Apr 27, 2026
@gonzaloriestra gonzaloriestra added this pull request to the merge queue Apr 27, 2026
Merged via the queue into main with commit 18bce2d Apr 27, 2026
25 of 47 checks passed
@gonzaloriestra gonzaloriestra deleted the faster-startup-3 branch April 27, 2026 14:42
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.

2 participants