Skip to content

Real contenthash for final emitted assets in webpack5.

Notifications You must be signed in to change notification settings

tuax/rehash-webpack-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REHASH Webpack Plugin

Real contenthash for emitted assets in webpack5.

Install

Webpack 5

  npm i -D rehash-webpack-plugin
  yarn add --dev rehash-webpack-plugin

Then, you can easily rename the emitted JS/CSS file using its md5 value.

Usage

The plugin will calculate the hash value based on the final webpack emitted assets' content, replacing the contenthash or chunkhash in the JS/CSS filename. Just follow these steps:

add the plugin to your webpack

Check and modify your webpack configuration files as appropriate Depending on whether the production environment exports sourcemap files or not, there are two recommended ways to set this up.

devtool is set to none of the source-map, nosources-source-map, hidden-nosources-source-map, hidden-source-map

for main configurations,

import RehashWebpackPlugin from 'rehash-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';

module.exports = {
  devtool: false,
  // ... Ignore other configurations ...
  output: {
    filename: '[contenthash].js',
     // for dynamic chunk
    chunkFilename: '[contenthash].js',
    hashFunction: 'md5',
    hashDigest: 'hex'
  },
  plugins: [
    new RehashWebpackPlugin({hashType:'contenthash'}),
    new MiniCssExtractPlugin({
        filename: '[contenthash].css',
        // for dynamic chunk
        chunkFilename: '[contenthash].css', 
    })
  ]
}

Note: ** All the files will be rename with the new hash. **

devtool is set to one of the source-map, nosources-source-map, hidden-nosources-source-map, hidden-source-map

In this case, the production environment generates a sourcemap file for the JS/CSS files, and output needs to be configured with the following filename and chunkFilename

import RehashWebpackPlugin from 'rehash-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';

module.exports = {
  devtool: 'source-map',
  // ... Ignore other configurations ...
  output: {
    filename: `[chunkhash]${RehashWebpackPlugin.specialSeparator}.js.js`,
     // for dynamic chunk
    chunkFilename: '[contenthash].js',
    hashFunction: 'md5',
    hashDigest: 'hex'
  },
  plugins: [
    new RehashWebpackPlugin({hashType:'chunkhash'}),
    new MiniCssExtractPlugin({
        filename:  `[chunkhash]${RehashWebpackPlugin.specialSeparator}.css.css`,
        // for dynamic chunk
        chunkFilename: '[contenthash].css', 
    })
  ]
}

Note: ** Only the main file(not including the dynamic file) will be renamed with the new hash. **

This is because if contenthash is also used in the output.filename configuration, this causes the sourcemap filename to be modified once during the processAssets phase, and the hash value generated based on the contents of the file in the intermediate state is not the correct hash value.

Options

options.hashType

string, contenthash | chunkhash, default to chunkhash

Note: This setting depends on whether the sourcemap file will be generated. See above.

Contributors

KnightWu (@wulijian) [email protected]

Special Notes

This plugin is partial based on webpack-plugin-hash-output, thanks for their work.

About

Real contenthash for final emitted assets in webpack5.

Resources

Stars

Watchers

Forks

Packages

No packages published