Skip to content

fix(config): merge preset and user hooks instead of overriding - #4809

Merged
pi0 merged 3 commits into
mainfrom
fix/preset-hooks-merge
Oct 17, 2026
Merged

pi0 merged 3 commits into
mainfrom
fix/preset-hooks-merge

Conversation

@pi0x

@pi0x pi0x commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Resolves #1382

Config layers were merged with plain defu, so when two layers defined a hook with the same name (for example a preset's compiled hook and hooks.compiled in the user config), the higher priority one replaced the other and the preset hook never ran.

This PR merges the top-level hooks key with hookable's mergeHooks instead:

  • Config loader: a custom c12 merger (and the same handling in the existing envMerger). Every layer's hook now runs, lowest priority first: base preset, preset, config file, $env overrides, then programmatic overrides. So user hooks run after the preset's and can work with what the preset produced.
  • Vite plugin: setupNitroContext used plain defu across nitro({...}) options, plugin config and the Vite config's nitro key, so the same problem happened there. It now uses the same merger. Order: preset, nitro.config, Vite config nitro key, then nitro() options.
  • The helper lives in src/config/_merge.ts. Nested and flat hook names (rollup: { before } and "rollup:before") are merged together too.

I checked every built-in preset that uses extends. None of them currently redefines a hook that its base preset also defines, so no preset needed changes and nothing runs twice.

Behavior change: before, defining a hook with the same name (even an empty one) silently disabled the preset's hook. That is no longer possible (noted in the config docs), and third-party presets that relied on replacing their base preset's hook will now run both. Also, when several hooks share a name they are combined into one serial async function, so removeHook(name, originalFn) won't find the original function in that case.

Tests:

  • test/unit/config-hooks.test.ts: preset + config + overrides, nested and flat names, $production with base hooks.
  • test/vite/plugin-hooks.test.ts: hooks in both nitro() options and the Vite config's nitro key.

Both fail on main and pass with this change. pnpm typecheck, pnpm test:rollup and the test/vite suite pass.

🤖 Generated with AI assistant

@pi0x
pi0x requested a review from pi0 as a code owner October 17, 2026 12:23
@vercel

vercel Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
nitro.build Ready Ready Preview Oct 17, 2026 12:33pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Currently processing new changes in this PR. This may take a few minutes, please wait...

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7c0d4d66-f7e8-4f53-916a-009f6b573a40
📥 Commits

Reviewing files that changed from the base of the PR and between 5c21e89 and 1f8ac2e.

📒 Files selected for processing: 6
  • docs/3.config/0.index.md
  • src/build/vite/plugin.ts
  • src/config/_merge.ts
  • src/config/loader.ts
  • test/unit/config-hooks.test.ts
  • test/vite/plugin-hooks.test.ts
+------------------------------------------------------------------------+
| Obi-Wan Kenobi has taught me well. I sense a disturbance in your code. |
+------------------------------------------------------------------------+
  \
   \     /)/)
        ( '')
        /  |
      o(___)
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Same-named hooks from base preset, preset, config, $env and overrides layers now all run in order (lowest priority first) using hookable's mergeHooks.
@pkg-pr-new

pkg-pr-new Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/nitro@4809

commit: 1f8ac2e

@pi0
pi0 merged commit 5e61db0 into main Oct 17, 2026
14 of 15 checks passed
@pi0
pi0 deleted the fix/preset-hooks-merge branch October 17, 2026 12:36

This branch was successfully deployed

1 active deployment
Preview — 1f8ac2ef Deployed Oct 17, 2026 by vercel[bot]
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.

Cannot override specific hooks once the preset already registered the same hook

2 participants