简体   繁体   English

具有高级优化和外部功能的Closure编译器

[英]Closure Compiler with Advanced Optimizations and Externs

I have downloaded the latest closure compiler jar file and followed the instructions here to see how I can get it to preserve references to JS functions defined elsewhere. 我已经下载了最新的闭包编译器jar文件,并按照此处的说明进行操作以了解如何获取它来保存对在其他地方定义的JS函数的引用。

My test script, hello.js is as follows 我的测试脚本hello.js如下

function hello(nom)
{
 alert(nom + familyName());
}

My externs are defined in the file externs.js which contains the code 我的实习医生在其中包含代码的文件externs.js定义

function familyName(){}

I then issue the command 然后我发出命令

java -jar closure.jar --js hello.js --externs externs.js --js_output_file hello-compiled.js  --compilation_level ADVANCED_OPTIMIZATIONS

Without ADVANED_OPTIMIZATIONS everything works correctly - effectively a spot of code minification. 如果没有ADVANED_OPTIMIZATIONS一切都将正常运行-有效地减少了代码量。 However, as soon as I put in the advanced flag the output hello_compiled.js comes out as an empty 1 byte file. 但是,一旦我输入了高级标志,输出hello_compiled.js就会显示为一个空的1字节文件。 Clearly, I am misunderstanding something somewhere. 显然,我在某处误解了。 I'd be much obliged to anyone who might be able to put me on the right track here. 对于那些可能能够将我带入正确轨道的人,我深感荣幸。

I suspect your hello.js only defines the hello function? 我怀疑您的hello.js仅定义了hello函数吗? If so, you need to add: 如果是这样,您需要添加:

hello("foo");

so that something actually happens. 这样实际发生的事情。 You can try this out with the online closure compiler . 您可以使用在线闭包编译器进行尝试。 The default code there is: 那里的默认代码是:

function hello(name) {
  alert('Hello, ' + name);
}
hello('New user');

If you comment out the last line and click the "advanced" button and compile, the result is successful but it is zero bytes. 如果注释掉最后一行并单击“高级”按钮并进行编译,则结果是成功的,但它是零字节。 This is because that code effectively does nothing, so the compiler is doing the right thing. 这是因为该代码实际上不执行任何操作,因此编译器在做正确的事情。

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

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