2013-03-04 5 views
6

मैं और दोनों से शुरू कर रहा हूं। मैं जैसे एप्लिकेशन में कोई डिबग ध्वज है:grunt/requirejs के दौरान डीबग ध्वज सेट/अनसेट करें

var debugEnabled = true; 

वहाँ किसी तरह इस false करने के लिए सेट किया जा सकता है एक grunt निर्माण के भीतर से चलाया जा रहा requirejs अनुकूलन के भीतर से स्वचालित रूप से है?

संपादित करें: स्पष्ट करने के लिए, मैं केवल एक डिफ़ॉल्ट कार्य है कि requirejs अनुकूलक चलाता है। परिवर्तनीय debugEnabled मेरे ऐप के भीतर मॉड्यूल में से एक के भीतर है, AppLogger कहें, जो main पर निर्भरता है।

वहाँ किसी तरह requirejs निर्माण false को यह चर सेट कर सकते हैं, ताकि AppLogger की minified संस्करण console.log आदि

उत्तर

6

कर बंद कर देंगे मान लीजिए कि आप दो टास्क करते हैं:

  • विकास
  • उत्पादन

development सब सामान यो करता है यू विकास में की जरूरत है, JSHint, coffeescript संकलन की तरह, ... production requirejs अनुकूलन, सीएसएस minification, ...

तो फिर तुम एक build कार्य जो अपने डिबग ध्वज की जाँच करता है रजिस्टर कर सकता है:

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

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

कमांडलाइन पर, grunt build इसे निष्पादित करेगा।

वैकल्पिक रूप से, आप घुरघुराना में option पैरामीटर इस्तेमाल कर सकते हैं:

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); 
    }); 

कमांडलाइन पर, grunt build --target=production यह निष्पादित करेंगे।

संपादित करें:

गलत समझा सवाल थोड़ा। एक ही रास्ता मैं देख रहा हूँ एक अलग मॉड्यूल में अपने डिबग ध्वज को अलग करने के लिए है:

path/to/debug.js

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

तो फिर तुम एक उत्पादन संस्करण (अपने घुरघुराना कार्यों के पास) बनाने के लिए:

पथ/से/grunt/कार्यों/डीबग।js

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

और अपने requirejs कार्य में, आप वह संस्करण निर्दिष्ट करें:

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

आपके उत्तर के लिए धन्यवाद। जो मैं खोज रहा था वह थोड़ा अलग है और मैंने इसे प्रश्न के अपडेट के साथ स्पष्ट करने की कोशिश की है। –

+0

मेरा जवाब संपादित किया गया। देखें कि क्या मदद करता है। – asgoth

+0

यह निश्चित रूप से काम करेगा, लेकिन [r.js उदाहरण बिल्ड फ़ाइल] से [pragmas' बनाने का एक और विकल्प निकाला गया है (https://github.com/jrburke/r.js/blob/master/build/example.build .js)। यह जाहिर है कि 'jquery.mobile' [भी उपयोग करता है] (https://github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.events.js)। मैंने आपका जवाब स्वीकार कर लिया है, लेकिन मेरे लिए जो कुछ भी काम कर रहा है उसके साथ मेरा दूसरा जोड़ देगा। धन्यवाद! –

15

@asgoth के answer निश्चित रूप से काम करेगा, लेकिन करने के साथ ही अन्य विकल्पों में से जोड़ी पता लगा 'इंजेक्षन' (या निर्माण प्रक्रिया के दौरान कोड को हटा दें) कोड।

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 
}, 

मैं jquery.mobilecode पर कार्रवाई, जो शायद से AMD और requirejs सीखने के लिए एक अच्छी जगह है में इस देख सकता था।

यहाँ क्या मेरे लिए काम किया है:

AppLogger.js:

/* 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:

requirejs:{ 
    compile:{ 
    options:{ 
     baseUrl:"js/", 
     mainConfigFile:"js/main.js", 
     name:'main', 
     out:'js/main.min.js', 
     pragmas:{ appBuildExclude:true } 
    } 
    } 
} 
requirejs के लिए इस कॉन्फ़िगरेशन के साथ

मेरी Gruntfile में ,के प्रागम्स के भीतर खंडऔर excludeEnd संकलित/निर्मित फ़ाइल से अलग हो गए थे।

मैं अभी भी requirejs सीख रहा हूं, इसलिए कोई दावा नहीं है कि यह इस तरह की चीज़ के लिए सबसे अच्छा अभ्यास है, लेकिन यह निश्चित रूप से मेरे लिए काम करता है।

+2

क्या यह बेहतर होगा (अनुकूलन बिंदु से) इस तरह की टिप्पणियों में कोई लॉग संदेश लपेटने के लिए, ध्वज नहीं? – shabunc

संबंधित मुद्दे