{"id":3163,"date":"2021-05-11T14:14:10","date_gmt":"2021-05-11T04:14:10","guid":{"rendered":"https:\/\/www.spiffystores.com.au\/blog\/?p=3163"},"modified":"2021-05-11T14:14:13","modified_gmt":"2021-05-11T04:14:13","slug":"adding-jquery-to-webpacker-6-under-rails-6","status":"publish","type":"post","link":"https:\/\/www.spiffystores.com.au\/blog\/2021\/05\/11\/adding-jquery-to-webpacker-6-under-rails-6\/","title":{"rendered":"Adding jQuery to Webpacker 6 under Rails 6"},"content":{"rendered":"\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">require(\"jquery\")<\/code><\/pre>\n\n\n\n<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>\n\n\n\n<p>If you&#8217;ve followed the migration documentation, you should have a custom.js under your webpack directory.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"javascript\" class=\"language-javascript\">module.exports = {\n   resolve: {\n     alias: {\n       jquery: 'jquery\/src\/jquery'\n     }\n   }\n }<\/code><\/pre>\n\n\n\n<p>Requiring jquery now, pulls in the source files instead, and this will build jQuery with the appropriate global references.<\/p>\n\n\n\n<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>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"ruby\" class=\"language-ruby\">&lt;%=\u00a0javascript_pack_tag('application',\u00a0'common',\u00a0'customer',\u00a0data:\u00a0{\u00a0turbolinks_track:\u00a0:reload\u00a0}) %><\/code><\/pre>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<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\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[277,278,12,276,275],"class_list":["post-3163","post","type-post","status-publish","format-standard","hentry","category-geek-stuff","tag-javascript","tag-jquery","tag-rails","tag-webpack","tag-webpacker"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pgk4DF-P1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/3163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/comments?post=3163"}],"version-history":[{"count":29,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/3163\/revisions"}],"predecessor-version":[{"id":3193,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/posts\/3163\/revisions\/3193"}],"wp:attachment":[{"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/media?parent=3163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/categories?post=3163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spiffystores.com.au\/blog\/wp-json\/wp\/v2\/tags?post=3163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}