【问题标题】:Grunt run task with override config带有覆盖配置的 Grunt 运行任务
【发布时间】:2016-02-10 16:30:15
【问题描述】:

我一直使用 gulp 来实现我所有的任务自动化,但在我们当前的项目中,我们使用 grunt,我不知道如何做非常简单的事情:

grunt.initConfig({
    watch: {
        scripts: {
            files: '**/*.js',
            tasks: ['karma:watch:run'],
        }
    },
    myWatch: {
        scripts: {
            files: '**/*.js',
            tasks: ['do_my_own_task_but_not_karma'],
        }
    }
});

//current task
grunt.registerTask('default', ['watch']);

//doesn't work
grunt.registerTask('myWatchTask', ['myWatch']);

基本上我只想为我自己的自定义任务覆盖当前的手表配置,因为我不希望 karma 每次更改 js 文件时都运行测试。

【问题讨论】:

    标签: gruntjs


    【解决方案1】:

    您需要在watch 配置对象中添加一个新配置:

    grunt.initConfig({
      watch: {
        main: {
          scripts: {
              files: '**/*.js',
              tasks: ['karma:watch:run'],
          }
        },
        myWatch: {
          scripts: {
              files: '**/*.js',
              tasks: ['do_my_own_task_but_not_karma'],
          }
      }
    },
    });
    

    然后你可以运行grunt watch:maingrunt watch:myWatchsee these docs

    如果您想始终监视某些内容,可以将其添加到 watch 配置对象的根目录中。

    grunt.initConfig({
      watch: {
        files: '**/always.js',
        tasks: ['always'],
        main: {
          scripts: {
              files: '**/*.js',
              tasks: ['karma:watch:run'],
          }
        },
        myWatch: {
          scripts: {
              files: '**/*.js',
              tasks: ['do_my_own_task_but_not_karma'],
          }
      }
    },
    });
    

    现在grunt watch:myWatch 将运行它的专用配置和watch 配置对象根目录中的配置。

    【讨论】:

      猜你喜欢
      • 2013-01-31
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2013-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多