简体   繁体   中英

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.

My test script, hello.js is as follows

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

My externs are defined in the file externs.js which contains the code

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. However, as soon as I put in the advanced flag the output hello_compiled.js comes out as an empty 1 byte file. 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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