【问题标题】:Live reload using grunt-contrib-connect and grunt-contrib-watch使用 grunt-contrib-connect 和 grunt-contrib-watch 实时重新加载
【发布时间】:2014-11-19 21:28:44
【问题描述】:

我是 nodeJS 的新手和咕噜声。我在这个项目中有这个 Gruntfile,我想为我的项目中的所有 html 文件进行实时重新加载,这样我就不必一直刷新浏览器来检测新的更改。不知何故,我遇到以下代码错误:

module.exports = function (grunt)
{
    // Project configuration.
    grunt.initConfig(
    {
        // Task configuration.
        jshint:
        {
            options:
            {
                curly: true,
                eqeqeq: true,
                immed: true,
                latedef: true,
                newcap: true,
                noarg: true,
                sub: true,
                undef: true,
                unused: true,
                boss: true,
                eqnull: true,
                browser: true,
                globals: {}
            },
            gruntfile:
            {
                src: 'Gruntfile.js'
            },
            lib_test:
            {
                src: ['lib/**/*.js', 'test/**/*.js']
            }
        },
        connect:
        {
            server:
            {
                options:
                {
                    hostname: 'localhost',
                    port: 80,
                    base: 'src',
                    keepalive: true,
                    livereload: true
                }
            }
        },
        watch:
        {
            options:
            {
                livereload:true
            }
        }

    });

    // These plugins provide necessary tasks.
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');

    // Default task.
    grunt.registerTask('default', ['connect', 'watch']);


};

似乎当我启动“grunt default”时它不会执行任务监视,因为在连接期间它是保持活动状态。

如果有任何 1 可以向我解释为什么我在 JSHint 检查我的代码并提出解决方案时会出现此错误,我将不胜感激。

【问题讨论】:

    标签: javascript node.js gruntjs grunt-contrib-watch grunt-contrib-connect


    【解决方案1】:

    您的watch 任务没有任何任务或文件。要使其与grunt-contrib-connect 一起使用,您需要包含的不仅仅是livereload 选项,如下所示:

    watch: {
        options: {
            livereload: true
        },
        taskName: {    // You need a task, can be any string
            files: [   // Files to livereload on
                "app/js/*.js",
                "app/templates/*.html"
            ]
        }
    }
    

    或者:

    watch: {
        taskName: {
            options: { // Live reload is now specific to this task
                livereload: true
            },
            files: [   // Files to livereload on
                "app/js/*.js",
                "app/templates/*.html"
            ]
        }
    }
    

    所有与此处的 glob 模式匹配的文件都应按预期工作。如果您只是为浏览器实时重新加载这些参数,则无需在此处指定 tasks 参数。

    另外,如果你打算在watch 旁边使用你的connect 服务器,你应该删除keepalive 参数,因为它是一个阻塞任务并且可以阻止执行watch 任务:

    connect: {
        server: {
            options: {
                port: 8080,
                base: 'src',
                livereload: true
            }
        }
    }
    

    【讨论】:

    • 喜欢你的建议,删除keepalive参数后,它对我有用。
    【解决方案2】:

    您的 jshint 配置中需要 node:true,请查看此示例 .jshintrc

    对于 watch 和 livereload,您需要指定要监视的文件,以及要在 Gruntfile 中执行的任务,再次查看此示例 Gruntfile

    例如这样:

     watch: {
          coffee: {
            files: ['<%%= config.app %>/scripts/{,*/}*.{coffee,litcoffee,coffee.md}'],
            tasks: ['coffee:dist']
          },
        }
    

    在此示例中,您将 glob 指定为 files 选项,并且每当文件更改时,相应的任务就会运行。

    【讨论】:

    • 对不起,我不太明白。您是在谈论另一个文件(jshint.config)还是在谈论上面 Gruntfile 中 jshint 下的选项?
    • 你可以有,在你的情况下,你在上面的 Gruntfile 中有它。 @Zackery
    • 好的,我已经让 jshint 工作了,2k 加上错误:P。但它并没有解决实时重载问题
    • 你的实时重载问题是什么更新你的问题@Zackery。
    • 当我更新我的 html 文件时,如果不刷新浏览器就看不到更改。我想要实现的是,每当我编辑和保存 html 文件时,我应该能够看到我在浏览器(localhost)中所做的更改,而无需在浏览器上执行任何操作。
    猜你喜欢
    • 2015-01-25
    • 1970-01-01
    • 2017-06-12
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2014-02-25
    • 1970-01-01
    相关资源
    最近更新 更多