Skip to content

fix(build)!: remove custom moduleSideEffects config - #4164

Merged
pi0 merged 11 commits into
mainfrom
fix/side-effects
Mar 29, 2026
Merged

pi0 merged 11 commits into
mainfrom
fix/side-effects

Conversation

@pi0

@pi0 pi0 commented Mar 29, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #4085

Nitro's custom treeshake.moduleSideEffects function returned false for all modules not in a small allowlist (["unenv/polyfill/"]), overriding the bundler default and causing aggressive tree-shaking that removed side-effect-only imports (e.g. import '@pothos/plugin-prisma'). Dev mode worked fine but production builds broke.

Changes

  • Removed treeshake.moduleSideEffects override from all bundler configs (rolldown, rollup, vite) — bundlers now use their defaults which correctly preserve side-effect imports
  • Removed moduleSideEffects config option from types, defaults, and docs (breaking change — the option was the root cause of the bug)
  • Added regression test (test/vite/side-effects.test.ts) that builds across rolldown/rollup/vite and verifies side-effect imports are preserved
  • Updated bundle size expectations for rolldown/vite in minimal fixture (19kb vs 16kb — we need to investigate since rollup properly treeshakes unctx but rolldown does not)
  • Strip bare node: side-effect imports in Cloudflare presets — added stripBareNodeImports() output plugin that removes no-op import "node:buffer" etc. from generated chunks. These bare imports are hoisted by the bundler during code-splitting and cause issues on worker runtimes. Applied to both cloudflare-pages and cloudflare-module presets.
  • Workaround for Cloudflare + Rollup — rollup (vite 7) entry format fix
  • Workaround for Rollup + Vue Router — moduleSideEffects: () => false in nitro environment config

Breaking changes

  • moduleSideEffects config option has been removed. Users who relied on it for marking modules with side effects no longer need it — bundlers now respect side-effect imports by default. Users can still use rolldownConfig.treeshake.moduleSideEffects or rollupConfig.treeshake.moduleSideEffects directly for advanced use cases.

Test plan

  • pnpm vitest run test/vite/side-effects.test.ts — regression test across rolldown/rollup/vite
  • pnpm vitest run test/minimal/minimal.test.ts — bundle sizes verified
  • pnpm vitest run test/presets/node.test.ts — no regressions
  • pnpm vitest run test/presets/cloudflare-pages.test.ts — cloudflare bare node: imports stripped, all tests pass
  • pnpm format && pnpm typecheck — clean

🤖 Generated with Claude Code

The `moduleSideEffects` function returned `false` for all modules not in a
small allowlist, causing bundlers to drop side-effect-only imports like
`import '@pothos/plugin-prisma'`. This broke plugin-pattern libraries that
register themselves through side effects.

For rolldown: return `true` only for user source files (rootDir, excluding
node_modules) so libraries with `sideEffects: false` in package.json still
get tree-shaken while user code side effects are preserved.

For rollup: use `moduleSideEffects: true` since `@rollup/plugin-node-resolve`
handles package.json sideEffects at a higher precedence level.

Also drops the `moduleSideEffects` config option (breaking change) as the
new logic no longer needs a user-configurable allowlist.

Closes #4085
@vercel

vercel Bot commented Mar 29, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
nitro.build Ready Ready Preview, Comment Mar 29, 2026 10:29pm

Request Review

@coderabbitai

coderabbitai Bot commented Mar 29, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 45f6c366-211f-421a-9c7f-825d882269e7

📥 Commits

Reviewing files that changed from the base of the PR and between 8e4eec2 and d0c1940.

📒 Files selected for processing (1)
  • src/presets/cloudflare/preset.ts

📝 Walkthrough

Walkthrough

Removed Nitro's moduleSideEffects setting and its default, deleted bundler treeshake.moduleSideEffects overrides (Rollup, Rolldown, Vite), updated docs and types, and added tests/fixtures and example config to validate and demonstrate explicit treeshake behavior.

Changes

Cohort / File(s) Summary
Config, types & docs
docs/3.config/0.index.md, src/types/config.ts, src/config/defaults.ts
Removed moduleSideEffects from documentation, default Nitro config and the NitroOptions type.
Bundler treeshake logic
src/build/rollup/config.ts, src/build/rolldown/config.ts, src/build/vite/bundler.ts
Removed custom treeshake.moduleSideEffects(id) override so bundlers use their defaults or externally supplied overrides.
Side-effects test fixture
test/vite/side-effects-fixture/nitro.config.ts, test/vite/side-effects-fixture/vite.config.ts, test/vite/side-effects-fixture/server.ts, test/vite/side-effects-fixture/server/utils/registry.ts, test/vite/side-effects-fixture/server/utils/register-items.ts
Added fixture implementing a registry populated by a top-level side-effect module and a server handler that returns registered items.
Side-effects test suite
test/vite/side-effects.test.ts
Added Vitest suite building with rolldown, rollup, and vite and asserting side-effect modules are preserved in builds.
Examples & docs update
docs/4.examples/vite-ssr-vue-router.md, examples/vite-ssr-vue-router/vite.config.mjs
Added example nitro Vite environment showing how to set treeshake.moduleSideEffects manually.
Tests & thresholds
test/examples.test.ts, test/minimal/minimal.test.ts
Improved error logging for unexpected HTTP responses; adjusted bundle size thresholds for rolldown and vite.
Cloudflare preset plugin
src/presets/cloudflare/preset.ts
Appended a Rollup output plugin to strip bare node: side-effect imports from generated chunks.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'fix(build)!: remove custom moduleSideEffects config' follows conventional commits format with proper scope, breaking change indicator (!), and clearly describes the main change being implemented.
Description check ✅ Passed The description comprehensively explains the bug being fixed, the breaking change, and all modifications made across bundler configs, types, defaults, and tests.
Linked Issues check ✅ Passed The PR successfully addresses all objectives from issue #4085: removes the custom moduleSideEffects override from all bundler configs, eliminates the configuration option entirely, and adds regression tests to verify side-effect imports are preserved.
Out of Scope Changes check ✅ Passed All changes are directly related to the objective of removing moduleSideEffects: documentation removal, bundler config cleanup, type/defaults updates, and regression tests are all in-scope. Bundle size threshold updates are justified by the functional change.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/side-effects

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 and usage tips.

@pi0 pi0 changed the title fix(build): stop aggressive tree-shaking of side-effect imports fix(build)!: stop aggressive tree-shaking of side-effect imports Mar 29, 2026

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 4

🧹 Nitpick comments (1)
test/vite/side-effects-fixture/server.ts (1)

1-2: Broaden this fixture to cover package/workspace side-effect imports.

This only proves that a relative source-file import survives bundling. The regression in #4085 also involved side-effect-only package/workspace imports, so this fixture can still pass while the original failure remains untested.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@test/vite/side-effects-fixture/server.ts` around lines 1 - 2, The fixture
only imports a relative source-file side-effect; add side-effect-only imports
that simulate package/workspace modules to cover the regression: add imports for
a published-package style module (e.g. import "example-package/register";) and a
workspace-scoped module (e.g. import "@workspace/example/register";) alongside
the existing "./server/utils/register-items.ts" import, and ensure the test
still references getItems to assert the side-effects ran; update any
mock/package entries in the fixture setup so those module specifiers resolve
during bundling.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/build/rolldown/config.ts`:
- Around line 42-44: The moduleSideEffects check currently only treats files
under nitro.options.rootDir as side-effectful, missing workspace-local packages;
update the moduleSideEffects function to return true when id startsWith either
nitro.options.rootDir or the resolved workspaceDir and still exclude paths that
match NODE_MODULES_RE (i.e., use startsWith(rootDir) || startsWith(workspaceDir)
&& !NODE_MODULES_RE.test(id)); reference the moduleSideEffects function,
nitro.options.rootDir, workspaceDir, and NODE_MODULES_RE when making the change.

In `@src/build/vite/bundler.ts`:
- Around line 43-47: The treeshake.moduleSideEffects function currently uses a
simple id.startsWith(nitro.options.rootDir) check which misclassifies sibling
workspace packages as side-effect-free; replace that rootDir-only heuristic with
the same workspace-aware predicate used elsewhere in the build (i.e., reuse the
workspace-aware module/source check the Rollup config uses) so
moduleSideEffects(id: string) returns the correct boolean for files inside
sibling workspace packages while still excluding "/node_modules/". Update the
moduleSideEffects implementation to call that shared predicate (or import it)
instead of id.startsWith(nitro.options.rootDir).

In `@test/vite/side-effects.test.ts`:
- Around line 13-37: The test "preserves side-effect imports" relies on the
shared mutable variable outDir being set in the separate "build" test, making
order-dependent and brittle tests; move the build/setup logic into a test
scaffold (e.g., beforeAll or beforeEach) so outDir is initialized and the Nitro
build is created regardless of test ordering: call createNitro(...) with rootDir
fixtureDir, output.dir = outDir (derived from tmpDir and builder), then await
prepare(nitro) and await build(nitro) inside the setup hook and remove the
standalone "build" test (or keep it as a no-op), ensuring the tests that
reference outDir (the "preserves side-effect imports" case that imports
server/index.mjs and calls the exported fetch) are decoupled from test execution
order; reference variables/functions: outDir, tmpDir, builder, fixtureDir,
createNitro, prepare, build, and the "preserves side-effect imports" test.
- Around line 1-2: The test currently uses a raw filesystem path (with
fileURLToPath imported) for a dynamic import which breaks on Windows; replace
that approach by importing pathToFileURL from "node:url", convert the built
entry filesystem path to a file:// URL using pathToFileURL(...) and pass that
URL (or its href) into the dynamic import() call so the test imports the module
cross-platform; update any places referencing fileURLToPath or the raw path to
use pathToFileURL before calling import().

---

Nitpick comments:
In `@test/vite/side-effects-fixture/server.ts`:
- Around line 1-2: The fixture only imports a relative source-file side-effect;
add side-effect-only imports that simulate package/workspace modules to cover
the regression: add imports for a published-package style module (e.g. import
"example-package/register";) and a workspace-scoped module (e.g. import
"@workspace/example/register";) alongside the existing
"./server/utils/register-items.ts" import, and ensure the test still references
getItems to assert the side-effects ran; update any mock/package entries in the
fixture setup so those module specifiers resolve during bundling.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 40d27e60-1398-4bbe-a363-3e3f1a61c54f

📥 Commits

Reviewing files that changed from the base of the PR and between 5cd5cb2 and ebb9329.

📒 Files selected for processing (12)
  • docs/3.config/0.index.md
  • src/build/rolldown/config.ts
  • src/build/rollup/config.ts
  • src/build/vite/bundler.ts
  • src/config/defaults.ts
  • src/types/config.ts
  • test/vite/side-effects-fixture/nitro.config.ts
  • test/vite/side-effects-fixture/server.ts
  • test/vite/side-effects-fixture/server/utils/register-items.ts
  • test/vite/side-effects-fixture/server/utils/registry.ts
  • test/vite/side-effects-fixture/vite.config.ts
  • test/vite/side-effects.test.ts
💤 Files with no reviewable changes (3)
  • docs/3.config/0.index.md
  • src/config/defaults.ts
  • src/types/config.ts

Comment thread src/build/rolldown/config.ts Resolved
Comment thread src/build/vite/bundler.ts Resolved
Comment thread test/vite/side-effects.test.ts Resolved
Comment thread test/vite/side-effects.test.ts Resolved
@pi0 pi0 changed the title fix(build)!: stop aggressive tree-shaking of side-effect imports fix(build)!: remove custom moduleSideEffects config Mar 29, 2026
@pi0
pi0 marked this pull request as ready for review March 29, 2026 20:58

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@examples/vite-ssr-vue-router/vite.config.mjs`:
- Line 11: The example config currently forces Nitro's
rollupOptions.treeshake.moduleSideEffects to () => false which reintroduces the
side-effect tree-shaking regression; remove this override from the nitro
configuration (the rollupOptions -> treeshake -> moduleSideEffects entry) so the
example relies on the bundler's default behavior (i.e., delete that
moduleSideEffects line or stop setting moduleSideEffects entirely).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 38a5fb60-2286-426a-9e5a-7a7cc3a9f46a

📥 Commits

Reviewing files that changed from the base of the PR and between 08e605c and 8e4eec2.

📒 Files selected for processing (3)
  • docs/4.examples/vite-ssr-vue-router.md
  • examples/vite-ssr-vue-router/vite.config.mjs
  • test/examples.test.ts

@pi0
pi0 merged commit 55f30f4 into main Mar 29, 2026
7 checks passed
@pi0
pi0 deleted the fix/side-effects branch March 29, 2026 22:30
DysektAI added a commit to tarkovtracker-org/TarkovTracker that referenced this pull request Jul 11, 2026
Nitro 2.13.4 leaves side-effect-only `import "node:*"` statements in
dist/_worker.js, which Cloudflare Pages' deploy esbuild warns about as
ignored-bare-import (unenv polyfills declare sideEffects:false). After
compile, strip only bare node: imports and leave named from "node:*"
imports that nitropack needs. Mirrors the not-yet-released approach in
nitrojs/nitro#4164 for cloudflare-pages.
github-actions Bot pushed a commit to tarkovtracker-org/TarkovTracker that referenced this pull request Jul 12, 2026
# [1.56.0](v1.55.7...v1.56.0) (2026-07-12)

### Bug Fixes

* **config:** address production-readiness review findings ([3af3b2c](3af3b2c))
* **config:** avoid CodeQL TOCTOU findings in CF worker postprocess ([921155d](921155d))
* **config:** declare empty preview APP_URL and analytics vars ([3c66b51](3c66b51))
* **ci:** harden link check and drop bogus changelog issue refs ([959c35e](959c35e)), closes [hi#cardinality](https://github.com/hi/issues/cardinality)
* **config:** quiet production build noise from sitemap runtime path ([e3a03a1](e3a03a1))
* **config:** strip bare node: side-effect imports from CF worker ([2725338](2725338)), closes [nitrojs/nitro#4164](nitrojs/nitro#4164)
* **config:** type readdir Dirent as utf8 string names ([480a563](480a563))

### Features

* **api:** harden rate limiting with per-IP backstop, token cap, and 429 observability ([#523](#523)) ([b34c04d](b34c04d)), closes [hi#cardinality](https://github.com/hi/issues/cardinality)
MathurAditya724 added a commit to getsentry/junior that referenced this pull request Nov 12, 2026
…#3284)

Fixes two stale references. No behavior change.

- `packages/junior/src/build/rolldown-workarounds.ts`: replace the
placeholder `nitrojs/nitro/issues/XXXX` link with the real issue,
nitrojs/nitro#4085. The TODO now names an owner and a removal condition,
per `policies/code-comments.md`. nitrojs/nitro#4164 removed the
allowlist upstream, and the oldest Nitro version that Junior supports
(`3.0.260415-beta`) includes that fix. So the override can probably go,
but this PR does not remove it. That needs a production build check that
pi-ai providers still register without it.
- `.agents/skills/vercel-sandbox/SOURCES.md`: #1567 removed
`chat/runtime/reply-executor.ts`. The row now points at
`chat/providers/slack/turn.ts` and
`chat/task-execution/conversation-turn.ts`, which save each new sandbox
ref through `onSandboxRefChanged`. This matches `SKILL.md` and
`references/api-surface.md`. The old "success-path only" description was
also stale, so it is replaced.

<!-- junior-request-attribution:start -->
via @MathurAditya724.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:agent%3A1704862f6367434a6de8ff84bf6d5be1 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/agent%3A1704862f6367434a6de8ff84bf6d5be1)
[[Sentry]](https://sentry.sentry.io/explore/conversations/agent%3A1704862f6367434a6de8ff84bf6d5be1/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: Aditya Mathur <[email protected]>

This branch was successfully deployed

1 active deployment
Preview — d0c19409 Deployed Mar 29, 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.

Nitro's moduleSideEffects defaults override Rolldown settings, causing aggressive tree-shaking

1 participant