Javascript npm 脚本,打包时将 package.json 复制到 dist
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38858718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
npm script, copy package.json to dist when bundling
提问by ajmajmajma
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.
我正在尝试将第二部分添加到我的 npm 包脚本中。第一部分运行良好,但是我试图将 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. 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!
在NODE_ENV=production webpack --output-file bundledFile.js
通过自身的伟大工程。不起作用的部分是&& cp package.json dist/
,我希望脚本将我的 package.json(实际上还有其他 2 个文件,但只是从这个文件开始)复制到 dist 文件夹。这些脚本是全新的,知道如何修复吗?感谢任何建议,谢谢!
回答by dvlsg
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
. 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).
为了跨平台兼容(cp
通常不适用于Windows),我也建议增加一个构建文件的地方,例如./tools/copy-distrubution-files.js
它会利用fs
到复制必需的文件,然后把它与故宫的脚本node ./tools/copy-distribution-files.js
。这将(主要)独立于平台(您仍然必须假设它node
可用作 nodejs 可执行文件,但这对我来说似乎相当合理)。
回答by IsraGab
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. Don't forget to use ""for each path. and if you need to define a long path, don't use / (slashes) but \ (backslashes)
复制而不是cp。不要忘记对每个路径使用""。如果你需要定义一个长路径,不要使用 /(斜杠)而是 \(反斜杠)
like:
喜欢:
copy "devices\VS-88UT\index.html" "devices\VS-88UT\dist"
Also, if you prefere ther is a nice pluginto run bash command before and after each build
此外,如果您更喜欢,有一个很好的插件可以在每次构建之前和之后运行 bash 命令
回答by sisco
To copy folders and files in windows just use
要在 Windows 中复制文件夹和文件,只需使用
xcopy git\* dist\ /e /i /h
I think this might help someone.
我认为这可能会帮助某人。
回答by Dustin Spengler
The quickest way for me was to reference powershell in a package.json script like this:
对我来说最快的方法是在 package.json 脚本中引用 powershell,如下所示:
"copyFile": "@powershell copy './source/package.json' './deploy'",