package scripts在前端项目的使用
前端的项目往往依赖了很多打包、部署工具,比如grunt,gulp,webpack.....,在这么多打包、部署工具里,有这各自的命令,这样给项目带来了很多烦恼,不同的项目不同的命令,有没有办法统一接口呢?那么可以考虑把命令都封装到npm scripts里。
之前都是知道个大概,抽空索性都了解下。
npm run
npm run xxx,可以执行package.json里script对应的命令,并且是shell脚本。但是在执行的时候有一个小处理。
npm run新建的这个 Shell,会将当前目录的node_modules/.bin子目录加入PATH变量,执行结束后,再将PATH变量恢复原样。
但是前提是在node_moduels/.bin 目录下要有对应到 node_modules 里的soft link。比如
$ cd node_modules/.bin
$ ls -al
$ lrwxr-xr-x 1 jan staff 25 Jun 3 17:03 webpack -> ../webpack/bin/webpack.js
hook
在每个命令前都会执行对应命令的pre+scriptname 脚本,每个命令结束后会执行对应买了的post+scriptname脚本。如果没有定义,则不会执行对应的pre ,post命令。
比如我们在scripts里定义。
scripts: {
"prebuild" : "echo \" this is pre build \"",
"build" : "echo \" this is build \"",
"postbuild" : "echo \" this is post build \""
}
npm run build
会输出
> test-npm-script@1.0.0 prebuild /Users/jan/workspace/web/test-npm-script
> echo " this is pre build "
this is pre build
> test-npm-script@1.0.0 build /Users/jan/workspace/web/test-npm-script
> echo " this is build "
this is build
> test-npm-script@1.0.0 postbuild /Users/jan/workspace/web/test-npm-script
> echo " this is post build "
this is post build
这点很棒,你可以在执行某些命令前、后做一些操作,比如build前清空目录,build后做一些压缩之类的事
默认的脚本
npm会默认设置一些script的值,比如start和install,当然你可以覆盖这2个值。
start
如果你在项目里根目录有一个server.js,然后你又没自己定义start script,那么npm run start,就会执行server.js
server.js
console.log("this is server.js");
$ npm run start
> this is server.js
当然可以设置prestart 和poststart脚本
scripts : {
"prestart" : "echo \" this is pre start \"",
"poststart" : "echo \" this is post start \""
}
$ npm run start
> test-npm-script@1.0.0 prestart /Users/jan/workspace/web/test-npm-script
> echo " this is pre start "
this is pre start
> test-npm-script@1.0.0 start /Users/jan/workspace/web/test-npm-script
> node server.js
this is server.js
> test-npm-script@1.0.0 poststart /Users/jan/workspace/web/test-npm-script
> echo " this is post start "
this is post start
install
当你的模块被安装时,会默认执行这个脚本。前提是你没自己定义install脚本,然后有一个binding.gyp 文件。具体的npm会执行
"install": "node-gyp rebuild"
这个脚本可以帮助node模块编译 c++ 模块。
生命周期事件
prepublish:在模块被发布前,其实在你安装本地包也会触发
publish, postpublish:在发布后执行
preinstall:模块被安装前执行
install, postinstall:模块安装后
preuninstall, uninstall:模块被卸载前执行
postuninstall:模块卸载后执行
preversion, version:获取版本号前执行
postversion:获取版本号之后执行
pretest, test, posttest:执行test脚本时会执行
prestop, stop, poststop:在脚本结束时执行
prestart, start, poststart:调用start时执行
prerestart, restart, postrestart:在执行restart时会调用
restart脚本比较特殊,如果你设置了restart脚本则只会执行:prerestart, restart, postrestart,但是如果你没有设置restart,则会执行stop,start脚本。比如我们设置如下脚本配置。
"scripts": {
"prestop" : "echo \" this is pre stop \"",
"stop" : "echo \" this is stop \"",
"poststop" : "echo \" this is post stop \"",
"prestart" : "echo \" this is pre start \"",
"start" : "echo \" this is start \"",
"poststart" : "echo \" this is post start \"",
"prerestart" : "echo \" this is pre start \"",
"postrestart" : "echo \" this is post start \"",
}
npm run restart
会输出
this is pre restart
this is pre stop
this is stop
this is post stop
this is pre start
this is start
this is post start
this is post restart
简写
有几个简写,不一定一些写全npm run xxx
npm start === npm run start
npm stop === npm run stop
npm test === npm run test
npm restart === npm run rerestart
一个完整的package
{
"name": "test-npm-script",
"version": "1.0.0",
"description": "",
"main": "index.js",
"bin" : {
"main":"bin/main.js",
"dev":"bin/dev.js"
},
"scripts": {
"pretest" : "echo \" this is pre test \"",
"test" : "echo \" this is test \"",
"posttest" : "echo \" this is post test \"",
"prerestart" : "echo \" this is pre restart \"",
"restart" : "echo \" this is restart \"",
"postrestart" : "echo \" this is post restart \"",
"prestop" : "echo \" this is pre stop \"",
"stop" : "echo \" this is stop \"",
"poststop" : "echo \" this is post stop \"",
"prestart" : "echo \" this is pre start \"",
"start" : "echo \" this is start \"",
"poststart" : "echo \" this is post start \"",
"preinstall" : "echo \" this is pre install \"",
"install" : "echo \" this is install \"",
"postinstall" : "echo \" this is post install \"",
"prepublish" : "echo \" this is pre install \"",
"publish" : "echo \" this is publish \"",
"postpublish" : "echo \" this is post install \"",
"preuninstall" : "echo \" this is pre uninstall \"",
"uninstall" : "echo \" this is uninstall \"",
"postuninstall" : "echo \" this is post uninstall \"",
"prebuild" : "echo \" this is pre build \"",
"build" : "echo \" this is build \"",
"postbuild" : "echo \" this is post build \""
},
"author": "",
"license": "ISC"
}
参考资料
package scripts在前端项目的使用的更多相关文章
- 前后端分离之前端项目构建(grunt+require+angular)
前言 前段时间做了一个项目,前端开发页面,然后把代码给到后端同学,后端同学通过vm再来渲染页面.后来才发现,这种方式简直是太low了,因为前端代码在服务端同学那里,每次前端需要更改的时候都需要去到服务 ...
- npm打包前端项目太慢问题分析以及暂时解决方案
npm build 打包前端项目实际上是执行 node build/build.js,但是随着项目的依赖包越来越多,项目打包时间不断延长,为了改善这个问题,需要从node入手 暂时解决方案:扩大nod ...
- nodejs 前端项目编译时内存溢出问题的原因及解决方案
现象描述 昨天用webpack打包Vue的项目时,node内存溢出而停止build项目,即是项目构建过程中频繁报内存溢出:FATAL ERROR: CALL_AND_RETRY_LAST Alloca ...
- 如何编写package.json配置NodeJS项目的模块声明
在NodeJS项目中,用package.json文件来声明项目中使用的模块,这样在新的环境部署时,只要在package.json文件所在的目录执行 npm install 命令即可安装所需要的模块. ...
- Webpack构建前端项目
前言 公司据说要搞前后端分离,趁这两天项目完成的差不多,抓紧时间学习一下前端知识 现在流行前端项目工程化,那么第一个问题就是如何创建工程(项目),第一次玩webpack 通过 NPM 创建项目 # 创 ...
- 基于node的前端项目编译时内存溢出问题
解决方法: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory JavaScript堆内存不足,这里说的 Jav ...
- 用gulp构建你的前端项目
前言 前端技术发展日新月异,随着模块化.组件化的提出,前端变得越来越复杂,静态资源越来越多,那么对静态资源的处理,如压缩,合并,去掉调试信息.. 如果还是人工去处理,效率非常之低且还容易出错,于是自动 ...
- 前端项目构建工具---Grunt
什么是Grunt? grunt是javascript项目构建工具,在grunt流行之前,前端项目的构建打包大多数使用ant.(ant具体使用 可以google),但ant对于前端而言,存在不友好,执行 ...
- [front]有效开展一个前端项目
今天的前端如果没有用到 npm,效率是比较低的:所以要从使用的工具来讲. 1. 一切都依赖于 nodejs: 下载一个 linux 的源码包就可以开始安装了. $ wget https://nodej ...
随机推荐
- visual c++ 动态链接库调用总结
由于每次使用动态链接库的时候都要重新去查资料,查调用方式,有些烦躁,本人抽点时间在此做个总结,希望可以对需要的朋友有所帮助. 1,显式加载方式加载动态链接库 简单易懂,随掉随用 (1) ...
- SVN下错误集锦
SVN下错误集锦 一SVN下的文件被locked不能update和commit 最近做项目的时候,遇到这个问题,SVN下的文件被locked不能update和commit.其提示如下: 解决办法:执行 ...
- 在VMware的Linux系统上安装Redis
在VMware的Linux系统上安装Redis 具体过程如下: 下载,解压和编译: 在执行make的时候报错,具体报错信息如下: zmalloc.o: In function `zmalloc_use ...
- C#对html的操作
1,获取相对路径的html然后保存到本地路径 /// <summary> /// Html方法 /// </summary> /// <returns></r ...
- socket网络编程快速上手(二)——细节问题(2)
2.TCP数据包接收问题 对初学者来说,很多都会认为:客户端与服务器最终的打印数据接收或者发送条数都该是一致的,1000条发送打印,1000条接收打印,长度都为1000.但是,事实上并不是这样,发送打 ...
- ASP.NET Web API下的HttpController激活:程序集的解析
ASP.NET Web API下的HttpController激活:程序集的解析 HttpController的激活是由处于消息处理管道尾端的HttpRoutingDispatcher来完成的,具体来 ...
- webservice的调用方法
一.WebService在cs后台程序中的调用 A.通过命名空间和类名直接调用 示例: WebService ws = new WebService(); string s = ws.HelloWor ...
- 自己动手用maven构建基于SSI的java EE应用
上篇跟大家聊了聊maven的简单使用,之前也写了一篇搭建基于SSI(struts2,spring,ibatis)的javaEE开发环境的文章,但是那篇只是给初学者搭建一个简单的SSI应用的框架,其实我 ...
- Linux下逻辑卷创建与管理
用虚拟机加一块硬盘后,查看硬盘状况,使用fdisk-l命令: [root@jerrybj ~]# fdisk -l Disk /dev/sda: 21.4 GB, 21474836480 bytes ...
- Linux IO控制命令生成
在驱动程序里, ioctl() 函数上传送的变量 cmd 是应用程序用于区别设备驱动程序请求处理内容的值.cmd除了可区别数字外,还包含有助于处理的几种相应信息. cmd的大小为 32位,共分 4 个 ...