npm-run-all ,这个工具是为了解决官方的 npm run 命令无法同时运行多个脚本的问题,它可以把诸如 npm run clean && npm run build:css && npm run build:js && npm run build:html 的一长串的命令通过 glob 语法简化成 npm-run-all clean build:* 这样精致小巧的模样。再者大家也知道 shell 的 & 语法实际上是不支持 cmd 的,为了跨平台也最好使用这样的第三方库来提供支持。preact 中就是用来对数量繁多的子模块进行并行构建和顺序测试。
安装
# 安装
npm install npm-run-all --save-dev
# 查看帮助信息
npm-run-all -h
参数
- --parallel:并行运行多个命令,例如:npm-run-all --parallel lint build
- --serial:多个命令按排列顺序执行,例如:npm-run-all --serial clean lint build:**
- --continue-on-error:是否忽略错误,添加此参数 npm-run-all 会自动退出出错的命令,继续运行正常的
- --race:添加此参数之后,只要有一个命令运行出错,那么 npm-run-all 就会结束掉全部的命令
这个包提供三个命令,分别是 npm-run-all run-s run-p,其中后两个都是 npm-run-all 带参数的简写,分别对应串行和并行。
顺序执行
npm-run-all clean lint build
并行执行
npm-run-all --parallel lint build
混合执行
npm-run-all clean lint --parallel watch:html watch:js
这段命令首先按顺序执行 clean lint 两个脚本,然后同时执行 watch:html 和 watch:js 的任务。
npm-run-all a b --parallel c d --sequential e f --parallel g h i
# or
npm-run-all a b --parallel c d --serial e f --parallel g h i
这段命令通过以下顺序执行:
- 顺序运行 a 和 b;
- 然后同时运行 c 和 d;
- 再依次运行 e 和 f;
- 最后同时执行 g, h, i。
示例
"scripts": {
"install": "npm-run-all --serial install:*",
"install:main": "cd main && npm i",
"install:sub-vue": "cd sub-vue && npm i",
"install:sub-react": "cd sub-react && npm i",
"install:sub-html": "cd sub-html && npm i",
"start": "npm-run-all --parallel start:*",
"start:sub-react": "cd sub-react && npm start",
"start:sub-vue": "cd sub-vue && npm start",
"start:main": "cd main && npm start",
"start:sub-html": "cd sub-html && npm start",
"build": "npm-run-all build:* && bash ./scripts/bundle.sh",
"build:sub-react": "cd sub-react && npm run build",
"build:sub-vue": "cd sub-vue && npm run build",
"build:sub-html": "cd sub-html && npm run build",
"build:main": "cd main && npm run build",
"test": "echo \"Error: no test specified\" && exit 1"
},
执行npm start 会并行执行下面的4个start命令