玩转npm script

399 阅读1分钟

npm run 的原理

npm run xxx 的基本步骤:

  1. package文件中读取scripts对象里面的配置
  2. 读取配置后,以npm run的第一个参数作为键在配置中获取对应的值,作为接下来执行的命令
  3. 用默认的shell执行这个命令

npm run xxx, xxx的命令来自哪里:

npm在执行script之前会把node_modules/.bin加到环境变量里,这个命令就来源于这里

npm 内置命令:

teststart

npm 管理命令

npm script 串行 (&&)

{
  "test": "npm run lint:js && npm run lint:css && npm run mocha tests/"
}

npm script 并行(&)

{
  "test": "npm run lint:js & npm run lint:css & npm run macha tests/"
}

npm 更好的执行命令管理工具

npm run all

串行

{
  "test": "npm-run-all lint:js lint:css macha tests/"
}

并行

{
  "test": "npm-run-all --parallel lint:js lint:css macha tests/"
}

通配符

{
	"test": "npm-run-all run lint:* macha tests/ "
}

npm script 的传参

npm script 传参

npm run <command> [-- <args>...]