詳解Vue CLI3配置之filenameHashing使用和源碼設(shè)計(jì)使用和源碼設(shè)計(jì)
執(zhí)行 npm run build 之后的 dist 目錄的靜態(tài)資源的文件名多會(huì)追加上 hash 值,比如: page1.f151b4d3.js
那如果不要 hash 呢,你只需要配置 vue.config.js 文件中的 filenameHashing
官方文檔也提到了因?yàn)?html 也是我們通過(guò)插件生成的,靜態(tài)資源直接就 inject 進(jìn)去的,所以,當(dāng) html 不是自動(dòng)生成或者其他情況時(shí)候,就不能加 hash 了,可以配置 false。
filenameHashing: false
我們看看源碼實(shí)現(xiàn):
首先它是 vue.config.js
的一個(gè)配置,在文件 cli-service/lib/options.js
中:
默認(rèn)值是 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 }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章

Vue3使用ref解決GetElementById為空的問(wèn)題

element?表格多級(jí)表頭子列固定的實(shí)現(xiàn)

vue項(xiàng)目中使用ts(typescript)入門(mén)教程

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