Skip to content

feat: expose programmatic watch controls - #1062

Merged
sxzz merged 2 commits into
mainfrom
codex/programmatic-watch-handle
Sep 3, 2026
Merged

sxzz merged 2 commits into
mainfrom
codex/programmatic-watch-handle

Conversation

@sxzz

@sxzz sxzz commented Sep 3, 2026 •

Copy link
Copy Markdown
Member

AI usage

  • No AI was used in this PR.
  • AI was used: Codex + GPT-5
    • I have carefully reviewed the AI-generated content myself.

Description

Expose watch lifecycle controls to programmatic consumers through TsdownHandle and UserConfigFnContext.

  • Return { bundles, watch } from build().
  • Add watch.restart(), returning the next handle, and watch.close() for graceful cleanup.
  • Reject watch controls before config resolution or outside watch mode.
  • Wait for watcher initialization and coalesce duplicate automatic restarts from multiple watchers.

This is a breaking change to the return type of build().

Linked Issues

N/A

Additional context

Verified with pnpm format, pnpm lint, pnpm typecheck, pnpm test run, pnpm build, and local watch lifecycle checks covering rebuild, restart, close, config reload, multiple watchers, and CLI shortcuts.

@netlify

netlify Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for tsdown-main ready!

Name Link
🔨 Latest commit 866ef37
🔍 Latest deploy log https://app.netlify.com/projects/tsdown-main/deploys/6a99a6514212de0007296fa5
😎 Deploy Preview https://deploy-preview-1062--tsdown-main.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@sxzz
sxzz marked this pull request as ready for review September 3, 2026 13:13
Copilot AI lite review requested due to automatic review settings September 3, 2026 13:13
@pkg-pr-new

pkg-pr-new Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

tsdown

pnpm add https://pkg.pr.new/tsdown@1062 -D
npm i https://pkg.pr.new/tsdown@1062 -D
yarn add https://pkg.pr.new/[email protected] -D

create-tsdown

pnpm add https://pkg.pr.new/create-tsdown@1062 -D
npm i https://pkg.pr.new/create-tsdown@1062 -D
yarn add https://pkg.pr.new/[email protected] -D

@tsdown/css

pnpm add https://pkg.pr.new/@tsdown/css@1062 -D
npm i https://pkg.pr.new/@tsdown/css@1062 -D
yarn add https://pkg.pr.new/@tsdown/[email protected] -D

@tsdown/exe

pnpm add https://pkg.pr.new/@tsdown/exe@1062 -D
npm i https://pkg.pr.new/@tsdown/exe@1062 -D
yarn add https://pkg.pr.new/@tsdown/[email protected] -D

tsdown-migrate

pnpm add https://pkg.pr.new/tsdown-migrate@1062 -D
npm i https://pkg.pr.new/tsdown-migrate@1062 -D
yarn add https://pkg.pr.new/[email protected] -D

commit: 866ef37

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Automatic and shortcut-triggered restarts can currently create unhandled promise rejections because async restart() calls are invoked without proper awaiting/catching in watch flows.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR exposes programmatic watch lifecycle controls by introducing a TsdownHandle return type (instead of TsdownBundle[]) and threading watch controls into config-function context so programmatic consumers can restart/close watch builds.

Changes:

  • Changed build() to return TsdownHandle with { bundles, watch: { restart, close } } and added restart/close proxying and watch-mode guards.
  • Added UserConfigFnContext.watch and propagated context (including rootConfig) through config loading and workspace resolution.
  • Updated docs, tests, and public .d.ts snapshots to reflect the new API surface.
File summaries
File Description
tests/utils.ts Updates test helper typings to accept the new TsdownHandle return type.
tests/e2e.test.ts Adapts tests to the new resolveConfig signature by providing a dummy watch controller.
src/utils/chunks.ts Introduces TsdownHandle interface to represent programmatic build/watch handles.
src/features/shortcuts.ts Adjusts shortcut types/signature to accept async restart actions.
src/features/plugin.test.ts Adapts tests to the new resolveConfig signature with a dummy watch controller.
src/config/workspace.ts Threads UserConfigFnContext through workspace config loading and sets rootConfig.
src/config/types.ts Defines UserConfigFnContext (including watch) and exports TsdownHandle types.
src/config/index.ts Changes resolveConfig() to accept watch controls and pass them into config loading context.
src/config/file.ts Updates loadConfigFile() to accept UserConfigFnContext and pass it to config functions.
src/config.ts Re-exports UserConfigFnContext from the public config module.
src/build.ts Implements new TsdownHandle return shape and watch restart/close lifecycle behavior.
package.json Bumps inlined pkg-types version.
docs/zh-CN/advanced/programmatic-usage.md Updates programmatic usage docs to describe TsdownHandle and watch controls.
docs/advanced/programmatic-usage.md Updates English programmatic usage docs for TsdownHandle and watch controls.
snapshots/tsnapi/index.snapshot.d.ts Updates API snapshot to include TsdownHandle, UserConfigFnContext, and new build signatures.
snapshots/tsnapi/config.snapshot.d.ts Updates config API snapshot to include UserConfigFnContext.
Review details

Suppressed comments (1)

src/build.ts:149

  • restart() can reject (e.g. rebuild failure or repeated restart). Because the readline handler in shortcuts() awaits the action without its own try/catch, passing restart directly can lead to unhandled promise rejections during watch shortcuts. Wrapping restart() here to log-and-swallow errors keeps the watch UI stable.
  if (hasWatchConfig) {
    // Watch mode with shortcuts
    disposeCbs.push(shortcuts(restart))
  • Files reviewed: 14/16 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/build.ts
Comment on lines +135 to +137
() => {
if (!restarted) restart()
},
@socket-security

socket-security Bot commented Sep 3, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedtsup@​7.3.0 ⏵ 8.5.198 +1100 +195 +183 +33100

View full report

@sxzz
sxzz force-pushed the codex/programmatic-watch-handle branch from 1f7d098 to 03633c9 Compare September 3, 2026 16:52
@sxzz
sxzz force-pushed the codex/programmatic-watch-handle branch from 03633c9 to 866ef37 Compare September 3, 2026 16:54
@sxzz
sxzz merged commit 96a4098 into main Sep 3, 2026
17 checks passed
@sxzz
sxzz deleted the codex/programmatic-watch-handle branch September 3, 2026 16:56
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