Skip to content

[Tracking] 🔥 Hot Module Replacement with webpack #6398

Closed

Description

@sis0k0

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 HotModuleReplacement plugin with nativescript-dev-webpack.
  • {N}-webpack Template for HMR runtime and logic that triggers the hot update on change.
  • {N}-webpack Loaders for invoking reloadPage on 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 hmr flag in the {N}-CLI section.

After NativeScript 5.0

The possible improvements are explained in the design doc.

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

  1. changed the title [-][Tracking] Hot Module Replacement with webpack[/-] [+][Tracking] 🔥Hot Module Replacement with webpack[/+] on Oct 11, 2018
  2. changed the title [-][Tracking] 🔥Hot Module Replacement with webpack[/-] [+][Tracking]🔥Hot Module Replacement with webpack[/+] on Oct 11, 2018
  3. shiv19 commented on Oct 21, 2018

    @shiv19
    Member

    @tsonevn Navigation state is also not maintained after HMR.

    do you want me to create a new issue to track it?

  4. sis0k0 commented on Oct 22, 2018

    @sis0k0
    ContributorAuthor

    @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.

  5. larssn commented on Nov 9, 2018

    @larssn

    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:
    NativeScript/nativescript-dev-webpack#708

  6. MarkPieszak commented on Nov 9, 2018

    @MarkPieszak

    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();
    }
  7. pinned this issue on Dec 18, 2018
  8. changed the title [-][Tracking]🔥Hot Module Replacement with webpack[/-] [+][Tracking] 🔥 Hot Module Replacement with webpack[/+] on Dec 18, 2018
  9. unpinned this issue on Dec 19, 2018
  10. vchimev commented on Feb 10, 2019

    @vchimev
    Contributor

    Updated for 5.2 release.

  11. hettiger commented on Apr 2, 2019

    @hettiger
    Contributor

    [ ] {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?

  12. vchimev commented on Apr 25, 2019

    @vchimev
    Contributor

    Hey @hettiger,

    This has been implemented for NativeScript applications with JavaScript and TypeScript:

  13. shiv19 commented on Apr 25, 2019

    @shiv19
    Member

    I tried it today and it works like a dream! Love it <3

  14. vahidvdn commented on May 10, 2019

    @vahidvdn

    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?

  15. vchimev commented on May 14, 2019

    @vchimev
    Contributor

    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.

  16. lambourn commented on May 15, 2019

    @lambourn

    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.

  17. vahidvdn commented on Sep 5, 2019

    @vahidvdn

    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.

  18. Jonatthu commented on Oct 29, 2019

    @Jonatthu

    @vahidvdn mine used to do the same until I removed the come on maint.ns.ts that used to listen for hmr module flag.

  19. vahidvdn commented on Oct 29, 2019

    @vahidvdn

    @Jonatthu Thanks, but I'm afraid I didn't catch on you. You just turned off the hrm?

  20. Jonatthu commented on Oct 29, 2019

    @Jonatthu

    image

    If you have a code like this just comment it out

  21. vahidvdn commented on Oct 29, 2019

    @vahidvdn

    @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.

  22. Jonatthu commented on Oct 29, 2019

    @Jonatthu

    @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

  23. trajano commented on Aug 10, 2021

    @trajano

    Would using mettro instead of webpack make things easlier since they seem to have solved the situation with Expo

  24. shirakaba commented on Aug 10, 2021

    @shirakaba
    Contributor

    Would using mettro instead of webpack make things easlier since they seem to have solved the situation with Expo

    @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.

  25. trajano commented on Aug 10, 2021

    @trajano

    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.

  26. shirakaba commented on Aug 10, 2021

    @shirakaba
    Contributor

    @trajano what specifically is not working, and which flavour of NativeScript are you using (Angular, Vue, etc.), and could you share your package.json?

  27. trajano commented on Aug 10, 2021

    @trajano

    @shirakaba I created #9507 so I don't make this go too long.

  28. NathanWalker commented on Oct 23, 2026

    @NathanWalker
    Contributor

    Closing this tracking issue: Hot Module Replacement shipped and is the default dev workflow, with @nativescript/webpack and now @nativescript/vite across the Angular, Vue, Solid, Svelte, React and plain flavors. Please open focused issues for any HMR problems on current tooling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions