5

我想在 中运行该jshint任务两次GruntJS,但每次都有不同的选项。

我将如何去做这样的事情?

目前,我的Gruntfile.js样子是这样的:

'use strict';

module.exports = function(grunt) {
    var opts = {
        pkg: grunt.file.readJSON('package.json'),
        jasmine_node: {
            matchall: true,
            forceExit: true
        },
        jshint: {
            files: [
                'gruntfile.js',
                'src/**/*.js', '!src/static/bin', '!src/static/js/libs/*.js',
                'test/spec/**/*.js'
            ],
            options: {
                jshintrc: '.jshintrc'
            }
        }
    };

    grunt.initConfig(opts);

    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-jasmine-node');

    grunt.registerTask('default', ['jshint', 'jasmine_node']);
    grunt.registerTask('travis', ['jshint', 'jasmine_node']);
};

我可能希望它是这样的:

var jshint: {
    files: [
        'gruntfile.js',
        'src/**/*.js', '!src/static/**',
        'test/spec/**/*.js'
    ],
    options: {
        jshintrc: '.jshintrc'
    }
}

var jshint2 = {
    files: [
        'src/static/**/*.js',
        '!src/static/bin',
        '!src/static/js/libs/*.js'
    ],
    options: {
        jshintrc: '.jshintrc-browser'
    }
};

这样我就可以为前端 javascript 和 NodeJS javascript 选择不同的选项,因为每个环境的 linting 应该是不同的

4

1 回答 1

7

jshint is a multitask. So it is possible to have to following config:

jshint: {
  browser: {
    files: [
        'src/static/**/*.js',
        '!src/static/bin',
        '!src/static/js/libs/*.js'
    ],
    options: {
        jshintrc: '.jshintrc-browser'
    }
  },
  node: {
    files: [
        'gruntfile.js',
        'src/**/*.js', '!src/static/**',
        'test/spec/**/*.js'
    ],
    options: {
        jshintrc: '.jshintrc'
    }
  }
}

To run the browser version:

grunt jshint:browser.

To run to node version:

grunt jshint:node.

Running just:

grunt jshint

will run both.

Of course, you probably will want to trigger them via another task, e.g:

grunt.registerTask('build', ['clean:all', 'jshint:browser', ...]);

You want want to read the section Specifying JSHint options and globals for more info.

于 2013-03-19T18:24:55.880 回答