Skip to content

fix(vite): serve HMR app modules with source maps - #11629

Merged
NathanWalker merged 1 commit into
mainfrom
fix/vite-hmr-sourcemaps
Nov 9, 2026
Merged

NathanWalker merged 1 commit into
mainfrom
fix/vite-hmr-sourcemaps

Conversation

@NathanWalker

@NathanWalker NathanWalker commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

PR Checklist

  • The PR title follows our guidelines.
  • There is an issue for the bug/feature this PR is for.
  • All existing tests are passing (packages/vite: 169 files / 1963 tests).
  • Tests for the changes are included.

What is the current behavior?

Under HMR, the /ns/m route serves app modules without a source map. Vite's transform does produce one, but the device pipeline then changes the code: it prepends guards and boot code, rewrites and hoists imports, and for some modules re-prints the whole file with Babel ([ast-normalized]). Then it sends the result without the map. The runtime reports scripts like http://<host>:5173/ns/m/src/app with an empty sourceMapURL, so a debugger (VS Code / js-debug, Chrome DevTools) cannot bind breakpoints in .ts/.tsx sources. This is why the NativeScript VS Code extension forces --no-hmr for Vite projects.

What is the new behavior?

App modules (not library/node_modules ones) served by /ns/m carry an inline source map:

  • hmr/server/served-module-sourcemap.ts aligns Vite's transform output with the served body using a Myers line diff (whitespace-insensitive), then carries Vite's mappings over:
    • Lines with the same text apart from indentation keep Vite's segments, with columns shifted by the indentation change. This covers the Babel re-print, which uses retainLines.
    • Hunks rewritten in place (same line count on both sides) map each line to the start of its original line.
    • Injected preamble and hoisted imports stay unmapped.
  • Sources are emitted as absolute file:// URLs, because the device URL doesn't mirror the project layout. js-debug and DevTools resolve them with no path overrides.
  • The map is appended before the response memo, so cached responses include it. Code that already has a sourceMappingURL is left as is.
  • New dependency: @jridgewell/sourcemap-codec (already in the tree through Babel).

Verified

On a Solid app (ns debug ios --watch, iOS simulator), driving js-debug 1.140 (the version VS Code bundles) over DAP:

  • Breakpoints in app.tsx, state/ui.ts, and a Babel-normalized data/music.ts bind at the requested lines.
  • After an HMR edit, the breakpoint re-binds to the new module version and pauses in App at app.tsx:15, with scopes and evaluation working.
  • The iOS and Android inspector clients pass data: source map URLs through unchanged.

Cost

  • About 0.3 ms to build the map for a 600-line module, only for responses not already cached.
  • About 40% more bytes per app module. Library modules, core, and the deps bundle are unchanged.

Not covered

Vue SFCs are served by /ns/sfc / /ns/asm and still have no maps. That can follow separately.

Related: the VS Code extension stops forcing --no-hmr once this ships: nativescript-community/nativescript-vscode-extension#303.

The /ns/m device route rewrites Vite's transform output (prepended guards,
import rewrites, AST normalization) and dropped Vite's source map, so
debuggers attached under HMR could not bind breakpoints in app sources.

Carry Vite's map onto the served body by aligning the two texts line by line
and append it inline, with absolute file:// sources. Library modules are left
unchanged.
@nx-cloud

nx-cloud Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 001db83

Command Status Duration Result
nx run-many -t build -p core,webpack5,vite ✅ Succeeded 10s View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-09 23:06:46 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 9, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11629
npm i https://pkg.pr.new/@nativescript/vite@11629
npm i https://pkg.pr.new/@nativescript/webpack@11629

commit: 001db83

@NathanWalker
NathanWalker merged commit 49c0474 into main Nov 9, 2026
10 checks passed
@NathanWalker
NathanWalker deleted the fix/vite-hmr-sourcemaps branch November 9, 2026 23:08
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.

1 participant