Har*_*til 6 javascript debugging build requirejs gruntjs
我从requirejs和gruntjs开始.我在应用程序中有一个调试标志,如:
var debugEnabled = true;
Run Code Online (Sandbox Code Playgroud)
是否有某种方法可以设置为false,从构建中requirejs运行的优化中自动设置grunt?
编辑:
为了澄清,我只有一个默认任务运行requirejs优化器.该变量debugEnabled位于我的应用程序本身的一个模块中,比如说AppLogger,它是一个依赖项main.
是否有一些方法requirejs可以将此变量设置为false,以便缩小版本AppLogger将停止执行console.log等.
Har*_*til 15
@asgoth的答案肯定会起作用,但在构建过程中找出了几个其他选项以及"注入"(或删除)代码.
如示例build.js文件中所述,我们可以使用构建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
},
Run Code Online (Sandbox Code Playgroud)
我可以在jquery.mobile 代码中看到这个,这可能是学习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);
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
Gruntfile.js:
requirejs:{
compile:{
options:{
baseUrl:"js/",
mainConfigFile:"js/main.js",
name:'main',
out:'js/main.min.js',
pragmas:{ appBuildExclude:true }
}
}
}
Run Code Online (Sandbox Code Playgroud)
有了这个配置对于requirejs我Gruntfile,的编译指示内的部分excludeStart,并excludeEnd从编译/内置文件剥离.
我还在学习requirejs,所以没有人声称这是这类事情的最佳实践,但这对我来说肯定有用.
假设你有两个任务:
development你需要开发所需的所有东西,比如jshint,coffeescript编译,......需要
production优化,css缩小,......
然后你可以注册一个build检查你的调试标志的任务:
grunt.registerTask('build', 'run build', function () {
var task = debugEnabled ? 'development' : 'production';
// run the targetted task
grunt.task.run(task);
});
Run Code Online (Sandbox Code Playgroud)
在命令行上,grunt build将执行它.
或者,您可以在grunt中使用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);
});
Run Code Online (Sandbox Code Playgroud)
在命令行上,grunt build --target=production将执行它.
编辑:
误解了这个问题.我看到的唯一方法是在单独的模块中分离调试标志:
路径/到/ debug.js
define(function() {
return true;
});
Run Code Online (Sandbox Code Playgroud)
然后你创建一个生产版本(靠近你的grunt任务):
路径/到/咕噜/任务/ debug.js
define(function() {
return false;
});
Run Code Online (Sandbox Code Playgroud)
在您的requirejs任务中,您指定该版本:
requirejs: {
options: {
paths: {
debug: 'path/to/grunt/tasks/debug.js'
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4334 次 |
| 最近记录: |