chunkhash、contenthash、hash

Hash:和整个项目的构建相关,只要项目文件有修改,整个项目构建的 hash 值就会更改

Chunkhash:和 webpack 打包的 chunk 有关,不同的 entry 会生成不同的 chunkhash 值

Contenthash:根据文件内容来定义 hash,文件内容不变,则 contenthash 不变

JS 的文件指纹设置

// webpack.config.js
module.exports = {
  //...
  output: {
    path: path.join(__dirname, 'dist'),
    filename: '[name]_[chunkhash:8].js',
  },
}

CSS 的文件指纹设置

// webpack.config.js
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
  //...
  module: {
    rules: [
      {
        test: /\\\\.css$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader'
        ]
      }
    ]
  },
  plugins: {
    new MiniCssExtractPlugin({
      filename: '[name]_[contenthash:8].css'
    }),
  },
}

图片的文件指纹设置

// webpack.config.js
module.exports = {
  //...
  module: {
    rules: [
      {
        test: /.(png|jpg|gif|jpeg)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: '[name]_[hash:8].[ext]',
            },
          },
        ],
      },
    ]
  }
}