在grunt / requirejs构建期间设置/取消设置调试标志

时间:2013-03-04 02:43:56

标签: javascript debugging build requirejs gruntjs

我从开始。我在应用程序中有一个调试标志,如:

var debugEnabled = true;

是否有某种方法可以将false设置为requirejs自动从grunt构建中运行的<{1}}优化内容?

修改 为了澄清,我只有一个默认任务运行requirejs优化器。变量debugEnabled位于我的应用程序本身的一个模块中,例如AppLogger,它是对main的依赖。

requirejs版本是否可以通过某种方式将此变量设置为false,以便AppLogger的缩小版本停止执行console.log等。

2 个答案:

答案 0 :(得分: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.mobile code上看到这一点,这可能是学习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的此配置,excludeStartexcludeEnd的编译语中的部分已从编译/构建的文件中删除。

我还在学习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'
      }
   }
}