14

I've got an internal application that's built using webpack that gets deployed frequently. In order to make bug reporting easier, I want to include an environment variable of the build hash [hash] that webpack adds to bundle names. This would let me quickly determine if the user is on the latest release.

Using the DefinePlugin, the following does not interpolate the string and instead just stores a literal [hash] string.

new webpack.DefinePlugin({
  'process.env': {
    'HASH': JSON.stringify('[hash]')
  }
})

Is there any way to access the hash directly as a variable or is there a specific way to make it interpolate?

Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167
Soviut
  • 88,194
  • 49
  • 192
  • 260

1 Answers1

15

https://github.com/webpack/docs/wiki/list-of-plugins#extendedapiplugin

ExtendedAPIPlugin

new webpack.ExtendedAPIPlugin()

Adds useful free vars to the bundle.

__webpack_hash__ The hash of the compilation available as free var.

This can't be used in the DefinePlugin() but it creates a global __webpack_hash__ variable that can be accessed from anywhere inside your bundle.

var hash = __webpack_hash__;
Soviut
  • 88,194
  • 49
  • 192
  • 260
Digger2000
  • 546
  • 3
  • 16
  • Does this mean `__webpack_hash__` is just a global var in my bundle now? or is there some other way to access it? – Soviut Aug 22 '16 at 17:16
  • Yes, it is. I don't know another ways to use it. – Digger2000 Aug 22 '16 at 17:31
  • 1
    It may be worth noting that if you use interpolation in html files "${__webpack_hash__}", the hash won't be the correct one. So I'm falling back to setting the hash in the html at runtime. – FloG Oct 27 '17 at 12:37
  • You're missing the underscores in the var name. – Mircea D. Feb 08 '23 at 15:42