我从requirejs和gruntjs开始。我在应用程序中有一个调试标志,如:
var debugEnabled = true;
是否有某种方法可以将false
设置为requirejs
自动从grunt
构建中运行的<{1}}优化内容?
修改
为了澄清,我只有一个默认任务运行requirejs
优化器。变量debugEnabled
位于我的应用程序本身的一个模块中,例如AppLogger
,它是对main
的依赖。
requirejs
版本是否可以通过某种方式将此变量设置为false
,以便AppLogger
的缩小版本停止执行console.log
等。
答案 0 :(得分: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.mobile
code上看到这一点,这可能是学习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
的此配置,excludeStart
和excludeEnd
的编译语中的部分已从编译/构建的文件中删除。
我还在学习requirejs
,所以没有人声称这是这类事情的最佳做法,但这肯定对我有用。
答案 1 :(得分:6)
假设你有两个任务:
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);
});
在命令行上,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);
});
在命令行上,grunt build --target=production
将执行它。
修改强>
稍微误解了这个问题。我看到的唯一方法是在单独的模块中分离调试标志:
<强>路径/到/ debug.js 强>
define(function() {
return true;
});
然后你创建一个生产版本(靠近你的grunt任务):
<强>路径/到/咕噜/任务/ debug.js 强>
define(function() {
return false;
});
在requirejs
任务中,您指定该版本:
requirejs: {
options: {
paths: {
debug: 'path/to/grunt/tasks/debug.js'
}
}
}