Grunt的使用

  在Node环境下。须要预先安装好Node。npm

一、安装grunt-clijson

[root@Luxh-01 ~]# npm install -g grunt-cli

 

二、建立一个目录testgrunt

[root@Luxh-01 ~]# mkdir test

 

三、进入test目录,建立一个package.json文件,内容以下:this

{
  "name": "grunt_test",
  "description": "this is a demo",
  "author": "Luxh"
}

 

四、安装grunt,只需安装到开发环境依赖中。spa

[root@Luxh-01 test]# npm install grunt --save-dev

  安装完成,package.json文件以下:插件

{
  "name": "grunt_test",
  "description": "this is a demo",
  "author": "Luxh",
  "devDependencies": {
    "grunt": "^0.4.5"
  }
}

 

五、这里我要是有grunt完成:clean(清除文件)、copy(复制文件)、coffee(coffee编译成js)三个功能,全部我须要安装三个grunt插件:code

[root@Luxh-01 test]# npm install grunt-contrib-clean --save-dev
[root@Luxh-01 test]# npm install grunt-contrib-copy --save-dev
[root@Luxh-01 test]# npm install grunt-contrib-coffee --save-dev

  安装完成后:package.json文件以下:blog

{
  "name": "grunt_test",
  "description": "this is a demo",
  "author": "Luxh",
  "devDependencies": {
    "grunt": "^0.4.5",
    "grunt-contrib-clean": "^0.6.0",
    "grunt-contrib-coffee": "^0.12.0",
    "grunt-contrib-copy": "^0.7.0"
  }
}

 

六、编写Grunt的配置文件:Gruntfile.js 内容以下:ip

module.exports = function(grunt) {

  grunt.initConfig({
   clean:{
    main:{
      src:'dest'  //清除dest目录
    }
   },
   copy: {
     main: {
       expand: true,
       cwd: 'src/',  //指定源文件目录
       src: ['**','!**/*.coffee'],  //不复制coffee文件
       dest: 'dest/'    //复制到dest目录下
    }
   },

  coffee:{
    main:{
     expand:true,
     cwd:'src',
     src:['**/*.coffee'],  //src目录下的coffee文件编译到dest目录
     dest:'dest',
     ext:'.js'
    }
  }
  });

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-coffee');

  grunt.registerTask('default', ['clean:main','copy:main','coffee:main']);

};

 

七、在test目录下建立src目录,在里面编写coffee。ci

 

八、执行grunt命令,将会依次执行 clean、copy、coffee,若是只须要执行clean,则容许 grunt copy

相关文章
相关标签/搜索