Skip to content

fix: Generate high resolution sourcemaps when injecting auto-imports - #2606

Open
zizzfizzix wants to merge 1 commit into
wxt-dev:mainfrom
zizzfizzix:claude/wxt-issue-2604-127tj6
Open

fix: Generate high resolution sourcemaps when injecting auto-imports#2606
zizzfizzix wants to merge 1 commit into
wxt-dev:mainfrom
zizzfizzix:claude/wxt-issue-2604-127tj6

Conversation

@zizzfizzix

Copy link
Copy Markdown
Contributor

Overview

unimport/unplugin calls MagicString#generateMap() with no options, which defaults to hires: 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:unimport plugin with UnimportPlugin.vite() in both the build path and WxtVitest(), dropping the hires option along the way. So today both paths are affected, not just testing.

This PR restores the wxt:unimport plugin with the sourcemap options from #381generateMap({ hires: 'boundary', source: id }). hires: 'boundary' is enough to keep columns accurate and is cheaper than a full hires: true map.

To keep the maintenance surface minimal, the plugin is the pre-#1412 one plus the two things #1412 actually needed: enforce: 'post' and a regex include/exclude filter. The filter defaults are imported from unimport/unplugin (defaultIncludes/defaultExcludes) rather than copied, so they can't drift out of sync. Everything else — dts generation, the autoImport toggle — was unused by WXT and isn't reimplemented.

The plugin is shared by packages/wxt/src/builtin-modules/unimport.ts and packages/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 on main, so every unplugin consumer is affected. If unimport changes its default, this plugin can go back to being UnimportPlugin.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 that sources contains the module ID. Both assertions fail when hires: 'boundary'/source are removed.

End-to-end check with a probe module in packages/wxt-demo that uses browser.runtime.id across four branches, with a test covering only one of them:

// src/utils/coverage-probe.ts
export function probe(input: number) {
  const id = browser.runtime.id;
  if (input > 10) return `big:${id}`;
  if (input > 5) return `medium:${id}`;
  if (input > 0) return `small:${id}`;
  return `zero:${id}`;
}
bun run test run --coverage --coverage.include='src/utils/coverage-probe.ts' \
  --coverage.reporter=text src/utils/__tests__/coverage-probe.test.ts

Before — the module is invisible to the coverage reporter and reports a perfect score:

File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|------------------
-------------------|---------|----------|---------|---------|------------------
Statements   : 100% ( 0/0 )
Branches     : 100% ( 0/0 )

After — real numbers, with the uncovered branches correctly attributed:

File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|------------------
 coverage-probe.ts |    37.5 |    16.66 |     100 |    37.5 | 6-12
-------------------|---------|----------|---------|---------|------------------
Statements   : 37.5% ( 3/8 )
Branches     : 16.66% ( 1/6 )

(The probe files were only used for verification and are not part of this PR.)

Also ran the full wxt package suite (bun run test run → 53 files / 554 tests passing) and bun run check (ESLint, Oxlint, Publint, TypeScript) in packages/wxt.

Related Issue

This PR closes #2604

`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
@zizzfizzix
zizzfizzix requested a review from aklinker1 as a code owner August 29, 2026 10:05
@netlify

netlify Bot commented Aug 29, 2026

Copy link
Copy Markdown

Deploy Preview for creative-fairy-df92c4 ready!

Name Link
🔨 Latest commit 4146a34
🔍 Latest deploy log https://app.netlify.com/projects/creative-fairy-df92c4/deploys/6a92aed4085722000823f78c
😎 Deploy Preview https://deploy-preview-2606--creative-fairy-df92c4.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.

@github-actions github-actions Bot added the pkg/wxt Includes changes to the `packages/wxt` directory label Aug 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg/wxt Includes changes to the `packages/wxt` directory

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-imports emit a line-only sourcemap, silently collapsing code coverage (regression since v0.19.27, affects build and test)

2 participants