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 ...
随机推荐
- 后台XML处理
public void GetInfo() { string message = @"<?xml version='1.0' encoding='utf-8' ...
- Web API 2
Asp.Net Web API 2 官网菜鸟学习系列导航[持续更新中] 前言 本来一直参见于微软官网进行学习的, 官网网址http://www.asp.net/web-api.出于自己想锻炼一下学 ...
- c# 即使服务又是可执行程序的代码实现
先看下代码 namespace UpdaterServer { class Program { static void Main(string[] args) { ) { ServiceBase[] ...
- [RM 状态机详解2] RMAppAttempt状态机详解
摘要 本文详细描述RMAppAttempt状态机内的状态与其转换关系,分析的代码基于Apache社区Hadoop最新的2.3.0版本. RMAppAttempt状态机 在RM中,一个RMApp可能对于 ...
- C#单例模式的三种写法 以及 继承面试题
1.没有考虑线程安全 public class Singleton { private static Singleton _instance = null; private Singleton(){} ...
- wpf中数据绑定(Datacontext)的应用
在winform开发中,我们常用到ado.net进行数据绑定,在编程技术日新月异的今天,这种繁杂的数据绑定方式已不能再适合开发人员,于是微软推出了wpf,更炫的界面美化,更简洁地编写控件,在wpf中使 ...
- python 爬虫总结【转】
1.基本抓取网页 get方法 import urllib2 url = "http://www.baidu.com" response = urllib2.urlopen(url) ...
- breakpad是Google开源的一套跨平台工具
windows下捕获dump之Google breakpad_client的理解 breakpad是Google开源的一套跨平台工具,用于dump的处理.很全的一套东西,我这里只简单涉及break ...
- poj1269
基础题,直线间关系 #include <iostream> #include <math.h> #include <iomanip> #define eps 1e- ...
- Oracle用脚本语言导入SCOTT用户
许多Oracle新手都遇到这样的问题,安装Oracle之后没有SCOTT用户,那就自己加入吧,打开Oracle 命令窗口复制下面SQL脚本直接输入就行了,包含了测试学习的DEPT.EMP.BONUS. ...