Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Webpack, how do I generate a content hash for only some files?

Tags:

webpack

Webpack has a caching mechanism:

https://webpack.js.org/guides/caching/

That allows you to generate a content hash for your JS files.

Suppose I'm building many JS files and I only want to use the content hash for one of them.

How do I do this in webpack?

like image 370
Mary Avatar asked Nov 01 '25 05:11

Mary


1 Answers

You can use a function for the filename like:

output: {
    filename: (pathData) => {
      return (pathData.chunk.name === 'main') ? '[name].[contenthash].js' : '[name].js';
    },
  },

This will only add the hash to the main.js file, or whatever logic you want to use based on the pathData.

like image 133
Nick N Avatar answered Nov 04 '25 03:11

Nick N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!