-
Notifications
You must be signed in to change notification settings - Fork 247
Lazy command loading: Option B — oclif pattern strategy with thin re-exports #7301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
byrichardpowell
wants to merge
4
commits into
main
Choose a base branch
from
lazy-loading-option-b
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ead29ba
Lazy command loading: oclif pattern strategy with thin re-exports (Op…
byrichardpowell 18f5823
Add ShopifyConfig with non-blocking hooks for faster startup
byrichardpowell 60b9fca
Rewrite lazy-loading-options.md to match actual branch contents
byrichardpowell 1a61fa9
Remove lazy loading docs from repo (moved to Google Docs)
byrichardpowell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/cli-kit/src/public/node/custom-oclif-loader.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import {fileExistsSync} from './fs.js' | ||
| import {cwd, joinPath, sniffForPath} from './path.js' | ||
| import {isDevelopment} from './context/local.js' | ||
| import {execaSync} from 'execa' | ||
| import {Command, Config} from '@oclif/core' | ||
| import {Options} from '@oclif/core/interfaces' | ||
|
|
||
| /** | ||
| * Custom oclif Config subclass for the Shopify CLI. | ||
| * | ||
| * This extends the stock oclif Config with two changes: | ||
| * 1. Hydrogen monorepo detection for dev mode (pre-existing, unrelated to lazy loading) | ||
| * 2. Non-blocking init hooks — the 'init' event fires in the background so the CLI | ||
| * doesn't wait for plugin init hooks (app-init, hydrogen-init) before running commands. | ||
| * These hooks do background setup (clearing caches, setting env vars) that doesn't | ||
| * need to complete before the target command executes. | ||
| */ | ||
| export class ShopifyConfig extends Config { | ||
| constructor(options: Options) { | ||
| if (isDevelopment()) { | ||
| // eslint-disable-next-line @shopify/cli/no-process-cwd | ||
| const currentPath = cwd() | ||
|
|
||
| let path = sniffForPath() ?? currentPath | ||
| // Hydrogen CI uses `hydrogen/hydrogen` path, while local dev uses `shopify/hydrogen`. | ||
| const currentPathMightBeHydrogenMonorepo = /(shopify|hydrogen)\/hydrogen/i.test(currentPath) | ||
| const ignoreHydrogenMonorepo = process.env.IGNORE_HYDROGEN_MONOREPO | ||
| if (currentPathMightBeHydrogenMonorepo && !ignoreHydrogenMonorepo) { | ||
| path = execaSync('npm', ['prefix']).stdout.trim() | ||
| } | ||
| if (fileExistsSync(joinPath(path, 'package.json'))) { | ||
| options.pluginAdditions = { | ||
| core: ['@shopify/cli-hydrogen'], | ||
| path, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| super(options) | ||
|
|
||
| if (isDevelopment()) { | ||
| // @ts-expect-error: This is a private method that we are overriding. OCLIF doesn't provide a way to extend it. | ||
| this.determinePriority = this.customPriority | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Override runHook to make init hooks non-blocking for faster startup. | ||
| * Init hooks (app-init, hydrogen-init) set up LocalStorage and check hydrogen — | ||
| * these are setup tasks that don't need to complete before commands run. | ||
| * | ||
| * @param event - The hook event name. | ||
| * @param opts - Options to pass to the hook. | ||
| * @param timeout - Optional timeout for the hook. | ||
| * @param captureErrors - Whether to capture errors instead of throwing. | ||
| * @returns The hook result with successes and failures arrays. | ||
| */ | ||
| // @ts-expect-error: overriding with looser types for hook interception | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| async runHook(event: string, opts: any, timeout?: number, captureErrors?: boolean): Promise<any> { | ||
| if (event === 'init' || event === 'prerun' || event === 'postrun') { | ||
| // Fire init, prerun, and postrun hooks in background — they don't need to block. | ||
| // - Init hooks: background setup (clearing caches, setting env vars) | ||
| // - Prerun hooks: analytics tracking, upgrade checks (best-effort) | ||
| // - Postrun hooks: analytics reporting (best-effort) | ||
| // eslint-disable-next-line no-void | ||
| void super.runHook(event, opts, timeout, captureErrors) | ||
| return {successes: [], failures: []} | ||
| } | ||
| return super.runHook(event, opts, timeout, captureErrors) | ||
| } | ||
|
|
||
| /** | ||
| * Custom priority logic for plugin commands. | ||
| * In development mode, external cli-hydrogen commands take priority over bundled ones. | ||
| * | ||
| * @param commands - The commands to sort. | ||
| * @returns The highest priority command. | ||
| */ | ||
| customPriority(commands: Command.Loadable[]): Command.Loadable | undefined { | ||
| const oclifPlugins = this.pjson.oclif.plugins ?? [] | ||
| const commandPlugins = commands.sort((aCommand, bCommand) => { | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| const pluginAliasA = aCommand.pluginAlias ?? 'A-Cannot-Find-This' | ||
| // eslint-disable-next-line no-restricted-syntax | ||
| const pluginAliasB = bCommand.pluginAlias ?? 'B-Cannot-Find-This' | ||
| const aIndex = oclifPlugins.indexOf(pluginAliasA) | ||
| const bIndex = oclifPlugins.indexOf(pluginAliasB) | ||
|
|
||
| // If there is an external cli-hydrogen plugin, its commands should take priority over bundled ('core') commands | ||
| if (aCommand.pluginType === 'core' && bCommand.pluginAlias === '@shopify/cli-hydrogen') { | ||
| return 1 | ||
| } | ||
|
|
||
| if (aCommand.pluginAlias === '@shopify/cli-hydrogen' && bCommand.pluginType === 'core') { | ||
| return -1 | ||
| } | ||
|
|
||
| // All other cases are the default implementation from the private `determinePriority` method | ||
| if (aCommand.pluginType === 'core' && bCommand.pluginType === 'core') { | ||
| return aIndex - bIndex | ||
| } | ||
|
|
||
| if (bCommand.pluginType === 'core' && aCommand.pluginType !== 'core') { | ||
| return 1 | ||
| } | ||
|
|
||
| if (aCommand.pluginType === 'core' && bCommand.pluginType !== 'core') { | ||
| return -1 | ||
| } | ||
|
|
||
| if (aCommand.pluginType === 'jit' && bCommand.pluginType !== 'jit') { | ||
| return 1 | ||
| } | ||
|
|
||
| if (bCommand.pluginType === 'jit' && aCommand.pluginType !== 'jit') { | ||
| return -1 | ||
| } | ||
|
|
||
| return 0 | ||
| }) | ||
| return commandPlugins[0] | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
runHook()intentionally firesinit/prerun/postrunhooks in the background, but thevoid super.runHook(...)promise isn’t handled. If any of those hooks throw/reject, this becomes an unhandled rejection (can crash the process or emit warnings depending on Node settings). Attach a.catch()handler (and optionally route errors to existing error/telemetry handling) to keep failures best-effort without destabilizing the CLI.