【问题标题】:Set/Unset a debug flag during grunt/requirejs build在 grunt/requirejs 构建期间设置/取消设置调试标志
【发布时间】:2013-03-04 02:43:55
【问题描述】:

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

var debugEnabled = true;

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

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

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

【问题讨论】:

    标签: requirejs gruntjs javascript debugging build requirejs gruntjs


    【解决方案1】:

    @asgothanswer 肯定会起作用,但还想出了几个其他选项来在构建过程中“注入”(或删除)代码。

    example build.js file 中所述,我们可以使用构建 pragmas 在构建过程中包含/排除代码 sn-ps。

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

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

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

    【讨论】:

    • 在这样的 cmets 中而不是标志中包装任何日志消息不是更好吗(从优化的角度来看)?
    【解决方案2】:

    假设你有两个任务:

    • 发展
    • 生产

    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 将执行它。

    编辑:

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

    path/to/debug.js

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

    然后您创建一个生产版本(靠近您的 grunt 任务):

    path/to/grunt/tasks/debug.js

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

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

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

    【讨论】:

    • 感谢您的回答。我正在寻找的内容略有不同,我试图通过更新问题来澄清它。
    • 编辑了我的答案。看看有没有帮助。
    • 这肯定行得通,但从r.js example build file 中找到了另一个构建pragmas 的选项。这显然是jquery.mobilealso uses。我已经接受了您的回答,但会在对我有用的内容中添加我自己的答案。谢谢!
    • 恭喜您获得 5 项盛誉! :-)
    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 2018-12-13
    • 1970-01-01
    • 2011-01-17
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多