Grunt front-end automation project build process

  • Grunt Introduction
    • Chinese Home Page : http://www.gruntjs.net/
    • It is a set of front-end automated build tool, a command-line tool based nodeJs
    • It is a task runner , with its rich and powerful plug-ins
    • Common Functions:
      • Merge files (js / css)
      • Compressed files (js / css)
      • Grammar checker (js)
      • less / sass process precompiled
      • other…
  • Installation nodejs, view version
 node -v
  • Creating a simple application grunt_test
  |- build----------构建生成的文件所在的文件夹
  |- src------------源码文件夹   
      |- js---------------js源文件夹
      |- css--------------css源文件夹
  |- index.html-----页面文件
  |- Gruntfile.js---grunt配置文件(注意首字母大写)
  |- package.json---项目包配置文件
      {
        "name": "grunt_test",
        "version": "1.0.0"   
      }
  • Global installed grunt-cli
 npm install -g grunt-cli 
  • Installation grunt
 npm install grunt --save-dev
  • Run command to build the project
 grunt  //提示 Warning: Task "default" not found
  • Profile: Gruntfile.js
    • This profile is essentially a function of the type of node module
    • Step 3 configure the encoder comprising:
      1. Initialization plugin configuration
      2. Load plugins task
      3. Registration build tasks
    • Basic Code:
    module.exports = function(grunt){
      // 1. 初始化插件配置
      grunt.initConfig({
          //主要编码处
      });
      // 2. 加载插件任务
      // grunt.loadNpmTasks('grunt-contrib-concat');
      // 3. 注册构建任务
      grunt.registerTask('default', []);
    };
  • Command: grunt // prompt success, but has no effect (not using plug-ins defined task)
  • Grunt plug presentations
    • Plug-in list page http://www.gruntjs.net/plugins grunt official website
    • Plug Category:
      • grunt team contribution plug: plug-in name mostly in the beginning contrib-
      • Third-party plug-ins: the beginning tend not to contrib-
    • Common plug-ins:
      • grunt-contrib-clean-- purge files (generated packing process)
      • grunt-contrib-concat-- merge code into a plurality of files in the file
      • grunt-contrib-uglify-- compressed js file
      • grunt-contrib-jshint - javascript syntax error checking;
      • grunt-contrib-cssmin-- compression / merge css file
      • grunt-contrib-htmlmin-- compressed html file
      • grunt-contrib-imagemin-- compressed image file (lossless)
      • grunt-contrib-copy-- copy files, folders
      • grunt-contrib-watch-- real-time monitoring file changes, call the appropriate tasks to re-execute
  • Merge js: the use of plug-concat
    • command:
   npm install grunt-contrib-concat --save-dev
  • coding:
    • src/js/test1.js
      (function () {
        function add(num1, num2) {
          return num1 + num2;
        }
        console.log(add(10, 20));
      })();
* src/js/test2.js
      (function () {
        var arr = [2,3,4].map(function (item, index) {
          return item+1;
        });
        console.log(arr);
      })();
  • Configuration: Gruntfile.js
    • Configuration tasks:
      concat: {
        options: { //可选项配置
          separator: ';'   //使用;连接合并
        },
        build: { //此名称任意
          src:  ["src/js/*.js"],  //合并哪些js文件
          dest: "build/js/built.js" //输出的js文件
        }
      }
  • Load plugins:
 grunt.loadNpmTasks('grunt-contrib-concat');
  • Registration tasks:
grunt.registerTask('default', ['concat']);
  • command:
 grunt   //会在build下生成一个built.js
  • Compression js: the use of plug-uglify
    • download
    npm install grunt-contrib-uglify --save-dev
  • Configuration: Gruntfile.js
    • Configuration tasks:
      pkg : grunt.file.readJSON('package.json'),
      uglify : {
        options: {  //不是必须的
          banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
          '<%= grunt.template.today("yyyy-mm-dd") %> */'
        },
        build: {
          files: {
            'build/js/built-<%=pkg.name%>-<%=pkg.version%>.min.js': ['build/js/built.js']
          }
        }
      }
  • Load task:
      grunt.loadNpmTasks('grunt-contrib-uglify');
  • Registration tasks:
grunt.registerTask('default', ['concat', 'uglify']);
  • command:
 grunt   //会在build下生成一个压缩的js文件
  • js syntax checking: using plug-jshint
    • command:
npm install grunt-contrib-jshint --save-dev
  • Coding: .jshintrc
    {
      "curly": true,
      "eqeqeq": true,
      "eqnull": true,
      "expr" : true,
      "immed": true,
      "newcap": true,
      "noempty": true,
      "noarg": true,
      "regexp": true,
      "browser": true,
      "devel": true,
      "node": true,
      "boss": false,
      
      //不能使用未定义的变量
      "undef": true,
      //语句后面必须有分号
      "asi": false,
      //预定义不检查的全局变量
      "predef": [ "define", "BMap", "angular", "BMAP_STATUS_SUCCESS"]
    }
    
  • Modify src / js / test1.js
   (function () {
     function add(num1, num2) {
       num1 = num1 + num3
       return num1 + num2;
     }
     console.log(add(10, 20));
   })();
  • Configuration: Gruntfile.js
    • Configuration tasks:
      jshint : {
        options: {
          jshintrc : '.jshintrc' //指定配置文件
        },
        build : ['Gruntfile.js', 'src/js/*.js'] //指定检查的文件
      }
  • Load task:
grunt.loadNpmTasks('grunt-contrib-jshint');
  • Registration tasks:
grunt.registerTask('default', ['concat', 'uglify', 'jshint']);
  • command:
 grunt   //提示变量未定义和语句后未加分号 -->修改后重新编译
  • Use cssmin plug
    • installation:
 npm install grunt-contrib-cssmin --save-dev
  • coding:
    • test1.css
      #box1 {
        width: 100px;
        height: 100px;
        background: red;
      }
  • test2.css
      #box2 {
        width: 200px;
        height: 200px;
        background: blue;
      }
  • index.html
      <link rel="stylesheet" href="build/css/output.min.css">
      <div id="box1"></div>
      <div id="box2"></div>
  • Configuration: Gruntfile.js
    • Configuration tasks:
      cssmin:{
        options: {
          shorthandCompacting: false,
          roundingPrecision: -1
        },
        build: {
          files: {
              'build/css/output.min.css': ['src/css/*.css']
          }
        }
      }
  • Load task:
grunt.loadNpmTasks('grunt-contrib-cssmin');
  • Registration tasks:
grunt.registerTask('default', ['concat', 'uglify', 'jshint', 'cssmin']);
  • command:
grunt    //在build/css/下生成output.min.css
  • Use watch plugin (true automation)
    • 命令: npm install grunt-contrib-watch --save-dev

    • Configuration: Gruntfile.js

      • Configuration tasks:
      watch : {
        scripts : {
          files : ['src/js/*.js', 'src/css/*.css'],
          tasks : ['concat', 'jshint', 'uglify', 'cssmin'],
          options : {spawn : false}  
        }
      }
  • Load task:
 grunt.loadNpmTasks('grunt-contrib-watch');
  • Registration tasks:
grunt.registerTask('default', ['concat', 'uglify', 'jshint', 'watch']);
改进:grunt.registerTask('myWatch', ['default','watch']);
  • command:
 grunt   //控制台提示watch已经开始监听, 修改保存后自动编译处理
Published 20 original articles · won praise 11 · views 1744

Guess you like

Origin blog.csdn.net/qq_16221009/article/details/103230331