dotenv-webpack是否将所有环境变量从.env公开到前端? [英] Does dotenv-webpack expose all environment variables from .env to front end?

查看:1036
本文介绍了dotenv-webpack是否将所有环境变量从.env公开到前端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中包含一些API的公钥和其他API的私钥。我想将所有这些内容存储在我的单个 .env 文件中,并使用 dotenv 来提供私钥我的服务器和 dotenv-webpack 为我的公共前端javascript提供公钥。使用这个软件包会使我的私钥在我的javascript中可用,还是只需要它需要它?

I am building an application that has public keys for some APIs and private keys for other APIs. I would like to store all of them inside of my single .env file and use dotenv to provide the private keys to my server and dotenv-webpack to provide the public keys to my public front-end javascript. Will using this package make my private keys available in my javascript or will it only take the ones it needs?

示例:

# .env file
public_key="12345"
private_key="45678"

这个需要在前端访问

// javascript file on front end
var publicKey = process.env.public_key

但我想要保持这个隐藏

// javascript file in node.js
var privateKey = process.env.private_key

我正在考虑使用的包是这一个:
https://www.npmjs.com/package/dotenv-webpack

The package I am considering using is this one: https://www.npmjs.com/package/dotenv-webpack

推荐答案

插件的github页面那个:

The github page of the plugin states that:


dotenv-webpack包装了dotenv和Webpack.DefinePlugin。因此,它会覆盖现有的任何现有DefinePlugin配置。此外,与DefinePlugin一样,它在process.env的任何实例的结果包中进行文本替换。

dotenv-webpack wraps dotenv and Webpack.DefinePlugin. As such, it overwrites existing any existing DefinePlugin configurations. Also, like DefinePlugin, it does a text replace in the resulting bundle for any instances of process.env.

您的.env文件可以包含敏感信息。因此,dotenv-webpack将只在最终包中包含已定义的环境变量。

Your .env files can include sensitive information. Because of this,dotenv-webpack will only include defined environment variables in the final bundle.

所以它是安全的。它只会暴露您在代码中实际使用的环境变量。

So its safe. It will only expose the environment variables which you actually use in the code.

这篇关于dotenv-webpack是否将所有环境变量从.env公开到前端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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