最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass env variable to rollup.config.js via npm cli? - Stack Overflow

programmeradmin1浏览0评论

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"

I would like to pass parameter from cli like npm run watch script_name=abc_script

It can be accessible in rollup.config.js via process.argv

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.

Rollup have --environment variable but it's bit long to use npm run watch -- --environment script:script_name

Is there any way to shorten this?

Thanks in advance.

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"

I would like to pass parameter from cli like npm run watch script_name=abc_script

It can be accessible in rollup.config.js via process.argv

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.

Rollup have --environment variable but it's bit long to use npm run watch -- --environment script:script_name

Is there any way to shorten this?

Thanks in advance.

Share Improve this question edited Oct 11, 2019 at 8:54 CaptainZero asked Oct 11, 2019 at 8:38 CaptainZeroCaptainZero 1,4911 gold badge20 silver badges39 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 16

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")

Create a .env file in your project's root directory and fill it with VAR_NAME=value on each line

NODE_ENV=development
SECRET_KEY=ahuehueheueheueheu

DON'T COMMIT THAT FILE. Instead, add .env to your .gitignore.

Next install dotenv node package

npm i -D dotenv
yarn add -D dotenv

And finally put this at the very top of your rollup.config.js

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

Alternatively, you can pass environment variables to the command - it's much easier to process than command-line arguments.

Cli usage:

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

package.json script:

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

rollup.config.js

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

You can pass arguments which will be caught by process.argv like this

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.

npm run watch -- --environment script=script_name worked for me, So I can access script_name via process.env in rollup config

发布评论

评论列表(0)

  1. 暂无评论