Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

简单说下webpack中的文件指纹策略 #125

Open
Cosen95 opened this issue Jul 12, 2020 · 1 comment
Open

简单说下webpack中的文件指纹策略 #125

Cosen95 opened this issue Jul 12, 2020 · 1 comment
Labels

Comments

@Cosen95
Copy link
Owner

Cosen95 commented Jul 12, 2020

No description provided.

@Cosen95
Copy link
Owner Author

Cosen95 commented Jul 12, 2020

文件指纹对应的其实就是hash

  • Hash:和整个项目的构建相关,只要项目文件有修改,整个项目构建的 hash 值就会更改
  • Chunkhash:和 webpack 打包的 chunk 有关,不同的 entry 会生成不同的 chunkhash 值
  • Contenthash:根据文件内容来定义 hash,文件内容不变,则 contenthash 不变

来看下这三种 hash 在 webpack 里面怎么配置:

module.exports = {
  entry: {
    app: "./src/app.js",
    info: "./src/info.js",
  },
  output: {
    filename: "[name][chunkhash:8].js",
    path: __dirname + "/dist",
  },
  plugins: [
    new MiniCssExtractPlugin({
      filename: `[name][contenthash:8].css`,
    }),
  ],
  module: {
    rules: [
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          {
            loader: "file-loader",
            options: {
              name: "img/[name][hash:8].[ext]",
            },
          },
        ],
      },
    ],
  },
};
  • JS 文件指纹设置:设置 output 的 filename,使用[chunkhash]
  • CSS 文件指纹设置:设置 MiniCssExtractPlugin 的 filename,使用[contenthash]
  • 图片文件指纹设置:设置 file-loader 的 name,使用[hash]
占位符名称 含义
[ext] 资源后缀名
[name] 文件名称
[path] 文件的相对路径
[folder] 文件所在的文件夹
[contenthash] 文件的内容 hash,默认是 md5 生成
[hash] 文件内容的 Hash,默认是 md5 生成
[emoji] 一个随机的指代文件内容的 emoj

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant