2013-03-04 8 views
6

Zaczynam od i . Mam flagę debugowania w aplikacji jak:Ustaw/Wyłącz flagę debugowania podczas kroku/requirejs build

var debugEnabled = true; 

Czy jest jakiś sposób ten może być ustawiony na false automatycznie z poziomu optymalizacji requirejs prowadzony od wewnątrz grunt budować?

EDIT: celu wyjaśnienia, mam tylko jedno domyślne zadanie uruchamiające requirejs Optimizer. Zmienna debugEnabled znajduje się w jednym z modułów w mojej aplikacji, na przykład AppLogger, która jest zależnością od main.

Czy jest jakiś sposób kompilacja requirejs można ustawić tę zmienną na false, tak że minified wersja AppLogger by przestać robić console.log itp

Odpowiedz

6

Powiedzmy, że mają dwa zadania:

  • rozwoju
  • produkcja

development nie wszystkie rzeczy yo u potrzeba rozwoju, jak jshint, coffeescript kompilacji ... production robi optymalizacji requirejs, CSS minifikacji ...

Następnie można zarejestrować się build zadania, które sprawdza flagę debug:

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

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

W linii poleceń wykona je grunt build.

Alternatywnie, można użyć parametru option w grunt:

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

w linii poleceń, grunt build --target=production będzie go wykonać.

Edit:

Misunderstood pytanie nieco. Jedynym sposobem, widzę to, aby oddzielić swoją flagę debugowania w oddzielnym module:

ścieżka/do/debug.js

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

Następnie należy utworzyć wersję produkcyjną (blisko swoich zadań Grunt):

ścieżka/do/grunt/zadania/debugowanie.js

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

A w zadaniu requirejs, określić, że wersję:

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

Dzięki za odpowiedź. To, czego szukałem, jest nieco inne i starałem się je wyjaśnić, aktualizując to pytanie. –

+0

Zmieniono moją odpowiedź. Sprawdź, czy to pomaga. – asgoth

+0

To zdecydowanie by działało, ale znalazłem inną opcję budowania 'pragmas' z przykładowego pliku budowania [r.js] (https://github.com/jrburke/r.js/blob/master/build/example.build .js). Najwyraźniej to właśnie "jquery.mobile" [również używa] (https://github.com/jquery/jquery-mobile/blob/master/js/jquery.mobile.events.js). Zaakceptowałem twoją odpowiedź, ale dodam kolejną z mojej, która działa dla mnie. Dziękuję Ci! –

15

@asgoth „s answer pewno będzie działać, ale zorientowali się kilka innych opcji, jak również do 'wstrzyknąć' (lub usuń raczej) kod podczas procesu budowania.

Zgodnie z dokumentacją w dokumencie example build.js file możemy wykorzystać kompilację pragmas do dołączania/wykluczania fragmentów kodu podczas procesu kompilacji.

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

mogłem zobaczyć w akcji na jquery.mobilecode, która prawdopodobnie jest dobrym miejscem do nauki AMD i requirejs z.

Oto co pracował dla mnie:

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

Przy tej konfiguracji dla requirejs w moim Gruntfile , sekcja w obrębie pragmy z excludeStart i excludeEnd zostały usunięte z skompilowanego/skompilowanego pliku.

Nadal uczę się requirejs, więc nie twierdzę, że jest to najlepsza praktyka dla tego typu rzeczy, ale to na pewno działało dla mnie.

+2

czy nie lepiej (z punktu widzenia optymalizacji) zawijać jakąkolwiek wiadomość w takich komentarzach, a nie w flagę? – shabunc

Powiązane problemy