用它的最主要的原因,是自己写的开源项目,需要用到自动化测试,压缩以及多个源码文件整合到一个文件中去。当然,选择用它而不是其他类似工具的原因仅是AngularJS使用了它。
本blog的内容来源于gruntJs中文社区+自己使用时的补充。
依赖
- nodeJs
- npm
这两个不必多说,不知道的也不会知道gruntJs。
安装
npm install -g grunt-cli
npm install -g grunt-init
git clone https://github.com/gruntjs/grunt-init-gruntfile.git ~/.grunt-init/gruntfile
- grunt-cli 为grunt版本管理工具
- grunt-init 为自动创建项目的脚手架工具
- git clone ... 下载最简单的grunt-init模板,如果需要其他的模板,请参考
提示:
如果下载了半天它不动的话,请结束下载指令,加上代理下载参数--registry=http://r.cnpmjs.org
或者设定全局代理 npm config set registry http://r.cnpmjs.org ,另一个代理地址 http://registry.npmjs.vitecho.com
如果你是windows系统,那么在下载grunt-init模板时,请参照
C:\Users\Administrator\.grunt-init>git clone https://github.com/gruntjs/grunt-init-gruntfile.git ./gruntfile
创建项目
cd '创建项目的目录'
npm install grunt --save-dev
grunt-init gruntfile
-
下载grunt并引入到该项目中
-
grunt-init gruntfile 搭建项目骨架,命令执行时,会出现:
Please answer the following: [?] Is the DOM involved in ANY way? (Y/n) y [?] Will files be concatenated or minified? (Y/n) y [?] Will you have a package.json file? (Y/n) y [?] Do you need to make any changes to the above before continuing? (y/N) n
这个时候,目录结构为
| node_modules
| | ...
| | ...
| package.json
| Gruntfile.js
配置
package.json
{
"name":"JSCommonUtil",
"version":"1.0.0",
"engines": {
"node": ">= 0.10.0"
},
"devDependencies": {
"grunt": "~0.4.2",
"grunt-contrib-jshint": "~0.7.2",
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-qunit": "~0.3.0",
"grunt-contrib-concat": "~0.3.0",
"grunt-contrib-uglify": "~0.2.7"
}
}
如果想稳定下版本,请把~
去掉。
接下来的事情,就是开始写代码了。下一篇会记录Gruntfile.js
的配置,以及合并javascript测试框架的集成。
吐槽:oschina对markdown的支持很令人抑郁
phantomjs@1.9.7-3不翻墙下载地址:http://pan.baidu.com/share/link?shareid=1256805640&uk=1845652516