详解Vue CLI3配置之filenameHashing使用和源码设计使用和源码设计

这篇文章主要介绍了详解Vue CLI3配置之filenameHashing使用和源码设计使用和源码设计,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

执行 npm run build 之后的 dist 目录的静态资源的文件名多会追加上 hash 值,比如: page1.f151b4d3.js

那如果不要 hash 呢,你只需要配置 vue.config.js 文件中的 filenameHashing

官方文档也提到了因为 html 也是我们通过插件生成的,静态资源直接就 inject 进去的,所以,当 html 不是自动生成或者其他情况时候,就不能加 hash 了,可以配置 false。

 filenameHashing: false

我们看看源码实现:

首先它是 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` )

他们多依赖函数 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 } 

以上就是详解Vue CLI3配置之filenameHashing使用和源码设计使用和源码设计的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Vue.js 教程