You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{
"scripts": {
"prebuild": "echo I run before the build script",
"build": "vue-cli-service build",
"postbuild": "echo I run after the build script"
}
}
当用户执行 npm run build 时会按如下顺序执行
npm run prebuild && npm run build && npm run postbuild
默认钩子
prepublishpostpublish
preinstallpostinstall
preuninstallpostuninstall
preversionpostversion
pretestposttest
prestoppoststop
prestartpoststart
prerestartpostrestart
注意
自定义的脚本命令也可以加上 pre 和 post 钩子
比如 deploy 也有 predeploy 和 postdeploy 钩子
使用 npm 脚本钩子
钩子说明
在
npm
脚本中有pre
和post
两个钩子比如
build
脚本命令的钩子就是prebuild
和postbuild
当用户执行
npm run build
时会按如下顺序执行默认钩子
prepublish
postpublish
preinstall
postinstall
preuninstall
postuninstall
preversion
postversion
pretest
posttest
prestop
poststop
prestart
poststart
prerestart
postrestart
注意
自定义的脚本命令也可以加上
pre
和post
钩子比如
deploy
也有predeploy
和postdeploy
钩子双重的
pre
和post
无效,比如
prepretest
和postposttest
相关资料
npm scripts 使用指南
实战 —— 检测并更新依赖包
当公司内部维护的
npm
包升级时,可能会因为各种原因导致业务使用方更新不及时,这时我们可以在业务项目中编写npm
脚本钩子,在执行start
脚本时对这些npm
包做检测并更新添加 pre 脚本
在
scripts
目录下新建checkDependence.js
,并写入如下代码这里用
dayjs
和axios
举栗修改
package.json
的scripts
{ "scripts": { ... + "prestart": "node ./scripts/checkDependence.js" } }
npm 相关命令说明
npm view <name>
查询并返回指定包的所有信息npm view <name> versions
查询并返回指定包的所有版本信息(数组格式)npm view <name> version
查询并返回指定包的最新版本号相关资料
npm-view
The text was updated successfully, but these errors were encountered: