带有调试和释放目标的grunt-contrib-watch

时间:2013-06-25 09:36:58

标签: gruntjs

使用concat插件,我们可以在Gruntfile.js中设置调试和发布目标:

grunt.initConfig({
  concat: {
    debug: {
    },
    release: {
    },
  }

是否可以为grunt-contrib-watch插件提供多种配置?

这样做时:

watch: {
  debug: {
    options: {
      livereload: true,
      nospawn: true
    },
    copy: {
      files: ['js/app/**/*.js', 'js-amd/**/*.js'],
      tasks: ['copy']
    }

我收到错误verifying property watch.debug.files exists in config

这也不起作用:

watch: {
   debug: {
      options: {
        livereload: true,
        nospawn: true
      },
      files: ['js/app/**/*.js', 'js-amd/**/*.js'],
      tasks: ['copy'],

      files: ['jade/**/*.jade'],
      tasks: ['jade:devmock']

...因为我不能有两个files - 数组或两个tasks - 数组。 (它忽略了除第一个files/tasks - 对之外的所有内容)

还有其他方法可以实现我的目标吗?

3 个答案:

答案 0 :(得分:1)

配置稍微扁平。

  watch: {
    debug: {
      files: ['js/app/**/*.js', 'js-amd/**/*.js'],
      tasks: ['copy'],
      options: {
        livereload: true,
        nospawn: true
      }
    }
  }

您可以在此处找到更多示例:https://github.com/gruntjs/grunt-contrib-watch

答案 1 :(得分:1)

如果您需要两组文件,则需要一组新的配置

watch: {
    debug: {
      files: ['js/app/**/*.js', 'js-amd/**/*.js'],
      tasks: ['copy'],
      options: {
        livereload: true,
        nospawn: true
      }
    },
    other-debug: {
      files: ['js/app/**/*.js', 'js-amd/**/*.js'],
      tasks: ['copy'],
      options: {
        livereload: true,
        nospawn: true
      }
    }
  }

答案 2 :(得分:0)

我使用的解决方案是定义多个监视目标并重命名监视任务,如下所示:

watch: {
    scripts: {
        files: ['js/**/*.js'],
        tasks: ['concat', 'uglify'],
        options: {
            spawn: false
        }
    }
},

// Don't uglify in dev task
watchdev: {
    scripts: {
        files: ['js/**/*.js'],
        tasks: ['concat'],
        options: {
            spawn: false
        }
    }
}

grunt.loadNpmTasks('grunt-contrib-watch');
// Rename watch to watchdev and load it again
grunt.renameTask('watch', 'watchdev');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('default', ['watch']);
grunt.registerTask('dev', ['watchdev']);