Search code examples
node.jsbashnpmwebpackgulp

Passing arguments to combined npm script


I have in my package.json the following

"scripts": {
    ...
    "prod": "gulp build --production && webpack --env.config=production"
}

I now want to pass a parameter "theme" to both gulp and webpack to be able to adjust the output of the build process from the command line.

I figured out how to pass it to webpack: npm run prod -- --env.theme=themename but gulp does not take care of this. I also played around with the yargs-package, processs.argv and bash string substitution by changing the npm script to "gulp build --production \"$1\" && webpack --env.config=production" but that did not work out either.

How can this be achieved? What am I missing? Any hints highly appreciated!


Solution

  • If you're using Bash you can use a function in your npm-script.

    For instance:

    "scripts": {
        ...
        "prod": "func() { gulp build --production \"$1\" && webpack --env.config=production \"$1\"; }; func"
    }
    

    However, for a cross-platform solution you'll need to consider invoking a nodejs script which exec's the commands - in a similar way shown in Solution 2 of my answer here.