本文介绍了npm脚本,捆绑时将package.json复制到dist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的npm捆绑脚本中添加第二部分。第一部分运行得很好,但我试图将3个文件与包一起复制。

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.

所以现在我有:

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

NODE_ENV =生产webpack --output-file bundledFile.js 有效本身很棒。无效的部分是&& cp package.json dist / ,我希望脚本能将我的package.json(以及其他2个文件实际上,但只是从这一个开始)复制到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!

推荐答案

语法应该有效(并且好像,看看你的评论)。我建议将你的npm脚本分成多个点,但是:

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"
}

为了实现跨平台兼容(Windows上通常不提供 cp ),我还建议在某处添加构建文件,例如。 /tools/copy-distrubution-files.js 这将使用 fs 来,然后使用节点在npm脚本中调用它./tools/copy - 配送-files.js 。这将(通常)与平台无关(你仍然必须假设节点可用作nodejs可执行文件,但这对我来说似乎相当合理)。

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

这篇关于npm脚本,捆绑时将package.json复制到dist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 20:21