简体   繁体   English

如何通过 npm cli 将环境变量传递给 rollup.config.js?

[英]How to pass env variable to rollup.config.js via npm cli?

I have a scripts folder in which many individual scripts inside seperate folder, I would like to build each separately via passing the script name as parameter.我有一个脚本文件夹,其中许多单独的脚本位于单独的文件夹中,我想通过将脚本名称作为参数传递来分别构建每个脚本。

I have set up rollup in package.json like "watch": "rollup --watch --config rollup.config.js"我已经在 package.json 中设置汇总,例如"watch": "rollup --watch --config rollup.config.js"

I would like to pass parameter from cli like npm run watch script_name=abc_script我想从 cli 传递参数,例如npm run watch script_name=abc_script

It can be accessible in rollup.config.js via process.argv它可以通过 process.argv 在 rollup.config.js 中访问

But getting this error但是得到这个错误

rollup v1.23.1 bundles abc_script → dist/bundle.js [:] Error: Could not resolve entry module

Everything seems fine without npm cli parameter.没有 npm cli 参数,一切似乎都很好。

Rollup have --environment variable but it's bit long to use npm run watch -- --environment script:script_name Rollup 有 --environment 变量,但使用npm run watch -- --environment script:script_name

Is there any way to shorten this?有什么办法可以缩短这个吗?

Thanks in advance.提前致谢。

While the following answer doesn't directly address OP's need (to pass variables in via command line), it does address their desire for brevity ("--environment variable but it's bit long to use")虽然以下答案没有直接解决 OP 的需求(通过命令行传递变量),但它确实解决了他们对简洁的渴望(“--环境变量,但使用起来有点长”)

Create a .env file in your project's root directory and fill it with VAR_NAME=value on each line在项目的根目录中创建一个.env文件,并在每一行上用VAR_NAME=value填充它

NODE_ENV=development
SECRET_KEY=ahuehueheueheueheu

DON'T COMMIT THAT FILE .不要提交那个文件 Instead, add .env to your .gitignore .相反,将.env添加到您的.gitignore

Next install dotenv node package接下来安装dotenv节点package

npm i -D dotenv
yarn add -D dotenv

And finally put this at the very top of your rollup.config.js最后把它放在你的rollup.config.js的最顶部

import dotenv from 'dotenv';
dotenv.config();

You can pass arguments which will be caught by process.argv like this您可以通过 arguments 像这样被process.argv捕获

npm run watch -- some_arg

In your program, you will get an array in process.argv in this the last value will be the value passed to the program.在您的程序中,您将在 process.argv 中获得一个数组,其中最后一个值将是传递给程序的值。

Alternatively, you can pass environment variables to the command - it's much easier to process than command-line arguments.或者,您可以将环境变量传递给命令 - 它比命令行 arguments 更容易处理。

Cli usage: cli用法:

minify=on./node_modules/.bin/rollup -c

package.json script: package.json 脚本:

{
  ...
  scripts: {
    ...
    "build-production": "minify=on rollup -c"
  }
}

rollup.config.js rollup.config.js

const enableMinification = process.env.minify === 'on'

npm run watch -- --environment script=script_name worked for me, So I can access script_name via process.env in rollup config npm run watch -- --environment script=script_name为我工作,所以我可以通过汇总配置中的 process.env 访问 script_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM