Skip to content

feat: debug Vite projects with HMR when @nativescript/vite serves source maps - #303

Open
NathanWalker wants to merge 1 commit into
masterfrom
fix/vite-hmr-debugging
Open

NathanWalker wants to merge 1 commit into
masterfrom
fix/vite-hmr-debugging

Conversation

@NathanWalker

Copy link
Copy Markdown
Member

Problem

Debugging a Vite project with ns debug ios and HMR on (the normal workflow) didn't work, so the extension forced --no-hmr for every Vite project. The cause was in @nativescript/vite: its /ns/m device route served app modules without source maps, so js-debug could not map src/*.tsx breakpoints to the code that ran.

That is fixed in NativeScript/NativeScript#11629, which serves app modules with inline maps.

Change

  • --no-hmr is added only when it is still needed:
    • the installed @nativescript/vite is older than VITE_HMR_SOURCE_MAPS_VERSION (8.0.23), or its version can't be read (missing package, Yarn PnP); or
    • the project depends on nativescript-vue, because .vue components are served by separate routes that still have no maps.
  • When the flag is added, the reason is logged to the NativeScript output channel.
  • The version is read from the nearest node_modules/@nativescript/vite, searching parent folders as well, so monorepos are covered.
  • An explicit --hmr / --no-hmr in tnsArgs is always used as given, as before.
  • The README is updated.

Before merging: VITE_HMR_SOURCE_MAPS_VERSION must match the @nativescript/vite release that ships NativeScript/NativeScript#11629.

Verification

  • Unit tests cover the version gate (older, prerelease, unknown, newer), the Vue exception, and getInstalledPackageVersion / hasDependency.
  • End to end on a Solid app (ns debug ios --watch, iOS simulator) with the patched @nativescript/vite. I drove js-debug 1.140 over DAP with the same pwa-node attach config DebugManager builds, including computeSourceMapOptions output:
    • Breakpoints in .ts/.tsx sources bind.
    • An HMR edit re-binds the breakpoint and pauses at the expected line.
    • HMR updates don't re-announce a debug port, so the session stays attached across saves.

…rce maps

@nativescript/vite 8.0.23 serves HMR app modules with inline source maps, so
breakpoints bind without restarting the app on every change. Only add
--no-hmr for older versions (or when the version can't be read) and for Vue
projects, whose single-file components are still served without maps.

@edusperoni edusperoni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good. Checked the version gate against the published packages: @nativescript/vite 8.0.23 was published at 23:13 UTC, 5 minutes after NativeScript/NativeScript#11629 merged, and it contains the fix (hmr/server/served-module-sourcemap.js is new, and websocket-ns-m.js now calls appendInlineSourceMap); 8.0.22 has neither. So VITE_HMR_SOURCE_MAPS_VERSION = '8.0.23' is right and the "before merging" condition is met.

One non-blocking note: hasDependency(appRoot, 'nativescript-vue') only reads the app's own package.json. In a monorepo where nativescript-vue is declared at the root (typical Nx setup), Vue isn't detected, HMR stays on, and breakpoints in .vue files won't bind. Detecting the installed package by walking up parent folders (like the @nativescript/vite version lookup already does) would be more consistent; the worst case of a false positive is losing HMR while debugging, not losing debugging. Fine as a follow-up.

Suggest merging this one before #304 so #304 can reuse hasDependency.

This branch has not been deployed

No deployments
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.

2 participants