svendelle
svendelle

Reputation: 31

How can I pass variable data to package.json file scripts as parameters?

We rename our assets directory on every single git push to handle browser caching issues. This mean that we store a random variable in a config.json file in our project.

I'm trying to move from gulp to npm as a build process, and therefore need to access this stored variable somehow from within the package.json file.

How would I go about such a task, and is it even possible?

"scripts": {
"build-offers": "uglifyjs src/pages/offers/*.js -mc > [HERE I NEED TO PREFIX THE OUTPUT FOLDER USING THE config.json FILE CONTENT] assets/offers.js",
"offers": "npm run build-offers"
},

Upvotes: 1

Views: 1397

Answers (1)

marcel
marcel

Reputation: 3149

You can acces environment variables in your scripts, but i think, that it isn't possible to read such variables from an other file with pure package scripts. Here is a way, you could do it within your package.json.

"config": {
  "prefix": "prefix"
},
"scripts": {
  "build-offers": "uglifyjs src/pages/offers/*.js -mc > %npm_package_config_prefix% assets/offers.js",
  ...
}

NOTE: The above version only works under windows.

Upvotes: 1

Related Questions