<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>webpack watch Archives - Yumasoft</title>
	<atom:link href="https://blog.yumasoft.pl/tag/webpack-watch/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yumasoft.pl/tag/webpack-watch/</link>
	<description>Software development blog</description>
	<lastBuildDate>Wed, 24 Nov 2021 02:06:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.yumasoft.pl/wp-content/uploads/2021/05/cropped-yumasoft_icon_transparent-32x32.png</url>
	<title>webpack watch Archives - Yumasoft</title>
	<link>https://blog.yumasoft.pl/tag/webpack-watch/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Webpack Watch Cached And Not Refreshing Bundles</title>
		<link>https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/</link>
					<comments>https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Wed, 24 Nov 2021 06:00:00 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[webpack]]></category>
		<category><![CDATA[webpack watch]]></category>
		<guid isPermaLink="false">https://blog.yumasoft.pl/?p=829</guid>

					<description><![CDATA[<p>If you use webpack as your JavaScript bundler, you might have experienced an issue with webpack --watch not refreshing your bundles when modifying the source files. In this case, webpack&#8217;s output after changing the file is similar to the following one: As you can see, there are some [cached] mentions there in a few places&#8230;</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/">Webpack Watch Cached And Not Refreshing Bundles</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>If you use <a href="https://webpack.js.org/">webpack</a> as your JavaScript bundler, you might have experienced an issue with <code>webpack --watch</code> not refreshing your bundles when modifying the source files. In this case, webpack&#8217;s output after changing the file is similar to the following one:</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="87" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-1024x87.png" alt="Webpack output showing [cached]" class="wp-image-830" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-1024x87.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-300x25.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache-768x65.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchNotRefreshingCache.png 1112w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>As you can see, there are some <code>[cached]</code> mentions there in a few places and no <code>[code generated]</code> statements at all. The effect is that your bundles are not rebuilt at all. In order to have them refreshed, you need to restart the building process, which takes time and makes your development flow painful. If you want to know how to solve that problem, read on ?</p>



<span id="more-829"></span>



<h2 class="wp-block-heading">webpack watch cached files</h2>



<p>We have struggled with this issue for quite some time. It starts with launching <code>webpack</code> with <code>--watch</code> option. Such build script can be defined in your <code>package.json</code> in the following or similar way:</p>



<script src="https://gist.github.com/dsibinski/974e74538fae860a5d1f908b8193fcd4.js"></script>



<p>The effect you would expect is for webpack to rebuild your bundles as soon as you make any change in the source code.</p>



<p>When the webpack watch cached issue occurs, the first change you make in a file is properly reloaded by <code>webpack</code> and looks similarly to that:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="375" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-1024x375.png" alt="webpack watch correctly rebuilding bundles with [code generated] statements" class="wp-image-832" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-1024x375.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-300x110.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime-768x281.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/2_WebpackWatchCorrectRefreshFirstTime.png 1048w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>However, as soon as you change the file for the second time, the output looks less promising:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="87" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-1024x87.png" alt="webpack watch cached issue" class="wp-image-833" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-1024x87.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-300x25.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache-768x65.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/1_WebpackWatchNotRefreshingCache.png 1112w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>As soon as you see some of these <code>[cached]</code> things, it probably means that webpack cached something and did not generate new bundles. Especially if there are no <code>[built]</code> and <code>[code generated]</code> statements in the output. You notice that your files are not refreshed, so your last change is not reflected in JS bundles ?</p>



<h2 class="wp-block-heading">Hotfix: disable webpack cache</h2>



<p>The first hotfix solution &#8211; especially if you can&#8217;t easily find the cause &#8211; is to disable <code>webpack</code> cache completely. In order to do that, you should set <code>cache</code> to <code>false</code> in your <a href="https://webpack.js.org/configuration/cache/">webpack configuration file</a>:</p>



<div class="wp-block-image"><figure class="aligncenter size-full"><img loading="lazy" decoding="async" width="413" height="216" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/3_WebpackDisableCache.png" alt="cache set to false in webpack config file" class="wp-image-836" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/3_WebpackDisableCache.png 413w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/3_WebpackDisableCache-300x157.png 300w" sizes="auto, (max-width: 413px) 100vw, 413px" /></figure></div>



<p>This will work, and webpack should now rebuild your bundles on every source change. However, it comes with a significant performance cost. Disabling the cache will make webpack actually rebuilding all your bundles on each change. This solution is actually a shortcut for restarting the build from scratch. Not what we really want ? Let&#8217;s see how to find the cause and solve webpack watch cached issue properly.</p>



<h2 class="wp-block-heading">Wrong casing &#8211; the real cause of webpack watch cached issue</h2>



<p>In our case, the problem turned out to basically be <strong>wrong casing when importing stuff</strong>. </p>



<p>The general advice is to find how given file you are modifying is entered into your webpack&#8217;s build flow. It can be <a href="https://flexiple.com/javascript-require-vs-import/">imported or required</a> directly. Maybe it&#8217;s used via <a href="https://webpack.js.org/configuration/resolve/">webpack alias</a>. I know these are quite general statements, so I&#8217;ll show you two examples.</p>



<h3 class="wp-block-heading">Wrong casing in import</h3>



<p>First, we had a problem of <code>webpack watch</code> not refreshing bundles after making changes in a single file. This file is named <code>questionnaireCorrectTemplate.tsx</code>. It exports a single React component called <code>QuestionnaireCorrectTemplate</code>.</p>



<p>In order to use this component, we imported it in another file in the following way:</p>



<script src="https://gist.github.com/dsibinski/a3e267ad6a41defc62fa22b405f8f96a.js"></script>



<p>Have you already noticed the problem? Note that the import contains the module name with <strong>capital</strong> letter: &#8220;./<strong>Q</strong>uestionnaireCorrectTemplate&#8221;. As soon as we changed it to a small letter, so exactly as the  <code>questionnaireCorrectTemplate.tsx</code>  file&#8217;s name:</p>



<script src="https://gist.github.com/dsibinski/af26050bee05f9c52a5a253326b801a6.js"></script>



<p>Everything started to magically work ? Since this change, <code>webpack watch</code> is properly rebuilding our bundles after changes in the affected file ?</p>



<h3 class="wp-block-heading">Wrong casing in webpack alias</h3>



<p>We had another issue with <code>webpack</code> not refreshing the bundles when we made changes in a <code>Site.css</code> file. This is a file in which we store custom CSS for our React app. The issue was very similar to the previous one. After starting <code>webpack watch</code>, the first modification of <code>Site.css</code> file triggered a proper rebuild of bundles:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img loading="lazy" decoding="async" width="1024" height="315" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-1024x315.png" alt="webpack watch correctly rebuilding bundles for css file" class="wp-image-840" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-1024x315.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-300x92.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh-768x236.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/4_WebpackCssCorrectRefresh.png 1114w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure></div>



<p>However, all subsequent modifications to the <code>Site.css</code> file causes the webpack watch cached issue:</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="66" src="https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-1024x66.png" alt="webpack watch cached issue for css file" class="wp-image-841" srcset="https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-1024x66.png 1024w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-300x19.png 300w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue-768x50.png 768w, https://blog.yumasoft.pl/wp-content/uploads/2021/11/5_WebpackCachedCssIssue.png 1094w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>The reason was wrong casing again. However, this time we found out that the <code>Site.css</code> file was included as an alias in webpack configuration file:</p>



<script src="https://gist.github.com/dsibinski/731c30adc2b09ac12120a8426be5b400.js"></script>



<p>I guess you can already see what is the problem ? The file was included as <code><strong>s</strong>ite.css</code>, instead of <code><strong>S</strong>ite.css</code>. As soon as we changed this part of the <code>webpack.config.js</code> file to the one with proper casing:</p>



<script src="https://gist.github.com/dsibinski/2d9900d68441aa83e50584d0ac2b91a8.js"></script>



<p>Everything started to work correctly. Since this change, <code>webpack watch</code> correctly refreshes the bundles after each modification of <code>Site.css</code> file ?</p>



<h2 class="wp-block-heading">Webpack watch cached &#8211; summary</h2>



<p>I hope this article helps you solve your <code>webpack watch</code> refreshing issue ? Webpack is still kind of magical tool for me and it surprises me very often. Such issues can be really painful &#8211; that&#8217;s why I wanted to share our solution to this problem with you. </p>



<p>The general advice is to find how the problematic file is imported/required/added to webpack build flow. It&#8217;s very probably that you have the wrong casing somewhere. I hope it saves you some pain one day!</p>
<p>The post <a rel="nofollow" href="https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/">Webpack Watch Cached And Not Refreshing Bundles</a> appeared first on <a rel="nofollow" href="https://blog.yumasoft.pl">Yumasoft</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.yumasoft.pl/2021/11/webpack-watch-cached-and-not-refreshing-bundles/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
