VUE为NX插件和使用环境变量(NX-Plus/Vue)

发布于 2025-02-01 22:14:13 字数 564 浏览 4 评论 0原文

我已经将VUE应用程序迁移到NX,现在一切都很好,除了一件事 - 以前,.env文件在开发环境和开发过程中均在开发和构建过程中使用,vue -Cli-Service处理了这一点。现在我在应用程序构建方面遇到困难 - 它似乎没有读取.ENV文件,否则我只是做错了。

我尝试添加dotenv-webpack,并将其添加到configure-webpack.js中,但是在构建过程中,没有一个变量无法访问。这是文件示例:

//.env
SOME_API_URL=$API_URL

// config.ts
export const CONFIG = {
  api_url = process.env.SOME_API_URL
}
// during build process.env is undefined

构建命令如下:

export API_URL='http://vue-app.com'; nx build frontend --skip-nx-cache

I've migrated vue app to nx, all is fine now except of one thing - previously, .env file was used in both dev and production environments, during development as well as build, vue-cli-service took care of that. Now I struggle with app build - it doesn't seem to read .env file, or I just do it wrong.

I've tried to add dotenv-webpack and add it in the configure-webpack.js but during build none of the variables was accessible. Here is the file example:

//.env
SOME_API_URL=$API_URL

// config.ts
export const CONFIG = {
  api_url = process.env.SOME_API_URL
}
// during build process.env is undefined

build command looks like this:

export API_URL='http://vue-app.com'; nx build frontend --skip-nx-cache

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2025-02-08 22:14:13

您需要切换到VITE,但是我们使用此插件在我们的应用程序中取得了良好的成功: https://github.com/chf007/vite-plugin-nx-dotenv

You would need to switch to Vite but we’ve had good success using this plugin for our applications: https://github.com/chf007/vite-plugin-nx-dotenv

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文