How do you use JSHint and Browserify together?

大城市里の小女人 提交于 2019-11-30 11:50:07

As of version 2.5.3 JSHint supports the browserify flag.

Like all flags you can use it directly in a source file:

/*jshint browserify: true */
// browserify code here

Or add it to a .jshintrc file:

{
   "browserify": true
}

I hate answering my own questions, it feels like theft, but nevertheless, here's the answer. There's a couple of ways to skin this particular cat, but this solution is probably the most "correct"...


Modify .jshintrc

The first thing you should do is modify your .jshintrc so

"globals": {
    "define": false
}

becomes

"globals": {
    "define": false,
    "module": false
}

Modify the code

Now you need to modify the code like this

module.exports = (function(){

    'use strict';

    var myComponent = {};

    myComponent.testController = function($scope){

        $scope.message = 'hello';
        console.log( 'Hello' );

    };

    myComponent.testDirective= function($scope){

        $scope.message = 'hello';
        console.log( 'Hello' );

    };

    return myComponent;

}());

Now JSHint will show linting errors for console.log but not for modules. This is courtesy of the .jshintrc ammend.

The use strict linting error is fixed my wrapping all the code in a function.


require()

Taking this a step further, since we're using browserify, we'll also need require(). So I need to modify .jshintrc one more time.

"globals": {
    "module": false,
    "require": false
}

Note: I've removed define from globals because I'm not using it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!