IOriens / ioriens.github.io

https://Junjie.xyz
12 stars 2 forks source link

npm 钩子使用指南 #4

Open IOriens opened 6 years ago

IOriens commented 6 years ago

来自同事 @pgfxm 的分享

npm 脚本有prepost两个钩子。举例来说,build脚本命令的钩子就是prebuildpostbuild:

{
    "scripts": {
        "prebuild": "echo I run before the build script",
        "build": "webpack",
        "postbuild": "echo I run after the build script"
    }
}

用户执行npm run build的时候,会自动按照下面的顺序执行。

npm run prebuild && npm run build && npm run postbuild

因此,可以在这两个钩子里面,完成一些准备工作和清理工作。下面是一个例子。

{
    "scripts": {
        "clean": "rm -rf ./dist && mkdir dist",
        "prebuild": "npm run clean",
        "build": "webpack"
    }
}

npm 默认提供下面这些钩子: