<?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: Hungmi</title>
    <description>The latest articles on DEV Community by Hungmi (@hungmi).</description>
    <link>https://dev.to/hungmi</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%2F711018%2Fdb398a42-9b77-4f0f-8c1e-eb6909c46618.jpeg</url>
      <title>DEV Community: Hungmi</title>
      <link>https://dev.to/hungmi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hungmi"/>
    <language>en</language>
    <item>
      <title>忘了 webpacker，舊版 Rails app 升級 jsbundling-rails + Hotwire</title>
      <dc:creator>Hungmi</dc:creator>
      <pubDate>Wed, 22 Sep 2021 11:12:33 +0000</pubDate>
      <link>https://dev.to/hungmi/webpacker-rails-app-jsbundling-rails-hotwire-4908</link>
      <guid>https://dev.to/hungmi/webpacker-rails-app-jsbundling-rails-hotwire-4908</guid>
      <description>&lt;h2&gt;
  
  
  本篇升級方式的好處
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;降低了現存的 Rails 4 項目升級門檻，不需要 webpacker 就能開心的使用 node packages。&lt;/li&gt;
&lt;li&gt;若想放棄 jQuery，新頁面將不需要再導入相關依賴。&lt;/li&gt;
&lt;li&gt;享受 Hotwire + Turbo 帶來的效率及速度優化。
希望能幫到像我一樣正在穩定開發但又想升級項目的朋友們！&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  先升級至 Rails 6
&lt;/h3&gt;

&lt;p&gt;因為 &lt;a href="https://github.com/rails/jsbundling-rails"&gt;jsbundling-rails&lt;/a&gt; 及 &lt;a href="https://github.com/hotwired/hotwire-rails"&gt;hotwire-rails&lt;/a&gt; 本身就限制至少 Rails 6，因此必須升級，但&lt;strong&gt;不需要安裝 webpacker，因此不會動到 JS &amp;amp; CSS 大幅降低了升級工作量，以我個人而言，從「太難了我放棄」到「兩天完成升級」🚀&lt;/strong&gt;。&lt;br&gt;
Rails 的升級步驟可參考 t27duck 撰寫的 &lt;a href="http://www.t27duck.com/posts/8-how-i-upgrade-ruby-on-rails"&gt;How I upgrade Ruby on Rails&lt;/a&gt;，其中特別注意 &lt;strong&gt;Step 6 Update the Default Configuration and Framework Files&lt;/strong&gt;。&lt;/p&gt;



&lt;p&gt;本篇文章的 &lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails"&gt;Sample Code 在此&lt;/a&gt;，建議可以看我分開 commit 的結果比較清楚✌️，也可使用 git checkout 回溯測試看看！&lt;/p&gt;

&lt;p&gt;首先我 &lt;code&gt;rails new rails_6_with_jsbundling_rails --skip-javascript&lt;/code&gt; 新建一個沒有任何 js 的&lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/42ba98f9c7b253e7ad0e794f83eb1a99e87d4d8a"&gt;新項目&lt;/a&gt;，當然，也沒裝 webpacker。&lt;/p&gt;

&lt;p&gt;然後&lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/655b22782dd6b4b16c8fe9851948efe116833efc"&gt;加入一些 JS&lt;/a&gt; 以及 &lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/85d9fe39a8ce74936c82e3dc5b2e4580a7243b36"&gt;Admin 區塊&lt;/a&gt;用來代表項目中已存在的功能。&lt;/p&gt;

&lt;p&gt;接著&lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/8ee77211104a41bef58c90e67c622ee42a631d3a"&gt;很單純的安裝 jsbundling-rails&lt;/a&gt;：&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;bundle add jsbundling-rails
rails javascript:install:esbuild
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;可以看到我已經有了 package.json，表示我可以透過 &lt;code&gt;yarn add&lt;/code&gt; 安裝所有 node packages👍。&lt;/p&gt;

&lt;h3&gt;
  
  
  重點
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;上面安裝 jsbundling-rails 的同時也裝了 foreman，因此請改用 &lt;code&gt;foreman start -f Procfile.dev&lt;/code&gt; 開啟 Rails server + yarn build。&lt;/li&gt;
&lt;li&gt;進到 &lt;a href="http://localhost:3000/"&gt;http://localhost:3000/&lt;/a&gt; 會發現舊的 JS 已經全炸了💥，而且layout 裡面載入了兩次 application.js
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;head&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;...&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= javascript_include_tag 'application' %&amp;gt;
&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;javascript_include_tag&lt;/span&gt; &lt;span class="s2"&gt;"application"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"data-turbo-track"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"reload"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;defer: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt; &lt;span class="sx"&gt;%&amp;gt;
...
&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;但 &lt;code&gt;application-thisisalonghash.js&lt;/code&gt; 原始碼卻是空白的，這是&lt;strong&gt;因為現在的 JS entry point 已經改用 &lt;code&gt;app/javascript/application.js&lt;/code&gt; 了。&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;而我們需要做的就是&lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/981d4850591a4a1d8a417d4a39b9131267686cad"&gt;將舊的 application.js 改名，並且微調 manifest.js 及相關的 &lt;code&gt;javascript_include_tag&lt;/code&gt;&lt;/a&gt;，就可以讓新舊程式同時運作。&lt;/p&gt;

&lt;p&gt;到這邊，我已經可以&lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/c6767db1370f0416b40fdaaf3fe1f16a12c9f2d4"&gt;安裝 hotwire-rails&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle add hotwire-rails
rails hotwire:install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;然後&lt;a href="https://github.com/hungmi/rails_6_with_jsbundling_rails/commit/99d9955e6406b320d4c8b350a5e3ad32bf57340a"&gt;測試一下 Turbo &amp;amp; Stimulus&lt;/a&gt; 都跟舊的 JS 一起運作。&lt;/p&gt;

&lt;p&gt;以上。&lt;/p&gt;




&lt;h2&gt;
  
  
  其他
&lt;/h2&gt;

&lt;h3&gt;
  
  
  雖然升級很難，但不用 webpacker 好嗎？
&lt;/h3&gt;

&lt;p&gt;jsbundling-rails 也支援 webpack，&lt;a href="https://github.com/rails/jsbundling-rails/blob/main/docs/comparison_with_webpacker.md"&gt;而且更好&lt;/a&gt;。&lt;br&gt;
另外，未來 Rails 的主軸將會擺在 &lt;a href="https://github.com/rails/importmap-rails"&gt;importmap&lt;/a&gt; 及 &lt;a href="https://github.com/rails/jsbundling-rails"&gt;jsbundling-rails&lt;/a&gt;，&lt;a href="https://world.hey.com/dhh/rails-7-will-have-three-great-answers-to-javascript-in-2021-8d68191b"&gt;DHH 也詳述了背後的原因&lt;/a&gt;。&lt;/p&gt;

&lt;h3&gt;
  
  
  幹嘛要用 Hotwire？
&lt;/h3&gt;

&lt;p&gt;其實繼續使用 Rails &amp;lt; 6 的開發方式並沒有太大的問題，例如 Stimulus 也有提供&lt;a href="https://stimulus.hotwired.dev/handbook/installing#using-without-a-build-system"&gt;不需編譯的安裝方式&lt;/a&gt;，安裝之後就可以避免掉許多 SJR 替換 HTML 後需要重新初始化 JS 功能的情境。&lt;/p&gt;

&lt;p&gt;雖然我對前端生態並不熟悉，甚至不知道將 JS 模組化的好處，但在目前的開發流程中，我發現比起 Stimulus 搭配 SJR 的組合技，使用 Hotwire 不只減少了請求數，並且維護性更佳。&lt;/p&gt;

&lt;h3&gt;
  
  
  幹嘛不用 CDN 安裝 hotwire 而要用 hotwire-rails gem？
&lt;/h3&gt;

&lt;p&gt;雖然 Hotwire 的 Custom HTML Element &lt;code&gt;&amp;lt;turbo-frame&amp;gt;&lt;/code&gt; 只是 plain HTML，但不使用 gem 的話，Rails controller 無法 &lt;code&gt;respond_to format.turbo_stream&lt;/code&gt;，甚至在 &lt;code&gt;*.turbo_stream.erb&lt;/code&gt; 的使用也會受限。&lt;/p&gt;

&lt;h3&gt;
  
  
  為何不用 importmap-rails？
&lt;/h3&gt;

&lt;p&gt;其實我有安裝過 importmap-rails 了，也不難使用，但因為要等 Rails 7 才能使用 ActiveStorage/ActionText/ActionCable ，而我的項目暫時無法升級，所以只好放棄。&lt;br&gt;
等未來 Rails 7 正式釋出時，我會考慮升級並轉換到 importmap-rails。&lt;/p&gt;

</description>
      <category>chinese</category>
      <category>rails</category>
      <category>hotwire</category>
      <category>webpacker</category>
    </item>
  </channel>
</rss>
