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

Sov*_*iut 10 javascript environment-variables webpack

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

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

new webpack.DefinePlugin({
  'process.env': {
    'HASH': JSON.stringify('[hash]')
  }
})
Run Code Online (Sandbox Code Playgroud)

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

Dig*_*000 11

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

ExtendedAPIPlugin

new webpack.ExtendedAPIPlugin()

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

__webpack_hash__ 编译的哈希值可用作自由变量.

这不能用于DefinePlugin()它,但它创建了一个全局__webpack_hash__变量,可以从bundle中的任何位置访问.

var hash = __webpack_hash__;
Run Code Online (Sandbox Code Playgroud)