hash
[hash] is replaced by the hash of the compilation(). 代表的是cpmpilation的hash。
compilation在项目中任何一个文件改动后就会被重新创建,然后webpack计算新的compilation的hash值,这个hash值便是hash。
chunkhash
[chunkhash] is replaced by the hash of the chunk. chunk(模块)的hash
代表的是chunk(模块)的hash值。
contenthash
插件extract-text-webpack-plugin
引入的contenthash
名称 | 说明 |
---|---|
hash | 代表的是compilation的hash值。compilation在项目中任何一个文件改动后就会被重新创建,然后webpack计算新的compilation的hash值 |
chunkhash | 代表chunk的hash,模块发生改变才会重新生成hash |
contenthash | 解决改变style文件导致js文件重新生成hash的问题(使用extract-text-webpack-plugin单独编译输出css文件) |
vue-cli举例
vue-cli脚手架中webpack的配置文件hash
, build/webpack.base.conf.js
vue-cli中,hash用于图片,音视频,和字体文件
// hash(hash,jpg,mo4,txt)
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
})
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
复制代码
chunkhash,build/webpack.prod.conf.js
chuunkhash主要用于js文件中
// chunkhash,js
filename: utils.assetsPath('js/[name].[chunkhash].js'),
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
复制代码
contenthash:build/webpack.prod.conf.js
使用extract-text-webpack-plugin单独编译输出css文件。extract-text-webpack-plugin提供了另外一种hash值:contenthash
// extract css into its own file
new ExtractTextPlugin({
filename: utils.assetsPath('css/[name].[contenthash].css')
}),
复制代码
至于缓存这一大块的内容,路漫漫,继续学习
参考资料