<?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>webpacker | Spiffy Stores Blog</title>
	<atom:link href="https://www.spiffystores.com.au/blog/tag/webpacker/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.spiffystores.com.au/blog</link>
	<description>Checkout - The Spiffy Stores Blog</description>
	<lastBuildDate>Tue, 11 May 2021 04:14:13 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
<site xmlns="com-wordpress:feed-additions:1">241205771</site>	<item>
		<title>Adding jQuery to Webpacker 6 under Rails 6</title>
		<link>https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/</link>
		
		<dc:creator><![CDATA[Brian]]></dc:creator>
		<pubDate>Tue, 11 May 2021 04:14:10 +0000</pubDate>
				<category><![CDATA[Geek stuff]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[webpack]]></category>
		<category><![CDATA[webpacker]]></category>
		<guid isPermaLink="false">https://www.spiffystores.com.au/blog/?p=3163</guid>

					<description><![CDATA[<p>Tips for migrating to Webpacker 6 using Rails 6.1 and Webpack 5. <a href="https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/">Continue reading <span class="meta-nav">&#8594;</span></a></p>
The post <a href="https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/">Adding jQuery to Webpacker 6 under Rails 6</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></description>
										<content:encoded><![CDATA[<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='Adding jQuery to Webpacker 6 under Rails 6' data-link='https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/' data-summary='Tips for migrating to Webpacker 6 using Rails 6.1 and Webpack 5.' data-app-id-name='category_above_content'></div>
<p>From time to time we post some notes about technical issues that we&#8217;ve encountered during our development work on Spiffy Stores. There are a lot of moving parts to manage, and sometimes we come across some tips or techniques that can help others to build their projects.</p>



<p>In this case, we&#8217;ve been looking at how to configure Webpacker 6 (currently Beta 7) under Rails 6.1, which acts as a wrapper to the latest version of Webpack 5. Now, as anyone who&#8217;s used Webpack since it was introduced as an option in Rails 5 will attest to, it&#8217;s not an easy package to get working properly.</p>



<p>The new versions of Webpacker and Webpack bring around some changes to the way things are configured, and unfortunately, some of the documentation is lacking, or indeed, wrong.</p>



<p>For our environment, we need jQuery as a starter. The trick with Webpack is to get the code loaded and assigned to the global variables, $ and jQuery. The trick with jQuery is to realize that the node modules version provides both a distributed module and also the source code which can be used to build the module with Webpack. If you just </p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">require("jquery")</code></pre>



<p>then you&#8217;ll get the pre-built module. It turns out that it&#8217;s better to build it from source instead.</p>



<p>If you&#8217;ve followed the migration documentation, you should have a custom.js under your webpack directory.</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">module.exports = {
   resolve: {
     alias: {
       jquery: 'jquery/src/jquery'
     }
   }
 }</code></pre>



<p>Requiring jquery now, pulls in the source files instead, and this will build jQuery with the appropriate global references.</p>



<p>Another important &#8220;gotcha&#8221; with Webpacker 6 is that when you include the javascript_pack_tag in your layout, you MUST include it only once.</p>



<pre class="wp-block-code"><code lang="ruby" class="language-ruby">&lt;%= javascript_pack_tag('application', 'common', 'customer', data: { turbolinks_track: :reload }) %></code></pre>



<p>Webpack builds a number of different file chunks for each entry point, and this means that the javascript_pack_tag will generate a number of &lt;script> tags, one for each chunk. If you have multiple entry point files, then you must include them all on a single javascript_pack_tag and not create a separate tag for each entry point.</p>



<p>Unfortunately, coming from a Sprockets background, most Rails programmers will be used to creating multiple javascript_include_tags, but this is not the case for Webpacker.</p>



<p>If you happen to use multiple javascript_pack_tags, you&#8217;ll likely find that scripts may be loaded multiple times. In many cases, this may not be obvious, but if you see errors from @rails/ujs, then this is probably because it is being loaded multiple times by Webpack.</p>



<p></p>
<div style='display:none;' class='shareaholic-canvas' data-app='share_buttons' data-title='Adding jQuery to Webpacker 6 under Rails 6' data-link='https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/' data-summary='Tips for migrating to Webpacker 6 using Rails 6.1 and Webpack 5.' data-app-id-name='category_below_content'></div><div style='display:none;' class='shareaholic-canvas' data-app='recommendations' data-title='Adding jQuery to Webpacker 6 under Rails 6' data-link='https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/' data-summary='Tips for migrating to Webpacker 6 using Rails 6.1 and Webpack 5.' data-app-id-name='category_below_content'></div>The post <a href="https://www.spiffystores.com.au/blog/2021/05/11/adding-jquery-to-webpacker-6-under-rails-6/">Adding jQuery to Webpacker 6 under Rails 6</a> first appeared on <a href="https://www.spiffystores.com.au/blog">Spiffy Stores Blog</a>.]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3163</post-id>	</item>
	</channel>
</rss>
