4

我是 nodeJS 和 grunt 的新手。我在这个项目中有这个 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”时,它不会执行任务监视,因为在连接期间它是保持活动状态。

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

4

2 回答 2

6

您的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如果您只是为浏览器实时重新加载这些参数,则无需在此处指定参数。

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

connect: {
    server: {
        options: {
            port: 8080,
            base: 'src',
            livereload: true
        }
    }
}
于 2014-10-22T16:07:52.483 回答
0

你需要node:true在你的 jshint 配置中,看看这个例子.jshintrc

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

例如像这样:

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

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

于 2014-09-25T07:56:11.463 回答