当想要重新加载Javascript文件时,使用grunt contrib watch可以查看哪些文件?

当想要重新加载Javascript文件时,使用grunt contrib watch可以查看哪些文件?,javascript,coffeescript,gruntjs,livereload,grunt-contrib-watch,Javascript,Coffeescript,Gruntjs,Livereload,Grunt Contrib Watch,我有一个用咖啡脚本编写的应用程序。我的grunt.js文件如下所示: module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), clean: { test: { files: [{ src: ['.tmp'] }] } }, coffee: {

我有一个用咖啡脚本编写的应用程序。我的grunt.js文件如下所示:

module.exports = function(grunt) {

  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    clean: {
      test: {
        files: [{
          src: ['.tmp']
        }]
      }
    },
    coffee: {
      compileScripts: {
        expand: true,
        flatten: true,
        src: '*/*.coffee',
        dest: '.tmp/scripts',
        ext: '.js'
      },
    },
    jst: {
      compile: {
        files: {
          '.tmp/scripts/templates.js': ['scripts/**/*.ejs']
        }
      }
    },
    watch: {
      all: {
        options: {
          livereload: true,
          spawn: false
        },
        files: ['scripts/*.coffee', 'scripts/*.ejs', 'index.html'],
        tasks: ['compile'],
      }
    },
    connect: {
      server: {
        options: {
          port: 8000,
          hostname: 'localhost',
          open: true,
          livereload: true
        }
      }
    }
  });

  grunt.loadNpmTasks('grunt-contrib-clean');
  grunt.loadNpmTasks('grunt-contrib-coffee');
  grunt.loadNpmTasks('grunt-contrib-jst');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-contrib-connect');

  grunt.registerTask('compile', ['clean', 'coffee', 'jst']);
  grunt.registerTask('default', ['compile', 'connect', 'watch']);
}
经过大量的修补,我终于让“手表”在我的JS文件被实时重新加载的地方工作了,但是我不清楚手表任务的“文件”部分是怎么回事?我做对了吗


我必须只看咖啡脚本文件吗?或者我还必须观看index.html,这样如果其中一个coffeescript文件发生更改,index.html将刷新并重新加载新的JS?(我所有的JS文件都是我index.html中的脚本。)

监视任务的
文件属性包含一个文件模式列表,在更改时要监视并执行一些操作(最常见的是运行某些任务)

任务
属性包含在相应的
文件
已更改时要运行的任务

任何在其上配置了
livereload
选项的目标都将在文件更改为实时重新加载服务器时向
文件提供数据。因此,如果您有
*.coffee
文件,它们将在更改时被馈送到实时重新加载服务器。live reload服务器只知道如何解释js、css文件,因此
.coffee
文件将刷新整个页面,而不仅仅是javascript

tasks
属性对于监视任务是可选的,因此更好地配置实时重新加载的常见解决方案如下:

watch: {
  options: { spawn: false },
  all: {
    files: ['scripts/*.coffee', 'scripts/*.ejs'],
    tasks: ['compile'],
  },
  livereload: {
    options: { livereload: true },
    files: ['.tmp/**/*.{html,js,css}']
  }
},
因此,当您的
compile
任务运行时,它将通过输入正确的文件触发
watch:livereload
目标和实时重新加载


关于
index.html
,在您的实例中,您可能不需要在
watch:all
目标中查看该文件。因为它没有任何任务可以从中进行任何构建(至少从我上面看到的配置)。

非常感谢您的详细解释,Kyle。读了这么多书之后,我仍然不明白它是如何工作的,所以现在一切都有了意义。我有一个问题…我的index.html只是在根目录中(而不是在.tmp/下),所以当我将它添加到watch:livereload下的“文件”中时,我会认为index.html页面会自动刷新,但不会。有什么我还不明白的吗?如果我手动刷新,我的更改将正确显示。我以为页面会自动刷新。我完整地使用了上面建议的脚本。