ajmajmajma ajmajmajma - 1 year ago 600
Javascript Question

npm script, copy package.json to dist when bundling

I am trying to add a second part to my npm bundle script. The first part runs great, however I am trying to copy in 3 files along with the bundle.

So right now I have :

"bundle": "NODE_ENV=production webpack --output-file bundledFile.js && cp package.json dist/",

NODE_ENV=production webpack --output-file bundledFile.js
works great by itself. The part that is not working is the
&& cp package.json dist/
, I would like the script to copy my package.json (along with 2 other files actually, but just starting with this one) to the dist folder. Brand new to these scripts, any idea how to fix? Appreciate any advice, thanks!

Answer Source

The syntax should work (and seems to, looking at your comments). I would suggest splitting your npm scripts across multiple points, though:

  "bundle": "NODE_ENV=production webpack --output-file bundledFile.js",
  "copy": "cp package.json dist/ && cp README.md dist/ && cp .npmrc dist/",
  "build": "npm run bundle && npm run copy"

In order to be cross-platform compatible (cp is not typically available on windows), I would also suggest adding a build file somewhere such as ./tools/copy-distrubution-files.js which would make use of fs to copy the necessary files, then call it in the npm scripts with node ./tools/copy-distribution-files.js. That will be (mostly) platform independent (you still have to assume that node is available as the nodejs executable, but that seems fairly reasonable to me).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download