Grunt介绍(译)

基本介绍大家可以看这个:http://www.oschina.net/question/89964_47198

实际上,你可以把grunt理解为一组task,比如说合并若干js(concat),比如说压缩js(min),比如说测试js(qunit)等等。然后,你只需要在命令行中输入:
grunt concat就执行合并工作
grunt min就执行压缩工作

下面的grunt的入门译自https://github.com/gruntjs/grunt/wiki/Installing-grunt和https://github.com/gruntjs/grunt/wiki/Getting-started

安装grunt

首先确保你已经安装了node.js和npm(好像现在npm是跟着node.js一起安装的,具体我不清楚,我是自己手动编译的node.js)。然后直接使用命令:npm install -g grunt (这里可能需要高权限)。这样你就安装完成了grunt,可以直接使用grunt xxx来执行task了。通过 grunt --help 可以看到grunt所有的自带命令。
Grunt介绍(译)

注意:如果你使用的是Windows,那么可能得使用grunt.cmd命令,详见:FAQ

使用grunt

关于grunt.js,也就是gruntfile的介绍

每当grunt运行的时候,首先会在当前目录中查找grunt.js文件。如果当前目录下找不到,那么会继续去上一级目录查找,直到找到为止。一般来说,这个文件应该在你的项目的根目录中。grunt.js是一个标准的Javascript文件,包含下面三个部分:

  • 项目配置(Project Configuration)
  • 加载grunt插件或者任务文件夹(Loading grunt plugins or tasks folders)
  • 任务和帮手(Tasks and helpers)

下面是一个非常基础的grunt.js范例:

module.exports = function(grunt) {

  // Project configuration.
  grunt.initConfig({
    lint: {
      all: ['grunt.js', 'lib/**/*.js', 'test/**/*.js']
    },
    jshint: {
      options: {
        browser: true
      }
    }
  });

  // Load tasks from "grunt-sample" grunt plugin installed via Npm.
  grunt.loadNpmTasks('grunt-sample');

  // Default task.
  grunt.registerTask('default', 'lint sample');

};

特别注意

你的grunt.js文件必须包含这些代码,而且仅仅只能包含一次。如果不这样做,grunt不会正常工作。在本文的以后的代码中,这部分代码会被剔除,但是这部分代码必须像前面的例子中一样存在。

module.exports = function(grunt) {
  // Your grunt code goes in here.
};

项目配置

每个grunt任务都依赖于配置信息,这些配置信息由grunt.initConfig函数传入。

下面的例子中,定义了一组文件。当输入命令:grunt lint的时候,lint任务(lint task)将检查这些文件

// Project configuration.
grunt.initConfig({
  lint: {
    all: ['lib/*.js', 'test/*.js', 'grunt.js']
  }
});
注意:lint任务是一个多目标任务(multi task)的范例。对于多目标任务,你定义的目标在执行时都会一并执行。在这个例子中,输入grunt lint的时候,会执行all以及其他lint下定义的目标。如果你想单独执行一个all目标,应该输入: grunt lint:all

下面是另一个例子,摘自JQuery的grunt.js。这里使用jQuery QUnit单元测试工具来执行测试任务。命令为:grunt qunit

// Project configuration.
grunt.initConfig({
  qunit: {
    index: ['test/index.html']
  }
});
只要你定义的任务名称不跟其他的任务冲突,你可以随意定义任务。而且由于这是标准的Javascript文件而不是JSON文件,所以你可以在这里添加任意正确的Javascript脚本,这允许你根据你的具体需求,程序化地生成配置对象。

grunt内建了若干任务,文档可以看这里:https://github.com/gruntjs/grunt/blob/master/docs/toc.md#built-in-tasks

另外需要注意的是你不必配置那些你不需要的任务。

// Project configuration.
grunt.initConfig({
  // Project metadata, used by some directives, helpers and tasks.
  meta: {},
  // Lists of files to be concatenated, used by the "concat" task.
  concat: {},
  // Lists of files to be linted with JSHint, used by the "lint" task.
  lint: {},
  // Lists of files to be minified with UglifyJS, used by the "min" task.
  min: {},
  // Lists of files or URLs to be unit tested with QUnit, used by the "qunit" task.
  qunit: {},
  // Configuration options for the "server" task.
  server: {},
  // Lists of files to be unit tested with Nodeunit, used by the "test" task.
  test: {},
  // Configuration options for the "watch" task.
  watch: {},
  // Global configuration options for JSHint.
  jshint: {},
  // Global configuration options for UglifyJS.
  uglify: {}
});

加载grunt插件或者任务文件夹

你可以在你的gruntfile中定义一些任务或者帮手,也可以从外部资源中加载这些任务。

// Load tasks and helpers from the "tasks" directory, relative to grunt.js.
grunt.loadTasks('tasks');

// Load tasks and helpers from the "grunt-sample" Npm-installed grunt plugin.
grunt.loadNpmTasks('grunt-sample');
注意:上面这种方式加载外部已定义的任务和帮手比使用命令行参数  --tasks 和 --npm 要好。这是由于grunt.js新建或加载任务时,任务就成了这个项目的一部分。这样无论何时运行grunt,你都能获得相同的效果。( 就是建议加载任务都写在grunt.js里

任务和助手

由于grunt提供了一定数量的内置任务,所以你完全可以不必在gruntfile中定义你自己的任务。但是grunt并没有提供default任务,所以你必须在gruntfile中定义default任务是什么,否则当你输入grunt时,grunt并不知道到底应该执行哪个任务。

定义default任务的最简单方法是定义任务别名(alias task)。

下面这个例子中,定义的default任务是:按顺序执行lint, qunit, concat和min。当你在命令行输入grunt时,就相当于输入了grunt lint qunit concat min一样。

// Default task.
grunt.registerTask('default', 'lint qunit concat min');

点击这里查看gruntfile的例子

你可能感兴趣的:(grunt)