Grunt参考

Grunt参考:

http://www.cnblogs.com/yexiaochai/p/3603389.html

http://blog.csdn.net/wangfupeng1988/article/details/46418203/

grunt打包seajs项目http://www.tuicool.com/articles/bEZNZnV

Grunt 实例之 构建 seajs 项目   http://www.tuicool.com/articles/zaUfI3

Gruntfile.js:

module.exports = function (grunt) {
  // 项目配置
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),


    //合并
    concat: {
      options: {
        separator: '/*文件分割*/',   //合并文件时的分隔符
        banner: '/*合并文件 <%= grunt.template.today("yyyy-mm-dd") %>*/',      //文件头注释信息,只会出现一次
        footer: '/*concat end...*/',     //文件尾信息,只会出现一次
        stripBanners:"/**/"          //去掉源代码注释信息(只会清除/**/这种注释)
      },
      //写法1:
      basic_and_extras: {
        files: {
          'build/ecnulib.js': 'common/jslib/*.js',
          'build/gLib.js': ['common/jslib/gClass.js', 'common/jslib/gConfig.js']
        }
      }


      //写法2
      // dist: {
      //   src: 'common/jslib/*.js',
      //   dest: 'build/ecnulib.js'
      // }
    },
    
  
    //压缩混淆
    uglify: {
      options: {
        //注释
        banner: '/*!ecnulib.js压缩后 <%= grunt.template.today("yyyy-mm-dd") %> */\n'
      },
      "my_target": {
        //写法1
         files: [{
          expand: true,
          cwd: 'common/jslib/',
          src: '**/*.js',
          dest: 'build'
        }]
        //写法2
        // "files": {
        //   'build/ecnulib.min.js': ['common/jslib/gClass.js', 'common/jslib/gConfig.js', 'common/jslib/gMap.js'],
        //   'build/index.min.js':['js/index.js']
        // }
      }
      //写法3
      // build: {
      //   src: 'js/index.js',
      //   dest: 'build/index.min.js'
      // }
    },


    //检查
    jshint:{
      options: {   //相关配置
        //jshintrc:'.jshintrc'   //在.jshintrc文件里配置jshint的检查规则 
        //或者直接在options里面逐个配置
        curly: true,  //循环或者条件语句必须使用花括号包围
        eqeqeq: true,  //强制使用三等号
        latedef: "nofunc",  //禁止定义之前使用变量
        strict:true       //严格模式
      },
      //写法1
      //all:["common/jslib/gConfig.js"]
      
      //写法2
     // build:['common/jslib/gConfig.js'],
   

      //写法3
      //合并之前做一次检查,合并之后再做一次检查 
      // 运行时用grunt jshint:pre   jshint:after
      pre: ['common/jslib/gClass.js', 'common/jslib/gConfig.js'],
      after: ['build/gLib.js']



    },

    //css压缩
    cssmin:{
      compress:{
        files:{
          'build/css/style.min.css':['css/index.css','css/codewin.css']
        }
      }

    },

    //复制文件
    copy:{
      main:{
        flatten:true,
        expand:true,
        src:'build/*.js',  //将src中的文件复制到dest路径下
        dest:'common/'

      }
    },



    //require引入文件的打包
     requirejs: {
      compile: {
        "options": {
          "baseUrl": "./",
          "name": 'src/test02.js',
          "paths": {
            "$": "src/zepto",
            "_": "src/underscore",
            "B": "src/backbone",
            "Test": "src/Test01"
          },
          "include": [
            "$",
            "_",
            "B",
            "Test"
          ],   //将include中包含的文件压缩打包至out中的路径下
          "out": "dest/libs.js"
        }
      }
    },

    //监测文件变动
    watch:{
      build:{   //各个插件的配置时,都是用了“build”这一名称作为一个配置项,这个名词不是必须的,可以用其他字符串替换
        files:['common/jslib/*.js','css/*.css'],
        tasks:['cat','csmin'],
        options:{
          spawn:true,  //默认true,会创建一个新的子进程来执行触发的任务;通过设置为 false,可以使得触发的任务可以共享进程上下文,并且提高速度。但是,这会导致监控任务容易崩溃,
          event:['all']   //可以为 'all', 'changed', 'added' 和 'deleted'.
        }
      }



    }






  });
  
  // 加载提供"uglify"任务的插件
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-watch');
  //grunt.loadNpmTasks('grunt-contrib-requirejs');

  // 默认任务
  grunt.registerTask('ug', ['uglify']);
  grunt.registerTask('cat', ['concat']);
  grunt.registerTask('jsh',['jshint']);
  grunt.registerTask('csmin',['cssmin']);
  grunt.registerTask('cpy',['copy']);
  grunt.registerTask('wat',['watch']);



  //自定义任务
  //运行 grunt mytask:hello:world
  grunt.registerTask('mytask',"this is a custom task",function (arg1,arg2){
    grunt.log.writeln("任务名:"+this.name);
    grunt.log.writeln("参数:"+arg1+" "+arg2);
  });



}

package.json

/*批量安装插件
//在上传代码时,node_modules文件夹中的插件可以不上传,只保留package.json和Gruntfile.js,然后在改目录下,运行“npm install”,即可将所需的插件批量下载下来
//
//将package.json文件移至某目录下,执行“npm install”命令,即可批量下载*/

{
  "name": "editor",
  "file": "editor",
  "version": "0.1.0",
  "description": "this is a  online editor",
  "devDependencies": {
    "grunt": "^1.0.1",
    "grunt-contrib-concat": "^1.0.1",
    "grunt-contrib-copy": "^1.0.0",
    "grunt-contrib-cssmin": "^1.0.1",
    "grunt-contrib-jshint": "^1.0.0",
    "grunt-contrib-uglify": "^1.0.1",
    "grunt-contrib-watch": "^1.0.0"
  }
}

 

posted @ 2016-04-28 17:05  freewalker  阅读(212)  评论(0编辑  收藏  举报