简体   繁体   中英

How to pass the build hash as an environment variable in webpack?

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?

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__;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM