<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Stanimira Vlaeva</title>
    <description>The latest articles on DEV Community by Stanimira Vlaeva (@stanimiravlaeva).</description>
    <link>https://dev.to/stanimiravlaeva</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F39045%2F8b55be74-88f9-45a2-9b33-7128b558635d.jpg</url>
      <title>DEV Community: Stanimira Vlaeva</title>
      <link>https://dev.to/stanimiravlaeva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stanimiravlaeva"/>
    <language>en</language>
    <item>
      <title>Great article!</title>
      <dc:creator>Stanimira Vlaeva</dc:creator>
      <pubDate>Wed, 20 Aug 2025 09:38:40 +0000</pubDate>
      <link>https://dev.to/stanimiravlaeva/-85</link>
      <guid>https://dev.to/stanimiravlaeva/-85</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/mongodb/pre-computing-statistics-for-a-time-frame-with-mongodb-g68" class="crayons-story__hidden-navigation-link"&gt;Pre-computing Statistics for a Time Frame With MongoDB&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;
          &lt;a class="crayons-logo crayons-logo--l" href="/mongodb"&gt;
            &lt;img alt="MongoDB logo" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F140%2F9639a040-3c27-4b99-b65a-85e100016d3c.png" class="crayons-logo__image"&gt;
          &lt;/a&gt;

          &lt;a href="/lucecarter1" class="crayons-avatar  crayons-avatar--s absolute -right-2 -bottom-2 border-solid border-2 border-base-inverted  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F186973%2Fac234f77-0eb4-48bb-8cc7-d1ec0a0c49e7.jpg" alt="lucecarter1 profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/lucecarter1" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Luce Carter
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Luce Carter
                
              
              &lt;div id="story-author-preview-content-2770232" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/lucecarter1" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F186973%2Fac234f77-0eb4-48bb-8cc7-d1ec0a0c49e7.jpg" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Luce Carter&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

            &lt;span&gt;
              &lt;span class="crayons-story__tertiary fw-normal"&gt; for &lt;/span&gt;&lt;a href="/mongodb" class="crayons-story__secondary fw-medium"&gt;MongoDB&lt;/a&gt;
            &lt;/span&gt;
          &lt;/div&gt;
          &lt;a href="https://dev.to/mongodb/pre-computing-statistics-for-a-time-frame-with-mongodb-g68" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Aug 12 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/mongodb/pre-computing-statistics-for-a-time-frame-with-mongodb-g68" id="article-link-2770232"&gt;
          Pre-computing Statistics for a Time Frame With MongoDB
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/programming"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;programming&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/mongodb"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;mongodb&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/dotnet"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;dotnet&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/csharp"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;csharp&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/mongodb/pre-computing-statistics-for-a-time-frame-with-mongodb-g68" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;3&lt;span class="hidden s:inline"&gt; reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/mongodb/pre-computing-statistics-for-a-time-frame-with-mongodb-g68#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              Comments


              &lt;span class="hidden s:inline"&gt;Add Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            8 min read
          &lt;/small&gt;
            
              &lt;span class="bm-initial"&gt;
                

              &lt;/span&gt;
              &lt;span class="bm-success"&gt;
                

              &lt;/span&gt;
            
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>programming</category>
      <category>mongodb</category>
      <category>dotnet</category>
      <category>csharp</category>
    </item>
    <item>
      <title>A Deep Dive into Hot Module Replacement with Webpack (Part Two - Handling Updates)</title>
      <dc:creator>Stanimira Vlaeva</dc:creator>
      <pubDate>Mon, 02 Sep 2019 14:17:07 +0000</pubDate>
      <link>https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-two-handling-updates-3c1f</link>
      <guid>https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-two-handling-updates-3c1f</guid>
      <description>&lt;p&gt;This article is the second part of a "deep dive" series on Hot Module Replacement with &lt;a href="https://webpack.js.org/"&gt;webpack&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: &lt;a href="https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-one-the-basics-39hn"&gt;The Basics&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Part 2: Handling Hot Updates with the module.hot API&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Part 2: Handling Hot Updates with the module.hot API
&lt;/h1&gt;

&lt;p&gt;In the first blog post of the HMR series, we discussed the four stages of the &lt;em&gt;Hot Module Replacement&lt;/em&gt; process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dGLKc9AW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/j27uvbdlva5xt0eqg0sn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dGLKc9AW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/j27uvbdlva5xt0eqg0sn.png" alt="Hot Module Replacement process"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today, we will focus on the last stage. We will learn how to instruct the modules in our application to refresh themselves when they receive a hot update.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;hot update handlers&lt;/em&gt; can either be injected by a webpack loader during the build or be manually added by you. We will discuss only the second way in this article. Webpack exposes a public interface from the &lt;code&gt;module.hot&lt;/code&gt; object. Let's explore it!&lt;/p&gt;

&lt;h1&gt;
  
  
  The module.hot API
&lt;/h1&gt;

&lt;p&gt;For our demos, we'll tinker with a simple webpage. It's best if you clone the project and follow the instructions, but it's not mandatory. You could also just read the blog post and trust me that everything works.&lt;/p&gt;

&lt;p&gt;Clone the repository from &lt;a href="https://github.com/sis0k0/christmas-tree"&gt;https://github.com/sis0k0/christmas-tree&lt;/a&gt;. If you're a fan of the command-line interface, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone https://github.com/sis0k0/christmas-tree.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Navigate to the cloned folder and install the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;christmas-tree
npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run the development server, execute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run watch

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the build finishes, a new tab in your browser will open. Toggle the devtools console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1csUiFcc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i18g1nxgwnmd6pljdiy0.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1csUiFcc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/i18g1nxgwnmd6pljdiy0.gif" alt="Devtools: Hot Module Replacement enabled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Merry Christmas! Yeah, I know it's September. But winter is coming.&lt;/p&gt;

&lt;p&gt;I would like you to notice two things here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;npm run watch&lt;/code&gt; starts the webpack development server, provided by the webpack-dev-server package.&lt;/li&gt;
&lt;li&gt;The console says that &lt;em&gt;Hot Module Replacement&lt;/em&gt; is enabled. By default, it is not. This project is &lt;em&gt;configured&lt;/em&gt; to run with HMR. We will find out how in the next section.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  What's in the Project?
&lt;/h1&gt;

&lt;p&gt;The image below shows the project structure (excluding &lt;code&gt;node_modules&lt;/code&gt;):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8hweM3fp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wmbg4sfstxw1f5frh39c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8hweM3fp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wmbg4sfstxw1f5frh39c.png" alt="Project structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;source&lt;/code&gt; directory is where we will be making changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;index.js&lt;/code&gt; imports all source files. That's the &lt;em&gt;entry&lt;/em&gt; module for webpack;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;lights.js&lt;/code&gt; creates the blinking effect for the Christmas tree;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;tree.js&lt;/code&gt; draws the tree itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;dist&lt;/code&gt; directory hosts the ready-to-run application.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;main.js&lt;/code&gt; is the single output &lt;em&gt;bundle&lt;/em&gt;, produced by webpack;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index.html&lt;/code&gt; is the web page that loads main.js.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We won't discuss &lt;code&gt;package.json&lt;/code&gt; and &lt;code&gt;package-lock.json&lt;/code&gt; in this post. If you want to learn more about them, check out the &lt;a href="https://docs.npmjs.com/files/package.json"&gt;npm docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And finally, &lt;code&gt;webpack.config.js&lt;/code&gt; - the place where we instruct webpack how to &lt;em&gt;bundle&lt;/em&gt; our application. The application runs with &lt;em&gt;HMR&lt;/em&gt; because the &lt;em&gt;Hot Module Replacement&lt;/em&gt; plugin is part of the configuration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;webpack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exports&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/index.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;main.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;path&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;devServer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;contentBase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;argv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;development&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;webpack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;HotModuleReplacementPlugin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Self-Accepted Modules
&lt;/h1&gt;

&lt;p&gt;Enough theory, let's go back to the browser. The devtools console says that &lt;em&gt;Hot Module Replacement&lt;/em&gt; is running. But does it really work? 🤔&lt;/p&gt;

&lt;p&gt;Open &lt;code&gt;src/tree.js&lt;/code&gt; and increase the value of &lt;code&gt;rowsCount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We get a bigger tree, but also a full page reload. It can be hard to notice. Look at the messages in the console - they disappear when the reload starts. When the scripts on the page are executed anew, the messages appear again. The goal of HMR is to avoid full page reloads.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jqd7bg0X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fyv1t0tuol99wq9uiat1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jqd7bg0X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/fyv1t0tuol99wq9uiat1.gif" alt="HMR: The tree isn't updated on change."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Currently, the application doesn't accept &lt;em&gt;hot updates&lt;/em&gt;, because we haven't instructed it to do so. Therefore, the &lt;code&gt;webpack-dev-server&lt;/code&gt; falls back to a full page reload.&lt;/p&gt;

&lt;p&gt;The easiest way to handle an incoming update is by &lt;em&gt;self-accepting&lt;/em&gt; it from the changed module. This will cause webpack to execute the new version of the module. All we need to add is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/tree.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, the &lt;code&gt;module.hot&lt;/code&gt; property is defined only when HMR is enabled. If we build the application for production, without HMR, the above code will throw an error. We need a check:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/tree.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One final note before trying it out - when we build for production, webpack knows that &lt;code&gt;module.hot&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt; and the code block, guarded by the if statement, will never be executed. The &lt;em&gt;UglifyJS/Terser&lt;/em&gt; plugin for webpack will remove it from the bundle. We don't have to worry that our development settings will end up in production.&lt;/p&gt;

&lt;p&gt;Let's change the &lt;code&gt;rowsCount&lt;/code&gt; again and see what happens. The page is not fully reloaded, but the tree still updates, because the new &lt;code&gt;tree.js&lt;/code&gt; module is executed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yhZXVk3i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/q2yun2dz1m7itpszwhpi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yhZXVk3i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/q2yun2dz1m7itpszwhpi.gif" alt="HMR: Tree is updated on change."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Disposal of Old Modules
&lt;/h1&gt;

&lt;p&gt;There's one more module in our simple app - &lt;code&gt;src/lights.js&lt;/code&gt;, which "illuminates" the tree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Changes the look of
 * some 'needles' in the tree
 * every 1000ms
 */&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;blinkRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rowsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rowsCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;needles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getNeedles&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;blink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rowsCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;needles&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;blinkRate&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To make &lt;code&gt;lights.js&lt;/code&gt; a self-accepted module, we need to extend it with the same code that we used for &lt;code&gt;tree.js&lt;/code&gt; earlier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's try decreasing the &lt;code&gt;blinkRate&lt;/code&gt; to make the lights go faster and increasing it for the opposite effect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nT-PrGp9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hfdm8ox14g2obifpf6wu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nT-PrGp9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hfdm8ox14g2obifpf6wu.gif" alt="Decrease blinkrate"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We didn't quite get the behavior we wanted. The light bulbs become more and more with every change. The &lt;em&gt;self-accept&lt;/em&gt; causes webpack to execute the module whenever a &lt;em&gt;hot update&lt;/em&gt; is needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;blink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rowsCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;needles&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;blinkRate&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// &amp;lt;-- gets called every time the module is changed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Executing the code above has a side effect - it triggers a repeating action with the &lt;code&gt;setInterval&lt;/code&gt; call. We never cancel the already started actions, but keep triggering new ones. Luckily, webpack provides a mechanism for &lt;em&gt;disposing&lt;/em&gt; old modules before replacing them.&lt;/p&gt;

&lt;p&gt;First, we need to keep the ID of the started action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lightsInterval&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
    &lt;span class="nx"&gt;lightsInterval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;setInterval&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;blink&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rowsCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;needles&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="nx"&gt;blinkRate&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we can clear it &lt;em&gt;before&lt;/em&gt; the new module is executed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dispose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lightsInterval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And finally, we can try changing the &lt;code&gt;blinkRate&lt;/code&gt; again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TMdcOtU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6d9erzo6hayhtnap57gt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TMdcOtU_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/6d9erzo6hayhtnap57gt.gif" alt="Lights Dispose"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Parent-Accepted Modules
&lt;/h1&gt;

&lt;p&gt;So far, we instructed webpack to execute the &lt;code&gt;tree.js&lt;/code&gt; and &lt;code&gt;lights.js&lt;/code&gt; modules whenever we change their code. The hot module replacement seems to work surprisingly well when the changed data is internal. But, what would happen if instead, we modify the public interface of a module? What would happen to the other modules depending on that interface 😱?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;tree.js&lt;/code&gt; module exports a single object - &lt;code&gt;fir&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;fir.draw()&lt;/code&gt; function visualizes the tree inside a container DOM element. It replaces the contents of the container with span elements needles. Each needle gets a &lt;code&gt;className&lt;/code&gt; equal to the value of the &lt;code&gt;NEEDLE_CLASS&lt;/code&gt; constant;&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;fir.getNeedles()&lt;/code&gt; function returns all DOM elements with the above &lt;code&gt;className&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;lights.js&lt;/code&gt; module imports tree.js and uses &lt;code&gt;fir.getNeedles()&lt;/code&gt; to obtain a list of the newly drawn DOM elements. The list is critically important for illuminating the Christmas tree. This is what the dependency graph looks like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nYsldTK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c9dzun9ldkf2vfyj7dv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nYsldTK5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/c9dzun9ldkf2vfyj7dv8.png" alt="index.js -&amp;gt; lights.js -&amp;gt; tree.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's put the HMR process to the test by modifying the value of &lt;code&gt;NEEDLE_CLASS&lt;/code&gt; in &lt;code&gt;tree.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MN9kRbjQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hjvzwzqbsetu36226xpc.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MN9kRbjQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/hjvzwzqbsetu36226xpc.gif" alt="Update NEEDLE_CLASS"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Our lights went out&lt;/strong&gt;! The HMR process failed the test miserably.&lt;/p&gt;

&lt;p&gt;Webpack executed &lt;code&gt;tree.js&lt;/code&gt; when we changed it. The &lt;code&gt;fir.draw()&lt;/code&gt; function created brand-new needles with &lt;code&gt;classNames&lt;/code&gt; matching the new value of &lt;code&gt;NEEDLE_CLASS&lt;/code&gt;. It also got rid of the previous needles.&lt;/p&gt;

&lt;p&gt;However, nothing happened in &lt;code&gt;lights.js&lt;/code&gt;. Its list of needles still references the old, already removed needles. We should refresh that list when &lt;code&gt;tree.js&lt;/code&gt; is changed.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;parent accept&lt;/em&gt; API allows us to handle hot updates for a module from other modules that import it. We can extend the HMR logic in &lt;code&gt;lights.js&lt;/code&gt; to restart the bulbs whenever &lt;code&gt;tree.js&lt;/code&gt; is changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lightsInterval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have update handling logic for &lt;code&gt;tree.js&lt;/code&gt; in two places:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;parent accept&lt;/em&gt; in &lt;code&gt;lights.js&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;self accept&lt;/em&gt; in the &lt;code&gt;tree.js&lt;/code&gt; module itself.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But which one will be preferred?&lt;/p&gt;

&lt;h1&gt;
  
  
  Refresh Strategies
&lt;/h1&gt;

&lt;p&gt;Let's visualize all possible update handling scenarios for &lt;code&gt;tree.js&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Self Accept in tree.js
&lt;/h2&gt;

&lt;p&gt;If there is a &lt;em&gt;self accept&lt;/em&gt; in &lt;code&gt;tree.js&lt;/code&gt;, webpack executes the module. The modules that import &lt;code&gt;tree.js&lt;/code&gt; (its "parents") are not notified of the change.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v7gpf-EV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ghgqp40sg77131if6asi.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v7gpf-EV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/ghgqp40sg77131if6asi.gif" alt="Self accept by tree.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Parent Accept in lights.js
&lt;/h2&gt;

&lt;p&gt;If there is no &lt;em&gt;self accept&lt;/em&gt; in &lt;code&gt;tree.js&lt;/code&gt;, webpack looks for update handlers inside the modules that import it.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;lights.js&lt;/code&gt; module imports &lt;code&gt;tree.js&lt;/code&gt;. Let's say that it has a handler for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Webpack will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;execute &lt;code&gt;tree.js&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;update the &lt;code&gt;tree.js&lt;/code&gt; import inside &lt;code&gt;lights.js&lt;/code&gt; to point to the new module;&lt;/li&gt;
&lt;li&gt;execute the &lt;code&gt;updateHandler()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WI9UMzyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/tmjpkyiam4kdam5fdjiv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WI9UMzyP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/tmjpkyiam4kdam5fdjiv.gif" alt="parent accept in lights.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Parent Accept in index.js
&lt;/h2&gt;

&lt;p&gt;If there is no handler for &lt;code&gt;tree.js&lt;/code&gt; in &lt;code&gt;lights.js&lt;/code&gt;, webpack will continue looking up the dependency graph.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;index.js&lt;/code&gt; module imports &lt;code&gt;lights.js&lt;/code&gt;. Webpack will check if it contains a handler for &lt;code&gt;lights.js&lt;/code&gt;. I want to highlight this part - it won't check for a handler for &lt;code&gt;tree.js&lt;/code&gt; (the changed module), but for &lt;code&gt;lights.js&lt;/code&gt; (the module it actually imports). Let's imagine for a moment that &lt;code&gt;index.js&lt;/code&gt; has a handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/index.js&lt;/span&gt;

&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./lights.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Webpack will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;execute &lt;code&gt;tree.js&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;execute &lt;code&gt;lights.js&lt;/code&gt; (the imported &lt;code&gt;tree.js&lt;/code&gt; module is updated)&lt;/li&gt;
&lt;li&gt;update the reference to &lt;code&gt;lights.js&lt;/code&gt; inside &lt;code&gt;index.js&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;execute the &lt;code&gt;updateHandler()&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YiatBKVd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/310wsll80byt82ho1lyn.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YiatBKVd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/310wsll80byt82ho1lyn.gif" alt="parent accept in index.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  No Update Handlers for tree.js
&lt;/h2&gt;

&lt;p&gt;Webpack continues looking for handlers until it finds a &lt;em&gt;'root'&lt;/em&gt; module - a module, that's not imported in any other module. In that case, the &lt;code&gt;webpack-dev-server&lt;/code&gt; will fallback to a full page reload, and in the case of &lt;strong&gt;NativeScript&lt;/strong&gt; - an app restart.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UqcpK51u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/k3itc5utmurf4q5rdrlt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UqcpK51u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/k3itc5utmurf4q5rdrlt.gif" alt="missing update handler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Choosing an Update Handler
&lt;/h1&gt;

&lt;p&gt;Back to the tree - we noticed that there are two update handlers for &lt;code&gt;tree.js&lt;/code&gt;. We want webpack to use the new logic in &lt;code&gt;lights.js&lt;/code&gt;. That's why we have to remove the self accept from &lt;code&gt;tree.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/tree.js&lt;/span&gt;

&lt;span class="c1"&gt;// comment or simply delete the code below&lt;/span&gt;
&lt;span class="c1"&gt;// if (module.hot) {&lt;/span&gt;
&lt;span class="c1"&gt;//     module.hot.accept();&lt;/span&gt;
&lt;span class="c1"&gt;// }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's try changing the value of &lt;code&gt;NEEDLE_CLASS&lt;/code&gt; again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UmWs3_dR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0ocxo9m1vfsopdgi5u0t.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UmWs3_dR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0ocxo9m1vfsopdgi5u0t.gif" alt="Change causes full page reload"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Aaaand...the HMR process fails. Instead of a refresh, we get a full page reload.&lt;/p&gt;

&lt;p&gt;I must admit that I lied to you. The &lt;code&gt;tree.js&lt;/code&gt; module is actually imported not only in &lt;code&gt;lights.js&lt;/code&gt; but also in &lt;code&gt;index.js&lt;/code&gt;. This is the real dependency graph:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fGOnw4AL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0lnt3xuf2z010y6d79qo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fGOnw4AL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0lnt3xuf2z010y6d79qo.png" alt="lights.js -&amp;gt; tree.js -&amp;gt; index.js -&amp;gt; lights.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The changed module should have an update handler in &lt;strong&gt;every&lt;/strong&gt; branch of its dependency graph. Currently, we are not accepting the changes for &lt;code&gt;tree.js&lt;/code&gt; in &lt;code&gt;index.js&lt;/code&gt; and the upcoming hot update is rejected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P_kMXPTj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5pj0uzd1uayi6f9q34em.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P_kMXPTj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/5pj0uzd1uayi6f9q34em.gif" alt="hot update is rejected"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice that if we have only one root module, we can add an application-wide update handler in it. We won't do that in our project for now. For example, if you're using Angular your task is a little easier, as most &lt;strong&gt;Angular&lt;/strong&gt; apps have a single entry module - &lt;code&gt;main.ts&lt;/code&gt;, which bootstraps the app. If there are no lazy loaded &lt;em&gt;NgModules&lt;/em&gt;, &lt;code&gt;main.ts&lt;/code&gt; will be the only root module. Adding the following handler to it will catch all hot updates in the app:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.ts&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./app/app.module&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./app/app.module&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Back on our project again - we need a &lt;em&gt;parent accept&lt;/em&gt; for &lt;code&gt;tree.js&lt;/code&gt; inside &lt;code&gt;index.js&lt;/code&gt;. And we don't even need a callback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/index.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the handler lookup process will be successful because the hot updates for tree.js are accepted in all of its parents.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7p9zFVUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/2685kirvvzqwm5dio01c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7p9zFVUg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/2685kirvvzqwm5dio01c.gif" alt="Hot update is accepted"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try changing the value of &lt;code&gt;NEEDLE_CLASS&lt;/code&gt; one last time before we give up:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dnAmvHqQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0r167lvn0c6qps56kw7e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dnAmvHqQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/0r167lvn0c6qps56kw7e.gif" alt="Parent update works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Refactoring
&lt;/h1&gt;

&lt;p&gt;Yeyyy! It works! But...&lt;/p&gt;

&lt;p&gt;The code we wrote doesn't really...feel good to me. The &lt;em&gt;parent accept&lt;/em&gt; in &lt;code&gt;index.js&lt;/code&gt; seems a bit artificial - it's there only because we have to accept the upcoming update. And what if we add a new module that imports &lt;code&gt;tree.js?&lt;/code&gt; We will have to add an update handler inside it too! It's time to refactor.&lt;/p&gt;

&lt;p&gt;Currently, we need to accept the tree.js changes in &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;lights.js&lt;/code&gt;, because both modules import it.&lt;/p&gt;

&lt;p&gt;Let's take a look at how &lt;code&gt;lights.js&lt;/code&gt; uses &lt;code&gt;tree.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rowsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rowsCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;needles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getNeedles&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of importing &lt;code&gt;fir&lt;/code&gt;, we can make it a parameter of the &lt;code&gt;turnOn&lt;/code&gt; function. In that case, the function shouldn't be called but exported instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="c1"&gt;// import fir from './tree.js';&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;rowsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;rowsCount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;needles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getNeedles&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// turnOn();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are not importing &lt;code&gt;tree.js&lt;/code&gt; anymore and we can also remove the handler for it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// module.hot.accept(['./tree.js'], function() {&lt;/span&gt;
    &lt;span class="c1"&gt;//    clearInterval(lightsInterval);&lt;/span&gt;
    &lt;span class="c1"&gt;//    turnOn();&lt;/span&gt;
    &lt;span class="c1"&gt;// });&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dispose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lightsInterval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Whoever uses &lt;code&gt;lights.js&lt;/code&gt; will have to import the &lt;code&gt;turnOn&lt;/code&gt; function, call it, and provide the &lt;code&gt;fir&lt;/code&gt; object as an argument. In our case the importer is &lt;code&gt;index.js&lt;/code&gt;. After doing the necessary modifications, &lt;code&gt;index.js&lt;/code&gt; should look like that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/index.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./lights.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we have an update handler for &lt;code&gt;tree.js&lt;/code&gt; only in &lt;code&gt;index.js&lt;/code&gt;. We can move the refresh logic for &lt;code&gt;lights.js&lt;/code&gt; inside it. Let's export a function that "restarts" the lights:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;restart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lightsInterval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And call that function from the update handler in &lt;code&gt;index.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/index.js&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;fir&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;restart&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./lights.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;turnOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;restart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's test out the changes we did by modifying the &lt;code&gt;tree.js&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8H4SR8NF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/h8low94q8v9x7a2cg808.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8H4SR8NF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/h8low94q8v9x7a2cg808.gif" alt="Final update works"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Editing &lt;code&gt;tree.js&lt;/code&gt; still works!&lt;/p&gt;

&lt;p&gt;However, now the &lt;code&gt;lights.js&lt;/code&gt; module is self-accepted and no longer imports &lt;code&gt;tree.js&lt;/code&gt;. If we try to modify it, the tree won't be redrawn. We need to move the &lt;code&gt;lights.js&lt;/code&gt; update handler to &lt;code&gt;index.js&lt;/code&gt; as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/index.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// add ./lights.js to the list of accepted dependencies &lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;accept&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./tree.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./lights.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;restart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fir&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Don't forget to remove the self accept from &lt;code&gt;lights.js&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/lights.js&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Comment or remove the line below&lt;/span&gt;
    &lt;span class="c1"&gt;// module.hot.accept();&lt;/span&gt;
    &lt;span class="c1"&gt;// Keep the disposal logic!&lt;/span&gt;
    &lt;span class="nx"&gt;module&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hot&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dispose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lightsInterval&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! Refactoring done! Let's stop making changes before we break something...&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;You can find the HMR-charged version of the demo in the branch, called finished: &lt;a href="https://github.com/sis0k0/christmas-tree/tree/finished"&gt;https://github.com/sis0k0/christmas-tree/tree/finished&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There is (at least) one bug in the disposal logic of the application. Try to find it! Feel free to open a PR in the Github repo. The first to do it, may (or may not) win something nice 🙂.&lt;/p&gt;

&lt;p&gt;We learned how to use the &lt;code&gt;module.hot&lt;/code&gt; API to manually handle hot updates. Some frameworks, like React, Vue, Angular, and NativeScript, provide built-in HMR support. In a dedicated article, we will explore how each framework solves the problem of refreshing the application and keeping its state intact.&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>javascript</category>
      <category>nativescript</category>
    </item>
    <item>
      <title>A Deep Dive into Hot Module Replacement with Webpack (Part One - The Basics)</title>
      <dc:creator>Stanimira Vlaeva</dc:creator>
      <pubDate>Mon, 02 Sep 2019 12:55:03 +0000</pubDate>
      <link>https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-one-the-basics-39hn</link>
      <guid>https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-one-the-basics-39hn</guid>
      <description>&lt;p&gt;The following article is part one of a "deep dive" series on Hot Module Replacement with webpack.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Part 1: The Basics&lt;/li&gt;
&lt;li&gt;Part 2: Handling hot updates with the module.hot API (&lt;a href="https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-two-handling-updates-3c1f"&gt;Available here&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Part 1: The Basics
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;Let's imagine for a moment it's still the middle of December and I have finished this blog post on time...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Christmas is quickly approaching and it's about time you decorate that tree in the living room. You fetch all boxes with the stuffed decoration from last year. First, you get the lights out. Make sure that you're not stressed at all while untangling them. You take a step back, maybe put on some Christmas music, make yourself a cup of tea. After a few hours, you are ready to put them on the tree. Cool, the hardest part is over. Now, you can hang some garlands and cool ornaments. Finally, you place the Star™ on the top of the tree and lit the lights. It's beautiful.&lt;/p&gt;

&lt;p&gt;But... was that the right Star™? You decide that you actually want the shiny new rainbow Star™ instead of the conservative old yellow Star™. Do you need to remove everything and start the whole process all over just to replace that piece of decoration? Obviously not! You only need to take down the old Star™ and put the new one.&lt;/p&gt;

&lt;p&gt;If you're still reading, you're probably wondering what was all that about. Well, your Christmas tree is just like a JavaScript application. The ornaments, garlands, and lights are the modules of the application. The mechanism that allows you to change pieces of decoration without having to disassemble the tree is called &lt;a href="https://webpack.js.org/concepts/hot-module-replacement/"&gt;Hot Module Replacement&lt;/a&gt;. And &lt;strong&gt;HMR&lt;/strong&gt; is what we'll be playing with today.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why should I care?
&lt;/h1&gt;

&lt;p&gt;Because development with HMR is faster.&lt;/p&gt;

&lt;p&gt;I work on the NativeScript framework and sometimes even build apps with it. Let's take a retrospective look at my life as a NativeScript developer before Hot Module Replacement:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IualCqD5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/4ruhqeepicgex80g1zz9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IualCqD5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/4ruhqeepicgex80g1zz9.gif" alt="Login without Hot Module Replacement"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the video above, I'm developing an application that has a login screen and an admin screen, accessible only by authorized users. After signing in, I make a change to the admin screen. The reload process starts. We can break it down into four stages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The changed module is transferred to the device.&lt;/li&gt;
&lt;li&gt;The entire application is torn down and restarted from the login screen.&lt;/li&gt;
&lt;li&gt;I sign in &lt;strong&gt;again&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;I'm redirected to the admin page where I see my change.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, let's see how the same process looks with Hot Module Replacement in the game:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pfyUlElm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/o3ztzjiad3ezqkrbzgka.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pfyUlElm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/o3ztzjiad3ezqkrbzgka.gif" alt="Login with Hot Module Replacement"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reload steps now are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The changed module is transferred to the device.&lt;/li&gt;
&lt;li&gt;The admin page is reloaded.&lt;/li&gt;
&lt;li&gt;I see my change.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It is significantly faster because only the current screen is reloaded and the user session is persisted.&lt;/p&gt;

&lt;h1&gt;
  
  
  How does it work?
&lt;/h1&gt;

&lt;p&gt;NativeScript uses webpack to process the JavaScript part of the application. Webpack finds all &lt;em&gt;modules&lt;/em&gt; that your app is using and &lt;em&gt;bundles&lt;/em&gt; them into a few output files.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Webpack has three closely related terms - &lt;strong&gt;module&lt;/strong&gt;, &lt;strong&gt;chunk&lt;/strong&gt;, and &lt;strong&gt;bundle&lt;/strong&gt;. A &lt;strong&gt;module&lt;/strong&gt; is a piece of JS code that encapsulates data and provides functionality. &lt;strong&gt;Modules&lt;/strong&gt; can depend on one another and thus, form a &lt;strong&gt;dependency graph&lt;/strong&gt;. In the webpack bundling process, a few modules form a &lt;strong&gt;chunk&lt;/strong&gt;. A &lt;strong&gt;bundle&lt;/strong&gt; is an output file, produced by the bundling process. In most cases, each &lt;strong&gt;chunk&lt;/strong&gt; emits exactly one &lt;strong&gt;bundle&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Webpack also supports a feature called &lt;em&gt;Hot Module Replacement&lt;/em&gt; (HMR). HMR exchanges, adds, or removes modules while an application is running, without a full reload.&lt;/p&gt;

&lt;p&gt;However, HMR doesn't magically reload your app when you change the code. The application itself must know how to react to the incoming changes. Let's take a closer look at the HMR process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wmf5lB4T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wbeb9u2nbi6apq98d201.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wmf5lB4T--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/wbeb9u2nbi6apq98d201.png" alt="HMR workflow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When you make a change, the webpack compiler is responsible for generating a 'diff' (&lt;em&gt;hot update&lt;/em&gt;) between the old version of the bundle and the new one.&lt;/li&gt;
&lt;li&gt;The webpack development server handles the transportation logistics for HMR. In other words, it makes sure the &lt;em&gt;hot update&lt;/em&gt; can be loaded when requested by the application. It's not mandatory to use &lt;code&gt;webpack-dev-server&lt;/code&gt; for transferring the files. In the NativeScript ecosystem, the NativeScript CLI plays that role.&lt;/li&gt;
&lt;li&gt;A special code, injected into your application bundle, fetches the &lt;em&gt;hot update&lt;/em&gt;. Then, it starts looking for handlers for each &lt;em&gt;module&lt;/em&gt;, that should be replaced.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;module&lt;/em&gt; is replaced if an appropriate handler is found. If not, the process fails and the &lt;em&gt;module&lt;/em&gt; is &lt;strong&gt;not&lt;/strong&gt; replaced. Obviously, it's good to have a fallback in that case. In the browser, the page is reloaded, and in NativeScript - the application is restarted.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;To wrap up, today we learned that:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hot Module Replacement is a feature provided by webpack;&lt;/li&gt;
&lt;li&gt;Hot Module Replacement makes our lives as developers easier by shortening the reload time and preserving the application state;&lt;/li&gt;
&lt;li&gt;You should be prepared to fail when untangling Christmas lights;&lt;/li&gt;
&lt;li&gt;Hot Module Replacement is not magic, but your application should know how to work with it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/stanimiravlaeva/a-deep-dive-into-hot-module-replacement-with-webpack-part-two-handling-updates-3c1f"&gt;next blog post&lt;/a&gt; in the HMR series, we'll turn our gaze to the last point. We will learn how to handle incoming changes inside our applications.&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>javascript</category>
      <category>nativescript</category>
    </item>
  </channel>
</rss>
