2

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!

mexn
  • 78
  • 2
  • 7
  • It's not completely clear what you want to achieve. Are you trying to pass the argument (i.e. `--env.theme=themename`) to both the _gulp_ and _webpack_ command in your `prod` script via the command line? You mention _"...but gulp does not take care of this"_ - what do you mean by that?. If your npm script was just `"prod": "gulp build --production"` and you invoke it via your CLI like this: `npm run prod -- --env.theme=themename` does _gulp_ do what you want it to do? – RobC Jul 30 '18 at 11:23
  • @RobC Thanks for reaching out. Basically I want to pass information from the command line to `npm run prod` in a way, that it can be processed in my gulpfile.js and in my webpack.config.js, so that I'm able to adjust the building process accordingly. And to your specific question: If I adjust the npm script and the CLI call accordingly, the information is available in the gulpfile.js, yes. – mexn Jul 30 '18 at 13:57

1 Answers1

4

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.

RobC
  • 22,977
  • 20
  • 73
  • 80