1.安装grunt
命令:npm install grunt-cli -save-dev
命令:npm install grunt -save-dev
2.安装要用的插件(以uglify为例)
命令:npm install grunt-contrib-uglify -save-dev
uglify是grunt下的一个插件,对代码进行压缩,从其插件名来看,contrib-**表明其是grunt官方的一个插件,第三方的插件命名没有contrib
新建完要压缩的文件之后,创建GruntFile.js
文件目录:
执行压缩命令:
ps:因为文件本身较小,压缩后添加了注释说明,所以压缩之后反而变大了。
压缩效果:
2-1.体验jshint
由于js没有编译这一过程,所以可以用jshint对js文件进行语法检查
安装jshint命令:npm install grunt-contrib-jshint -save-dev
配置GruntFile.js文件
执行命令grunt jshint即可执行jshint。
2-2体验concat
合并文件,减少http请求
安装concat命令:npm install grunt-contrib-concat -save-dev
配置GruntFile.js文件
合并完成后,生成的文件如下: