14

我正在尝试使用 Angular 和 Browserify 构建一个项目。我的controllers.js文件看起来像这样...

'use strict';

module.exports.testController = function($scope){
    $scope.message = 'Controller 1';
    console.log( 'hello' );
};

如您所料,这会产生三个 linting 错误。

  • 使用 Strict 的函数形式
  • “模块”未定义
  • “控制台”未定义

我确实在这里找到了一些解决方案,它使 JSHint 能够通过jslint node: true像这样放在文件顶部来处理 Node.js 文件

   /*jslint node: true */
   'use strict';

    module.exports.testController = function($scope){
        $scope.message = 'Controller 1';
        console.log( 'hello' );
    };

但是,这显然解决了太多问题;'console.log(...)' 仍应未定义。

有谁知道如何将 JSHint 与 Browserify 一起使用?

4

2 回答 2

30

2.5.3版开始, JSHint 支持该browserify标志。

像所有标志一样,您可以直接在源文件中使用它:

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

或者将其添加到.jshintrc文件中:

{
   "browserify": true
}
于 2014-08-03T14:17:00.993 回答
3

我讨厌回答自己的问题,感觉就像是在偷窃,但无论如何,这就是答案。有几种方法可以给这只特殊的猫剥皮,但这个解决方案可能是最“正确”的......


修改.jshintrc

你应该做的第一件事是修改你的 .jshintrc 所以

"globals": {
    "define": false
}

变成

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

修改代码

现在你需要像这样修改代码

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;

}());

现在 JSHint 将显示 linting 错误,console.log但不会显示modules. 这是由.jshintrc修正案提供的。

lintinguse strict错误已修复,我将所有代码包装在一个函数中。


要求()

更进一步,由于我们使用的是 browserify,我们还需要require(). 所以我需要再修改.jshintrc一次。

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

注意:我已从define全局变量中删除,因为我没有使用它。

于 2014-01-29T14:25:35.467 回答