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:
Implementation details and motivation of the feature can be found in its design document.
HotModuleReplacement
plugin with nativescript-dev-webpack
.reloadPage
on changes in JS/TS-only applications.tns preview
.hmr
flag in the {N}-CLI section.The possible improvements are explained in the design doc.
app.(css|scss)
: https://github.com/NativeScript/NativeScript/issues/6645main-page.(css|scss)
, etc.: https://github.com/NativeScript/NativeScript/issues/6953Page
, but TabView
, SideDrawer
, etc../ sync recursive...
item in the bundle which is not being accepted, so this will make the CLI restart the app to apply the changeHMR 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!
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.
@tsonevn Navigation state is also not maintained after HMR.
do you want me to create a new issue to track it?
@shiv19, 10x for the feedback! You don't need to log an issue. I will add Research ways to persist nav history
to the list of possible improvements.
TLDR;
This is the expected behavior of the reloadPage function that's used for applying the changes.
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 with tns run android --hmr
, which seems to be the culprit? Solution: Outdated webpack.config.js
(still seeing the above behavior though)
EDIT2 Found our particular issue:
https://github.com/NativeScript/nativescript-dev-webpack/issues/708
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)
// // Enable either Hot Module Reloading or production mode
if (module['hot']) {
module['hot'].accept();
module['hot'].dispose(() => {
modulePromise.then(appModule => appModule.destroy());
});
} else {
enableProdMode();
}
Updated for 5.2 release.
[ ] {N}-modules Improve the reload strategy when the root view is not Page, but TabView, SideDrawer, etc.
Are there any workarounds? When can we expect this to work out of the box?
Hey @hettiger,
This has been implemented for NativeScript applications with JavaScript and TypeScript:
tns-core-modules@next
I tried it today and it works like a dream! Love it <3
Hey guys. When I close the app and open it again, every changes with --hmr
is gone. But this doesn't happen without --hmr
. Any idea?
Hey @vahidvdn,
There was a regression in some of the next versions of nativescript-dev-webpack
and that could be the reason. Could you please make sure you use the most recent latest
or next
version?
Let me know if still experience this problem.
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.
Hi @vchimev
This still happens in nativescript 6.1. I just update my cli and created a project with Angular tab template.
Now hmr
is 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.
@vahidvdn mine used to do the same until I removed the come on maint.ns.ts that used to listen for hmr module flag.
@Jonatthu Thanks, but I'm afraid I didn't catch on you. You just turned off the hrm?
If you have a code like this just comment it out
@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 hmr
and by deleting it we miss the advantages of it.
@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
Most helpful comment
Updated for 5.2 release.