Skip to content

Conversation

@logaretm
Copy link
Contributor

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

I took a look at this while working on getsentry/sentry-javascript#19304 and thought about how to address this and I though we could first do a stop gap measure to prevent the worst case which wipes all user code mappings along with the lib code.

So the change from some to every effectively makes the mappings get wiped only if the entire chunk is from node_modules, if some user code makes into the chunk it doesn't get wiped.

In a follow up PR, we can try more aggressive approaches like parsing the VLQ and find the segments containing node_modules and strip only those. Not sure if this can work tho.

But this fixes #3826 from what I tested and means I don't have to explicitly disable it for the SDK.

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@logaretm logaretm requested a review from pi0 as a code owner February 12, 2026 20:46
Copilot AI review requested due to automatic review settings February 12, 2026 20:46
@vercel
Copy link

vercel bot commented Feb 12, 2026

@logaretm is attempting to deploy a commit to the Nitro Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link

coderabbitai bot commented Feb 12, 2026

πŸ“ Walkthrough

Walkthrough

This PR modifies the sourcemap minification plugin to change when sourcemap mappings are cleared. Instead of clearing mappings when any source path contains "node_modules", it now only clears mappings when all source paths contain "node_modules". Comprehensive unit tests are added to validate this behavior and edge cases.

Changes

Cohort / File(s) Summary
Sourcemap minification logic
src/build/plugins/sourcemap-min.ts
Changed conditional from clearing mappings on ANY node_modules match to ALL node_modules match in source paths. Control flow altered from broad to stricter matching.
Plugin test suite
test/unit/sourcemap-min.test.ts
Added 112 lines of new unit tests validating sourcesContent removal, x_google_ignoreList removal, mappings preservation for mixed library/user code, and edge cases with empty sources arrays.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

πŸš₯ Pre-merge checks | βœ… 5 | ❌ 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 (5 passed)
Check name Status Explanation
Description check βœ… Passed The PR description clearly explains the bug fix, the change from 'some' to 'every' logic, and references the related issue #3826 that it aims to resolve.
Linked Issues check βœ… Passed The code changes directly address issue #3826 by modifying the condition to preserve sourcemap mappings when user code is present in chunks, fixing the empty mappings bug.
Out of Scope Changes check βœ… Passed All changes are directly scoped to fixing the sourcemap mappings issue; modifications to sourcemap-min.ts and addition of comprehensive unit tests are both aligned with the stated objective.
Merge Conflict Detection βœ… Passed βœ… No merge conflicts detected when merging into main
Title check βœ… Passed The title 'fix: preserve sourcemap mappings for chunks containing user code' follows conventional commits format with 'fix:' prefix and clearly describes the main behavioral change in the pull request.

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

✨ Finishing touches
  • πŸ“ Generate docstrings
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

Copy link
Member

@pi0 pi0 left a comment

Choose a reason for hiding this comment

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

Thanks, my friend ❀️

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adjusts Nitro’s sourcemapMinify build plugin to avoid wiping sourcemap mappings when a chunk contains any user code, addressing cases where mixed (user + node_modules) chunks were losing all mappings.

Changes:

  • Change the β€œwipe mappings” condition from some() to every() so only pure node_modules chunks get their mappings cleared.
  • Add unit tests covering removal of sourcesContent / x_google_ignoreList, wiping behavior for pure library chunks, and preservation for user/mixed chunks.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/build/plugins/sourcemap-min.ts Adjusts the heuristic for when to blank sourcemap mappings.
test/unit/sourcemap-min.test.ts Adds unit tests to validate the new mappings-wiping/preservation behavior.

πŸ’‘ Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}),
};
const results = runPlugin(bundle);
expect(results["chunk.mjs.map"].mappings).toBe("");
Copy link

Copilot AI Feb 12, 2026

Choose a reason for hiding this comment

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

The "handles empty sources array" assertion expects mappings to be wiped when sources: []. With the current implementation this happens because .every(...) is true for an empty array, but an empty sources list doesn't indicate a "pure library chunk" and can lead to dropping mappings for sourcemaps where sources is empty/missing. Consider changing this test to expect mappings to be preserved for empty/undefined sources, and keep the wiping behavior covered by the "pure library chunks" test (where all sources are under node_modules).

Suggested change
expect(results["chunk.mjs.map"].mappings).toBe("");
expect(results["chunk.mjs.map"].mappings).toBe("AAAA,CAAC");

Copilot uses AI. Check for mistakes.
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 12, 2026

Open in StackBlitz

npm i https://pkg.pr.new/nitrojs/nitro@4031

commit: a7e3667

@pi0 pi0 changed the title fix: Preserve sourcemap mappings for user code fix: preserve sourcemap mappings for chunks containing user code Feb 12, 2026
@pi0 pi0 merged commit 0f4c665 into nitrojs:main Feb 12, 2026
15 of 17 checks passed
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/vite strips mappings when generating sourcemaps

2 participants