简体   繁体   English

是否有带有条件指令的JavaScript缩小器?

[英]Is there a JavaScript minifier with conditional directives?

I'm looking for a JavaScript minifier which will have some kind of support for something similar to conditional compilation directives . 我正在寻找一种JavaScript缩小器,它将对类似于条件编译指令的东西提供某种支持。

So for example, the original JavaScript code could look like something similar to this: 例如,原始JavaScript代码可能看起来像这样:

//#if Dev
    showLoginScreen();
//#else
    login("DevUser", "qwerty1");
//#endif

So the original code could contain a directive #define Dev but once it goes into production that #define Dev would be removed, and the minifier would exclude the line login("DevUser", "qwerty1"); 所以原始代码可以包含一个指令#define Dev但是一旦进入生产阶段就会删除#define Dev ,并且minifier会排除行login("DevUser", "qwerty1"); from its output. 从它的输出。

Do any minifiers support this kind of behavior? 任何缩小器是否支持这种行为?

Just run the code through the C preprocessor, then run that output through the minifier. 只需通过C预处理器运行代码,然后通过minifier运行该输出。 The C preprocessor isn't really C-specific beyond its handling of string literals. 除了处理字符串文字之外,C预处理器并不是特定于C的。

Example: 例:

cpp -DDev file.js /dev/stdout | jsmin > file-min.js

where file.js is: 其中file.js是:

#if Dev
    showLoginScreen();
#else
    login("DevUser", "qwerty1");
#endif

will include the Dev code and then minify it. 将包括Dev代码然后缩小它。

In order to make the original source code executable as-is, you can add one more step to the processing pipeline. 为了使原始源代码可执行,您可以向处理管道添加一个步骤。 Write your code like this: 像这样写你的代码:

//#if Dev
    showLoginScreen();
//#else
    login("DevUser", "qwerty1");
//#endif

and then process it like this: 然后像这样处理它:

sed 's!^//#!#!' file.js | cpp -DDev /dev/stdin /dev/stdout | jsmin > file-min.js

The sed portion serves to strip the leading // from lines beginning with //# . sed部分用于从//#开始的行中删除前导// cpp then preprocesses the source as normal, and the output from cpp is fed through jsmin for minification. cpp然后正常地处理源,并且cpp的输出通过jsmin馈送以进行缩小。

A better solution is probably just to do as C programmers do and always preprocess your source file appropriately before running. 一个更好的解决方案可能只是像C程序员那样做,并且在运行之前总是适当地预处理你的源文件。 You might edit a .jspp ("pre-process JavaScript") file, and have your system set up to preprocess it appropriately for development or deployment whenever you change things. 您可以编辑.jspp (“预处理JavaScript”)文件,并将系统设置为在发生更改时适当地对其进行预处理以进行开发或部署。 If you're using frequent testing, you can make the preprocessing part of your test tool's invocation. 如果您正在使用频繁的测试,则可以在测试工具的调用中进行预处理。

(NOTE: The command line might be off; I worked out the cpp part from the manpage and the jsmin part from the jsmin.c source code file, so I haven't actually tested this command line.) (注意:命令行可能已关闭;我从联机帮助页中找到了cpp部分,从jsmin.c源代码文件中找到了jsmin部分,所以我实际上没有测试过这个命令行。)

I looked at the C preprocessor and a few other contenders based on answers to similar questions. 我根据类似问题的答案查看了C预处理器和其他一些竞争者。 Then found what I think is the best answer by far - PHP. 然后找到了我认为迄今为止最好的答案 - PHP。 Consider: 考虑:

1) PHP is indeed a preprocessor (it's in the acronym). 1)PHP确实是一个预处理器(它是首字母缩略词)。

2) PHP is ready to go on most servers. 2)PHP已准备好在大多数服务器上运行。 All you have to do is either change your file extensions from ".js" to ".php" or ".js.php", or (my preference), make a simple edit to a config file (you can find the details elsewhere). 您所要做的就是将文件扩展名从“.js”更改为“.php”或“.js.php”,或者(我的偏好),对配置文件进行简单编辑(您可以在其他地方找到详细信息) )。

3) Many JavaScript developers are already familiar with, or expert at, PHP. 3)许多JavaScript开发人员已经熟悉或熟悉PHP。

4) PHP is Turing-complete, unlike many other preprocessors. 4)与许多其他预处理器不同,PHP是图灵完备的。

5) You only need basic PHP knowledge to benefit a lot. 5)您只需要基本的PHP知识就可以获益良多。

6) There is way more documentation/tutorials/forums for PHP than other preprocessors. 6)PHP的文档/教程/论坛比其他预处理器更多。

But mostly, it's so weird it's cool. 但大多数情况下,这很酷,很酷。 Thanks to whoever gave me the idea - I can't find their post at the moment. 感谢无论谁给我这个想法 - 我现在找不到他们的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM