Set / Remove debug flag during grunt / requirejs build

I start with and . I have a debug flag in the application, for example:

var debugEnabled = true; 

Is there a way to set the value to false automatically from the requirejs optimization performed from the grunt assembly?

EDIT: To clarify, I have only one default task that runs the requirejs optimizer. The debugEnabled variable is included in one of the modules of my application itself, say AppLogger , which is a dependency on main .

Could some kind of build method requirejs set this variable to false so that the mini version of AppLogger stops doing console.log , etc.

+6
source share
2 answers

Let's say you have two tasks:

  • Development
  • production

development does everything necessary for development, for example jshint, coffeescript, ... production requires optimization, css minification, ...

Then you can register a build task that checks your debug flag:

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

At the command line, grunt build will execute it.

Alternatively, you can use the option parameter in 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); }); 

At the command line, grunt build --target=production will execute it.

Edit:

I understood the question a little. The only way I can see is to separate your debug flag in a separate module:

path / to / debug.js

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

Then you create a production version (next to your tasks):

path / to / footman / tasks / debug.js

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

And in your requirejs task, specify this version:

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

@asgoth's answer would definitely work, but came up with a couple of other options to 'enter' (or delete quite) the code during the build process.

As described in the sample build.js file , we could use build pragmas to include / exclude code fragments during the build process.

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

I could see this in action on jquery.mobile code , which is probably a good place to learn AMD and requirejs from.

Here is what worked for me:

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

With this configuration for requirejs in my Gruntfile section within pragmas excludeStart and excludeEnd been removed from the compiled / embedded file.

I'm still studying requirejs , so there is no claim that this is the best practice for this kind of thing, but it certainly worked for me.

+15
source

All Articles