Grunt学习--watch(包含完整的代码)

如果觉得时间过长,可以改变watch任务中的debounceDelay参数来更改相邻两次执行任务时间的时间间隔

module.exports = function(grunt) {
  grunt.initConfig({
    //css文件压缩
    cssmin: {
      foo:{
        options: {
          report:'gzip', 
          sourceMap:true, 
          mergeIntoShorthands: false, 
          roundingPrecision: -1   
        },
        files:[{
          expand:true,
          cwd:'src/css/',
          src:['**'],
          dest:'build/css/'
        }]
      }
    },

    //html文件压缩
    htmlmin:{
      dist:{
        options:{
          removeComments: true, 
          collapseWhitespace: true 
        },
        files:[{
          expand:true,
          cwd:"src/html/",
          src:['**'],
          dest:"build/html/"
        }]
      }
    },

    //babel监听js文件,将es6转换为es5
    babel:{
      options:{
        presets:["@babel/preset-env"]
      },
      dist:{
        files:[{
          expand:true,
          cwd:"src/js/",
          src:["**"],
          dest:"src/babel"
        }]
      }
    },

    //js文件压缩
    uglify:{
      options:{
        report:'min',
        beautify:false
      },
      dist:{
        files:[{
          expand:true,
          cwd:'src/babel',
          src:['**'],
          dest:'build/js/'
        }]
      }
    },

    //压缩图片
    imagemin:{
      options:{
        iterlaced:true,//交错扫描优化
        optimizationLevel: 3, //选择介于0和7之间的优化级别
        svgoPlugins: [{removeViewBox: false}]
      },
      dist:{
        files:[{
          expand:true,
          cwd:'src/image',
          src:['**'],
          dest:'build/image/'
        }]
      }
    },

     //concat合并文件
    concat:{
      options:{
        separactor:";", //分隔符号
        sourceMap:true,
        nonull:true
      },
      dist:{
        files:[{
          src:'src/css/*.css',
          dest:'src/concat/index.css'
        }]
      }
    },

    //copy文件
    copy:{
      options:{
        timestamps:true, //设置修改的时间戳
      },
      dist:{
        files:[{
          expand: true, 
          src: ['src/*'], 
          dest: 'bulid/'
        }]
      }
    },

    //监听事件
    watch:{
      scripts:{
        files:['**/*.css','**/*.js','**/*.html','**/*.{jgp,png}'],
        tasks:['cssmin','babel','uglify','htmlmin','imagemin'],
        optins:{
          spawn:false,
          reload: true,
          debounceDelay: 250 //距离上一次改变的响应时间
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-cssmin');
  grunt.loadNpmTasks('grunt-contrib-htmlmin');
  grunt.loadNpmTasks('grunt-babel');
  grunt.loadNpmTasks('grunt-contrib-uglify');
  grunt.loadNpmTasks('grunt-contrib-imagemin');
  grunt.loadNpmTasks('grunt-contrib-concat');
  grunt.loadNpmTasks('grunt-contrib-copy');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');

  grunt.registerTask('task', ['cssmin','htmlmin','babel','uglify','imagemin']);
  grunt.registerTask('default',['task','watch']);
  grunt.registerTask('concat', ['concat']);
  grunt.registerTask('copy', ['copy']);
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值