简体   繁体   English

通过观看Grunt Live-Reload

[英]Grunt Live-Reload via Watch

I'm trying to configure grunt to livereload js and less/css files on changes. 我正在尝试将grunt配置为livereload js和less / css文件的更改。 While grunt does correctly "watch" and execute assigned tasks, it does not livereload the files. 虽然grunt正确地“监视”并执行分配的任务,但它不会对文件进行实时重载。 Below is my configuration, does anyone see what is wrong? 下面是我的配置,有没有人看到有什么问题?

module.exports = function(grunt) {
grunt.initConfig({
    pkg: grunt.file.readJSON("package.json"),
    jshint: {
        files: ["Gruntfile.js", "src/javascripts/**/*.js"],
        options: {
            globals: {
                jQuery: true,
                console: true,
                module: true
            }
        }
    },
    concat: {
        options: {
            stripBanners: true,
            banner: "/*! <%= pkg.name %> - v<%= pkg.version %> - <%= grunt.template.today('yyyy-mm-dd') %> */\n",
            separator: "\n"
        },
        js: {
            src: ["src/javascripts/main.js", "src/javascripts/**/*.js"],
            dest: "../app/assets/javascripts/application.js"
        },
        less: {
            src: ["src/stylesheets/**/*.less"],
            dest: "../app/assets/stylesheets/application.less"
        }
    },
    watch: {
        js: {
            files: ["<%= jshint.files %>"],
            tasks: ["jshint", "concat:js"],
            options: {
                livereload: true
            }
        },
        less: {
            files: ["<%= concat.less.src %>"],
            tasks: ["concat:less"],
            options: {
                livereload: true
            }
        }
    }
});

grunt.loadNpmTasks("grunt-contrib");

grunt.registerTask("default", ["jshint", "concat"]);
};

Note: I have included the following script tag within the html head tag. 注意:我在html head标记中包含以下脚本标记。

<script src="http://localhost:35729/livereload.js"></script>

Your config is trying to start 2 live reload servers on the same port. 您的配置尝试在同一端口上启动2个实时重新加载服务器。 If you would like 1 live reload server to trigger on all your watch targets then just add 1 livereload option at the task level: 如果您希望在所有监视目标上触发1个实时重装服务器,则只需在任务级别添加1个livereload选项:

watch: {
  options: {
    livereload: true
  },
  js: {
    files: ["<%= jshint.files %>"],
    tasks: ["jshint", "concat:js"],
  },
  less: {
    files: ["<%= concat.less.src %>"],
    tasks: ["concat:less"],
  }
}

I was missing the script tag and after adding this 我错过了脚本标签并在添加之后

it started working for me. 它开始为我工作。 :)! :)!

Thanks, 谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM