<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[進擊的 Front End‘s - Medium]]></title>
        <description><![CDATA[新手起步 - Medium]]></description>
        <link>https://medium.com/html-test?source=rss----7e8ed50b6485---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>進擊的 Front End‘s - Medium</title>
            <link>https://medium.com/html-test?source=rss----7e8ed50b6485---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 04:31:47 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/html-test" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[【LeetCode】728. Self Dividing Numbers]]></title>
            <link>https://medium.com/html-test/leetcode-728-self-dividing-numbers-2a9634ff0179?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/2a9634ff0179</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Fri, 30 Nov 2018 06:38:52 GMT</pubDate>
            <atom:updated>2018-11-30T06:38:52.187Z</atom:updated>
            <content:encoded><![CDATA[<p>最近想要重新練練演算法，想到之前同事有跟我提過 LeetCode 上有很多題目可以練習，加強對程式邏輯上的訓練。最近想重新回歸練習一下，決定從最簡單的題目開始練習。</p><p><a href="https://leetcode.com/problems/self-dividing-numbers/description/">Self Dividing Numbers - LeetCode</a></p><p>Self Dividing Numbers 題目是：給最小值(Left) 與最大值(Right)，然後找出這兩數之間所有的自除數。</p><p>自除數定義：可以被自己的每一位數整除的數，即是自除數。比方說：128可以被 1、2、8整除，128即是整除數。但有個前提是，如果數字本身有0就一定不是自除數。比方說 10、101、2040都不是自除數。</p><h3>我的解法</h3><p>給參考，但不一定是最佳解：</p><ol><li>先寫一個迴圈從 left 到 right，迴圈在開始與重複時，d 設為 i</li><li>迴圈裡遇到10的倍數則 for continue</li><li>進到 do 迴圈</li><li>如果 i 不能被 d 的個位數整除，即非自除數，continue for loop i</li><li>如果 i 可以被 d 的個位數整除，則將 d 整除整除 10</li><li>如果 d 非 0 則跳至步驟4</li><li>離開 while 迴圈，把 i push 到 numbers 陣列裡</li><li>回傳 numbers 陣列。</li></ol><pre>/**<br> * <a href="http://twitter.com/param">@param</a> {number} left<br> * <a href="http://twitter.com/param">@param</a> {number} right<br> * <a href="http://twitter.com/return">@return</a> {number[]}<br> */<br><strong>var</strong> selfDividingNumbers = <strong>function</strong>(left, right) {<br>  <strong>let</strong> d = 0;<br>  <strong>let</strong> numbers = [];<br>  labelI:<br>  <strong>for</strong> (<strong>let</strong> i = left, d = i; i &lt;= right; i++, d = i) {<br>    if (!(i % 10)) continue;<br>    <strong>do</strong> {<br>      if (i % (d % 10) !== 0) <strong>continue</strong> labelI;<br>      d = ~~(d / 10);<br>    } <strong>while</strong> (d);<br>    numbers.push(i);<br>  }<br>  <strong>return</strong> numbers;<br>};</pre><h3>執行結果</h3><p>left = 1, right = 22, Runtime: 48 ms</p><h3>如果你覺得不錯，請幫我按拍手，覺得屌，按3秒，覺得超屌，按5秒</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a9634ff0179" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/leetcode-728-self-dividing-numbers-2a9634ff0179">【LeetCode】728. Self Dividing Numbers</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[從零開始: 測試吧! 前端]]></title>
            <link>https://medium.com/html-test/%E5%BE%9E%E9%9B%B6%E9%96%8B%E5%A7%8B-%E6%B8%AC%E8%A9%A6%E5%90%A7-%E5%89%8D%E7%AB%AF-c32c29df875d?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/c32c29df875d</guid>
            <category><![CDATA[mocha]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[development]]></category>
            <category><![CDATA[chai]]></category>
            <category><![CDATA[test]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Wed, 06 Sep 2017 01:31:23 GMT</pubDate>
            <atom:updated>2020-04-24T17:15:28.223Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*ZBnOKDjg8tRsXHWdXgWh2g.jpeg" /><figcaption>mocha.js</figcaption></figure><h3>為什麼要測試</h3><p>料理煮好先試吃，講稿寫完先試講，車子做好也會先經過多次的耐撞與性能測試，其實日常生活中許多我們正在用的東西，都是經過無數測試與考驗，確定了產品的穩定性與品質，最後才會讓我們買到、用到。說測試維護了整個社會的穩定性與安全也不為過XD。</p><h3>沒有測試遇到的情況</h3><p>就是東西吃了有人中毒，車子急煞車不靈。以前端來說，就是功能爆掉，無法(或錯使) 使用某某功能，影響小則流量下降，大則減少收益讓產品不再有用使用。通常還沒在寫測試的我們，都交給客戶來幫我們測試。</p><p>最後你會在凌晨三點時收到來自公司電話。說客戶在睡前一點測試了一些功能，發現網站竟不跑出來。這樣客戶明早沒辦法跟老闆交待，希望你可以在明天中午前把該功能修正。</p><p>然後你匆忙的9點到了公司塞三口把三明治吃掉，一邊喝著冰紅茶一邊趕在中午12點前找出問題並把它修正。結果11點客戶就打來問可以測試了嗎因為老闆在問能不能給他看，所以你咬著牙終於在11點半時把東西交了出去然後把還沒喝完的紅茶一口乾掉。</p><p>下午你很自豪的救了今早的火，結果就在準備踏出公司的前一步，AE美眉跟你說客戶很火大，因為中午demo給老闆看的時候竟然出了大包，一堆東西跑不出來，想不到不能跑的東西竟然比可以跑的東西還多! 客戶森77到直接打給老闆，老闆叫AE美眉找出問題後，列一個清單給工程師，並請工程師今天離開公司前一定要處理好。</p><p>凌晨三點鐘，你在公司頂樓抽了一根菸，吐出的煙都是這幾年在公司加班的回憶。此時你覺得自己的命好不值錢，於是往下一跳，提早登出了人生。</p><blockquote>等等！其實你可以不用登出！</blockquote><h3>如果你事先就知道測試的存在</h3><p>它可以預先幫我們測試各種可能的狀況，然後提早幫我們比客戶早一步找到問題並修正。它可以救你的客戶，你的老闆，你的AE美眉，還有你的人生。</p><h3>請你跟我這樣做</h3><p>首先建立一個資料夾，然後進去後輸入 yarn init 來建立 package.json</p><pre>mkdir demo &amp;&amp; cd demo<br>yarn init</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u8I9cfOLixYhiZ9JseTe8w.png" /><figcaption>建立 demo 並進去下個 yarn init</figcaption></figure><p>打開編輯器，你的 package.json 應該是這樣地簡單明瞭。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-DSdRW5p63zazVLqjUB0lA.png" /><figcaption>package.json 內容</figcaption></figure><ul><li>name: 應該就沒什麼好說的，就專案名稱</li><li>version: 預設都是 1.0.0 但我建議一開始從 0.1.0</li><li>main: 主程式檔案，等等說</li><li>license: 沒意外就 <a href="https://zh.wikipedia.org/wiki/MIT%E8%A8%B1%E5%8F%AF%E8%AD%89">MIT</a></li></ul><h3>撰寫主程式</h3><p>我們的主程式很簡單，就寫一個加法。規格如下：</p><pre>add(x, y) = x + y</pre><p>也就是我給 x 跟 y 參數，它應該要回傳 x + y 給我 。趕快來寫一下主程式。還記得剛剛 package.json 裡的 main 內容是什麼嗎？就是 index.js！代表主程式指向專案目錄裡的 index.js，目前沒有，那就建一個，並寫好內容：</p><blockquote>main 指向的 index.js 可以改</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1oz53IluccUiDbJsZggJ9w.png" /><figcaption>index.js 內容</figcaption></figure><p>主程式就叫 add, 內容就很單純的回傳 x + y 的結果，然後注意最後一行：</p><pre>module.exports = add</pre><p>這代表如果有其他檔案引入 index.js 這隻檔案，就等同給它 add 方法使用。</p><h3>安裝 mocha</h3><p>用 yarn add mocha -D 即可安裝， -D 代表 安裝的是 開發用套件，程式上線時不會用到的套件。 mocha 就是測試工具，只有開發會用到。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/862/1*kBdpE3wb_vzmYrHIfYftJg.png" /><figcaption>安裝 mocha</figcaption></figure><p>然後你可看到你的 package.json 多了一些東西， devDependencies 裡多了 mocha 這個套件。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uC9nd_tegpCrH1XXyoWYFA.png" /><figcaption>package.json 已記錄 mocha 資訊</figcaption></figure><p>然後這邊我們需要加入一段程式碼：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cGGVwAHFp4wNZ7gAUYEnlg.png" /><figcaption>加入 scripts test: mocha</figcaption></figure><p>意思是稍後我們可以透過 yarn test or npm test 去執行mocha測試。當然，你也可以馬上就試。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*q5vZ4QN2n9kP46FvFIks0Q.png" /><figcaption>沒有寫 test, yarn test 會報錯</figcaption></figure><p>如圖所示，因為我們還沒開始寫測試，所以它會跟你說找沒 測試檔。它這邊有特別提到，只要路徑檔名包含 test 的，都會被它視為測試檔。</p><h3>建立測試檔</h3><p>因為它會掃描有 test 的資料夾與檔案，所以我建議直接建一個 test 目錄在專案根目錄。然後我們直接在 test 目錄裡建一個 add.spec.js 檔案，內容如下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*07zM2dQRtHWHRL20nq5Qmg.png" /><figcaption>test/add.spec.js</figcaption></figure><p>檔名叫 add.spec.js 只是方便讓我知道這隻檔案在幫我測試 add 這個方法，其實要叫什麼，也沒有硬性規定。然後你就可以下 yarn test 來看一下結果：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/1*LiaNTWiPgqgHH_Ua3uR8kQ.png" /><figcaption>hello test, 0 passing</figcaption></figure><p>我們有看到它執行了 console.log 而且 0 passing， 0 passing代表沒有任何測試通過，因為我們還沒開始寫測試，所以也沒有什麼好通不通過的。</p><h3>安裝斷言式 chai</h3><p>mocha 只是只是個測試框架，它可以幫我們建立測試環境，但測試的判定工具，我們之後就簡稱斷言式。</p><pre>yarn add chai -D</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/768/1*kV4PX4cLsnSyZ1cUBHVbXw.png" /><figcaption>安裝斷言式 chai</figcaption></figure><p>然後回到 add.spec.js 我們試著寫一段測試看看。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eD_9oOeEdcqJrxIs6bmYBQ.png" /><figcaption>寫一段 expect</figcaption></figure><p>第一行我們載入的斷言式expect，等下會由它來判定。<br>然後 describe 就寫你想測試什麼東西，等下我會把它改成 測試 add。<br>it 適合寫測試實例，這邊可以寫你拿什麼去測試。以上例來說，我預期了 1+2 可以拿到 3 。接著去 yarn test 一下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IxgmnCxk5btoSriHLneVMA.png" /><figcaption>1+2預期拿到3</figcaption></figure><p>我們可以看到測試有 1 passing 一項通過，很好，也就是 1+2 真的就是 3。接著我們故意寫一段錯誤的預期。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CQCsxWRxy35agOJ6twjauQ.png" /></figure><p>然後 yarn test</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A2oBkEu37W1mA9eWmtYKRw.png" /><figcaption>預期 8 卻拿到 7</figcaption></figure><p>很好，這次就 1 passing 1 failing。剛剛故意寫的錯也報了： 預期拿到8但實際卻拿到 7。它特別標綠色就是 8 是我們的預期。而紅色是實際拿到的結果。由此可見，這次的結果我們有兩種可能：</p><ol><li>參數給錯：把 3 + 4 改掉，改成一個可以給出 8 的運算</li><li>結果給錯：把 8 改成 7</li></ol><p>這邊我們就選 2，所以把 8 改成 7 再 yarn test 試試</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/892/1*R9AYagiZRSKYLgstUQIveg.png" /><figcaption>完美的修正了</figcaption></figure><p>這就是傳說中的全綠燈，恭禧測試都通過了。</p><h3>開始測試 add方法</h3><p>目前為止，我們只示範 mocha 怎麼建，except 怎麼用，現在我們才要進入主題：測試你寫的程式！ 試著把測試檔改成：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Tpy3_J1CDrHLxvedBZBMHQ.png" /><figcaption>測試add方法</figcaption></figure><p>把剛剛的測試用 add 方法帶入參數，然後看，預期不變， yarn test</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GNk2pFP1IDxiTTAw8pb4fw.png" /><figcaption>都是預期的結果</figcaption></figure><p>然後我們再稍微改一下斷言式：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4FDTIGSNhEkwAzn_WwOirg.png" /><figcaption>為預期做各式斷言</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7JCDVnEWHZ5kua_dF26w-A.png" /><figcaption>跟之前一樣</figcaption></figure><h3>很好你已經會了初步的測試!! 但…?</h3><h3>我本來就知道結果啊? 為什麼這就叫測試呢？</h3><p>測試不是應該測試我可能不知道的結果嗎？這樣真的會測到所有可能嗎？</p><h3>換做法，先寫測試，再寫程式</h3><p>這次讓我們換個做法，我們先寫測試，再寫程式。這次我們先想一個難一點點的程式好了： x加1 再乘 2 然後減3 再除4。一樣，打開編輯器，寫下我們預先想好的結果：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3Wef1mf1niqOZQIFP12DlA.png" /><figcaption>測試 method1</figcaption></figure><p>我們把剛剛的方法先定義另一個檔 method1.js，然後在測試裡先寫好對它的測試，然後 yarn test</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DKOV3-bzXHrNYUO7fNtLWw.png" /><figcaption>method1 不是一個方法</figcaption></figure><p>一定會錯，因為我們 method1.js 裡面什麼也沒寫，回到 method1.js ，稍微寫一下：</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZtNtNmLxVItusHLVpYOxVg.png" /><figcaption>故意寫錯</figcaption></figure><p>然後 yarn test</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*g1mDPwG0DW93QZ8VQN0_aQ.png" /><figcaption>拿到了一個非預期的結果</figcaption></figure><p>綠色是我們預期的結果，紅色是我們實際拿到的結果。很明顯，一定是我們的程式有錯，所以才會冒出這些東西。再讓我們回到 method1 把除4加上去</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WoR-w0J3E1pANMd4JbVkRA.png" /><figcaption>method1 內容</figcaption></figure><p>然後再 yarn test 試試看</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*X2ZXGUmaDLp-R_0gCYsWDQ.png" /><figcaption>剛剛的紅字都變成了綠字</figcaption></figure><p>剛剛的紅字變成了綠字。這代表著：我們剛剛之後寫的程式是正確的。是的，寫到這邊相信有感受到一些好處：</p><ol><li>測試檔可以先寫好一堆斷言式，即便要被測的程式根本還沒寫。</li><li>主程式寫好直接看所有的斷言式是否為綠燈。</li><li>主程式未來有修改，測試應該也要過，如果不過，代表主程式可能寫錯。但如果是主程式需求改了，那要改的反而是測試檔。</li></ol><h3>可是…我的程式好像很難測試?</h3><p>有些情況，我們會發現自己的程式好像不用給「參數」也沒有「回傳」東西，只是單純的執行一些程序，這時你就會覺得什麼東西也測不出來。這其實代表著程式無法測試，此時應修改它。</p><h3>不能測試的程式不是好程式，回傳結果無法被預期</h3><h3>學習回顧</h3><p>看完本篇文，你應該會：</p><ol><li>安裝 mocha 與 chai</li><li>知道 mocha 只是測試框架，判定測試要使用斷言式 chai</li><li>expect 可以放主程式，它預期要 equal 一個結果。</li><li>先寫測試再寫程式，可以先從需求面得知結果，之後寫主程式也有綠字打勾作為通關依據。</li></ol><h3>停一下，你可以想更多</h3><p>測試不是完美的，我們也可能無法列出所有的可能。如果這程式有10種可能，但我們只寫了一個斷言式，到時還是有可能會被測出其他9種錯誤。所以盡可能地，幫我們的程式想多種可能的結果。</p><p>當我們站在公司頂樓往下看時，更需要停一下，是否我們過去做了很多不是別人預期的樣子，也許我們過去也在測試別人的耐心，亮了別人的紅燈。調整自己的心態，重構自己看事的角度，試著賞自己一個綠燈吧！</p><h4>如果這文章有幫助到您，希望您也可以幫我按個讚！謝謝您</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fbutton.like.co%2Fin%2Fembed%2Fsexyoung%2Fbutton%2F&amp;display_name=LikeCoin&amp;url=https%3A%2F%2Fbutton.like.co%2Fsexyoung&amp;image=https%3A%2F%2Fstatic.like.co%2Flikecoin_de-portrait.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=like" width="485" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/0294a31a86f6600fa844a512be84e938/href">https://medium.com/media/0294a31a86f6600fa844a512be84e938/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c32c29df875d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E5%BE%9E%E9%9B%B6%E9%96%8B%E5%A7%8B-%E6%B8%AC%E8%A9%A6%E5%90%A7-%E5%89%8D%E7%AB%AF-c32c29df875d">從零開始: 測試吧! 前端</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[比 NPM 更快的套件管理工具 — Yarn]]></title>
            <link>https://medium.com/html-test/%E6%AF%94-npm-%E6%9B%B4%E5%BF%AB%E7%9A%84%E5%A5%97%E4%BB%B6%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7-yarn-7f274b823ec6?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/7f274b823ec6</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[yarn]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[package]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Mon, 04 Sep 2017 06:18:49 GMT</pubDate>
            <atom:updated>2017-09-04T06:18:54.347Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*m6zlwvyKm9BPeFQCKvGQEQ.png" /><figcaption>Yarn 與它的貓咪 Logo</figcaption></figure><p>今天要介紹一個比 NPM 更快的工具 — Yarn，如果你沒有使用過 NPM，沒聽過它，建議你可以先到另一個文章了解它，認識它：</p><p><a href="https://medium.com/html-test/%E5%BE%9E%E9%9B%B6%E9%96%8B%E5%A7%8B-%E4%BD%BF%E7%94%A8npm%E5%A5%97%E4%BB%B6-317beefdf182">從零開始: 使用NPM套件</a></p><p>yarn 是大名鼎鼎 Facebook 出產的工具之一，它的出現是為了 解決 npm 下載套件很慢的垢病。如果套件一多，npm install 絕對有感。</p><h3>安裝 yarn</h3><p>安裝 yarn 有很多方式，安裝方法清單可到 <a href="https://yarnpkg.com/zh-Hans/docs/install">yarn</a> 官網查看，這邊我們僅使用 npm 來進行安裝。首先不管在何處，下這個指令：</p><pre><strong>npm install -g</strong> yarn</pre><p>這邊的 -g 指的就是 global，也就是安裝某套件之後，某套件可在任一處直接使用，不只限於專案目錄下使用。</p><p>然後一樣我們下個 <strong>yarn --version</strong> 來查看 yarn 是否灌好了</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/696/1*kGXt0jqK-usiIYuzE82CGg.png" /></figure><h3>建立專案並安裝套件</h3><p>我們一樣先建立一個 demo-project 的資料夾。然後使用 <strong>npm init</strong> 來取得 package.json</p><p>此例以安裝 jquery, bootstrap, react, react-dom, babel-core 做為範例。</p><h3>用 NPM 安裝</h3><pre><strong>npm install</strong> jquery bootstrap react react-dom babel-core <strong>--save</strong></pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F6HpfInO6lWwfu%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fnpm-install-6HpfInO6lWwfu&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F6HpfInO6lWwfu%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/72ac08a4f4187a2b26adfe9734282bab/href">https://medium.com/media/72ac08a4f4187a2b26adfe9734282bab/href</a></iframe><p>差不多要 20 秒才能安裝完畢，如果更多套件那就不用說了，半小時都是有可能的。</p><h3>用Yarn安裝</h3><p>這邊要注意我們使用的是 <strong>yarn add</strong> 它等於使用 <strong>npm install --save</strong> 只是指令更少而已。</p><pre><strong>yarn add</strong> jquery bootstrap react react-dom babel-core</pre><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FCVSpjELvWMS8U%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fyarn-install-CVSpjELvWMS8U&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FCVSpjELvWMS8U%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="208" frameborder="0" scrolling="no"><a href="https://medium.com/media/eae3fbc4bb87b687f199884ea6ee9558/href">https://medium.com/media/eae3fbc4bb87b687f199884ea6ee9558/href</a></iframe><p>大約花費4秒左右，而且更有趣的是，如果你之前有安裝過某套件，但之前把它刪除，再安裝一次的話，速度會更快。原因是當我們安裝某套件後， yarn 會幫我們建立快取，以便你下次安裝套件時節省時間。</p><h3>學習回顧</h3><p>yarn 的指令大多與 npm 類似，這邊我們做個整理：</p><ul><li>yarn 與 npm install 作用相同。</li><li>yarn add [package] 與 npm install [package] --save 作用相同。</li><li>yarn remove [package] 與 npm uninstall [package] --save 作用相同。</li></ul><p>差不多只要會這些指令，即可享受yarn優雅敏捷。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f274b823ec6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E6%AF%94-npm-%E6%9B%B4%E5%BF%AB%E7%9A%84%E5%A5%97%E4%BB%B6%E7%AE%A1%E7%90%86%E5%B7%A5%E5%85%B7-yarn-7f274b823ec6">比 NPM 更快的套件管理工具 — Yarn</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[從零開始: 使用NPM套件]]></title>
            <link>https://medium.com/html-test/%E5%BE%9E%E9%9B%B6%E9%96%8B%E5%A7%8B-%E4%BD%BF%E7%94%A8npm%E5%A5%97%E4%BB%B6-317beefdf182?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/317beefdf182</guid>
            <category><![CDATA[package]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[development]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Sun, 03 Sep 2017 16:40:17 GMT</pubDate>
            <atom:updated>2020-04-24T17:15:59.679Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AagfZZ8bGOI4iTbLqewg6Q.png" /><figcaption>node package manager</figcaption></figure><p>如果一個有在寫網站的人，應該百分之兩百知道 NPM 是什麼東西。好吧如果你不知道我還是解釋一下（喂）</p><blockquote><strong>NPM 是 Node Package Manager 的簡稱</strong>，它是一個線上套件庫，可以下載各式各樣的 Javascript 套件來使用。</blockquote><p>以前我<strong>還在龜</strong>的時候，比方說要使用 jQuery，我就會去它們官網下載 jQuery, 然後把該套件到專案目錄裡的某個叫lib資料夾，然後再到HTML將它引用進來。</p><p>這步驟其實不難，但套件一多要反複的做這些工很麻煩。所以如果像我一樣夠懶的人，就會想找其他方法代替。</p><h3>安裝 NVM (node version manager)</h3><p>說穿了它還是服務給 node 用的工具，所以我們可以利用 NVM (Node Version Manager) 來安裝 node 跟 npm。但在此之前，你要先安裝 NVM。打開你的 terminal 然後輸入：</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dff463c1d34392b2117dd080fb666493/href">https://medium.com/media/dff463c1d34392b2117dd080fb666493/href</a></iframe><p>當然如果你不習慣指令式的安裝，也可以到 Node 官網下載安裝包。</p><p><a href="https://nodejs.org/en/">Node.js - Run JavaScript Everywhere</a></p><p>總之裝好之後，你還是要<strong>重新打開 Terminal </strong>輸入：</p><pre>npm -v</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dAsVS0bu5r30xd5ZMlwTpQ.png" /><figcaption>npm -v 來顯示 npm 版本</figcaption></figure><p>如果有看到 npm 的版號，那就灌成功了，恭禧你現在可以使用 npm 了！</p><h3>建立一個新專案</h3><p>建一個名為 `demo-project` 的資料夾，並進去。</p><pre><strong>mkdir</strong> demo-project &amp;&amp; <strong>cd</strong> demo-project</pre><p>然後輸入 <strong>npm install</strong> jquery ，來完成安裝 jquery。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Al_wU9O3BACMd9Lviqb7zQ.png" /><figcaption>用 npm 安裝 jquery</figcaption></figure><p>如你所見，它跳了幾個 WARN ，主要是因為專案資料夾裡沒有 package.json 這隻檔案。 package.json 是掌管 <strong>專案資訊</strong>的重要檔案 。所以我們重新來過。一樣在專案目錄下輸入：</p><pre><strong>npm init</strong></pre><p>接著它會跳出一堆需要輸入的資訊，如果沒什麼特別的需求，就一直 Enter 下去就好。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FQPQsm0_7y1GwMMpSa58tA.png" /><figcaption>npm init 初始專案資訊</figcaption></figure><ul><li>name: 就是該專案的名字，它預設就是該目錄名。</li><li>description: 專案描述。</li><li>entry point: 專案切入點，這有點複雜，之後再說。</li><li>test command: 專案測試指令，之後說。</li><li>git repository: 專案原始碼的版本控管位置。</li><li>keywoard: 專案關鍵字</li><li>author: 專案作者，以 author-name &lt;author@email.com&gt; 寫之。</li><li>license: 專案版權。</li></ul><p>最後它秀一個預覽的樣子，如果沒問題就 Enter 來產生 package.json。到編輯器裡打開它，它就是一個 json 格式的檔案。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3BQm_pm1RAjpn2YPuDwfbQ.png" /><figcaption>剛建立好的 package.json</figcaption></figure><h3>安裝套件</h3><p>剛剛試過的，安裝 jquery ，再試一次看看</p><pre><strong>npm install</strong> jquery</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LxW-GayOFCgwT6eTwP6Wrg.png" /><figcaption>安裝 jquery 出現警告</figcaption></figure><p>看起來貌似裝好了，但怎麼又 WARN 2行東西? 原來是我們的 package.json 裡的 description 跟 repository 沒有給內容，所以我們再到 package.json 去編輯一下：</p><blockquote>repository 裡的 url 要先去 github 建立一個空白的專案哦</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZSx0OHapDPaevn5Edo1OrQ.png" /><figcaption>加入 description 與 repository 欄位</figcaption></figure><p>然後再去 Terminal 裝一次，它這次就不會叫了。但我們這次換個指令：</p><pre><strong>npm install</strong> jquery <strong>--save</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*11zhSOO-VsiOx_88lNgoXg.png" /><figcaption>安裝 jquery 帶--save 參數</figcaption></figure><p>--save 參數是安裝套件時，也同步更新 package.json 裡的資訊。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*760K2cTeXemNMC4yZNOKgg.png" /><figcaption>package.json 裡多了 dependencies 欄位</figcaption></figure><p>package.json 多了一個 dependencies 欄位，裡面記錄了我們剛剛安裝的 jquery 。也就是說，未來如果我們想知道專案有使用什麼套件，我們可以從 dependencies 這個欄位知道。</p><h3>套件安裝的位置</h3><p>每個用 npm 安裝的套件，一律放在 node_modules 資料夾裡，以剛剛安裝的 jquery 來說，我們可以在 node_modules/jquery/dist/jquery.min.js 找到 最小化的 jquery 的版本。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oYBNbEEV4T_pRXmQJaYB-g.png" /></figure><h3>建立 index.html 並引用 jquery</h3><p>既然都知道位置了，就直接寫一段 jquery 來看看。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TwsRWh0KkU6b62eWaPkSfg.png" /><figcaption>引入 jquery 並寫一段程式</figcaption></figure><p>打開 index.html 看看！</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*z6qrgkHA84Vj-_Ix2F77RA.png" /></figure><p>果然順利執行！</p><h3>建立 .gitignore</h3><p>在 push repository 之前，我們一件事要注意，就是要加入 .gitignore ，它會幫我們擋住一些 不必要push的檔案 ，像 node_modules 裡的所有套件，網路上 全部都下載得到 所以也就沒必要推到我們的 repository。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T0B-RR6JQF75ObXnl-6Cxg.png" /><figcaption>用 .gitignore 擋住不要上傳的檔案或資料夾</figcaption></figure><p>如果你的專案已經有使用 版本控管 ，那麼被 .gitignore 掉的東西就會變灰色，或直接看不到。(atom editor)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cg8UKvTIEKHuYxKaLS9ZxQ.png" /><figcaption>用 .gitignore 擋住的檔案或資料夾會不見</figcaption></figure><p>這樣一來， node_modules 就不會被推進我們的 repository 。到 github 上看也是一樣的。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sOKfQrx6V5vxVPNAfcghWw.png" /><figcaption>github 上沒有 node_modules</figcaption></figure><h3>根據 packge.json 下載套件</h3><p>如果今天是另一個朋友，或是當我們在公司電腦推上去之後，想回家到另一台電腦使用這個專案，那我們要先用 git clone 把該專案下載下來。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wcMvxV7t-bK2evIOY0aedg.png" /></figure><p>打開該資料夾會發現真的沒有 node_modules 這個資料夾。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LN74GEYwUc4Us3u8ueHuNw.png" /></figure><p>但別忘了，我們有 package.json</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b_-OHCOR2Wc2iFSI7pOkTQ.png" /><figcaption>注意 dependencies 欄位裡有我們剛剛灌的 jquery</figcaption></figure><p>此時只要在 Terminal 使用</p><pre><strong>npm install</strong></pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1004/1*H-fYrTHgC3T60gqxDHyoZA.png" /><figcaption>使用 npm install 把套件安裝回來</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*n_3bFaxXmEqXomkAsX64xw.png" /><figcaption>可愛的 node_modules 跟剛剛的 jquery 都肥來囉</figcaption></figure><p>你的 node_modules 與它的小夥伴 jquery 就都回來囉~</p><h3>移除套件</h3><p>很簡單，就下個</p><p><strong>npm uninstall</strong> jquery 就可以把 jquery 移除囉。<strong>但要注意，這樣並不會把 </strong><strong>package.json 更新!!!!</strong></p><p><strong>npm uninstall</strong> jquery <strong>--save</strong> 這樣才能也把 package.json 中的 jquery 資訊移除~</p><h3>學習回顧</h3><p>看完本章，你應該會使用：</p><ul><li>npm -v 或 npm --version → 查看 npm 版本</li><li>npm i [package] 或 npm install [package] → 安裝套件在專案下的 node_modules 目錄</li><li>npm i [package] --save 或 npm install [package] --save → 安裝套件在專案下的 node_modules 目錄，並更新該套件資訊到 package.json</li><li>npm un [package] 或 npm uninstall [package] → 移除某套件</li><li>npm un [package] --save或 npm uninstall [package] --save → 移除某套件，並更新該套件資訊到 package.json</li></ul><h3>下回來寫個 怎麼建立自己的 NPM 套件</h3><h4>如果這文章有幫助到您，希望您也可以幫我按個讚！謝謝您</h4><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fbutton.like.co%2Fin%2Fembed%2Fsexyoung%2Fbutton%2F&amp;display_name=LikeCoin&amp;url=https%3A%2F%2Fbutton.like.co%2Fsexyoung&amp;image=https%3A%2F%2Fstatic.like.co%2Flikecoin_de-portrait.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=like" width="485" height="212" frameborder="0" scrolling="no"><a href="https://medium.com/media/0294a31a86f6600fa844a512be84e938/href">https://medium.com/media/0294a31a86f6600fa844a512be84e938/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=317beefdf182" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E5%BE%9E%E9%9B%B6%E9%96%8B%E5%A7%8B-%E4%BD%BF%E7%94%A8npm%E5%A5%97%E4%BB%B6-317beefdf182">從零開始: 使用NPM套件</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[一行JS都不用寫的折合Panel]]></title>
            <link>https://medium.com/html-test/%E4%B8%80%E8%A1%8Cjs%E9%83%BD%E4%B8%8D%E7%94%A8%E5%AF%AB%E7%9A%84%E6%8A%98%E5%90%88panel-95a9fa924b08?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/95a9fa924b08</guid>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Tue, 16 May 2017 14:56:01 GMT</pubDate>
            <atom:updated>2017-05-16T14:56:01.213Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?url=https%3A%2F%2Fcodepen.io%2Fsexyoung%2Fpen%2FdWjzOY&amp;src=https%3A%2F%2Fcodepen.io%2Fsexyoung%2Fembed%2Fpreview%2FdWjzOY%3Fheight%3D600%26slug-hash%3DdWjzOY%26default-tabs%3Dhtml%2Cresult%26host%3Dhttp%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;type=text%2Fhtml&amp;key=d04bfffea46d4aeda930ec88cc64b87c&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5ced01b3b06282715a128d536fd4966/href">https://medium.com/media/a5ced01b3b06282715a128d536fd4966/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=95a9fa924b08" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E4%B8%80%E8%A1%8Cjs%E9%83%BD%E4%B8%8D%E7%94%A8%E5%AF%AB%E7%9A%84%E6%8A%98%E5%90%88panel-95a9fa924b08">一行JS都不用寫的折合Panel</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[【教學】學習 Redux]]></title>
            <link>https://medium.com/html-test/%E6%95%99%E5%AD%B8-%E5%AD%B8%E7%BF%92-redux-d0aced876abc?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/d0aced876abc</guid>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Mon, 05 Dec 2016 08:37:50 GMT</pubDate>
            <atom:updated>2016-12-05T08:37:49.694Z</atom:updated>
            <content:encoded><![CDATA[<p>我就不多說了，自己看唄</p><p><a href="https://learnredux.com/">Learn Redux</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0aced876abc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E6%95%99%E5%AD%B8-%E5%AD%B8%E7%BF%92-redux-d0aced876abc">【教學】學習 Redux</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[【css】讓溢出的文字變點點點]]></title>
            <link>https://medium.com/html-test/css-%E8%AE%93%E6%BA%A2%E5%87%BA%E7%9A%84%E6%96%87%E5%AD%97%E8%AE%8A%E9%BB%9E%E9%BB%9E%E9%BB%9E-3bbf61d10082?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/3bbf61d10082</guid>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Tue, 15 Nov 2016 06:34:28 GMT</pubDate>
            <atom:updated>2016-11-15T06:34:27.564Z</atom:updated>
            <content:encoded><![CDATA[<p>如果你知道 overflow的話，你可以選擇讓超出的文字用 點點點「…」來呈現，只要寫這樣就行了。</p><pre>text-overflow: ellipsis</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3bbf61d10082" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/css-%E8%AE%93%E6%BA%A2%E5%87%BA%E7%9A%84%E6%96%87%E5%AD%97%E8%AE%8A%E9%BB%9E%E9%BB%9E%E9%BB%9E-3bbf61d10082">【css】讓溢出的文字變點點點</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[ESLint 說明]]></title>
            <link>https://medium.com/html-test/eslint-%E8%AA%AA%E6%98%8E-7b4abad8d208?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/7b4abad8d208</guid>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Fri, 23 Sep 2016 03:40:04 GMT</pubDate>
            <atom:updated>2016-09-23T03:40:03.732Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e9625c5330e6bbb3ffdfdb9c0a8ec22/href">https://medium.com/media/1e9625c5330e6bbb3ffdfdb9c0a8ec22/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b4abad8d208" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/eslint-%E8%AA%AA%E6%98%8E-7b4abad8d208">ESLint 說明</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[從無到有建立 webpack 設定檔（三）進階設定]]></title>
            <link>https://medium.com/html-test/%E5%BE%9E%E7%84%A1%E5%88%B0%E6%9C%89%E5%BB%BA%E7%AB%8B-webpack-%E8%A8%AD%E5%AE%9A%E6%AA%94-%E4%B8%89-%E9%80%B2%E9%9A%8E%E8%A8%AD%E5%AE%9A-d3d7583e61cf?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/d3d7583e61cf</guid>
            <category><![CDATA[webpack]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Mon, 04 Jul 2016 07:52:05 GMT</pubDate>
            <atom:updated>2016-07-04T08:18:31.653Z</atom:updated>
            <content:encoded><![CDATA[<p>老話一句，如果你沒看過上一篇的話，請先看完上一篇再來：</p><p><a href="https://medium.com/html-test/%E5%BE%9E%E7%84%A1%E5%88%B0%E6%9C%89%E5%BB%BA%E7%AB%8B-webpack-%E8%A8%AD%E5%AE%9A%E6%AA%94-%E4%BA%8C-%E8%A8%AD%E5%AE%9A%E6%A8%A3%E5%BC%8F-61c210d63411">從無到有建立 webpack 設定檔（二）設定樣式</a></p><p>上一篇我們已經為 <strong>開發階段 </strong>與 <strong>產品階段</strong> 設定了不同的 webpack.config，如果你有仔細實作的話，你會發現它們有一些共同處，所以我們可以把這些共同處獨立出來！</p><h4>建立 webpack.base.config.js</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/679/1*CFP6q3IXWw_QGs3t_AsKqg.png" /></figure><p>把檔案共同處獨立出 webpack.base.config.js 之後，接下來就可以簡化 原本的 hot 與 prod 檔案：</p><h4>簡化 hot 檔</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/686/1*bByZ9W_8u9bgXX-An7ujXw.png" /></figure><p>如圖所示把 base 檔讀進來變成一個 object 然後再對 config 做 push 內容，最後把再 config exports 出去~ (記得這些邊的設定會跟 prod 不同）</p><h4>簡化 prod 檔</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/681/1*R4tcGg9nH1DflfQN2rjOVw.png" /></figure><p>prod 檔一樣要載入 base 檔，要特別注意下面的 ExtractText.extract 這邊 css?modules 一定要寫！（這樣打包時才會解譯css到你的 DOM裡）</p><h4>重新測試你的 hot 與 prod</h4><pre>npm run start #開發用</pre><pre>npm run prod #打包上線用</pre><p>如果是打包的話，它會產生一堆檔案在 static 資料夾</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*xYcbBPN8GHBybwkoLLJMJw.png" /></figure><h3>然後這些檔再把它們引入你的 頁面就可以囉~</h3><h3>最後附上檔案</h3><p><a href="https://github.com/sexyoung/webpack-junior-guide">sexyoung/webpack-junior-guide</a></p><p>文章引用：<br><a href="http://alexfedoseev.com/post/66/webpack-declarative-way">http://alexfedoseev.com/post/66/webpack-declarative-way</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3d7583e61cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E5%BE%9E%E7%84%A1%E5%88%B0%E6%9C%89%E5%BB%BA%E7%AB%8B-webpack-%E8%A8%AD%E5%AE%9A%E6%AA%94-%E4%B8%89-%E9%80%B2%E9%9A%8E%E8%A8%AD%E5%AE%9A-d3d7583e61cf">從無到有建立 webpack 設定檔（三）進階設定</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[從無到有建立 webpack 設定檔（二）設定樣式]]></title>
            <link>https://medium.com/html-test/%E5%BE%9E%E7%84%A1%E5%88%B0%E6%9C%89%E5%BB%BA%E7%AB%8B-webpack-%E8%A8%AD%E5%AE%9A%E6%AA%94-%E4%BA%8C-%E8%A8%AD%E5%AE%9A%E6%A8%A3%E5%BC%8F-61c210d63411?source=rss----7e8ed50b6485---4</link>
            <guid isPermaLink="false">https://medium.com/p/61c210d63411</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[webpack]]></category>
            <dc:creator><![CDATA[sexyoung]]></dc:creator>
            <pubDate>Thu, 16 Jun 2016 17:45:39 GMT</pubDate>
            <atom:updated>2016-06-16T17:45:39.269Z</atom:updated>
            <content:encoded><![CDATA[<p>如果你還沒看過第一篇，請到這邊</p><p><a href="https://medium.com/html-test/%E5%BE%9E%E7%84%A1%E5%88%B0%E6%9C%89%E5%BB%BA%E7%AB%8B-webpack-%E8%A8%AD%E5%AE%9A%E6%AA%94-%E4%B8%80-42fbc76a2d37">從無到有建立 webpack 設定檔（一）</a></p><p>這篇主要以scss當作樣式檔做開發，跟樣式相關的像是字型檔、圖片檔也會一拼在此篇一起說明。</p><h4>安裝樣式loaders</h4><pre>npm i --save-dev style-loader css-loader sass-loader autoprefixer-loader file-loader url-loader</pre><p>style-loader、css-loader、sass-loader這一系列的loader，就是讓我們可以讀取 css, sass 用的。而 autoprefixer 則是自動產生各種瀏覽器專用的語法，就像這樣：</p><pre>border-radius: 3px; /* 寫的時候寫這樣 */</pre><pre>/* 它會幫我們自動轉成這樣 */<br>-webkit-border-radius: 3px;<br>-moz-border-radius: 3px;<br>border-radius: 3px;</pre><p>而 file-loader 則是用來讀取字型檔、圖片檔用的。最後 url-loader 可以幫我們偵測如果 woff(2) 這些來源如果在多少大小以內，就幫轉成base64（減少 request）</p><pre>特別說明：如果安裝 sass-loader 不成功，你可能需要先安裝 node-sass<br>npm i --save-dev node-sass</pre><h4>修改 webpack.hot.config.js</h4><p>就在 module 裡面多加幾個 loader</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*T4v4WaME90pqbuSe1PlOQw.png" /><figcaption>注意，我們只有新增 loaders</figcaption></figure><h4>新增 styles</h4><p>新增 /components/app.scss 並修改 /components/app.js</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BLfmj1aKwvEvmTdHhw4ZAg.png" /></figure><p>接著重新啟動你的 webpack，你可以看到瀏覽器</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BvEWHtUfDv3mnRU-OxN98g.png" /><figcaption>已經被我們變黃色的 app</figcaption></figure><p>以此類推，其他的Components：Header, Content, Footer 也都使用這樣的方式就可以設定樣式了~</p><h4>咦？好像怪怪的？</h4><p>你可能已經發現，你的 index.html 並沒有戴入樣式檔，為什麼可以顯示呢？因為在開發狀態中，webpack 會把你的樣式動態的產生出來（用javascript產的）所以並不會產生新的檔案，而是直接append在 index.html 的 head 裡哦。</p><h4>那上線的話，還是這樣嗎？</h4><p>當然不是，上線的話我們就要把scss轉成css，react檔轉成js，所以我們要使用另一個上線專用的 webpack 設定檔：</p><h4>安裝 extract-text-webpack-plugin</h4><pre>npm i --save-dev extract-text-webpack-plugin</pre><h4>建立 webpack.prod.config.js</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_5FlthV1WaiWFZPar5XFKg.png" /></figure><p>這裡要注意的是 vendor 部分，因為我們的 app, Header, Content, Footer 都有 import React, 但打包的時候總不能每一個 component 都把 React 包進來，這樣就會包4次 React, 所以我們多定義一個進入點：vendor，內容為 <strong>[‘react’, ‘react-dom’]</strong> ，並在使用 CommonsChunkPlugin 幫我們把 React, React-DOM 抽出來。還有 test: /\.scss$/ 那邊，也使用了 ExtractText.extract 來幫我們把sass 抓出來轉成 css。</p><h4>新增 npm run prod 指令</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JAoQ0UzaDojgBSTyzQIBjQ.png" /></figure><p>所以我們之後只要下：</p><pre>npm run prod</pre><p>它就會幫我們產出這些相關檔案了~</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kNyZZnOs0Chz_9pAixMSfw.png" /><figcaption>app-xxx.js 是主程式打包檔, vendor-xxx.js 是套件打包檔</figcaption></figure><h3>下一章我們講進一步的 webpack 設定~</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61c210d63411" width="1" height="1" alt=""><hr><p><a href="https://medium.com/html-test/%E5%BE%9E%E7%84%A1%E5%88%B0%E6%9C%89%E5%BB%BA%E7%AB%8B-webpack-%E8%A8%AD%E5%AE%9A%E6%AA%94-%E4%BA%8C-%E8%A8%AD%E5%AE%9A%E6%A8%A3%E5%BC%8F-61c210d63411">從無到有建立 webpack 設定檔（二）設定樣式</a> was originally published in <a href="https://medium.com/html-test">進擊的 Front End‘s</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>