Grunt 从零开始

Inthis article I will go through:

-Grunt环境配置

-Grunt相关组件安装

-一个简单的Grunt例子

 

What needed 配置环境

-安装 NodeJS 并且设置全局变量

-打开NodeJs console 运行‘npm install 检查当前环境是否配置正确

-"npm install -g grunt-cli"cli(Command Line Interface), -g will install it globally cligrunt命令入口

 

Hot to use Grunt

新建文件夹,并从console进入到这个文件夹中。这里需要package.json Gruntfile.js 两个文件作为项目的开始,下面是这两个文件的说明:

package.json文件 (two ways to createit)

-Createfrom 'grunt-init' template

 This would need first 'npm install grunt-init'

-'npminit, follow the prompts and enter the project information

  This is much easier as below:


 

在下一步前要安装Grunt plugin, 具体做法如下:

'npminstall <Module> --save-dev'

e.g.:

npminstall grunt --save-dev

npminstall grunt-contrib-uglify --save-dev

Package.jsonfile will be auto updated after run the commands.

 

Gruntfile.js文件

Whatthe file has:

-The"wrapper" function

-Projectand task configuration

-LoadingGrunt plugins and tasks

-Customtasks

Let'shave a sample to quick go through this, content of the file as below:

 

 

module.exports=function(grunt){

grunt.initConfig({

//readdata from package.json, so that pkg can be used as data resources here

pkg:grunt.file.readJSON('package.json'),

 

//<%=%> can be used as place holder where it's content could be changed bywhatever imported.

//uglifyis one of the grunt plugin

uglify:{

options:{

banner:'/*!<%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %>*/\n'

},

build:{

src:'src/<%=pkg.name %>.js',

dest:'build/<%=pkg.name %>.min.js'

}

}

});

 

//LoadingGrunt plugins and tasks

//plugin name could be found from package.json file

//enablethe plug in by using

grunt.loadNpmTasks('grunt-contrib-uglify');

 

//Customtasks

//definethe default task as uglify, and uglify is from initConfig uglify above.

grunt.registerTask('default',['uglify']);

 

 

//anotherbasid task which will not use any plugin

grunt.registerTask('logInfo','Loginformation from console',function(){

grunt.log.write('Thisis output from grunt task LogInfo').ok();

});

};

这些都做完后就可以从Console窗口运行 grunt logInfo’了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值