2013-03-04 6 views
6

私はの両方から始めています。私のようなアプリでデバッグフラグを持っている:grunt/requirejsビルド中にデバッグフラグを設定/解除する

var debugEnabled = true; 

が、これは自動的に最適化requirejs内からgruntビルド内から実行され、falseに設定することができ、いくつかの方法はありますか?

EDIT: 明確にするために、私はrequirejsオプティマイザを実行している唯一のデフォルトのタスクを持っています。変数debugEnabledは、mainへの依存性である、AppLoggerと言う私自身のモジュール内の1つのモジュール内にあります。

AppLoggerの縮小さバージョンがconsole.logをやって停止するようにrequirejsビルドは、falseにこの変数を設定することができますいくつかの方法がなどあり

答えて

6

のは、次の2つのタスクがあるとしましょう:

  • 開発を
  • 生産

すべてのものuがjshintのように、開発に必要な、CoffeeScriptのコンパイル、... productionはrequirejs最適化、CSSの縮小を行います...

次に、あなたがあなたのデバッグフラグをチェックbuildタスク登録ができます

grunt.registerTask('build', 'run build', function() { 
     var task = debugEnabled ? 'development' : 'production'; 

     // run the targetted task 
     grunt.task.run(task); 
    }); 

をコマンドラインでgrunt buildが実行されます。 grunt build --target=productionはそれを実行し、コマンドラインで

grunt.registerTask('build', 'run build', function() { 
     // start development build by default 
     var target = grunt.option('target') || 'development'; 

     // run the targetted task 
     grunt.task.run(target); 
    }); 

また、あなたは作男でoptionパラメータを使用することができます。

編集:

誤解質問ビット。私が見る唯一の方法は、別のモジュールにデバッグフラグを分離することである:

:/そして、あなたは(あなたの単調な作業に近い)製品版を作成

define(function() { 
    return true; 
}); 

をdebug.jsとするのが最良する

パス/

パス/ to/grunt/tasks/debug。JS

define(function() { 
    return false; 
}); 

そして、あなたのrequirejsタスクでは、あなたはそのバージョンを指定します。

requirejs: { 
    options: { 
     paths: { 
     debug: 'path/to/grunt/tasks/debug.js' 
     } 
    } 
} 
+0

あなたの答えをありがとう。私が探していたものは若干異なり、私は質問の更新でそれを明確にしようとしました。 –

+0

私の答えを編集しました。それが役立つかどうかを見てください。 – asgoth

+0

これは間違いなく動作しますが、[r.js example build file](https://github.com/jrburke/r.js/blob/master/build/example.build)から 'pragmas'をビルドする別のオプションを考え出しました。 .js)。これは明らかに 'jquery.mobile'も[https://github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.events.js]を使用しています。私はあなたの答えを受け入れましたが、私のために働いているものを自分の別のものに追加します。ありがとうございました! –

15

@asgothanswerは間違いなく動作しますが、これらに同様に他のオプションのカップルを考え出し 『』注入(またはビルドプロセス中にコードを削除する)。

example build.js fileに記載されているように、ビルドプロセス中にコードスニペットを組み込む/除外するには、ビルドpragmasを使用できます。

//Specify build pragmas. If the source files contain comments like so: 
//>>excludeStart("fooExclude", pragmas.fooExclude); 
//>>excludeEnd("fooExclude"); 
//Then the comments that start with //>> are the build pragmas. 
//excludeStart/excludeEnd and includeStart/includeEnd work, and the 
//the pragmas value to the includeStart or excludeStart lines 
//is evaluated to see if the code between the Start and End pragma 
//lines should be included or excluded. If you have a choice to use 
//"has" code or pragmas, use "has" code instead. Pragmas are harder 
//to read, but they can be a bit more flexible on code removal vs. 
//has-based code, which must follow JavaScript language rules. 
//Pragmas also remove code in non-minified source, where has branch 
//trimming is only done if the code is minified via UglifyJS or 
//Closure Compiler. 
pragmas: { 
    fooExclude: true 
}, 

//Same as "pragmas", but only applied once during the file save phase 
//of an optimization. "pragmas" are applied both during the dependency 
//mapping and file saving phases on an optimization. Some pragmas 
//should not be processed during the dependency mapping phase of an 
//operation, such as the pragma in the CoffeeScript loader plugin, 
//which wants the CoffeeScript compiler during the dependency mapping 
//phase, but once files are saved as plain JavaScript, the CoffeeScript 
//compiler is no longer needed. In that case, pragmasOnSave would be used 
//to exclude the compiler code during the save phase. 
pragmasOnSave: { 
    //Just an example 
    excludeCoffeeScript: true 
}, 

私はおそらくからAMDrequirejsを学ぶための良い場所ですjquery.mobilecode上のアクション、でこれを見ることができました。ここで

は私のために働いていたものです:

AppLogger.js:私のGruntfilerequirejsため、このような構成により

requirejs:{ 
    compile:{ 
    options:{ 
     baseUrl:"js/", 
     mainConfigFile:"js/main.js", 
     name:'main', 
     out:'js/main.min.js', 
     pragmas:{ appBuildExclude:true } 
    } 
    } 
} 

/* global console: false */ 
define(function() { 

    var debugEnabled = false; 
//>>excludeStart('appBuildExclude', pragmas.appBuildExclude); 
    debugEnabled = true; 
//>>excludeEnd('appBuildExclude'); 
    return { 
    log:function (message) { 
     if (debugEnabled && console) { 
     console.log('APP DEBUG: ' + message); 
     } 
    } 
    }; 

}); 

Gruntfile.js 、プラグマ内のセクションはですおよびexcludeEndは、コンパイル/ビルドされたファイルから削除されました。

私はまだrequirejsを学んでいますので、これはこの種のもののベストプラクティスではありませんが、確かに私のために働いています。

+2

これは、(最適化の観点から)このようなコメントではなく、フラグで任意のログメッセージをラップする方が良いでしょうか? – shabunc

関連する問題