安装

  1. yarn add grunt

使用

基本使用

在项目根目录添加 gruntfile.js ,这是 grunt 项目的入口文件。

  1. // Grunt 的入口文件
  2. // 用于定义一些需要 Grunt 自动执行的任务
  3. // 需要导出一个函数
  4. // 此函数接收一个 grunt 的对象类型的形参
  5. // grunt 对象中提供一些创建任务时会用到的 API
  6. module.exports = grunt => {
  7. // 调用命令: yarn grunt foo
  8. grunt.registerTask('foo', 'a sample task', () => {
  9. console.log('hello grunt')
  10. })
  11. // 调用命令: yarn grunt bar
  12. grunt.registerTask('bar', () => {
  13. console.log('other task')
  14. })
  15. }

如果定义的任务名为 default ,那将作为默认任务执行,输入命令行时无需带上任务名。

  1. // 调用: yarn grunt
  2. grunt.registerTask('default', () => {
  3. console.log('default task')
  4. })
  1. defalut 任务可以执行其他任务。
  1. // 第二个参数可以指定此任务的映射任务,
  2. // 这样执行 default 就相当于执行对应的任务
  3. // 这里映射的任务会按顺序依次执行,不会同步执行
  4. grunt.registerTask('default', ['foo', 'bar'])
  1. 也可以在任务函数中执行其他任务。
  1. grunt.registerTask('run-other', () => {
  2. // foo 和 bar 会在当前任务执行完成过后自动依次执行
  3. grunt.task.run('foo', 'bar')
  4. console.log('current task runing~')
  5. })
  1. 默认情况下, grunt 采用同步模式编码,如果需要异步可以使用 this.async() 方法创建回调函数。
  1. // 由于函数体中需要使用 this,所以这里不能使用箭头函数
  2. grunt.registerTask('async-task', function () {
  3. const done = this.async()
  4. setTimeout(() => {
  5. console.log('async task working~')
  6. done()
  7. }, 1000)
  8. })
  1. 通过 return false 可以标记为任务失败。异步任务则需要在回调函数传入 false
  1. grunt.registerTask('bad', () => {
  2. console.log('bad working ~')
  3. return false
  4. })
  5. // 这里执行到bad的时候失败,后面的bar会失败,如果要继续执行,可以在命令后加上 --force
  6. grunt.registerTask('default', ['foo','bad', 'bar'])
  7. // 异步失败任务
  8. grunt.registerTask('bad-async', function () {
  9. const done = this.async()
  10. setTimeout(() => {
  11. console.log('bad async task~')
  12. done(false)
  13. }, 1000)
  14. })

配置选项

initConfig 是 grunt 的配置选项方法。

  1. module.exports = grunt => {
  2. grunt.initConfig({
  3. foo: {
  4. bar: 123
  5. }
  6. })
  7. grunt.registerTask('foo', () => {
  8. console.info(grunt.config('foo.bar'))
  9. })
  10. }

多目标任务

  1. module.exports = grunt => {
  2. // 多任务必须有一个同名的配置选项
  3. grunt.initConfig({
  4. build: {
  5. options: { // options 会作为配置选项而不是 target 出现
  6. foo: 'bar'
  7. }
  8. css: {
  9. options: { // target 中的 options 会覆盖任务中的 options
  10. foo: 'bar'
  11. }
  12. }, // 单独运行命令:yarn grunt build:css
  13. js: '2'
  14. }
  15. })
  16. // 多任务模式下,可以让任务根据配置形成多个子任务
  17. grunt.registerMultiTask('build', function() {
  18. console.info(this.options()) // 获取任务的配置选项
  19. console.info(`target: ${this.target}, data: ${this.data}`) //通过target获取到任务目标,data获取到数据
  20. })
  21. }

插件

插件是 grunt 的核心。

  1. module.exports = grunt => {
  2. grunt.initConfig({
  3. // clean插件的配置选项
  4. clean: {
  5. temp_1: 'temp/*.js', // 清除temp文件夹的所有js文件
  6. temp: 'temp/**' // 清除temp文件夹下的所有文件
  7. }
  8. })
  9. // 清除文件插件
  10. grunt.loadNpmTasks('grunt-contrib-clean')
  11. }
  1. const sass = require('sass')
  2. const loadGruntTasks = require('load-grunt-tasks')
  3. module.exports = grunt => {
  4. grunt.initConfig({
  5. // 编译sass插件配置选项
  6. sass: {
  7. options: {
  8. sourceMap: true, //生成sourceMap
  9. implementation: sass //指定处理sass编译的模块
  10. },
  11. main: {
  12. files: {
  13. 'dist/css/main.css': 'src/scss/main.scss'
  14. }
  15. }
  16. },
  17. // 编译 js 的插件配置选项
  18. babel: {
  19. options: {
  20. sourceMap: true,
  21. presets: ['@babel/preset-env'] // 预设
  22. },
  23. main: {
  24. files: {
  25. 'dist/js/app.js': 'src/js/app.js'
  26. }
  27. }
  28. },
  29. // 监听文件变化的插件配置选项
  30. watch: {
  31. js: {
  32. files: ['src/js/*.js'],
  33. tasks: ['babel']
  34. },
  35. css: {
  36. files: ['src/scss/*.scss'],
  37. tasks: ['sass']
  38. }
  39. }
  40. })
  41. // grunt.loadNpmTasks('grunt-sass')
  42. loadGruntTasks(grunt) // 自动加载所有的 grunt 插件中的任务
  43. grunt.registerTask('default', ['sass', 'babel', 'watch'])
  44. }