Gruntjs Grunt使用watch进行无生命编译

Gruntjs Grunt使用watch进行无生命编译,gruntjs,less,grunt-contrib-watch,Gruntjs,Less,Grunt Contrib Watch,我知道有一些非常类似的问题,但我无法让它发挥作用 我正在使用grunt,包括连接、减少和观看。一切都正常,除了.css的livecompilement,它使用的是less和watch。像这样,我总是要重新启动grunt等。 这是我的代码: module.exports = function(grunt) { grunt.initConfig({ pkg: grunt.file.readJSON('package.json'), connect: {

我知道有一些非常类似的问题,但我无法让它发挥作用

我正在使用grunt,包括连接、减少和观看。一切都正常,除了.css的livecompilement,它使用的是less和watch。像这样,我总是要重新启动grunt等。 这是我的代码:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        connect: {
            server: {
                options: {
                    port: 8000,
                    hostname: 'localhost',
                    base: 'public',
                    keepalive: true
                }
            }
        },
        less: {
            development: {
                options: {
                    compress: true,
                    yuicompress: true,
                    optimization: 2
                },
                files: {
                    "public/style/style.css": "public/style/main.less"
                }
            }
        },
        watch: {
            files: ['**/*'],
            tasks: ['less', 'connect'],

            styles: {
                files: ['public/style/main.less'], //which files to watch
                tasks: ['less'],
                options: {
                    livereload: true
                }
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-less');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', ['less','connect','watch']);
};

如果需要更多信息,请告诉我。

您必须在html文件中添加livereload代码段才能使其正常工作:

<script>
   document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js?snipver=1"><\/script>')
</script>

文件。写入(“”)
35729
是livereload服务器的端口


有关更多信息,请参阅。你也应该读一读。

除了马里奥的答案,我试了一下

我找到了这样的解决方案: 我独自运行watch,没有使用“连接”或“连接”,我注意到它在控制台中留下了一些东西,当它与“连接”和“连接”和“连接”一起运行时,我在控制台中没有看到这些东西。我注意到的下一件事是控制台中的connect条目中的“永远等待…”。像这样,什么也没有发生,因为它永远在等待。这样做的结果是,监视无法执行。这也解释了为什么我从不在控制台中观看

因此,解决办法是: 我不得不从Grunfile中的connect中删除“keepalive:true”