设置vite环境变量值在pnpm脚本中

huangapple go评论71阅读模式
英文:

Set vite env-varible value in the pnpm script

问题

我尝试在pnpm脚本中设置变量,但出现错误:

"build": BUILD_PLATFORM=web vite build

但是会出现错误:

'BUILD_PLATFORM' 不被识别为内部或外部命令、可执行程序或批处理文件

我需要在脚本调用中设置环境变量的值。

使用.env文件的方法似乎不正确,因为每次我想调用我的脚本之一时,都需要相应地更改.env文件中的变量值。

英文:

I tried to set varible right in to the pnpm script:

"build": BUILD_PLATFORM=web vite build

But getting an error:

'BUILD_PLATFORM' is not recognized as an internal or external command
operable program or batch file

I need to set value for environment variable in script call

.env file approach seems to be incorrect, because every time I want to call one of my scripts, I should change varible value in .env file accordingly

答案1

得分: 0

我在预启动模块中找到了我的解决方案,它在执行当前脚本之前修改了.env文件。

在我的情况下,它看起来像这样:

"build:desktop": "tsx prebuild-setup/desktop.ts && vite build",
"build:web": "tsx prebuild-setup/web.ts && vite build",

desktop.tsweb.tsvite build 之前执行,并对 .env 文件进行更改,因此在构建时,代码将使用正确的变量值,无需在调用脚本之前每次手动更改 .env 文件。

注意:tsx - 用于运行TypeScript的包。

英文:

I found my solution in pre-launching module that modifies the .env file before executing current script

In my case it looks like this:

"build:desktop": "tsx prebuild-setup/desktop.ts && vite build",
"build:web": "tsx prebuild-setup/web.ts && vite build",

desktop.ts & web.ts are executing before vite build and make changes to .env file, so at build time, code will use proper variable values, no need to change .env file manualy every time before calling scripts

note: tsx - package for running TypeScript

huangapple
  • 本文由 发表于 2023年8月4日 03:55:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定