詳解Vue CLI3配置之filenameHashing使用和源碼設(shè)計使用和源碼設(shè)計
執(zhí)行 npm run build 之后的 dist 目錄的靜態(tài)資源的文件名多會追加上 hash 值,比如: page1.f151b4d3.js
那如果不要 hash 呢,你只需要配置 vue.config.js 文件中的 filenameHashing
官方文檔也提到了因為 html 也是我們通過插件生成的,靜態(tài)資源直接就 inject 進去的,所以,當(dāng) html 不是自動生成或者其他情況時候,就不能加 hash 了,可以配置 false。
filenameHashing: false
我們看看源碼實現(xiàn):
首先它是 vue.config.js
的一個配置,在文件 cli-service/lib/options.js
中:
默認值是 true
filenameHashing: true
先看 css 部分,在文件 cli-service/lib/config/css.js 中:
const filename = getAssetPath( options, `css/[name]${options.filenameHashing ? '.[contenthash:8]' : ''}.css` )
再看 js 部分,在文件 cli-service/lib/config/prod.js
const filename = getAssetPath( options, `js/[name]${isLegacyBundle ? `-legacy` : ``}${options.filenameHashing ? '.[contenthash:8]' : ''}.js` )
他們多依賴函數(shù) getAssetPath,在文件 util/getAssetPath.js 中定義了
const path = require('path') module.exports = function getAssetPath (options, filePath, placeAtRootIfRelative) { return options.assetsDir ? path.posix.join(options.assetsDir, filePath) : filePath }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

Vue3使用ref解決GetElementById為空的問題

vue實現(xiàn)el-menu與el-tabs聯(lián)動的項目實踐