diff --git a/packages/cli/src/hooks/app-init.ts b/packages/cli/src/hooks/app-init.ts index 9a30373a7a..5cb417492a 100644 --- a/packages/cli/src/hooks/app-init.ts +++ b/packages/cli/src/hooks/app-init.ts @@ -1 +1,20 @@ -export {AppInitHook as default} from '@shopify/app' +import {Hook} from '@oclif/core' +import {randomUUID} from 'crypto' + +/** + * Inlined version of the `\@shopify/app` init hook. + * The original hook imports `\@shopify/app` → local-storage → cli-kit error chain (~1s). + * This inlined version avoids those imports entirely. It lazily imports the + * LocalStorage class only at call time, and uses Node's native crypto. + */ +const init: Hook<'init'> = async (_options) => { + // Lazy import to clear the command storage (equivalent to clearCachedCommandInfo) + const {LocalStorage} = await import('@shopify/cli-kit/node/local-storage') + const store = new LocalStorage({projectName: 'shopify-cli-app-command'}) + store.clear() + + // Set a unique run ID so parallel commands don't collide in the cache + process.env.COMMAND_RUN_ID = randomUUID() +} + +export default init diff --git a/packages/cli/src/hooks/hydrogen-init.ts b/packages/cli/src/hooks/hydrogen-init.ts index 5e0fc7ebf5..8ddb44ca71 100644 --- a/packages/cli/src/hooks/hydrogen-init.ts +++ b/packages/cli/src/hooks/hydrogen-init.ts @@ -1,4 +1,21 @@ -import {HOOKS} from '@shopify/cli-hydrogen' +import {Hook} from '@oclif/core' + +/** + * Hydrogen init hook — only loads `\@shopify/cli-hydrogen` when running a hydrogen command. + * This avoids the ~300ms+ import cost for non-hydrogen commands. + */ +const hook: Hook<'init'> = async (options) => { + // The hydrogen init hook only does work for hydrogen commands. + // Skip loading the heavy module entirely for non-hydrogen commands. + if (!options.id?.startsWith('hydrogen:') || options.id === 'hydrogen:init') { + return + } + + const {HOOKS} = await import('@shopify/cli-hydrogen') + if (HOOKS.init && typeof HOOKS.init === 'function') { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + await (HOOKS.init as any).call(this, options) + } +} -const hook = HOOKS.init export default hook