javascript - Reading webpack's .env files with grunt
问题描述
I'm using grunt-replace to perform some substitutions of variables according to the environment (dev/testing/production). Those variables are set like this:
config: {
dev: {
options: {
variables: {
base_url: 'localhost'
}
}
},
testing: {
options: {
variables: {
base_url: 'testing.example.com'
}
}
},
prod: {
options: {
variables: {
base_url: 'production.example.com'
}
}
}
},
Then I'm replacing the variables with grunt-replace, like this:
replace: {
main: {
options: {
patterns: [
{
match: 'BASE_URL',
replacement: '<%= grunt.config.get("base_url") %>'
}
]
},
src: 'src/main.js' ,
dest: 'dist/main.js'
},
}
Since I'm using an .env
file configured for the mentioned environments, is it possible to tell grunt-env to load that file and perform the replacements in replacement of grunt-config? I would like to do this in order to avoid having duplicated code for setting those variables in each environment along both webpack and grunt.
Thanks!
解决方案
您可以使用dotenv读取 .env 文件,然后将其传递给 grunt config。
const dotenv = require('dotenv');
const envConfig = dotenv.config({ path: '/custom/path/to/.env' }).parsed;
推荐阅读
- html - 当我尝试在 chrome 中打开 .html 文件时,它偶尔会打开存储库
- python - Selenium - 网页抓取;如何使用硒获取特定标签?
- linux - 在两个以上共享库之间切换 (LD_PRELOAD)
- discord - 如何将 Discord Youtube 通知程序更改为仅在 if 语句为真时发布
- python - 另一种根据日期值获取最新记录的方法
- regression - 在 ARIMA 中拟合第二个季节性术语有什么问题
- javascript - 有人可以帮我我的谷歌开发控制台说 .getcontext 不是一个函数
- oracle - Hive 中的隐式数据类型转换
- android - 如何使用 Spinner 更改变量的值?
- selenium-grid - 使用 selenium 网格 4 将节点连接到集线器