简体   繁体   English

npm 脚本,打包时将 package.json 复制到 dist

[英]npm script, copy package.json to dist when bundling

I am trying to add a second part to my npm bundle script.我正在尝试将第二部分添加到我的 npm 包脚本中。 The first part runs great, however I am trying to copy in 3 files along with the bundle.第一部分运行良好,但是我试图将 3 个文件与捆绑包一起复制。

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. NODE_ENV=production webpack --output-file bundledFile.js本身NODE_ENV=production webpack --output-file bundledFile.js 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.不起作用的部分是&& cp package.json dist/ ,我希望脚本将我的 package.json (实际上还有其他 2 个文件,但只是从这个文件开始)复制到 dist 文件夹。 Brand new to these scripts, any idea how to fix?这些脚本是全新的,知道如何修复吗? Appreciate any advice, thanks!感谢任何建议,谢谢!

The syntax should work (and seems to, looking at your comments).语法应该可以工作(并且似乎可以查看您的评论)。 I would suggest splitting your npm scripts across multiple points, though:不过,我建议将您的 npm 脚本拆分为多个点:

{
  "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 .为了跨平台兼容( cp通常在 Windows 上不可用),我还建议在某处添加一个构建文件,例如./tools/copy-distrubution-files.js这将利用fs 复制必要的files ,然后在 npm 脚本中使用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).这将(主要)独立于平台(您仍然必须假设node可用作 nodejs 可执行文件,但这对我来说似乎相当合理)。

If you're running on windows use the following command:如果您在 Windows 上运行,请使用以下命令:

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

copy instead of cp.复制而不是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此外,如果您喜欢,有一个很好的插件可以在每次构建之前和之后运行 bash 命令

对我来说最快的方法是在 package.json 脚本中引用 powershell,如下所示:

"copyFile": "@powershell copy './source/package.json' './deploy'",

To copy folders and files in windows just use要在 Windows 中复制文件夹和文件,只需使用

xcopy git\\* dist\\ /e /i /h

I think this might help someone.我认为这可能对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM