Javascript Can';不要让livereload在gruntfile.js中工作

Javascript Can';不要让livereload在gruntfile.js中工作,javascript,gruntjs,livereload,Javascript,Gruntjs,Livereload,这是我第一次构建Gruntfile.js,一切正常,但实时重新加载,我不断收到一个任务…当我将livereload:true,添加到手表选项时出错,我是遗漏了什么还是做错了什么 我看了这里,并遵循了指南中的说明,即在选项中添加livereload并将其设置为true module.exports = function(grunt) { // configure the tasks grunt.initConfig({ //Build task copy: { build: { cw

这是我第一次构建Gruntfile.js,一切正常,但实时重新加载,我不断收到一个任务…当我将livereload:true,添加到手表选项时出错,我是遗漏了什么还是做错了什么

我看了这里,并遵循了指南中的说明,即在选项中添加livereload并将其设置为true

module.exports = function(grunt) {
// configure the tasks
grunt.initConfig({

//Build task
copy: {
  build: {
    cwd: 'source',
    src: [ '**', '!**/*.less', '!**/*.coffee', '!**/*.jade' ],
    dest: 'build',
    expand: true
  },
},

// Clean task
clean: {
  build: {
    src: [ 'build' ]
  },
  stylesheets: {
      src: [ 'build/**/*.css', '!build/application.css' ]
  },
  scripts: {
    src: [ 'build/**/*.js', '!build/application.js' ]
  },
},

// Less task
less: {
  build: {
    options: {
      linenos: true,
      compress: false
    },
    files: [{
      expand: true,
      cwd: 'source',
      src: [ '**/*.less' ],
      dest: 'build',
      ext: '.css'
    }]
  }
},

// Autoprefixer task
autoprefixer: {
  build: {
    expand: true,
    cwd: 'build',
    src: [ '**/*.css' ],
    dest: 'build'
  }
},

// CSS Minify task
cssmin: {
  build: {
    files: {
      'build/application.css': [ 'build/**/*.css' ]
    }
  }
},

// Coffee task
coffee: {
  build: {
    expand: true,
    cwd: 'source',
    src: [ '**/*.coffee' ],
    dest: 'build',
    ext: '.js'
  }
},


// Uglify
uglify: {
  build: {
    options: {
      mangle: false
    },
    files: {
      'build/application.js': [ 'build/**/*.js' ]
    }
  }
},

// Html task
jade: {
  compile: {
    options: {
      data: {}
    },
    files: [{
      expand: true,
      cwd: 'source',
      src: [ '**/*.jade' ],
      dest: 'build',
      ext: '.html'
    }]
  }
},


// Watch task
watch: {
  stylesheets: {
    files: 'source/**/*.less',
    tasks: [ 'stylesheets' ]
  },
  scripts: {
    files: 'source/**/*.coffee',
    tasks: [ 'scripts' ]
  },
  jade: {
    files: 'source/**/*.jade',
    tasks: [ 'jade' ]
  },
  copy: {
    files: [ 'source/**', '!source/**/*.less', '!source/**/*.coffee', '!source/**/*.jade' ],
    tasks: [ 'copy' ]
  }
},

// Connect to server task
connect: {
  server: {
    options: {
      port: 4000,
      base: 'build',
      hostname: '*'
      livereload: true,
    }
  }
}

});


// load the tasks
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-less');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-coffee');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jade');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-connect');

// builds and cleans the task
grunt.registerTask(
  'build',
  'Compiles all of the assets and copies the files to the build directory.',
  [ 'clean:build', 'copy', 'stylesheets', 'scripts', 'jade' ]
);

// Adds stylesheet to the task
grunt.registerTask(
  'stylesheets',
  'Compiles the stylesheets.',
  [ 'less', 'autoprefixer', 'cssmin', 'clean:stylesheets' ]
);
  // Adds javascript to the task
  grunt.registerTask(
    'scripts',
    'Compiles the JavaScript files.',
    [ 'coffee', 'uglify', 'clean:scripts' ]
  );

  //watches and connects to the server
  grunt.registerTask(
    'default',
    'Watches the project for changes, automatically builds them and runs a server.',
    [ 'build', 'connect', 'watch']
  );
};

您需要将该选项添加到监视任务的配置中。当前,您在“连接”任务中有此设置,而不是在“监视”任务中。试着这样做:

watch: {
  options: {
    livereload:true,
  },
  stylesheets: {
    files: 'source/**/*.less',
    tasks: [ 'stylesheets' ]
  },
  scripts: {
    files: 'source/**/*.coffee',
    tasks: [ 'scripts' ]
  },
  jade: {
    files: 'source/**/*.jade',
    tasks: [ 'jade' ]
  },
  copy: {
    files: [ 'source/**', '!source/**/*.less', '!source/**/*.coffee', '!source/**/*.jade' ],
    tasks: [ 'copy' ]
  }
},

您还需要将其从有效的连接任务中删除,但现在我遇到一个致命错误:端口35729已被另一个进程使用?您可以使用livereload设置指定一个新端口。请参见此处的示例设置: