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

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

问题描述

使用 Coffeescript 我无论如何都需要通过构建脚本来更新我的 .js 文件,我有两个,一个用于调试,一个用于生产(一个使用 Uglify 来最小化文件,一个不使用).所以我在想,也可以方便地进行一些条件编译,代码只进入调试版本.

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.

实现这一点的最简单方法是什么,最好是通过一个简单的命令行开关来控制,我可以将它提供给咖啡或 uglify?

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?

推荐答案

如果你正在编写一个构建脚本,你可以添加一个预处理器步骤.由于 CoffeeScript 使用 # 来表示注释,因此 C 预处理器似乎是一个不错的选择.您可以使用 #ifdefs:

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

然后,您可以使用 cpp -E -Xpreprocessor -DDEBUG <filename> 预处理调试版本-o <outfile> 并用 CoffeeScript 编译 <outfile>.同样,使用 cpp -E <filename> 预处理生产版本.-o <outfile>.

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>.

这很难,因为这意味着任何未缩进的 CoffeeScript 注释都会破坏预处理步骤.不知道这对你有多大的问题.例如,

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

会破坏构建,但是

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天全站免登陆