fix: Generate high resolution sourcemaps when injecting auto-imports - #2606
Open
zizzfizzix wants to merge 1 commit into
Open
fix: Generate high resolution sourcemaps when injecting auto-imports#2606zizzfizzix wants to merge 1 commit into
zizzfizzix wants to merge 1 commit into
Conversation
`unimport/unplugin` calls `MagicString#generateMap()` with no options, which produces a line-level sourcemap with no `source`. Since auto-imports are injected at the top of the file, tools that rely on columns - like V8 code coverage - see the whole module as a single statement. The result is that any module referencing an auto-imported variable (`browser`, `storage`, ...) silently drops out of coverage reports, which inflates the reported percentage and lets thresholds pass when they shouldn't. This was fixed once in wxt-dev#381, but wxt-dev#1412 replaced the `wxt:unimport` plugin with `UnimportPlugin.vite()` in both the build path and `WxtVitest()`, dropping the `hires` option along the way. Restore the `wxt:unimport` plugin with the sourcemap options from wxt-dev#381, keeping the `enforce: 'post'` ordering and the include/exclude filter that wxt-dev#1412 relied on. The defaults are imported from `unimport/unplugin`, so they can't drift. Both the build path and `WxtVitest()` share the plugin. Fixes wxt-dev#2604 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011PEx8LKp7AtQkR3tJfyFrY
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
unimport/unplugincallsMagicString#generateMap()with no options, which defaults tohires: false— a line-level sourcemap. Because auto-imports are injected at the top of the file, every mapping points back at the first line of the original module, so tools that rely on columns (like V8 code coverage) see the whole module as a single statement.The result: any module referencing an auto-imported variable (
browser,storage, …) silently drops out of coverage reports, inflating the reported percentage and letting thresholds pass when they shouldn't.This was originally fixed for the build path in #381, but #1412 replaced WXT's own
wxt:unimportplugin withUnimportPlugin.vite()in both the build path andWxtVitest(), dropping thehiresoption along the way. So today both paths are affected, not just testing.This PR restores the
wxt:unimportplugin with the sourcemap options from #381 —generateMap({ hires: 'boundary', source: id }).hires: 'boundary'is enough to keep columns accurate and is cheaper than a fullhires: truemap.To keep the maintenance surface minimal, the plugin is the pre-#1412 one plus the two things #1412 actually needed:
enforce: 'post'and a regexinclude/excludefilter. The filter defaults are imported fromunimport/unplugin(defaultIncludes/defaultExcludes) rather than copied, so they can't drift out of sync. Everything else —dtsgeneration, theautoImporttoggle — was unused by WXT and isn't reimplemented.The plugin is shared by
packages/wxt/src/builtin-modules/unimport.tsandpackages/wxt/src/testing/wxt-vitest-plugin.ts, so build and test are fixed with one change.The root cause is filed upstream as unjs/unimport#562 — the bare
s.generateMap()is still onmain, so every unplugin consumer is affected. If unimport changes its default, this plugin can go back to beingUnimportPlugin.vite(options); the JSDoc says so and links the ticket.Manual Testing
Added
packages/wxt/src/builtin-modules/__tests__/unimport.test.ts, which asserts the generated sourcemap has more than one segment per line and thatsourcescontains the module ID. Both assertions fail whenhires: 'boundary'/sourceare removed.End-to-end check with a probe module in
packages/wxt-demothat usesbrowser.runtime.idacross four branches, with a test covering only one of them:Before — the module is invisible to the coverage reporter and reports a perfect score:
After — real numbers, with the uncovered branches correctly attributed:
(The probe files were only used for verification and are not part of this PR.)
Also ran the full
wxtpackage suite (bun run test run→ 53 files / 554 tests passing) andbun run check(ESLint, Oxlint, Publint, TypeScript) inpackages/wxt.Related Issue
This PR closes #2604