<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>jiazhen (Jiazhen Xie)</title>
    <link>https://ruby-china.org/jiazhen</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>在 Rails 中使用 Yarn 管理三方 assets</title>
      <description>&lt;p&gt;近期觉得 Rails asset 不是那么顺畅，又刚好弄了一下 Yarn，故整合了一下。以下是我的 &lt;a href="https://sheerdevelopment.com/posts/using-yarn-with-rails" rel="nofollow" target="_blank" title=""&gt;原文&lt;/a&gt; :) &lt;/p&gt;
&lt;h2 id="Background"&gt;Background&lt;/h2&gt;
&lt;p&gt;As the rise of Javascript, it's undeniable that we would need to use Javascript libraries to get better user experiences on the web application. In Rails, however, adding a Javascript library is not as smoonth as adding a gem. &lt;/p&gt;

&lt;p&gt;You could download the javascript library and manually put it in &lt;code&gt;vendor&lt;/code&gt;. However, the responsiblities of configurations and correcting font paths are on you. Or maybe rely on a relavant gem if luckily find it. But what if the gem is no longer maintained?&lt;/p&gt;

&lt;p&gt;Look at Javascript ecosystem, &lt;a href="https://www.npmjs.com/" rel="nofollow" target="_blank" title=""&gt;npm&lt;/a&gt;, &lt;a href="https://bower.io/" rel="nofollow" target="_blank" title=""&gt;bower&lt;/a&gt; and the most recent - &lt;a href="https://yarnpkg.com/" rel="nofollow" target="_blank" title=""&gt;yarn&lt;/a&gt;, they are meant to manage Javascript libraries, so why don't we use one of them?&lt;/p&gt;
&lt;h2 id="Install Yarn"&gt;Install Yarn&lt;/h2&gt;
&lt;p&gt;I decided to use Yarn in which is a fascinating package manament tool as introduced in my &lt;a href="https://sheerdevelopment.com/posts/facebook-js-5" rel="nofollow" target="_blank" title=""&gt;previous post&lt;/a&gt;. So firstly we need to install Yarn. With MacOS just install it with brew&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew update
brew install yarn
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And add &lt;code&gt;export PATH="$PATH:$HOME/.yarn/bin"&lt;/code&gt; to your profile (this may be in your &lt;code&gt;.profile&lt;/code&gt;, &lt;code&gt;.bashrc&lt;/code&gt;, &lt;code&gt;.zshrc&lt;/code&gt;, etc.)&lt;/p&gt;

&lt;p&gt;Test that Yarn is installed by running:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn --version
# 0.16.1
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id="Initialise a Rails project"&gt;Initialise a Rails project&lt;/h2&gt;
&lt;p&gt;First of all, initialise a new Rails project, run:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails new yarn-with-rails
cd yarn-with-rails
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And create a test page&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails g controller static_pages show
rails s
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You should be able to see the test page on &lt;a href="http://localhost:3000/static_pages/show" rel="nofollow" target="_blank" title=""&gt;http://localhost:3000/static_pages/show&lt;/a&gt; as below&lt;/p&gt;

&lt;p&gt;&lt;img src="http://i.imgur.com/8vi2Waz.png" title="" alt="StaticShow"&gt;&lt;/p&gt;

&lt;p&gt;Next to initialise yarn&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;OK, it's time to do the actual experiment.&lt;/p&gt;
&lt;h2 id="Rails with Yarn"&gt;Rails with Yarn&lt;/h2&gt;
&lt;p&gt;I will use &lt;a href="http://fontawesome.io/" rel="nofollow" target="_blank" title=""&gt;FontAwesome&lt;/a&gt; for this experiment. Run:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add font-awesome --modules-folder ./vendor/assets/components/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The latest font-awesome library is now installed in the Rails project! Add the following to &lt;code&gt;config/initializers/assets.rb&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Rails.application.config.assets.paths &amp;lt;&amp;lt; Rails.root.join("vendor", "assets", "components")
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Rename &lt;code&gt;app/assets/stylesheets/application.css&lt;/code&gt; to &lt;code&gt;app/assets/stylesheets/application.scss&lt;/code&gt; and change the contents to the following:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 *= require font-awesome/scss/font-awesome
 */
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now can use awesome icons on our test page! In &lt;code&gt;app/views/static_pages/show.html.erb&lt;/code&gt;, add&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;
  I &amp;lt;i class="fa fa-heart" aria-hidden="true"&amp;gt;&amp;lt;/i&amp;gt; Ruby
&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Restart the server and visit &lt;a href="http://localhost:3000/static_pages/show" rel="nofollow" target="_blank" title=""&gt;http://localhost:3000/static_pages/show&lt;/a&gt;, font-awesome is on the page! \o/&lt;/p&gt;

&lt;p&gt;&lt;img src="http://i.imgur.com/GY8zWjD.png" title="" alt="static page with font awesome"&gt;&lt;/p&gt;
&lt;h2 id="Font path dependency"&gt;Font path dependency&lt;/h2&gt;
&lt;p&gt;On development it's now perfect. However, if we deploy to production (on Heroku in my case), the icon is not showing and has issue with incorrect font path. We need to overwrite &lt;code&gt;$fa-font-path&lt;/code&gt; to point to the correct path.&lt;/p&gt;

&lt;p&gt;Create &lt;code&gt;app/assets/stylesheets/font-path-overwrite.scss&lt;/code&gt; and add:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$fa-font-path: "font-awesome/fonts/";
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then change &lt;code&gt;app/assets/stylesheets/application.css&lt;/code&gt;:&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
/*
 * This is a manifest file that'll be compiled into application.css, which will include all the files
 * listed below.
 *
 *= require font_path_overwrite
 *= require font-awesome/scss/font-awesome
 */
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That's it!&lt;/p&gt;
&lt;h2 id="Conclusion"&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;So now with Yarn, we can easily retrieve the appropriate Javascript libraries and manage them. The feeling is as using the right tool for the job, saving time with the most elegant way. I love Ruby, Rails is my most favourite web framework, hope this can make it better. If you have any question, feel free to comment or drop me a line.&lt;/p&gt;</description>
      <author>jiazhen</author>
      <pubDate>Fri, 18 Nov 2016 07:24:27 +0800</pubDate>
      <link>https://ruby-china.org/topics/31625</link>
      <guid>https://ruby-china.org/topics/31625</guid>
    </item>
    <item>
      <title>Show RC - ReleaseHub</title>
      <description>&lt;p&gt;自己做的部署请求 app，集成了 github 和 slack。公司内部用了几个月反响不错，决定开源。欢迎大家指教 :)&lt;/p&gt;

&lt;p&gt;Github link - &lt;a href="https://github.com/jiazhen/releasehub" rel="nofollow" target="_blank"&gt;https://github.com/jiazhen/releasehub&lt;/a&gt;&lt;/p&gt;</description>
      <author>jiazhen</author>
      <pubDate>Wed, 30 Dec 2015 22:12:59 +0800</pubDate>
      <link>https://ruby-china.org/topics/28573</link>
      <guid>https://ruby-china.org/topics/28573</guid>
    </item>
    <item>
      <title>Virtual Conference - hacksummit</title>
      <description>&lt;p&gt;RT, looks coo :)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hacksummit.org/" rel="nofollow" target="_blank"&gt;https://hacksummit.org/&lt;/a&gt;&lt;/p&gt;</description>
      <author>jiazhen</author>
      <pubDate>Wed, 12 Nov 2014 20:17:33 +0800</pubDate>
      <link>https://ruby-china.org/topics/22631</link>
      <guid>https://ruby-china.org/topics/22631</guid>
    </item>
  </channel>
</rss>
