Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm script, copy package.json to dist when bundling

Tags:

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/",

The 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!

like image 243
ajmajmajma Avatar asked Aug 09 '16 19:08

ajmajmajma


People also ask

Is there a way to copy distribution files to NPM?

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.

How to copy files or directory in NodeJS NPM scripts?

How to copy files or directory in nodejs npm scripts First, Go to your application project, Install copyfiles dependency with below command Note: please add this as devDependencies using –save-dev npm install --save-dev copyfiles Or you can also copy below code in package..json

How do I add a script to an NPM package?

In the package.json file, There is a script tag, add the below line of code source: - input source directory or regular expression files target: - files copied to a target directory Finally, You can script by calling the npm run copy command in terminal Suppose you have a javascript file in the src/assets folder.

How to bundle npm packages in a tarball file?

In cases where you need to preserve npm packages locally or have them available through a single file download, you can bundle the packages in a tarball file by specifying the package names in the bundledDependencies array and executing npm pack. If we define a package.json like this:


2 Answers

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).

like image 138
dvlsg Avatar answered Sep 21 '22 14:09

dvlsg


If you're running on windows use the following command:

"copy": "copy \"package.json\" \"dist\" && copy \"README.md\" \"dist\" && copy \".npmrc\" \"dist\""

copy instead of cp. Don't forget to use "" for each path (escape them with \ within the quoted command). and if you need to define a long path, don't use / (slashes) but \ (backslashes)

like:

copy "devices\\VS-88UT\\index.html" "devices\\VS-88UT\\dist"

Also, if you prefere ther is a nice plugin to run bash command before and after each build

like image 38
IsraGab Avatar answered Sep 18 '22 14:09

IsraGab