Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"

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.

like image 269
CaptainZero Avatar asked Oct 11 '19 08:10

CaptainZero


2 Answers

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'
like image 171
Maciej Krawczyk Avatar answered Sep 19 '22 03:09

Maciej Krawczyk


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.

like image 23
iAmADeveloper Avatar answered Sep 20 '22 03:09

iAmADeveloper