6

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

var debugEnabled = true;

是否可以通过某种方式将其设置为,从构建中运行的优化中false自动设置?requirejsgrunt

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

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

4

2 回答 2

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

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

});

Gruntfile.js:

requirejs:{
  compile:{
    options:{
      baseUrl:"js/",
      mainConfigFile:"js/main.js",
      name:'main',
      out:'js/main.min.js',
      pragmas:{ appBuildExclude:true }
    }
  }
}

使用requirejsmy中的这种配置,编译/构建文件中的和Gruntfile的编译指示中的部分被剥离。excludeStartexcludeEnd

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

于 2013-03-04T17:36:49.630 回答
6

假设您有两个任务:

  • 发展
  • 生产

development做你在开发中需要的所有东西,比如 jshint,coffeescript 编译,...... production确实需要js优化,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 任务):

路径/to/grunt/tasks/debug.js

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

在您的requirejs任务中,您指定该版本:

requirejs: {
   options: {
      paths: {
         debug: 'path/to/grunt/tasks/debug.js'
      }
   }
}
于 2013-03-04T06:42:54.550 回答