Repository navigation
Description
Overview
Hot Module Replacement (HMR) is a feature provided by webpack. It makes it possible to replace modules in a bundled application while the application is running.
Integrating HMR within NativeScript will improve the development experience by:
- reducing the time to apply the updates when changing files;
- preserving the application state between reloads.
Design
Implementation details and motivation of the feature can be found in its design document.
Tasks
NativeScript 5.0
- Demo applications.
- {N}-webpack Integrate the
HotModuleReplacementplugin withnativescript-dev-webpack. - {N}-webpack Template for HMR runtime and logic that triggers the hot update on change.
- {N}-webpack Loaders for invoking
reloadPageon changes in JS/TS-only applications. - {N}-webpack Add Vue template. feat: add Vue bundling support nativescript-dev-webpack#676
- {N}-CLI Flag for enabling HMR.
- {N}-CLI Sync the backup files and restart the application when the hot update fails.
- {N}-CLI Integrate with
tns preview. - Integrate with NativeScript Playground.
- {N}-Angular Modify the bootstrap API to allow for dynamic module refetching.
- {N}-Angular Application logic for bootstrapping on changes.
- {N}-Angular Research the Angular compiler API for ways to improve the application reload logic in NativeScript Angular application.
- [5.4] Livesync tests.
- Benchmark tests.
- [5.4, all flavors] Provide a guide on persisting the application state between reloads.
- Documentation.
- Update the {N}-webpack article.
- Mention the
hmrflag in the {N}-CLI section.
After NativeScript 5.0
The possible improvements are explained in the design doc.
- [5.2] {N}-webpack Loaders for dynamically inserting the reload logic in NativeScript Angular applications: feat: support HMR in Angular apps nativescript-dev-webpack#788
- {N}-Vue Avoid unexpected navigation on a script change: HMR support for script changes in NativeScript-Vue applications nativescript-vue/nativescript-vue#388
- {N}-Vue Apply deletion of styles: HMR does not apply deletion of styles nativescript-vue/nativescript-vue#425
- {N}-Vue HMR stops listening on changes: NS and Vue --hmr stops listening to changes nativescript-cli#3927
- {N}-CLI Research ways to sync updates without FS
- {N}-modules Apply styles at runtime
- [5.2 - all flavors] Application styles -
app.(css|scss): [HMR] Apply changes in application styles at runtime #6645 - [5.4 - JS/TS only] Page styles -
main-page.(css|scss), etc.: [HMR] Apply changes in page styles at runtime #6953
- [5.2 - all flavors] Application styles -
- [5.4 - JS/TS only] {N}-modules Apply new UI at runtime
- [5.4 - JS/TS only] {N}-modules Improve the reload strategy when the root view is not
Page, butTabView,SideDrawer, etc. - {N}-modules Come up with a reload strategy for custom components.
- {N}-Angular Research ways to preserve the last route between reloads.
- {N}-modules/{N}-Angular Research ways to preserve the navigation history between reloads.
Known issues
- {N}-modules Currently if you add a new -page.css, -page.xml or -page.ts/js file it won't be accepted as it will trigger an update in the
./ sync recursive...item in the bundle which is not being accepted, so this will make the CLI restart the app to apply the change - [5.2] nativescript-dev-sass Changes in app.scss are not applied in Angular applications
- {N}-Angular Changes in plugin do NOT apply in angular projects:
[HMR] Changes in plugin do NOT apply in angular projects nativescript-dev-webpack#914
Contributing
HMR provides us with so many possibilities!
If you want to work on a task from the list, tell us and we will do our best to help you!
If you have another great idea on how to make HMR even cooler, share it in the comments!
Discussion
Please, don't report problems here. Instead, open a new issue and link it to this one.
Let's use this discussion for suggestions and improvement ideas. We would love to hear from you!
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Activity
[-][Tracking] Hot Module Replacement with webpack[/-][+][Tracking] 🔥Hot Module Replacement with webpack[/+][-][Tracking] 🔥Hot Module Replacement with webpack[/-][+][Tracking]🔥Hot Module Replacement with webpack[/+]shiv19 commentedon Oct 21, 2018
@tsonevn Navigation state is also not maintained after HMR.
do you want me to create a new issue to track it?
sis0k0 commentedon Oct 22, 2018
@shiv19, 10x for the feedback! You don't need to log an issue. I will add
Research ways to persist nav historyto the list of possible improvements.TLDR;
This is the expected behavior of the reloadPage function that's used for applying the changes.
larssn commentedon Nov 9, 2018
Is HMR supposed to tear down the entire angular app when reloading? Thought it was supposed to maintain the state? We're seeing ngOnDestroy getting called for everything on reloads, and it's causing problems.
Can anyone clarify?
(I'll create an issue if it's not supposed to do that)
EDIT: So
module['hot']is always undefined, even withtns run android --hmr, which seems to be the culprit? Solution: Outdatedwebpack.config.js(still seeing the above behavior though)EDIT2 Found our particular issue:
NativeScript/nativescript-dev-webpack#708
MarkPieszak commentedon Nov 9, 2018
Typically when trying to do HMR like this, you might need to check for
module['hot']and dispose the previous one.(As an example I do for aspnetcore & angular integration with HMR)
[-][Tracking]🔥Hot Module Replacement with webpack[/-][+][Tracking] 🔥 Hot Module Replacement with webpack[/+]vchimev commentedon Feb 10, 2019
Updated for 5.2 release.
hettiger commentedon Apr 2, 2019
Are there any workarounds? When can we expect this to work out of the box?
vchimev commentedon Apr 25, 2019
Hey @hettiger,
This has been implemented for NativeScript applications with JavaScript and TypeScript:
tns-core-modules@nextshiv19 commentedon Apr 25, 2019
I tried it today and it works like a dream! Love it <3
vahidvdn commentedon May 10, 2019
Hey guys. When I close the app and open it again, every changes with
--hmris gone. But this doesn't happen without--hmr. Any idea?vchimev commentedon May 14, 2019
Hey @vahidvdn,
There was a regression in some of the next versions of
nativescript-dev-webpackand that could be the reason. Could you please make sure you use the most recentlatestornextversion?Let me know if still experience this problem.
lambourn commentedon May 15, 2019
I understand that {N} + Angular + HMR means that the application will bootstrap on change.
Does this imply that objects created by Angular's DI / bootstrap mechanisms will get recreated from scratch? Or will a @Injectable object be "the same" before and after HMR executes?
Right now I see lots of (non-UI) objects getting re-created after HMR.
vahidvdn commentedon Sep 5, 2019
Hi @vchimev
This still happens in nativescript 6.1. I just update my cli and created a project with Angular tab template.
Now
hmris enabled by default. I save some changes, close the app, then I open it, I don't see the changes. And due to closing the app, I can't save new changes anymore.Jonatthu commentedon Oct 29, 2019
@vahidvdn mine used to do the same until I removed the come on maint.ns.ts that used to listen for hmr module flag.
vahidvdn commentedon Oct 29, 2019
@Jonatthu Thanks, but I'm afraid I didn't catch on you. You just turned off the hrm?
Jonatthu commentedon Oct 29, 2019
If you have a code like this just comment it out
vahidvdn commentedon Oct 29, 2019
@Jonatthu You are just falling back to the legacy one. Actually that is not a solution. The legacy reloader dropped in version 6.0
It's a bug with
hmrand by deleting it we miss the advantages of it.Jonatthu commentedon Oct 29, 2019
@vahidvdn I was having problems with my hmr until I commented out this piece of code.
After I removed this legacy code, my start working with 6.1
trajano commentedon Aug 10, 2021
Would using mettro instead of webpack make things easlier since they seem to have solved the situation with Expo
shirakaba commentedon Aug 10, 2021
@trajano It's not realistic to port our very Webpack-reliant ecosystem over to Metro, which in any case has far worse documentation and is a far less open ecosystem. Callstack are in fact building solutions to move away from Metro to Webpack, which I believe Expo are interested in (as it would allow a common and well-understood bundler for both React Native and React Native Web, and super-fast builds with esbuild).
The bundler itself is a relatively minor factor in how good the HMR experience is.
trajano commentedon Aug 10, 2021
Thanks @shirakaba I am just trying to see how to get all of this working to the same level as we have with Expo and React-Native where we can make changes and it updates the app automatically. I see announcements for HMR with Nativescript but I could never get it working and a few StackOverflow questions lead to issues that lead here.
shirakaba commentedon Aug 10, 2021
@trajano what specifically is not working, and which flavour of NativeScript are you using (Angular, Vue, etc.), and could you share your
package.json?trajano commentedon Aug 10, 2021
@shirakaba I created #9507 so I don't make this go too long.
NathanWalker commentedon Oct 23, 2026
Closing this tracking issue: Hot Module Replacement shipped and is the default dev workflow, with
@nativescript/webpackand now@nativescript/viteacross the Angular, Vue, Solid, Svelte, React and plain flavors. Please open focused issues for any HMR problems on current tooling.