在CoffeeScript / UglifyJS中的条件编译 [英] Conditional compilation in CoffeeScript/UglifyJS

查看:127
本文介绍了在CoffeeScript / UglifyJS中的条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Coffeescript我需要一个构建脚本来更新我的.js文件,我有两个,一个用于调试和一个用于生产(一个使用Uglify最小化文件,一个没有) 。所以我想,有一些条件编译也很方便,只有进入调试版本的代码。



这是最简单的方法,

解决方案

如果你正在编写一个构建函数脚本,您可以向其中添加预处理器步骤。因为CoffeeScript使用来表示注释,所以C预处理器看起来是个不错的选择。您可以用 #ifdef 表示调试代码:

 一些代码。 .. 
#ifdef DEBUG
调试代码...
#endif

然后,您可以使用 cpp -E -Xpreprocessor -DDEBUG< filename> -o< outfile> 并使用CoffeeScript编译< outfile> 类似地,使用 cpp -E< filename> -o< outfile>



编辑:这很难,因为这意味着任何CoffeeScript注释不缩进都会破坏预处理步骤。不知道这是给你多少问题。例如,

 代码... 
关于代码的注释



会破坏构建,但

  ... 
缩进代码...
#indented comment

罚款,因为预处理器不查看行,除非它们的第一个字符是


Using Coffeescript I need to have a go through a build script anyway to update my .js files, and I have two of them, one for debugging and one for production (one uses Uglify to minimize the files, one does not). So I was thinking that it would be convenient to have some conditional compilation as well, with code that only enters the debug build.

What is the easiest way to achieve this, ideally controlled by a simple command line switch that I can give to either coffee or uglify?

解决方案

If you're writing a build script anyway, you can add a preprocessor step to it. Since CoffeeScript uses # to denote comments, the C preprocessor seems like a good choice. You can denote debug code with #ifdefs:

some code...
#ifdef DEBUG
debug code...
#endif

Then, you can preprocess the debug version with cpp -E -Xpreprocessor -DDEBUG <filename> -o <outfile> and compile <outfile> with CoffeeScript. Similarly, preprocess the production version with cpp -E <filename> -o <outfile>.

Edit: This one's tough, because it means any CoffeeScript comments that are not indented will break the preprocessing step. Not sure how much of a problem this is to you. For example,

code...
#comment about the code

will break the build, but

code...
  indented code...
  #indented comment

will work fine, because the preprocessor doesn't look at lines unless their first character is a #.

这篇关于在CoffeeScript / UglifyJS中的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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