npm script了解多少?
# 项目初始化 npm init
npm 为我们提供了快速创建 package.json 文件的命令 npm init,执行该命令会问几个基本问题,如包名称、版本号、作者信息、入口文件、仓库地址、许可协议等,多数问题已经提供了默认值
{
"name": "npm-script-demo",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"eslint": "eslint **.js"
},
"devDependencies": {
"eslint": "latest"
},
"author": "",
"license": "ISC",
"keywords": [],
"description": ""
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 运行多个 npm script
# 多 npm script 串行
方式很简单,只需要用 && 符号把多条 npm script 按先后顺序串起来即可。
{
"scripts": {
"test": "npm run lint:js && npm run lint:css && npm run lint:json && npm run lint:markdown && mocha tests/"
}
}
1
2
3
4
5
2
3
4
5
# 多 npm scripts 并行
{
"scripts": {
"test": "npm run lint:js & npm run lint:css & npm run lint:json & npm run lint:markdown & mocha tests/"
}
}
1
2
3
4
5
2
3
4
5
# 多命令管理 npm-run-all
npm run lint:js & npm run lint:css & npm run lint:json & npm run lint:markdown & mocha tests/ & wait
加上 wait 的额外好处是,如果我们在任何子命令中启动了长时间运行的进程,比如启用了 mocha 的 --watch 配置,可以使用 ctrl + c 来结束进程,如果没加的话,你就没办法直接结束启动到后台的进程。
npm-run-all:实现更轻量和简洁的多命令运行,还支持通配符匹配分组的 npm script,而且不需在后面加& wait
{
// 串行
"test": "npm-run-all lint:js lint:css lint:json lint:markdown mocha",
// 并行
"test": "npm-run-all --parallel lint:* mocha",
"test": "npm-run-all lint:* mocha"
}
1
2
3
4
5
6
7
2
3
4
5
6
7
# 给npm script 传参和注释
# 传参
上次更新: 2022/03/09, 00:16:11