如果 JSHint 在监视任务期间失败,如何使 Grunt 构建失败?

How to fail Grunt build if JSHint fails during watch task?

这似乎是一个基本问题,但我不知道该怎么做。 .

我想在保存带有 jshint 错误的文件时使 Grunt 构建失败。输出表明 jshint 失败但 Grunt 仍然成功完成。

grunt.initConfig({
    watch: {
      js: {
        files: ['/scripts/{,**}/*.js'],
        tasks: ['newer:jshint:all']
      }
    }
})

我知道有 grunt.fail 但我如何在这里使用它?

以下要点将通过 CLI 报告 jshint 错误,并且在保存 .js 文件时无法执行任何后续构建步骤。

您需要根据自己的要求进行调整:

目录结构:

project
│
├──package.json
│
├───scripts
│   │
│   └───test.js
│
├─── Gruntfile.js
│
└───node_modules
    │
    └─── ...

package.json

{
  "name": "stack40031078",
  "version": "0.0.1",
  "description": "Answer to stack question 40031078",
  "author": "RobC",
  "license": "Apache-2.0",
  "devDependencies": {
    "grunt": "^1.0.1",
    "grunt-contrib-jshint": "^1.0.0",
    "grunt-contrib-watch": "^1.0.0",
    "grunt-newer": "^1.2.0"
  }
}

Gruntfile.js

module.exports = function (grunt) {

    grunt.initConfig({

        pkg: grunt.file.readJSON('package.json'),

        // VALIDATE JS
        jshint: {
            // Note we're using 'src:' instead of 'all:' below.
            files: {
                src: './scripts/{,**}/*.js'
            },
            options: {
                // Use your jshint config here or define them in
                // a separate .jshintrc file and set the flag to:
                //
                // jshintrc: true
                curly: true,
                eqeqeq: true,
                immed: true,
                latedef: true,
                newcap: true,
                noarg: true,
                sub: true,
                undef: true,
                boss: true,
                eqnull: true,
                browser: true,
                smarttabs: true,
                globals: {}
            }
        },

        // WATCH THE JS FILES
        watch: {
            js: {
                files: ['./scripts/{,**}/*.js'],
                // NOTE: we're not using 'newer:jshint:all' below, just 'newer:jshint'
                tasks: ['newer:jshint' /* <-- Add subsequent build tasks here. E.g. ,'concat' - A registered task can also be added. E.g. 'default' */]
            }
        }

    });

    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-newer');

    grunt.registerTask('default', [

    ]);

};

test.js

console.log('Hello World');

var test = function() {
    return 'test';
};

测试演示要点

  1. cdproject 目录
  2. 运行 $ npm install
  3. 运行 $ grunt watch
  4. 打开 test.js 并进行简单编辑(例如,在文件末尾添加新行),然后保存更改。

CLI报错如下:

    Running "jshint:files" (jshint) task
       ./scripts/test.js
          1 |console.log('Hello Universe');
             ^ 'console' is not defined.

    >> 1 error in 1 file
    Warning: Task "jshint:files" failed. Use --force to continue.

    Aborted due to warnings.
    Completed in 0.965s at Fri Oct 14 2016 10:22:59 GMT+0100 (BST) - Waiting...

注意:

watch.js 对象的 tasks 数组中指定的任何后续构建任务(例如 Gruntfile.js 中注释的 concat)将不会被调用使用此要点作为 jshint 任务失败(...当然 concat 任务尚未定义!)。

但是,当 JavaScript file/s 成功通过 jshint 任务时,[=23= 的 tasks 数组中定义的任何后续构建任务] 对象将被调用。

希望对您有所帮助!