2012-10-17 18 views
9

私は現在、主導プロジェクトの自動ビルドスクリプト()を設定しています。そのため、すべての必要なファイルについて/を実行してから、r.jsを連結して縮小します。 jsフォルダにはたくさんの開発ファイルが入っているので、lintを使いたくないので、js/**/*.jsをJSLintに渡すことはできません。私の最初の考えは、optimizer: 'none'でr.jsを実行し、連結したファイルをlintしてからそれを小さくすることでしたが、これは2つの理由からオプションではありません。まず、ベンダーのlibsが含まれています。私は、lintと2番目のエラーを見つけて、そのクラスを見つけ、devフォルダ内の適切なjsファイルを見つけてそこに修正し、r.jsをもう一度実行して、もう一度、私たちのワークフローのために非常に面倒です。ですから、lintをr.jsオプティマイザプロセスに接続するか、少なくとも何らかの方法でrequirejs依存関係ツリーのリストを取得し、それを解析してlintに渡すことができるかどうかを探しています。あるいは、自動化されたプロセスのために実用的な解決策があれば、それを思いつくでしょう。JSLint/Hint with requirejsを使用

+0

https://github.com/jshint/jshint#ignoring-files-and-directoriesような何か:

は、ここに私の仕事ですか? –

答えて

1

この回答はバイパスのグランツですが、あなたがしたいことでうまくいくはずです。 r.jsを見て、ロードされているさまざまなモジュールへのパスを受け取り、モジュール名をインターセプトし、r.jsがロードしてモジュールをコンパイルしている間にファイルをlintする関数をオーバーライドしようとします。私はそうのようにそれをやった:

var requirejs = require('requirejs'); 
var options = {/*r.js options as JSON*/}; 
var oldNewContext = requirejs.s.newContext; 
requirejs.s.newContext = function(){ 
    var context = oldNewContext.apply(this, arguments); 
    var oldLoad = context.Module.prototype.load; 
    context.Module.prototype.load = function(){ 
     var module = oldLoad.apply(this, arguments); 

     if(/\.js$/.test(this.map.url) && !/^empty:/.test(this.map.url)) 
      console.log(this.map.url); 

     return module; 
    } 
    return context; 
} 
requirejs.optimize(options) 

を次に、あなたがあなたのモジュール上requirejs.optimize実行すると、コンソールにログインし、すべての非空のJavaScriptのURLを取得する必要があります。コンソールにログを記録する代わりに、URLを使用してファイルをlintすることができます。

+0

これはまさに私が探していたものだと思う。明日テストするつもりだ。 –

2

最初に、コンパイルしてください。あなたが糸くずにしたいと思っているファイルを特定して使用してください!接頭辞は特定のファイルを無視します:

+0

2つのファイル(main.jsとgrunt)を維持しなければならないので、間違いなく良い方法ですが、IMHOは非常に繰り返します。 ...あなたの答えはいつもおかげさまです。 –

2

私はr.jsのメソッドをオーバーライドしていない好む、またはあなたが特定のバージョンに不要な依存関係を作成します(あなたのコードが変更をr.jsすべき更新する必要があります)

これは、私が使用したコードです同じ目的で、requireのonBuildRead関数とjavascriptのオブジェクトが参照渡しされるという事実を利用しています。私はrequireビルドを最初に実行した後、jsファイルのソースをリントするようにします。

欠点は、ビルドが完了した後にリントすることです。私のセットアップには問題はありません。代わりにlintタスクを使用しての

module.exports = function(grunt) { 



var jsHintOptions = { 
     options: { 
      curly: true, 
      eqeqeq: true, 
      eqnull: true, 
      browser: true, 
      globals: { 
       jQuery: true 
      } 
     }, 
     all: [] // <--- note this is empty! We'll fill it up as we read require dependencies 
    }; 

var requirejsOptions = { 
     compile: { 
      options: { 
       paths: { 
        "jquery": "empty:" 
       }, 
       baseUrl: "./", 
       name: "src/mypackage/main", 
       mainConfigFile: "src/mypackage/main.js", 
       out: 'build/mypackage/main.js', 
       onBuildRead: function (moduleName, path, contents) { 
        jsHintOptions.all.push(path); // <-- here we populate the jshint path array 
        return contents; 
       } 
      } 
     } 
    }; 

grunt.initConfig({ 
    pkg: grunt.file.readJSON('packages/mypackage.package.json'), 
    requirejs: requirejsOptions, 
    jshint: jsHintOptions 
}); 

// load plugin that enabled requirejs 
grunt.loadNpmTasks('grunt-contrib-requirejs'); 

// load code quality tool 
grunt.loadNpmTasks('grunt-contrib-jshint'); 


grunt.registerTask('default', ['requirejs', 'jshint']); // <-- make sure your run jshint AFTER require 
}; 
0

、インストール、ロード、およびgrunt-contrib-jshintを設定します。特定のファイルまたはファイルパスパターンを無視するオプションがignoresです。

jshint: { 
    options: { 
     // options here to override JSHint defaults 
     boss : true, // Suppress warnings about assignments where comparisons are expected 
     browser : true, // Define globals exposed by modern browsers (`document`, `navigator`) 
     curly : false, // Require curly braces around blocks 
     devel : false, // Define `console`, `alert`, etc. (poor-man's debugging) 
     eqeqeq : false, // Prohibit the use of `==` and `!=` in favor of `===` and `!==` 
     "-W041" : false, // Prohibit use of `== ''` comparisons 
     eqnull : true, // Suppress warnings about `== null` comparisons 
     immed : true, // Prohibit the use of immediate function invocations w/o wrapping in parentheses 
     latedef : true, // Prohibit the use of a var before it's defined 
     laxbreak: true, // Suppress warnings about possibly unsafe line breaks 
     newcap : true, // Require you to capitalize names of constructor functions 
     noarg : true, // Prohibit the use of `arguments.caller` and `arguments.callee` 
     shadow : true, // Suppress warnings about var shadowing (declaring a var that's declared somewhere in outer scope) 
     sub  : true, // Suppress warnings about using `[]` notation, e.g. `person['name']` vs. `person.name` 
     trailing: true, // Trailing whitespace = error 
     undef : false, // Prohibit the use of explicitly undeclared variables 
     unused : false, // Warn when you define and never use your variables 
     white : false, // Check JS against Douglas Crawford's coding style 
     jquery : true, // Define globals exposed by jQuery 
     // Define global functions/libraries/etc. 
     globals : { 
      amplify : true 
     }, 
     ignores: [ 
      'src/app/templates/template.js', 
      'src/scripts/plugins/text.min.js' 
     ] 
    }, 
    gruntfile: { 
     src: 'Gruntfile.js' 
    }, 
    app: { 
     src: 'src/app/**/*.js' 
    }, 
    scripts: { 
     src: 'src/scripts/**/*.js' 
    } 
} 
関連する問題