現在、 require.js駆動プロジェクト用に自動ビルド スクリプト ( gruntjsを使用) をセットアップしています。そのため、必要なすべてのファイルに対してjslint / jshintを実行してから、r.js で連結および縮小したいと考えています。js フォルダーには、lint したくない開発ファイルがたくさん含まれているため、JSLint に渡すことはできません。私の最初の考えは、r.jsを実行することでしたjs/**/*.js
optimizer: 'none'
、連結されたファイルをリントしてから縮小しますが、これは2つの理由からオプションではありません. 最初に、lint したくないベンダー ライブラリが含まれます。次に、エラーのある行を見つけて、それがクラスであることを見つけ、dev フォルダーで適切な js ファイルを見つけて、そこで修正し、r.js を再度実行して、最後に lint します。繰り返しますが、私たちのワークフローにとって非常に面倒です。したがって、リンティングをr.jsオプティマイザプロセスに接続するか、少なくとも何らかの方法でrequirejs依存関係ツリーのリストを取得して、解析してリントに渡す可能性を探しています。または、自動化されたプロセスで実行可能なソリューションを思いつくでしょう。
4 に答える
最初にLintし、後でコンパイルします。lintしたいファイルについて具体的に説明し、!を使用します。特定のファイルを無視するプレフィックス:
grunt.initConfig({
lint: {
// Specify which files to lint and which to ignore
all: ['js/src/*.js', '!js/src/notthisfile.js']
},
requirejs: {
compile: {
options: {
baseUrl: 'js/src',
name: 'project',
out: 'js/scripts.js'
}
}
}
});
// Load the grunt-contrib-requirejs module.
// Do `npm install grunt-contrib-requirejs` first
grunt.loadNpmTasks('grunt-contrib-requirejs');
// Our default task (just running grunt) will
// lint first then compile
grunt.registerTask('default', ['lint', 'requirejs']);
r.js のメソッドをオーバーライドしたくない場合は、特定のバージョンに不要な依存関係を作成する可能性があります (r.js が変更された場合は、コードを更新する必要があります)。
これは私が同じ目的で使用するコードであり、require の onBuildRead 関数と、javascript のオブジェクトが参照によって渡されるという事実を利用しています。最初に require ビルドを実行してから、js ファイル ソースをリントするようにします。
欠点は、ビルドが完了した後にリントすることです。私のセットアップでは、それは問題ではありません。
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
};
この答えはGruntをバイパスしますが、やりたいことにはうまくいくはずです。私が行う方法は、r.jsを調べて、ロードされているさまざまなモジュールへのパスを受け取る関数をオーバーライドし、モジュール名をインターセプトし、r.jsがモジュールをロードおよびコンパイルしている間にファイルをリントすることです。私はそのようにそれをしました:
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を実行すると、空でないすべてのJavaScriptURLがコンソールに記録されます。それらをコンソールに記録する代わりに、URLを使用してファイルをリントすることができます。
タスクを使用する代わりに、grunt-contrib-jshintlint
をインストール、ロード、セットアップします。特定のファイルまたはファイル パス パターンを無視するオプションがあります。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'
}
}