前端的项目往往依赖了很多打包、部署工具,比如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在前端项目的使用的更多相关文章

  1. 前后端分离之前端项目构建(grunt+require+angular)

    前言 前段时间做了一个项目,前端开发页面,然后把代码给到后端同学,后端同学通过vm再来渲染页面.后来才发现,这种方式简直是太low了,因为前端代码在服务端同学那里,每次前端需要更改的时候都需要去到服务 ...

  2. npm打包前端项目太慢问题分析以及暂时解决方案

    npm build 打包前端项目实际上是执行 node build/build.js,但是随着项目的依赖包越来越多,项目打包时间不断延长,为了改善这个问题,需要从node入手 暂时解决方案:扩大nod ...

  3. nodejs 前端项目编译时内存溢出问题的原因及解决方案

    现象描述 昨天用webpack打包Vue的项目时,node内存溢出而停止build项目,即是项目构建过程中频繁报内存溢出:FATAL ERROR: CALL_AND_RETRY_LAST Alloca ...

  4. 如何编写package.json配置NodeJS项目的模块声明

    在NodeJS项目中,用package.json文件来声明项目中使用的模块,这样在新的环境部署时,只要在package.json文件所在的目录执行 npm install 命令即可安装所需要的模块. ...

  5. Webpack构建前端项目

    前言 公司据说要搞前后端分离,趁这两天项目完成的差不多,抓紧时间学习一下前端知识 现在流行前端项目工程化,那么第一个问题就是如何创建工程(项目),第一次玩webpack 通过 NPM 创建项目 # 创 ...

  6. 基于node的前端项目编译时内存溢出问题

    解决方法: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory JavaScript堆内存不足,这里说的 Jav ...

  7. 用gulp构建你的前端项目

    前言 前端技术发展日新月异,随着模块化.组件化的提出,前端变得越来越复杂,静态资源越来越多,那么对静态资源的处理,如压缩,合并,去掉调试信息.. 如果还是人工去处理,效率非常之低且还容易出错,于是自动 ...

  8. 前端项目构建工具---Grunt

    什么是Grunt? grunt是javascript项目构建工具,在grunt流行之前,前端项目的构建打包大多数使用ant.(ant具体使用 可以google),但ant对于前端而言,存在不友好,执行 ...

  9. [front]有效开展一个前端项目

    今天的前端如果没有用到 npm,效率是比较低的:所以要从使用的工具来讲. 1. 一切都依赖于 nodejs: 下载一个 linux 的源码包就可以开始安装了. $ wget https://nodej ...

随机推荐

  1. iOS基础 - 核心动画

    一.核心动画 l 核心动画基本概念 l 基本动画 l 关键帧动画 l 动画组 l 转场动画 l Core Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事 ...

  2. Python语言在企业级应用上的十大谬误

    英文原文:https://www.paypal-engineering.com/2014/12/10/10-myths-of-enterprise-python/ 翻译原文:http://www.os ...

  3. How to upload a file in MVC4

    Uploading a file in Asp.Net MVC application is very easy. The posted file is automatically available ...

  4. windows 服务器系统日志分析及安全

    一.利用Windows自带的防火墙日志检测入侵 下面是一条防火墙日志记录 2005-01-1300:35:04OPENTCP61.145.129.13364.233.189.104495980 200 ...

  5. UI基础UIWindow、UIView

    UI基础UIWindow.UIView 在PC中,应用程序多是使用视窗的形式显示内容,手机应用也不例外,手机应用中要在屏幕上显示内容首先要创建一个窗口承载内容,iOS应用中使用UIWindow.UIV ...

  6. 相对于C#,PHP中的个性化语法

    相对于C#,PHP中的个性化语法 背景 今天把PHP的基本语法结构熟悉了一下,包括:变量.类型.常量.运算符.字符串.作用域和函数等,本文列举一些我需要强化记忆的结构(和C#不同). 一些个性化的结构 ...

  7. iOS 7 beta4 体验

    iOS 7 beta4终于来了,安装后感觉稳定了不少.下面列几点我个人感受比较深得地方. 1.锁屏界面有滑动方向箭头了,而且“滑动来解锁”几个字也有动态颜色变化,让人不再迷惑该往那边滑动了. 2.通知 ...

  8. 一个吊丝android个人开发者的逆袭之路

    转眼间,一年多过去了,记得我开发第一款android应用的时候,那是在前年的冬天,我本人是做java的,android的学习和开发完全是业余爱好,从前年上半年到前年下半年大约花了半年的业余时间把and ...

  9. c# 关于10进制和16进制转换以及显示

    直接举例说明: int i = 15;//一个10进制数 string txt = Convert.ToString(i,16);//将上面10进制以16进制形式显示为f string s = &qu ...

  10. Java的23种设计模式

    1.FACTORY—追MM少不了请吃饭了,麦当劳的鸡翅和肯德基的鸡翅都是MM爱吃的东西,虽然口味有所不同,但不管你带MM去麦当劳或肯 德基,只管向服务员说“来四个鸡翅”就行了.麦当劳和肯德基就是生产鸡 ...