Skip to content

fix(dev-bundle): keep sourcemaps usable when preserving usage stats - #25709

Closed
totally-not-ai[bot] wants to merge 2 commits into
mainfrom
fix/usage-stats-plugin-breaking-dev-bundle-sourcemaps
Closed

totally-not-ai[bot] wants to merge 2 commits into
mainfrom
fix/usage-stats-plugin-breaking-dev-bundle-sourcemaps

Conversation

@totally-not-ai

Copy link
Copy Markdown
Contributor

Summary

Dev bundles built with build.sourcemap enabled produced .map files with no sources and no mappings, so the browser could not map the bundle back to the original files. The usage statistics Vite plugin now leaves untouched modules alone and emits a proper sourcemap for the one module it rewrites.

What changed

Behavior change: dev bundles built with build.sourcemap enabled now get usable .map files. This only affects apps that turn sourcemaps on for the dev bundle; nothing else in the build changes, and no public Java API is removed or renamed.

  • vite.generated.ts: the vaadin:preserve-usage-stats plugin used to return the code of every module it saw without a sourcemap. The transform hook sees all modules of the bundle, so every module was dropped from its chunk's map. The plugin now returns null for modules it does not touch, which keeps the maps the bundler already has.
  • The one module it does rewrite (the vaadin-dev-mode:start comment) is now rewritten with MagicString, so the plugin hands back a real sourcemap for it (magic-string was already a build dependency).
  • Added SourceMapTestUtil in flow-test-util with the sourcemap assertions that were previously private to SourceMapsIT, so the production and dev bundle tests share them.
  • The dev bundle test app now imports a stand-in for @vaadin/vaadin-usage-statistics and builds with sourcemap: true, so the rewrite branch of the plugin is actually exercised.

Follow-up to #25691, related to #16679.

API Changes

com.vaadin.flow.testutil.SourceMapTestUtil

// Added
public final class SourceMapTestUtil
public static List<String> assertSourceMapUsable(String name, String sourceMap) // asserts a sourcemap has sources, mappings and source contents; returns the source file names

Test summary

# Status What the test verifies Why it matters
1 ✅ Every .js.map of a dev bundle built with sourcemap: true has non-empty sources, non-empty mappings, and the contents of each source This is the exact regression: all dev bundle maps came out empty and unusable
2 ✅ The dev bundle maps refer back to the real sources, both a normal module (lit-view.ts) and the rewritten usage statistics module Proves the untouched modules keep the bundler's map and the rewritten module gets its own
3 ✅ The built chunk contains /*! vaadin-dev-mode:start and no longer the original /** vaadin-dev-mode:start The fix must not break the rewrite itself — the comment still has to survive in the bundle
4 ✅ Production bundle maps still point to original sources (existing test, now using the shared helper) Guards against the refactoring changing the production checks
5 ❗ gap The console.error paths (marker missing, rewritten comment fails the regexp) Only logging; a regression here is silent but low risk
  • DevBundleSourceMapsIT.devBundleSourceMaps_pointToOriginalSources → rows 1, 2
  • DevBundleSourceMapsIT.usageStatisticsComment_isRewrittenInTheBundle → row 3
  • SourceMapsIT.bundleSourceMapsPointToOriginalSources → row 4 (unchanged assertions, moved into SourceMapTestUtil)

Deliberately untested: the plugin's error logging branches (row 5), and SourceMapTestUtil itself, which is covered indirectly by every test that calls it.

The usage statistics plugin returned the code of every module it saw
without a sourcemap, which drops the module from the map of its chunk.
As the transform hook sees every module of the bundle, all .map files
of a dev bundle built with build.sourcemap enabled came out with no
sources and no mappings.

Follow-up to #25691, Related to #16679
The dev bundle test application did not contain a module the usage
statistics plugin rewrites, so the branch that produces a sourcemap of
its own was never run. It now imports a stand-in for the
@vaadin/vaadin-usage-statistics module, and the test also asserts that
the comment of that module is rewritten in the built chunk.

The sourcemap assertions are shared with the production bundle test
instead of being copied.
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

 1 475 files  +1   1 559 suites  +1   1h 40m 13s ⏱️ -28s
12 241 tests +2  12 173 ✅ +2  68 💤 ±0  0 ❌ ±0 
12 559 runs  +2  12 491 ✅ +2  68 💤 ±0  0 ❌ ±0 

Results for commit 0683090. ± Comparison against base commit aaf20ce.

@totally-not-ai

Copy link
Copy Markdown
Contributor Author

Closing as a duplicate of #25708, which has the same change to vite.generated.ts. The SourceMapTestUtil helper and the shared assertions from this PR are being ported there.

@totally-not-ai totally-not-ai Bot closed this Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants