2

I have a webpack config with currently three parameters, which works when I invoke it in the following way:

webpack --env.p1 = "p1_value" --env.p2 = "p2_value" --env.p3 = "p3_value" 

Now I want to encapsulate that webpack command in the following package.json script:

"scripts": {

        "prod": "webpack --env.p1 --env.p2 --env.p3"
    }

How do I have to change that script such that I can invoke it from the CLI in the following way

npm run prod p1="p1_value" p2="p2_value" p3="p3_value" 

(where the named parameters are indispensible, because I need to be able to work with default values inside the webpack config?)

Oblomov
  • 8,953
  • 22
  • 60
  • 106
  • 1
    I believe you have to split the npm script arguments and the CLI command arguments by a double dash `--`. Example : `npm run prod -- --env.p1 --env.p2 --env.p3` – Seblor Feb 19 '18 at 13:12

1 Answers1

3

You can pass any arguments provided to the npm command through to webpack using the placeholder ${@:1}.

package.json

"scripts": {
  "prod": "webpack ${@:1}"
}

From the command line, add parameters to be passed through using -- as separator, like so:

npm run prod -- --env.p1="p1_value" --env.p2="p2_value" --env.p3="p3_value" 
Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
  • 1
    Thanks - is there shorter version for the npm cli? I don't mind verbosity of the script, it's encapsulated anyway. The CLI part is what matters, because that's what clients need to invoke regularly. So is there a way to use npm run prod p1="p1_value" p2="p2_value" p3="p3_value" etc, as I had asked in the original question? – Oblomov Feb 19 '18 at 13:50
  • 1
    I'm afraid not, the double dashes are the “npm way” of passing parameters to npm scripts – Patrick Hund Feb 19 '18 at 13:56
  • @PatrickHund what npm version is used? I tried it & it gives me "Bad substitution". What I tried exactly is `"foo": "echo ${@:1}"` and then called `yarn run foo bar`. Tried it with npm also with the double dashes `npm run foo -- bar`. Update: when used `"foo": "echo $1"`, it worked. Any idea why? – Hossam El-Deen Aug 27 '18 at 05:18
  • @HossamEl-Deen I've just tested it, it works for me with Node 8.9.4, npm 5.8.0. It seems you can leave out the `${@:1}` bit in `package.json`, try if it works for you without it. Make sure you use the extra double dashes as separator between the `npm run` command and the parameters. – Patrick Hund Aug 27 '18 at 06:57
  • @PatrickHund Thanks. It doesn't throw the error if removed `{@:1}`. Have same node & npm versions. I'm not fairly confident the problem is that the script is run by `sh`, not `bash`. Seems like this syntax isn't supported in all shells. E.g.,: https://stackoverflow.com/a/23277604/6690391 – Hossam El-Deen Aug 27 '18 at 09:34
  • Indeed, changed shells like in [this answer](https://askubuntu.com/a/328368/725387) & the error is gone. However, now with the script being `echo ${@:3}` and calling it with `npm run foo -- bar baz hiw ihrwi hiht` just prints them all as if the `${@:3}` has no effect. Any idea why, @PatrickHund? – Hossam El-Deen Aug 27 '18 at 09:50