<?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>gulp &#8211; Larry的午茶時光</title>
	<atom:link href="https://blog.yuyansoftware.com.tw/tag/gulp/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.yuyansoftware.com.tw</link>
	<description></description>
	<lastBuildDate>Mon, 14 Apr 2025 04:14:10 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://blog.yuyansoftware.com.tw/wp-content/uploads/2022/10/favicon-45x45.png</url>
	<title>gulp &#8211; Larry的午茶時光</title>
	<link>https://blog.yuyansoftware.com.tw</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>JavaScript ES6 的模組化功能：import, export。建置前端時 require 與 import 有何不同？</title>
		<link>https://blog.yuyansoftware.com.tw/2023/09/javascript-export-import-require/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Sun, 17 Sep 2023 03:08:41 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[gulp]]></category>
		<guid isPermaLink="false">https://blog.yuyansoftware.com.tw/?p=16865</guid>

					<description><![CDATA[這篇文章我們來聊一個不新，但重要的議題：JavaScript 的模組化。2015年，JavaScript ECMAScript 2015 發布，也稱為ES6，從此開始，JavaScript 導入了模組化的概念以及語法。]]></description>
										<content:encoded><![CDATA[
<p>這篇文章我們來聊一個不新，但重要的議題：JavaScript 的模組化。</p>



<p>其實 JavaScript 早期只是一個腳本式語言，做做 HTML DOM 的簡單變化而已。也許直接寫在 HTML 裡，也許引用幾支 JS 檔，這樣而已。</p>



<p>2010 年 AngularJS 問世，開啟了 201x 年前端工程的飛速成長，包含各種前端框架的興起，與 Single Page Application (單頁式網站)。AngularJS 後來變成了 Angular，加上 React、Vue.js，這三大家前端框架維持了好一段時間。</p>



<p>延伸閱讀，後來 Laravel 預設的前端框架從 Vue.js 改為使用 Alpine.js (<a href="https://blog.yuyansoftware.com.tw/2021/05/alpine-js-frontend-framework/">我的文章</a>)</p>



<p>2015年，JavaScript <strong>ECMAScript</strong> 2015 發布，也稱為 <strong>ES6</strong>。為什麼是 6 這個數字？是 JavaScript 這個語言的第 6 版。也就是從 ES6 開始，JavaScript 導入了模組化的概念以及語法。</p>



<h2 class="wp-block-heading">ES6 的 import、export</h2>



<p>基本上一個 JS 檔案就是一個模組，用 export 語法，將你想要輸出的變數、函式、物件、類別，都可以輸出。例如</p>



<pre class="wp-block-code"><code>export const your_var = 'test';

export function your_func() {
    // …
}</code></pre>



<p>假設上面檔案叫 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">your_module.js</mark></code>。你可以在 HTML 裡直接輸入 JS 模組，注意一定要寫 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">type="module"</mark></code>，且 import 的後方是大括號</p>



<pre class="wp-block-code"><code>&lt;script type="module"&gt;
    import { your_var, your_func, your_object, your_class } from './your_module.js';

    // 就可以使用 your_var, your_func …
&lt;/script&gt;</code></pre>



<p>或是在另一支 JS (假設名稱是 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">abc.js</mark></code>) 裡面 import，HTML 直接引用 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">abc.js</mark></code>，注意一樣要寫 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">type="module"</mark></code>。也就是 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">your_module.js</mark></code> 的變數輸入到 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">abc.js</mark></code>，並在 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">abc.js</mark></code> 執行</p>



<pre class="wp-block-code"><code>&lt;script type="module" src="./abc.js"&gt;&lt;/script&gt;</code></pre>



<h3 class="wp-block-heading">輸出或輸入都可以改變數名稱</h3>



<p>在輸出時可以改變數名稱</p>



<pre class="wp-block-code"><code>export { your_var as new_name, your_function as new_func };</code></pre>



<p>當然，輸入時就要用新名稱輸入。</p>



<p>輸入時也可以改名</p>



<pre class="wp-block-code"><code>import { your_var as new_name } from "./your_module.js";</code></pre>



<p>這樣在輸入模組就是用新名稱。</p>



<p>還有一種寫法也滿常見的，把整個模組輸入</p>



<pre class="wp-block-code"><code>import * as your_module from './your_module.js';

your_module.func_a();  // 在 your_module.js, func_a 要 named export</code></pre>



<h3 class="wp-block-heading">named 與 default export / import</h3>



<p>以上的 export / import 範例是 <strong>named</strong> export / import (具名輸出/輸入)，還有一種是 <strong>default</strong> export / import。例如</p>



<pre class="wp-block-code"><code>// 輸出
export default your_var;
// 輸入
import new_name from './your_module.js';</code></pre>



<p><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">default export<strong> </strong>最重要的就是在輸出模組「只能出現一次」。</mark>在輸入模組則是任意取名去接。</p>



<p>這樣就是一個最簡單的 export / import 架構了。完整的 export / import 寫法還有很多變形，可以參考 MDN 的 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/export" target="_blank" rel="noreferrer noopener nofollow">export 語法文件</a>、<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import" target="_blank" rel="noreferrer noopener nofollow">import 語法文件</a></p>



<h2 class="wp-block-heading">CommonJS 的 require</h2>



<p>目前大部分網站，都是在 Node.js 開發環境 build 前端。常常會看到一個 JS 關鍵字 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">require('...')</mark></code></p>



<p>require 來自於 <strong>CommonJS</strong>，始於開發者的獨立專案。後來 Node.js 用 CommonJS 實作，也採用了 require，直至今日。但必須說明，require 並非 JavaScript ES6 的原生規格。</p>



<p><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">require</mark></code> 的參數常常是「不加」檔名，為什麼？例如</p>



<pre class="wp-block-code"><code>require(‘./your_your_module’);</code></pre>



<p>依照 <a href="https://nodejs.org/api/modules.html#modules_file_modules" target="_blank" rel="noreferrer noopener nofollow">Node.js 官方文件</a>，Node.js 會先找 .js、.json、.node。如果你要輸入的不是這3個副檔名，require 參數就要寫檔名全稱 (包含副檔名)。</p>



<p><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">require</mark></code> 參數也可以填入 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code> 中列出的套件名稱，這樣 Node.js 就會知道要輸入的檔案路徑 (通常是在 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">node_modules</mark></code> 資料夾底下)。</p>



<h2 class="wp-block-heading">建置前端時 require 與 import 的不同</h2>



<p>我們以 Laravel 舉例。基本上 Laravel 是用 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">require</mark></code>，因為 Laravel 前端建置基底是 Node.js，用的是 CommonJS module system。當然，Node.js 稍作調整可以支援 ES6 module system，不在這裡討論。</p>



<p>如果你有 <strong>local module</strong> (非 Node.js 核心，非 npm 安裝，開發者自己擺在專案的 js 檔案)，require 跟 import 都可以。</p>



<p>這邊要說明一下，不管是 require 或 import，一般情形只是「執行」該 js 檔案。在建置前端時，是將輸入的模組一起 compile 起來。</p>



<h2 class="wp-block-heading">結語</h2>



<p>本篇回顧了一下 JavaScript ES6 的 import / export，也藉這個機會比較了 Node.js 建置前端時用的 require。</p>



<p>本篇重點並「不在」比較 <strong>CommonJS</strong> 與 <strong>ES6</strong> module system，兩者寫法不同，規格也不同。例如，require 輸入的是整個模組，import 可以輸入一個模組中部分的變數、函式、物件、類別。</p>



<p>import / export 與 require 不是新題目，但卻是現代前端技術很重要的模組化功能。很多前端建置工具是基於 Node.js (例如 Laravel)，require 與 import 也大量被使用，很值得再回顧一下。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Laravel Vite：繼 Laravel Mix 之後新的前端檔案管理工具</title>
		<link>https://blog.yuyansoftware.com.tw/2022/12/laravel-vite/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Sat, 10 Dec 2022 11:53:49 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[gulp]]></category>
		<category><![CDATA[Link7]]></category>
		<category><![CDATA[新功能]]></category>
		<guid isPermaLink="false">https://blog.yuyansoftware.com.tw/?p=13078</guid>

					<description><![CDATA[從 Laravel 9.19 開始 (2022/6/30)，Laravel 前端檔案管理工具從 Mix 改為 Vite。Laravel Mix 的底層是 Webpack，而 Vite 是基於原生 JavaScript ESM 的前端開發工具。Vite 在熱更新時 (HMR) 是先 refresh 瀏覽器，才編譯與該頁面有關的檔案。]]></description>
										<content:encoded><![CDATA[
<p>從 Laravel 5.4 開始 (2017/1/24)，前端檔案管理工具就是 <strong>Laravel Mix</strong>，我當年還有一篇關於 <a rel="noreferrer noopener" href="https://blog.yuyansoftware.com.tw/2017/04/laravel-mix/" target="_blank">Laravel Mix 的文章</a></p>



<p>Laravel 9.19 開始 (2022/6/30)，Laravel 前端檔案管理工具從 Laravel Mix 改為 <strong>Vite</strong>。</p>



<p>每次寫關於軟體工程演進類的文章，都會有感於時間過得真的太快。2017~2022，五年多就這樣過去了。五年的時間，希望大家在人生中都有累積些什麼。</p>



<p>Laravel Mix 的底層是 <strong>Webpack</strong>，在 Laravel 根目錄會有 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code>, <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark></code> 兩支檔案。執行</p>



<pre class="wp-block-code"><code>npm install</code></pre>



<p>會依照 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code> 去安裝套件到你的開發環境。</p>



<p><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code> 中有一個 “scripts” 欄位，定義了很多 npm 指令，並將 npm 指令轉成 mix 指令。mix 指令就會去跑 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark></code> 這支檔案裡的 script，包含 compile css / js。</p>



<p><strong>Laravel Vite</strong> 是基於 <a rel="noreferrer noopener" href="https://vitejs.dev/" target="_blank">Vite</a>，是一個基於原生 JavaScript ESM 的前端開發工具。與 Webpack 機制上不同的是，Webpack 一律依照 script 打包全部檔案素材 (dev mode 也是打包全部)。</p>



<p>而 Vite 在<strong>熱更新</strong>時 (hot module replacement，HMR)，是先 refresh 瀏覽器，檢查現在前端頁面有關的檔案是否有更新，如果有，才編譯與該頁面有關的檔案 (而不是每次都全部打包)。因此在開發階段，尤其是大型專案，會大幅加速開發時間。</p>



<p><strong>Laravel Vite</strong> 一樣有 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code>，一樣要跑 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">npm install</mark></code>。只是根目錄 script 檔案不是 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark></code>，改為用 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">vite.config.js</mark></code>。</p>



<p>Laravel Vite 的 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json </mark></code>中一樣有 “scripts” 欄位：</p>



<pre class="wp-block-code"><code>npm run dev  // 轉成 “vite”
npm run build  // 轉成 "vite build"</code></pre>



<p>vite 指令就會去跑 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">vite.config.js</mark></code> 這支檔案裡的 script，預設是</p>



<pre class="wp-block-code"><code>export default defineConfig({
    plugins: &#91;
        laravel({
            input: &#91;
                'resources/sass/app.scss',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
});</code></pre>



<p>入口的 css / js 一樣是 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">resources/sass/app.scss</mark></code>, <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">resources/js/app.js</mark></code>。注意有一個設定 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">refresh: true</mark></code>，這代表在執行 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">npm run dev</mark></code> 時，如果底下這幾個路徑檔案有更新</p>



<ul class="wp-block-list">
<li>app/View/Components/**</li>



<li>lang/**</li>



<li>resources/lang/**</li>



<li>resources/views/**</li>



<li>routes/**</li>
</ul>



<p>瀏覽器已 load 的頁面會整頁 refresh。以上是預設的監聽路徑，你也可以將自己想監聽的檔案路徑設定給 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">refresh</mark></code> 參數 (以設定 array 代替 true)，這樣 Vite 就會監聽你定義的路徑。</p>



<p>跟之前一樣 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">app.scss</mark></code> 中預設 import 隔壁的 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">_variables.scss</mark></code>。</p>



<p><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">app.js</mark></code> 中預設 import 隔壁的 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">bootstrap.js</mark></code>，在其中 load 你需要的套件，例如：jQuery，Bootstrap，Vue，等。</p>



<p>在前端檔案加一行 (與 <strong>Mix</strong> 不同，<strong>Vite </strong>引用 css / js 在同一行)</p>



<pre class="wp-block-code"><code>@vite(&#91;'resources/sass/app.scss', 'resources/js/app.js'])</code></pre>



<p>就可以將 compile 好的 css / js 引入 (並加 preload 屬性)。如果是 Build Mode (<code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">npm run build</mark></code>)，css / js 檔名會加上版本號 (auto-versioning)。</p>



<p>目前如果先安裝 laravel/ui，build 出來的 css / js 會包含 Bootstrap 5.2.3，是目前 (2022年12月) Bootstrap 的最新版。</p>



<p>Vite 也可以處理前端素材檔案，例如圖檔跟字型檔。在 JS 的入口處 (<code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">app.js</mark></code>) 寫入</p>



<pre class="wp-block-code"><code>import.meta.glob(&#91;
  '../images/**',
  '../fonts/**',
]);</code></pre>



<p>執行 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">npm run build</mark></code>，這樣 Vite 就會將素材檔名編碼過，並移到 public folder 底下。前端要用素材檔可以這樣寫</p>



<pre class="wp-block-code"><code>&lt;img src="{{ Vite::asset('resources/images/logo.png') }}"&gt;
{{-- &lt;img src="http://your-url.com/build/assets/logo.1234ooxx.png"&gt; --}}</code></pre>



<h4 class="wp-block-heading">結論</h4>



<p>從 Webpack 到 Vite，底層是完全不同了。但可以看出來 <strong>Laravel Vite</strong> 創作時，作者盡量不動 Laravel 的架構，與之前一樣的 css / js 入口檔案。</p>



<p>Vite config 當然有一些獨立的功能和寫法。既然 Laravel 決定改用 Vite 了，我們應該也要隨之學習和習慣 Vite 的使用。</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Laravel Mix</title>
		<link>https://blog.yuyansoftware.com.tw/2017/04/laravel-mix/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Sat, 15 Apr 2017 10:59:00 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[gulp]]></category>
		<guid isPermaLink="false">http://test234.yuyansoftware.com.tw/2017/04/15/laravel-frontend/</guid>

					<description><![CDATA[Laravel 5.4 開始使用 Laravel Mix 去處理前端的 js / css 檔案，Laravel Mix 這套件是由 npm 管理的，所以首先確認你的開發環境中有裝 npm.]]></description>
										<content:encoded><![CDATA[
<p>圖片來源 <a href="https://github.com/JeffreyWay/laravel-mix" target="_blank" rel="noreferrer noopener" aria-label=" (在新分頁中開啟)">https://github.com/JeffreyWay/laravel-mix</a></p>



<p><a rel="noreferrer noopener" aria-label=" (在新分頁中開啟)" href="https://laravel.com/docs/5.4/frontend" target="_blank">https://laravel.com/docs/5.4/frontend</a><br><a rel="noreferrer noopener" aria-label=" (在新分頁中開啟)" href="https://laravel.com/docs/5.4/mix" target="_blank">https://laravel.com/docs/5.4/mix</a><br>Laravel 5.4 開始使用 Laravel Mix 去處理前端的 js / css 檔案，Laravel Mix 這套件是由 npm 管理的，所以首先確認你的開發環境中有裝 npm.</p>



<p>裝完 Laravel 後你的 Laravel 根目錄會有 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code>, <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark></code>, 確認這兩支 file 也在你的開發環境，執行</p>



<pre class="wp-block-code"><code>npm install</code></pre>



<p>或是 (如果你的開發環境是 Windows)</p>



<pre class="wp-block-code"><code>npm install --no-bin-links</code></pre>



<p>npm 就會依照 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json </mark></code>去 download 套件到你的開發環境。注意 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">package.json</mark></code> 中預設就有 Bootstrap, jQuery, Laravel Mix, Vue. 執行</p>



<pre class="wp-block-code"><code>npm run dev</code></pre>



<p>或是 (如果你要壓縮你的 js/css output)</p>



<pre class="wp-block-code"><code>npm run production</code></pre>



<p>npm 就會去執行 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark></code> 中所指定的步驟 (有 gulp 經驗的朋友應該不陌生)。 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark></code> 預設會 compile</p>



<ul class="wp-block-list">
<li><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">resources/assets/sass/app.scss</mark></code> 到 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">public/css/app.css</mark></code></li>



<li><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">resources/assets/js/app.js</mark></code> 到 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">public/js/app.js</mark></code></li>
</ul>



<p>注意 resources folder 是外部不可 access 的，public folder 才是公開的。</p>



<p>如果你看一下 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">resources/assets/sass/app.scss</mark></code> 就會了解，他就是一般 Sass 的架構，預設就有個 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">_variables.scss</mark></code> 在隔壁，import 到 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">app.scss</mark></code></p>



<p><code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">resources/assets/js/app.js</mark></code> 中預設 import 隔壁的 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">bootstrap.js</mark></code>，在其中 load 了 jQuery, Bootstrap, Vue, 可以在其中作 configuration.</p>



<p>最後有點值得一提，在 <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">webpack.mix.js</mark> 最後加上 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">version()</mark></code></p>



<pre class="wp-block-code"><code>mix.js('resources/assets/js/app.js', 'public/js')
    .sass('resources/assets/sass/app.scss', 'public/css')
    .version();</code></pre>



<p>這樣產出的 <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">app.css</mark></code>, <code><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-cyan-blue-color">app.js</mark></code> 檔案名稱都會被編碼過 (參考 <a rel="noreferrer noopener" href="https://blog.yuyansoftware.com.tw/2016/01/gulp-css-js-browser-cache.html" target="_blank">如何利用 gulp 自動添加 JS 及 CSS 版號，避免瀏覽器快取</a>)。使用時</p>



<pre class="wp-block-code"><code>&lt;link rel="stylesheet" href="{{ mix('/css/app.css') }}"&gt;
&lt;script src="{{ mix('/js/app.js') }}"&gt;&lt;/script&gt;</code></pre>



<p>mix function 會自行去找最新產出的 css / js.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>如何利用 gulp 自動添加 JS 及 CSS 版號 (auto-versioning)，避免瀏覽器快取</title>
		<link>https://blog.yuyansoftware.com.tw/2016/01/gulp-css-js-browser-cache/</link>
		
		<dc:creator><![CDATA[Larry]]></dc:creator>
		<pubDate>Sun, 03 Jan 2016 06:07:00 +0000</pubDate>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[前端工程]]></category>
		<category><![CDATA[gulp]]></category>
		<guid isPermaLink="false">http://test234.yuyansoftware.com.tw/2016/01/03/%e5%a6%82%e4%bd%95%e5%88%a9%e7%94%a8-gulp-%e6%9b%bf-css-js-%e5%8a%a0%e4%b8%8a%e7%89%88%e8%99%9f%ef%bc%8c%e9%81%bf%e5%85%8d-browser-cache/</guid>

					<description><![CDATA[當一個使用者瀏覽了你的網站，過沒幾天你更新了 JS 或 CSS，但因為該使用者的瀏覽器記住了上次瀏覽的 JS / CSS，以至於你更新的 JS / CSS 不會在他的瀏覽器上生效。同理，也不會對其他最近瀏覽你網站的使用者生效。]]></description>
										<content:encoded><![CDATA[
<p>圖片來源 <a href="https://github.com/gulpjs/gulp" target="_blank" rel="noreferrer noopener nofollow">https://github.com/gulpjs/gulp</a></p>



<p>一般來說，當使用者開過一個網站，他的瀏覽器會快取網站的 JS 及 CSS (就是記在瀏覽器的意思)。所以當一個使用者瀏覽了你的網站，過沒幾天你更新了 JS 或 CSS，但因為該使用者的瀏覽器記住了上次瀏覽的 JS / CSS，以至於你更新的 JS / CSS 不會在他的瀏覽器上生效。</p>



<p>同理，其他最近瀏覽你網站的使用者(已快取)，如果再打開你的網站，也看不到你剛剛更新的 JS / CSS。</p>



<p>每次更新時自動添加 JS 及 CSS 版號 (auto-versioning) 是一個避免瀏覽器快取的方法。前端工具 gulp 有一套完整的解法，本篇文章會依照 <a href="https://www.npmjs.com/package/gulp-rev-replace" target="_blank" rel="noreferrer noopener nofollow">gulp-rev-replace</a> 的做法一步一步來看。首先它使用&nbsp;<a href="https://www.npmjs.com/package/gulp-useref" target="_blank" rel="noreferrer noopener nofollow">gulp-useref</a> 分別合併 JS 及 CSS.&nbsp;</p>



<pre class="wp-block-code"><code>&lt;!-- build:css css/combined.css --&gt;
your css &lt;link&gt;...
&lt;!-- endbuild --&gt;

&lt;!-- build:js scripts/combined.js --&gt;
your js &lt;script&gt;...
&lt;!-- endbuild --&gt;</code></pre>



<p>照上述 html 轉完後會變成</p>



<pre class="wp-block-code"><code>&lt;link rel="stylesheet" href="css/combined.css"/&gt;

&lt;script src="scripts/combined.js"&gt;&lt;/script&gt; </code></pre>



<p>要注意的是 combined.css, combined.js 目前都還未壓縮，要使用 <a href="https://www.npmjs.com/package/gulp-uglify" target="_blank" rel="noreferrer noopener nofollow">gulp-uglify</a> 壓縮 JS 檔，使用 <a href="https://www.npmjs.com/package/gulp-csso" target="_blank" rel="noreferrer noopener nofollow">gulp-csso</a> 壓縮 CSS 檔，接著使用 <a href="https://github.com/sindresorhus/gulp-rev" target="_blank" rel="noreferrer noopener nofollow">gulp-rev</a> 去改目前 file stream 上的檔案名稱。</p>



<p>例如 combined.js 會改成 combined-12345abcde.js 放在你的 destination folder，combined.css 也會被更名，這個步驟就是所謂的「上版號」。</p>



<p>最後再用 <a href="https://www.npmjs.com/package/gulp-rev-replace" target="_blank" rel="noreferrer noopener nofollow">gulp-rev-replace</a> 將 source html 中的 &lt;link>, &lt;script> 改成指向加上版號的 JS 及 CSS，例如</p>



<pre class="wp-block-code"><code>&lt;link rel="stylesheet" href="css/combined-12345abcde.css"/&gt;

&lt;script src="scripts/combined-12345abcde.js"&gt;&lt;/script&gt; </code></pre>



<p>以上就是 <a href="https://www.npmjs.com/package/gulp-rev-replace" target="_blank" rel="noreferrer noopener nofollow">gulp-rev-replace</a> 文件裡介紹的自動上版號 (auto-versioning) 的流程。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
