简体   繁体   English

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

[英]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. 我有一个内部应用程序,它使用经常部署的webpack构建。 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. 为了使bug报告更容易,我想要包含webpack添加到bundle名称的build hash [hash]的环境变量。 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. 使用DefinePlugin ,以下内容不会插入字符串,而只是存储文字[hash]字符串。

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 https://github.com/webpack/docs/wiki/list-of-plugins#extendedapiplugin

ExtendedAPIPlugin ExtendedAPIPlugin

new webpack.ExtendedAPIPlugin()

Adds useful free vars to the bundle. 向捆绑包添加有用的免费变量。

__webpack_hash__ The hash of the compilation available as free var. __webpack_hash__可用作免费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. 这不能在DefinePlugin()使用,但它会创建一个全局__webpack_hash__变量,可以从bundle中的任何位置访问它。

var hash = __webpack_hash__;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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