Grunt - 实时重装不使用grunt-contrib-watch

时间:2013-11-02 10:05:36

标签: javascript gruntjs

我正在尝试让Grunt在使用grunt-contrib-watch进行更改时重新加载我的js文件。这是我的Gruntfile

module.exports = function(grunt) {

  require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

  grunt.initConfig({

     connect: {
      all: {
        options:{
          port: 9000,
          hostname: "0.0.0.0",
          base: 'app',
          keepalive: true,
          middleware: function(connect, options) {

            return [

              require('grunt-contrib-livereload/lib/utils').livereloadSnippet,

              connect.static(options.base)
            ];
          }
        }
      }
    },
    open: {
      all: {
        path: 'http://localhost:<%= connect.all.options.port%>'
      }
    },
    watch: {
      options: {
        livereload: true
      },
      js: {
        files: ['app/js/**/*.js'],
        tasks: ['jshint'],
      }
    }
  });

  // Creates the `server` task
  grunt.registerTask('server',[
    'open',
    'livereload-start',
    'connect',
    'watch'
  ]);
};

当我更改js文件时没有任何反应。任何帮助都会很棒。

1 个答案:

答案 0 :(得分:5)

之前我没有尝试在中间件中使用livereloadSnippet,所以我不确定那里可能有什么问题。但是如果你只想要一个常规的L​​iveReload设置,你可以使用livereload插件提供的grunt-contrib-connect选项:

require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

grunt.initConfig({
  connect: {
    all: {
      options:{
        port: 9000,
        hostname: '0.0.0.0',
        base: 'app',
        keepalive: true,
        livereload: true,
        open: true
      }
    }
  },
  watch: {
    options: {
      livereload: true
    },
    js: {
      files: ['app/js/**/*.js'],
      tasks: ['jshint']
    }
  }
});

grunt.registerTask('server',[
  'connect',
  'watch'
]);
相关问题