Skip to content

Expose preview and deploy from nitro/builder #4617

Closed

Description

@pi0x

Describe the feature

nitro/builder covers dev and build well for tools that drive Nitro programmatically (createNitro with an inline config, then prepare / copyPublicAssets / prerender / build / createDevServer). preview and deploy have no equivalent: their logic lives only in the citty commands under src/cli/commands/, so a wrapper CLI that wants to offer <tool> preview / <tool> deploy has to spawn the nitro bin instead of calling an API.

Spawning works but has rough edges:

  • The wrapper cannot pass its inline config; the Nitro CLI resolves everything from rootDir again. For deploy this means the wrapper must run its own build and then call nitro deploy --prebuilt.
  • getBuildInfo(rootDir) finds the output via node_modules/.nitro/last-build.json under rootDir, falling back to <rootDir>/.output. A tool that rebases output.dir somewhere else must pick a rootDir for the spawned CLI that happens to make the fallback resolve correctly.
  • Preset-specific behaviour tied to the CLI flow (globalThis.__nitroDeploying__ / __nitroDeployed__, used by presets that deploy from a build hook) is not reachable from an API.

Suggested additions to nitro/builder:

  1. Export startPreview (src/preview.ts). It already has a clean shape ({ rootDir, loader } → { fetch, upgrade, close }) and is used by both the CLI command and the Vite preview integration, so this is mostly an export.
  2. A deploy helper extracted from src/cli/commands/deploy.ts, e.g. deploy({ rootDir } | nitro, { args?: string[] }): resolve the build info, error when the preset has no commands.deploy, rewrite the ./ paths relative to outputDir, and run the command. The CLI command would become a thin wrapper around it, and the deploying/deployed globals could be set there so preset hooks keep working when called via the API.
  3. Optionally let getBuildInfo / startPreview accept an explicit outputDir so wrappers that rebase the output do not depend on last-build.json lookup.

Alternatives considered: spawning the nitro bin from the wrapper (works today, with the caveats above), or reimplementing the ~20 lines of deploy logic in the wrapper (drifts from Nitro's behaviour and misses the preset globals).

Happy to open a PR if this direction sounds right.


This issue was drafted with the help of an AI assistant, reviewed by a human before posting.

Additional information

  • Would you be willing to help implement this feature?

Activity

  1. added
    pending triage
    on Sep 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions