<?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: Vladyslav at Prime Board</title>
    <description>The latest articles on DEV Community by Vladyslav at Prime Board (@vladyslav_uk).</description>
    <link>https://dev.to/vladyslav_uk</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%2F114054%2Fa0158696-a3a4-43ea-82c9-1d00f2cfdaaf.PNG</url>
      <title>DEV Community: Vladyslav at Prime Board</title>
      <link>https://dev.to/vladyslav_uk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vladyslav_uk"/>
    <language>en</language>
    <item>
      <title>Implementing HMR for Pinia in NativeScript-Vue with Webpack 5</title>
      <dc:creator>Vladyslav at Prime Board</dc:creator>
      <pubDate>Tue, 12 Dec 2023 19:12:09 +0000</pubDate>
      <link>https://dev.to/vladyslav_uk/implementing-hmr-for-pinia-in-nativescript-vue-with-webpack-5-580l</link>
      <guid>https://dev.to/vladyslav_uk/implementing-hmr-for-pinia-in-nativescript-vue-with-webpack-5-580l</guid>
      <description>&lt;p&gt;In a recent project, we encountered a challenge involving Pinia, the default state management library for Vue 3, and NativeScript-Vue. Our objective was to enable Hot Module Reload (HMR) functionality with Webpack 5, despite Pinia's official HMR support being limited to Vite. This article outlines our approach and the solution we developed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmm03wx0aqrw209i27tb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdmm03wx0aqrw209i27tb.png" alt="Implementing HMR for Pinia in NativeScript-Vue with Webpack 5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Challenge: Bridging Pinia with NativeScript-Vue in Webpack 5
&lt;/h3&gt;

&lt;p&gt;With the beta release of Vue 3 support in NativeScript-Vue, there was a clear opportunity to leverage Pinia. However, Pinia's HMR support is designed for Vite, while NativeScript-Vue operates on Webpack 5. This required a creative solution to enable HMR for Pinia within a Webpack environment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Our Solution Process
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Step 1: Configuring TypeScript for Webpack
&lt;/h4&gt;

&lt;p&gt;Our first step was to configure TypeScript for compatibility with Webpack's module system. This involved updating &lt;code&gt;projectRoot/types/node.d.ts&lt;/code&gt; to include a reference to &lt;code&gt;webpack/module&lt;/code&gt; and ensuring the "types" were correctly configured in the &lt;code&gt;tsconfig&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 2: Understanding Webpack's &lt;code&gt;.accept()&lt;/code&gt; Method
&lt;/h4&gt;

&lt;p&gt;We identified a key difference in how Webpack handles its &lt;code&gt;.accept()&lt;/code&gt; method compared to Vite. In Webpack, this method does not automatically call the callback function. This necessitated a manual approach to managing module updates.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 3: Adapting &lt;code&gt;acceptHMRUpdate&lt;/code&gt; for Webpack
&lt;/h4&gt;

&lt;p&gt;Next, we adapted Pinia's &lt;code&gt;acceptHMRUpdate&lt;/code&gt; function to suit Webpack's methodology. The modified function now receives the initial store ID directly and uses &lt;code&gt;getActivePinia&lt;/code&gt; to obtain the Pinia instance.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 4: Implementing HMR in Store Files
&lt;/h4&gt;

&lt;p&gt;In our store files, we implemented HMR using &lt;code&gt;import.meta.webpackHot.accept()&lt;/code&gt; and &lt;code&gt;import.meta.webpackHot.dispose()&lt;/code&gt;. The &lt;code&gt;dispose&lt;/code&gt; method is essential for preserving the store ID for the updated &lt;code&gt;acceptHMRUpdate&lt;/code&gt; function.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 5: Handling the Module Update
&lt;/h4&gt;

&lt;p&gt;A significant hurdle was managing the new module, especially given the limitations of &lt;code&gt;module.exports&lt;/code&gt; in ES Modules. We addressed this by passing an object containing only the necessary stores to &lt;code&gt;piniaAccept&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 6: Creating a Helper Function
&lt;/h4&gt;

&lt;p&gt;For ease of use, we wrapped the HMR logic in a helper function. For a single store, the implementation is as follows:&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="k"&gt;if &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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webpackHot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;usePiniaWebpackHotHMR&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webpackHot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useAuthStore&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;For multiple stores in one file (which should not usually be the case), you can pass an array of stores:&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="k"&gt;if &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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webpackHot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;usePiniaWebpackHotHMR&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;meta&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;webpackHot&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;useAuthStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useUiStore&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 then add a &lt;code&gt;utils/piniaHmr.ts&lt;/code&gt; file with the below contents, or visit this &lt;a href="https://gist.github.com/primeboard-dev/79d230e4178c4dea2ff598601ed265bf" rel="noopener noreferrer"&gt;gist link&lt;/a&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Your stores should be importing the &lt;code&gt;usePiniaWebpackHotHMR&lt;/code&gt; method from the above &lt;code&gt;piniaHmr.ts&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Integrating HMR support for Pinia in NativeScript-Vue with Webpack 5 was a complex but rewarding endeavor. Our solution demonstrates the importance of adapting and overcoming challenges in technology integrations. We hope this solution proves valuable for developers working with NativeScript-Vue and Pinia in a Webpack environment.&lt;/p&gt;

&lt;p&gt;Separate thanks to &lt;a class="mentioned-user" href="https://dev.to/vallemar"&gt;@vallemar&lt;/a&gt; and &lt;a class="mentioned-user" href="https://dev.to/mrsnoozles"&gt;@mrsnoozles&lt;/a&gt; - their initial work on HMR improvement has been paramount to this success.&lt;/p&gt;

&lt;p&gt;Keep coding and exploring the vast world of web development!&lt;/p&gt;

</description>
      <category>nativescript</category>
      <category>vue</category>
      <category>nativescriptvue</category>
      <category>crossplatform</category>
    </item>
  </channel>
</rss>
