grunt简记

grunt和gulp都是前端自动化的工具,grunt更成熟,插件社区全、大;gulp比较年轻,性能更好,更简单容易。具体使用哪种可根据实际项目组来决定。
创建任务
grunt默认执行的是default文件
1 module.exports = function(grunt) {
2     grunt.registerTask('default',function(){
3         console.log('Hello Grunt');
4     })
5 }
在执行他的时候,需要给参数赋值,比如在命令行输入:grunt greet:marymei
1 module.exports = function(grunt) {
2     grunt.registerTask('greet',function(name){
3         console.log('Hello' +name);
4     })
5 }
处理错误,遇到错误warn默认就不继续执行了,但是可以在命令行设置grunt greet:n --force来强制执行
但是如果是fatal,那么即使使用了grunt greet:n --force他也是不向下执行的
1 module.exports = function(grunt) {
2     grunt.registerTask('greet',function(name){
3         if(name.length < 2) {
4             grunt.warn('名字太短了~');
5         }
6         grunt.log.writeln('Hello' +name);
7     })
8 }
链接多个任务:
 1 module.exports = function(grunt) {
 2     grunt.registerTask('greet-1',function(){
 3         grunt.log.writeln('Hello');
 4     });
 5     grunt.registerTask('greet-2',function(){
 6         grunt.log.writeln('Hola');
 7     });
 8     grunt.registerTask('greet-3',function(){
 9         grunt.log.writeln('您好');
10     });
11     grunt.registerTask('greentAll',['greet-1','greet-2','greet-3']);
12 };

初始化:

 1 module.exports = function(grunt) {
 2     grunt.initConfig({
 3         greet: {
 4             english: 'Hello'
 5         }
 6     });
 7     grunt.registerTask('greet',funtion(){
 8         grunt.log.writeln(grunt.config.get('greet.english'));
 9     });
10 };
多任务:
 1 module.exports = function(grunt) {
 2     grunt.initConfig({
 3         greet: {
 4             english: 'Hello' 5             spanish:'Hola',
 6             chinese:'您好'
 7         }
 8     });
 9     grunt.registerMultiTask('greet',funtion(){
10         grunt.log.writeln(this.target + ':' + this.data);
11         
12     });
13 };
文件与目录
创建目录
1 module.exports = function(grunt) {
2     grunt.registerTask('createFolders',function(){
3         grunt.file.mkdir('dist/stylesheets');
4     });
5 };

tree -I node_modules 命令可以查看文件目录


删除目录:
1 module.exports = function(grunt) {
2     grunt.registerTask('createFolders',function(){
3         grunt.file.mkdir('dist/stylesheets');
4     });
5     grunt.registerTask('clean',function(){
6         grunt.file.delete('dist');
7     });
8 };
显示版本
1 module.exports = function(grunt) {
2     grunt.initConfig({
3         pkg:grunt.file.readJSON('package.json');
4     });
5     grunt.registerTask('copyright',function(){
6         var content = grunt.tamplate.process('<%= pkg.name%>这个项目是由<%= pkg.author %>创建的,现在的版本<%= pkg.version %>');
7         grunt.file.write('copyright.txt', content);
8     });
9 };

复制文件:
 1 module.exports = function(grunt){ 2 grunt.loadNpmTasks('grunt-contrib-copy'); 3 } 

配置要复制的文件:
 1 module.exports = function(grunt){
 2     //加载复制文件的插件
 3     grunt.loadNpmTasks('grunt-contrib-copy');
 4     grunt.initConfig({
 5         copy: {
 6             html: {
 7                 src: 'index.html',
 8                 dest: 'dist/'
 9             },
10             style: {
11                 src: 'stylesheets/*.css',
12                 dest: 'dist/'
13             },
14             js: {
15                 src:'javascript/**/*.js'
16                 dest:'dist/'
17             }
18         }
19     })
20 };
监听文件变化的插件:
先安装如下命令
插件:npm install grunt-contrib-watch --save-dev

 1 module.exports = function(grunt){
 2     //加载复制文件的插件
 3     grunt.loadNpmTasks('grunt-contrib-copy');
 4     //加载监听文件变化,文件发生变化执行指定的任务
 5     grunt.loadNpmTasks('grunt-contrib-watch');
 6     grunt.initConfig({
 7         watch: {
 8             html:{
 9                 files: ['index.html'],
10                 tasks:['copy.html']
11             }
12         },
13         copy: {
14             html: {
15                 src: 'index.html',
16                 dest: 'dist/'
17             },
18             style: {
19                 src: 'stylesheets/*.css',
20                 dest: 'dist/'
21             },
22             js: {
23                 src:'javascript/**/*.js',
24                 dest:'dist/'
25             }
26         }
27     })
28 };
插件
创建服务器:grunt-contrib-connect
配置服务器
实时刷新:livereload
编译sass:grunt-contrib-sass
编译less:grunt-contrib-less
合并文件:grunt-contrib-concat
选项:options
最小化js:grunt-contrib-uglify
最小化css:grunt-contrib-cssmin
最小化图像:grunt-contrib-imagemin
......

安装插件的命令 sudo npm install xxxx --save-dev


配置服务器:
 1 module.exports = function(grunt){
 2     //加载复制文件的插件
 3     grunt.loadNpmTasks('grunt-contrib-copy');
 4     //加载监听文件变化,文件发生变化执行指定的任务
 5     grunt.loadNpmTasks('grunt-contrib-watch');
 6     grunt.loadNpmTasks('grunt-contrib-connect');
 7     grunt.initConfig({
 8         connect:{
 9             server:{
10                 options: {
11                     port:8000,
12                     base:'dist'
13                 }
14             }
15         },
16         watch: {
17             html:{
18                 files: ['index.html'],
19                 tasks:['copy.html']
20             }
21         },
22         copy: {
23             html: {
24                 src: 'index.html',
25                 dest: 'dist/'
26             },
27             style: {
28                 src: 'stylesheets/*.css',
29                 dest: 'dist/'
30             },
31             js: {
32                 src:'javascript/**/*.js',
33                 dest:'dist/'
34             }
35         }
36     })
37 };
原文地址:https://www.cnblogs.com/marymei0107/p/4620715.html