grunt学习笔记(适用初学者)

1、在学习grunt之前,首先要对nodejs有一些简单的理解。安装nodejs的步骤很简单,根据官网的提示安装即可,在此文中将不再累述。

2. Grunt介绍

Grunt是一个自动化的项目构建工具. 如果你需要重复的执行像压缩, 编译, 单元测试, 代码检查以及打包发布的任务. 那么你可以使用Grunt来处理这些任务, 你所需要做的只是配置好Grunt, 这样能很大程度的简化你的工作.

如果在团队中使用Grunt, 你只需要与其他人员约定好使用Grunt应该规避的问题, 就能够很方便的自动化的处理大部分的常见工作任务, 你所付出的努力几乎为0.

3. Grunt安装

Grunt和Grunt插件都是通过npm, Node.js包管理器安装和管理的.

D:\>cd nodeTest1\nodejs-grunt\

D:\>cd nodeTest1\nodejs-grunt\npm install -g grunt-cli

这是全局安装。

D:\>cd nodeTest1\nodejs-grunt\grunt

将会出现如下错误:

grunt-cli: The grunt command line interface. (v0.1.9)

Fatal error: Unable to find local grunt.

If you're seeing this message, either a Gruntfile wasn't found or grunt
hasn't been installed locally to your project. For more information about
installing and configuring grunt, please see the Getting Started guide:

http://gruntjs.com/getting-started

执行grunt命令,我们发现系统报错了,提示不能加载本地库。因为,grunt命令执行,是需要当前目录中包括package.json和Gruntfile.js两个文件。

package.json,是npm项目配置文件
Gruntfile.js,是专门用来配置grunt的配置文件

为了做验证,本文做了一个例子:

~ D:\>express -e nodejs-grunt
~ D:\>cd nodejs-grunt && npm install	 
~ D:\nodejs-grunt>npm install grunt --save-dev
package.json文件中输入如下量:

{
  "name": "nodejs-grunt",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.2.2",
    "ejs": "*"
  },
  "devDependencies": {
    "grunt": "~0.4.1",
  }
}

并在Gruntfile.js文件中输入如下量(需要新建):

module.exports = function(grunt) {
  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    uglify: {
      options: {
        banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      build: {
        src: 'src/<%= pkg.name %>.js',
        dest: 'build/<%= pkg.name %>.min.js'
      }
    }
  });
  // Load the plugin that provides the "uglify" task.
  grunt.loadNpmTasks('grunt-contrib-uglify');
  // Default task(s).
  grunt.registerTask('default', ['uglify']);
};

再次编辑package.json, 在devDependencies中增加grunt-contrib-uglify的依赖库

{
  "name": "nodejs-grunt",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.2.2",
    "ejs": "*"
  },
  "devDependencies": {
    "grunt": "~0.4.1",
    "grunt-contrib-uglify": "~0.1.1"
  }
}
继而,再在 nodejs-grunt目录下创建 src和build,和nodejs-grunt.js的文件,其中nodejs-grunt.js

var sayHello=function(name) {
	return "你好 "+ name;
}
为确保依赖库被加载,需要 再次执行npm install 命令

接着执行:

D:\nodejs-grunt>grunt
控制台输入如下:

Running "uglify:build" (uglify) task
File "build/nodejs-grunt.min.js" created.
Uncompressed size: 59 bytes.
Compressed size: 40 bytes gzipped (43 bytes minified).

Done, without errors.
打开nodejs-grunt\build\nodejs-grunt.min.js

/*! nodejs-grunt 2014-09-08 */
var sayHello=function(l){return"hello "+l};
这是一个压缩实例。

Grunt常用插件

  • grunt-contrib-uglify:压缩js代码
  • grunt-contrib-concat:合并js文件
  • grunt-contrib-qunit:单元测试
  • grunt-contrib-jshint:js代码检查
  • grunt-contrib-watch:监控文件修改并重新执行注册的任务
上述已经讲述一个压缩实例。后面几个插件就不再一一介绍。

下面将demo放到资源中供大家免费下载

http://download.csdn.net/detail/zz410675629/7881187

参考文章:http://blog.fens.me/nodejs-grunt-intro/














  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值