Grunt自动化工具之初学笔记

来源:互联网 发布:电脑软件制作 编辑:程序博客网 时间:2024/06/05 02:11

为何要用构建工具?
自动化。对于需要反复重复的任务,例如压缩(minification)、编译、单元测试、linting等,自动化工具可以减轻你的劳动,简化你的工作。当你在 Gruntfile 文件正确配置好了任务,任务运行器就会自动帮你或你的小组完成大部分无聊的工作。
为什么要使用Grunt
Grunt生态系统非常庞大,并且一直在增长。由于拥有数量庞大的插件可供选择,因此,你可以利用Grunt自动完成任何事,并且花费最少的代价。如果找不到你所需要的插件,那就自己动手创造一个Grunt插件,然后将其发布到npm上吧。先看看入门文档吧。

中文文档: http://www.gruntjs.net/getting-started

Grunt插件列表:http://www.gruntjs.net/plugins

Grunt API: http://www.gruntjs.net/api/grunt


package.json 配置文件

{  "name": "grunt",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1"  },  "author": "",  "license": "ISC",  "devDependencies": {    "grunt-contrib-clean": "^0.6.0",    "grunt-contrib-copy": "^0.8.0",    "load-grunt-tasks": "^3.2.0",    "time-grunt": "^1.2.1"  }}


Gruntfile.js文件

/** * Created by Administrator on 2015/8/23. */'use strict'module.exports = function (grunt) {  // 计划执行Task所需要的时间  require('time-grunt')(grunt);  // 加载Task任务  //require('load-grunt-tasks')(grunt);  // 下面二句相当于它require('load-grunt-tasks')(grunt);  grunt.loadNpmTasks("grunt-contrib-copy");  grunt.loadNpmTasks("grunt-contrib-clean");  var config = {    app: "app",    dist: "dist"  };  grunt.initConfig({    config: config,    // Task任务    copy: {      // 这是Task里的其中一个Target      dest: {        src:  '<%=config.app%>/newFolder/aa.html',        dest: '<%=config.dist%>/newFolder/cc.html'      }    },    clean: {      dest: {        expand: true, // 动态匹配        src: '<%=config.dist%>/**/**'      }    }  });  // Task组合任务  grunt.registerTask("build", "description", function(dist){    grunt.task.run([      "copy:dest",      "clean:dest"    ]);  });};




中文文档: http://www.gruntjs.net/getting-started

Grunt插件列表:http://www.gruntjs.net/plugins

Grunt API: http://www.gruntjs.net/api/grunt

0 0