如何在Webpack中将构建哈希作为环境变量传递? [英] How to pass the build hash as an environment variable in webpack?

查看:127
本文介绍了如何在Webpack中将构建哈希作为环境变量传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内部应用程序,它是使用经常部署的webpack构建的。为了使错误报告更容易,我想要包含构建哈希 [hash] 的环境变量,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.

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

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

ExtendedAPIPlugin

new webpack.ExtendedAPIPlugin()

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

Adds useful free vars to the bundle.

__ webpack_hash __ 散列汇编可用作免费变量。

__webpack_hash__ The hash of the compilation available as free var.

这不能用于 DefinePlugin()但它创建了一个全局 __ webpack_hash __ 变量,可以从捆绑包内的任何地方访问。

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

这篇关于如何在Webpack中将构建哈希作为环境变量传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆