如何在Webpack中将构建哈希作为环境变量传递?

时间:2016-08-22 15:05:56

标签: javascript webpack environment-variables

我有一个内部应用程序是使用经常部署的webpack构建的。为了使错误报告更容易,我想要包含webpack添加到包名称的构建哈希[hash]的环境变量。这样我就可以快速确定用户是否在最新版本中。

使用DefinePlugin,以下内容不会插入字符串,而只是存储文字[hash]字符串。

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

有没有办法直接将哈希作为变量访问,还是有特定的方法来进行插值?

1 个答案:

答案 0 :(得分:10)

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

  

ExtendedAPIPlugin

     

new webpack.ExtendedAPIPlugin()

     

向捆绑包添加有用的免费变量。

     

__webpack_hash__编译的哈希值为free var。

这不能在DefinePlugin()中使用,但会创建一个全局__webpack_hash__变量,可以从捆绑包中的任何位置进行访问。

var hash = __webpack_hash__;