2013-03-04 63 views
6

我从开始。我有一个调试标志的应用程式,例如:在grunt/requirejs编译期间设置/取消设置调试标志

var debugEnabled = true; 

requirejs优化内是否有某种方式这可以被设置为false,自动从grunt构建内正在运行?

编辑: 为了澄清,我有一个运行的requirejs优化只有一个默认的任务。变量debugEnabled位于我的应用程序本身内的其中一个模块内,如AppLogger,它与main相关。

是否有某种方式requirejs构建可以将此变量设置为false,使AppLogger缩小的版本将停止做console.log

回答

6

比方说,你有两个任务:

  • 发展
  • 生产

development做所有的东西哟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将执行它。

或者,你可以在咕噜使用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将执行它。

编辑:

误解了这个问题有点。我看到的唯一的办法就是你的调试标志在一个单独的模块分开:

路径/到/ debug.js

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

然后创建一个产品版本(靠近你的呼噜声任务):

path/to/grunt/tasks/debug。JS

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

而在你requirejs任务,您可以指定版本:

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

感谢您的回答。我所寻找的东西稍有不同,我试图通过对问题的更新来澄清它。 – 2013-03-04 12:51:52

+0

编辑我的答案。看看是否有帮助。 – asgoth 2013-03-04 13:31:42

+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)。我已经接受了你的答案,但是会为我的工作增加另一个我自己的答案。谢谢! – 2013-03-04 17:23:42

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

我可以在jquery.mobilecode行动,这可能是从学习AMDrequirejs的好地方看到这一点。

这里是我工作:

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

利用这种配置为requirejsGruntfile ,的编译内部分从编译/构建的文件中剥离了和excludeEnd

我还在学习requirejs,所以没有人声称这是这类事情的最佳实践,但这肯定对我有用。

+2

是不是会更好(从优化的角度来看)在这些评论中包装任何日志消息,而不是标志? – shabunc 2013-10-02 16:37:40