Skip to content

feat: in-place component HMR under Vite - #183

Draft
edusperoni wants to merge 4 commits into
mainfrom
feat/web-style-hmr
Draft

edusperoni wants to merge 4 commits into
mainfrom
feat/web-style-hmr

Conversation

@edusperoni

Copy link
Copy Markdown
Collaborator

PR Checklist

Draft, for tracking progress. This is the Angular side of NativeScript/NativeScript#11650 (Vite thin client mode). So far it has been verified only on an iOS 26.3 simulator, using the Angular 22 + Analog 2.9.7 fixture from that PR. There are no unit tests yet: the renderer has no spec harness in this repo.

What is the current behavior?

  • HMR under Vite never bootstraps the app. runNativeScriptAngularApp waits for launchEvent, but the dev server's boot UI has already consumed it. The HMR dispose and reboot hooks are also gated on import.meta.webpackHot only.
  • Angular's web-style HMR (ɵɵreplaceMetadata) can't apply styles:
    • every emulated component's CSS shares a single tag;
    • componentReplaced isn't implemented;
    • external runtime styles (getExternalStyles, which dev servers emit for style HMR) are ignored, so a restyled component loses its styles.
  • Edits to those stylesheets arrive as Vite's css-update, which nothing on device handles.

What is the new behavior?

Bootstrap and HMR hooks:

  • The app bootstraps immediately when it has already launched (Application.hasLaunched()).
  • The HMR hooks are wired for import.meta.hot as well as webpackHot.

Per-component styles:

  • Each EmulatedRenderer tags its CSS with <rootModuleId>:<componentId>.
  • NativeScriptRendererFactory.componentReplaced() drops the component's renderer and styles, so the replacement metadata's styles apply. ngOnDestroy cleans up all of them.

External runtime styles:

  • They are fetched with Accept: text/css, as a <link> would request them, and resolved against the new NATIVESCRIPT_STYLE_BASE_URL token. That token defaults to globalThis.__NS_DEV_ORIGIN__, which @nativescript/vite sets during dev.
  • They are applied under the component's tag, followed by _onCssStateChange().

Stylesheet HMR:

  • The renderer factory listens for vite:beforeUpdate on @nativescript/vite's device HMR registry (__NS_HOT_REGISTRY__.on/off).
  • For each css-update, every emulated renderer that loaded that path refetches it with t=<timestamp> and rebuilds its tag. This is a stylesheet swap like Vite's <link> handling on the web, so no views are recreated.
  • The registry only exists during Vite dev, so release and webpack builds are unaffected.

Verified on the iOS simulator, same process throughout:

  • template edits apply in place with component state kept;
  • four CSS edits in a row each apply;
  • a template edit followed by a CSS edit both apply.

Known gaps:

  • ViewEncapsulation.None components don't load external styles yet.
  • .ts edits under thin mode fail on the @nativescript/vite side; see the companion PR.

No issue yet; this PR tracks the experiment.

Support Angular's web-style HMR (ɵɵreplaceMetadata) on device instead of
rebooting the app for template and stylesheet edits.

- Bootstrap right away when the app has already launched (a dev server's
  boot UI owns launchEvent), and wire the HMR dispose hooks for
  import.meta.hot as well as webpackHot.
- Tag each emulated component's CSS separately and implement
  componentReplaced(), so a replaced component's styles are dropped and
  re-applied from the new metadata.
- Load external runtime styles (getExternalStyles), which dev servers
  emit for style HMR, resolved against the new NATIVESCRIPT_STYLE_BASE_URL
  token (defaults to __NS_DEV_ORIGIN__ set by @nativescript/vite).
- Reload those stylesheets on Vite's css-update (vite:beforeUpdate via
  @nativescript/vite's device HMR registry), as Vite's client swaps a
  <link> on the web.
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 665557de-5137-4492-8272-3aaf9a461f9a

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@pkg-pr-new

pkg-pr-new Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/NativeScript/angular/@nativescript/angular@183
npm i https://pkg.pr.new/NativeScript/angular/@nativescript/zone-js@183

commit: deaf49a

@NathanWalker

Copy link
Copy Markdown
Contributor

@coderabbitai if able to compare with this pr as well as additional context: http://github.com/NativeScript/angular/pull/161

@coderabbitai

coderabbitai Bot commented Nov 18, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

Analysis chain

Script executed:

#!/bin/bash
set -u
for n in 161 183; do
  echo "===== PR $n METADATA ====="
  gh api "repos/NativeScript/angular/pulls/$n" | jq '{number,title,state,draft,merged,created_at,updated_at,base:{ref:.base.ref,sha:.base.sha},head:{ref:.head.ref,sha:.head.sha},body,html_url}'
  echo "===== PR $n CHANGED FILES AND PATCHES ====="
  gh api --paginate "repos/NativeScript/angular/pulls/$n/files?per_page=100" | jq -r '.[] | "FILE: \(.filename) STATUS: \(.status) ADDITIONS: \(.additions) DELETIONS: \(.deletions)\n\(.patch // "[patch unavailable]")\n"'
  echo "===== PR $n ISSUE COMMENTS ====="
  gh api --paginate "repos/NativeScript/angular/issues/$n/comments?per_page=100" | jq -r '.[] | "AUTHOR: \(.user.login) CREATED: \(.created_at) URL: \(.html_url)\n\(.body)\n"'
  echo "===== PR $n REVIEWS ====="
  gh api --paginate "repos/NativeScript/angular/pulls/$n/reviews?per_page=100" | jq -r '.[] | "AUTHOR: \(.user.login) STATE: \(.state) URL: \(.html_url)\n\(.body // "")\n"'
  echo "===== PR $n REVIEW COMMENTS ====="
  gh api --paginate "repos/NativeScript/angular/pulls/$n/comments?per_page=100" | jq -r '.[] | "AUTHOR: \(.user.login) PATH: \(.path) LINE: \(.line // .original_line) URL: \(.html_url)\n\(.body)\n"'
done

Length of output: 84662


Script executed:

#!/bin/bash
set -eu
echo "===== PR 183 SOURCE OUTLINES ====="
ast-grep outline packages/angular/src/lib/application.ts --match 'runNativeScriptAngularApp' --view expanded
ast-grep outline packages/angular/src/lib/nativescript-renderer.ts --match 'NativeScriptRendererFactory|EmulatedRenderer|addScopedStyleToCss' --view expanded
echo "===== PR 183 APPLICATION SOURCE ====="
cat -n packages/angular/src/lib/application.ts | sed -n '215,230p;265,335p;440,525p'
echo "===== PR 183 RENDERER SOURCE ====="
cat -n packages/angular/src/lib/nativescript-renderer.ts | sed -n '118,240p;510,680p'
for f in application.ts nativescript-renderer.ts; do
  tmp="/tmp/pr161-$f"
  gh api "repos/NativeScript/angular/contents/packages/angular/src/lib/$f?ref=3c23605f940a5ac0136cf62845a4929a7c21f2f0" --jq '.content' | base64 -d > "$tmp"
  echo "===== PR 161 $f OUTLINE ====="
  ast-grep outline "$tmp" --match 'runNativeScriptAngularApp|NativeScriptRendererFactory|EmulatedRenderer|reapplyChangedStyles' --view expanded
  echo "===== PR 161 $f SOURCE ====="
  if [ "$f" = application.ts ]; then
    cat -n "$tmp" | sed -n '215,240p;270,350p;445,540p'
  else
    cat -n "$tmp" | sed -n '120,200p;460,570p'
  fi
done

Length of output: 40911


@NathanWalker I compared #161 at 3c23605 with this PR at e815cd1. They overlap, but they address different parts of Vite HMR.

  • Bootstrap and reboot: Both handle an already-launched app. #161 also installs the Vite entry-option handoff and runs hooks around disposal and bootstrap. Those hooks support route and dialog restoration. This PR does not include that reboot contract. See application.ts in #161, Line 223 and Line 488.
  • Styles: #161 detects changed inline styles and adds them under the root tag. This PR replaces emulated component styles using per-component tags and adds external stylesheet fetching and css-update handling. See nativescript-renderer.ts in #161, Line 498 and this PR, Line 213.
  • Dialogs: #161 adds a stable modal host for in-place template replacement and can reopen opted-in dialogs after a reboot. This PR does not include those changes.

Integration checks: If the PRs are combined, preserve #161’s reboot hooks and option handoff while avoiding duplicate style application. Also test ViewEncapsulation.None: this PR removes old styles only for EmulatedRenderer, while #161 appends changed inline styles under the shared root tag. Removed declarations could therefore remain in either approach.

The PR descriptions report broader iOS and Android testing and unit tests for #161. This PR reports iOS simulator testing and no new unit tests. I did not run those tests. I recommend testing repeated CSS edits and template edits in an open dialog, followed by a .ts reboot and route restore. The thin-client .ts limitation remains a separate known gap.

You are interacting with an AI system.

On vite:beforeFullReload (registered once per process through import.meta.hot) dispose the current app's modules and platform, detach its launch/exit handlers and reset compiled components; the re-evaluated entry bootstraps again because the app has already launched. The renderer reloads runtime stylesheets from import.meta.hot's vite:beforeUpdate, and NATIVESCRIPT_STYLE_BASE_URL defaults to the origin this package was served from. Webpack HMR keeps its own hooks under import.meta.webpackHot.
prepareHmrReboot() saves HMR participants from the current root injector, detaches the launch/exit handlers and disposes the modules and platform; whenHmrRebootSettled() waits for the re-bootstrap to settle and restores the participants in the new injector. @nativescript/vite calls them from the app entry's HMR dispose, so only the changed import chain re-runs. Participants are a multi-provider (NATIVESCRIPT_HMR_PARTICIPANTS: key, version, save, restore); none are registered yet. requestNativeScriptAppRestart(reason) asks the dev server for an app restart. Everything is a no-op without import.meta.hot.
Reboot state (pending snapshots, teardown done) lives in hmr-reboot-state, so prepareHmrReboot is a no-op when a retry runs it again on an app that is already torn down, and the snapshots captured from the last good app are restored once a retried reboot bootstraps. A failed bootstrap is reported to the dev client through whenHmrRebootSettled's result instead of as a completed reboot.

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