在缩小过程中排除调试JavaScript代码 [英] Exclude debug JavaScript code during minification

查看:128
本文介绍了在缩小过程中排除调试JavaScript代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找各种方法来缩小我的JavaScript代码,包括常规 JSMin ,Packer和 YUI 解决方案。我真的对新的 Google Closure Compiler 感兴趣,因为它看起来异常强大。 / p>

我注意到 Dean Edwards包装器一个用于排除以三个分号开头的代码行的功能。这是方便排除调试代码。例如:

  ;;; console.log(Starting process); 

我花了一些时间清理我的代码库,并希望添加这样的提示,调试代码。在准备这个,我想知道这是否是最好的解决方案,或者如果有其他技术。



因为我没有选择如何缩小但是,我想以一种兼容任何minifier我的方式来清理代码。所以我的问题是这些:


  1. 使用分号是一种标准技术,还是有其他方法吗?


  2. Is Packer是唯一能提供此功能的解决方案吗?


  3. 我最终可能会开始使用Closure Compiler。



解决方案

下面是关闭编译器的(终极)答案:

  / ** @const * / 
var LOG = false;
...
LOG&& log('hello world!'); //编译器将删除此行
...

c $ c> SIMPLE_OPTIMIZATIONS 且不需要 - define =


I'm looking into different ways to minify my JavaScript code including the regular JSMin, Packer, and YUI solutions. I'm really interested in the new Google Closure Compiler, as it looks exceptionally powerful.

I noticed that Dean Edwards packer has a feature to exclude lines of code that start with three semicolons. This is handy to exclude debug code. For instance:

;;;     console.log("Starting process");

I'm spending some time cleaning up my codebase and would like to add hints like this to easily exclude debug code. In preparation for this, I'd like to figure out if this is the best solution, or if there are other techniques.

Because I haven't chosen how to minify yet, I'd like to clean the code in a way that is compatible with whatever minifier I end up going with. So my questions are these:

  1. Is using the semicolons a standard technique, or are there other ways to do it?

  2. Is Packer the only solution that provides this feature?

  3. Can the other solutions be adapted to work this way as well, or do they have alternative ways of accomplishing this?

  4. I will probably start using Closure Compiler eventually. Is there anything I should do now that would prepare for it?

解决方案

here's the (ultimate) answer for closure compiler :

/** @const */
var LOG = false;
...
LOG && log('hello world !'); // compiler will remove this line
...

this will even work with SIMPLE_OPTIMIZATIONS and no --define= is necessary !

这篇关于在缩小过程中排除调试JavaScript代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆