Is it possible to make the Google Closure compiler * not * embedded in certain functions?

The Closure compiler is a function, but the code size is smaller if this function is not built-in (I only need the code size - this is for JS1k ). Can I tell the compiler that I do not want this function to be inlined?

Edit: Just to explain a little better, here is my function:

function lineTo(x,y) { a.lineTo(x,y); } 

where a in the context of the canvas. Because there is so much a.lineTo in the code that this function is used. So my code is 1019 bytes (and all lineTo are replaced with a.lineTo ). If I changed the function to:

 function lineTo(x,y) { a.lineTo(x,y); console.log(); } 

the new line somehow forces the compiler not to embed this function, which gives me 993 bytes. Therefore, if I could get rid of console.log(); , I would save another 14 bytes.

+6
javascript google-closure-compiler js1k
source share
1 answer

From the tutorial :

If ... you find that the Closure Compiler removes the functions you want to keep, there are two ways to prevent this:
* Move function calls to code processed by the Closure compiler.
* Export the characters you want to save.

You probably want the second one, which is discussed here , but basically comes down to the fact that it explicitly sets it as a window property:

 function foo() { } window['foo'] = foo; 

For your JS1k view, you just leave the last line as it is not needed. note that Closure will rename this function anyway, but as it begins to rename your characters with the name a and continues from there, it is unlikely that your names will be larger in general.

You can try it using the online compiler service . If you paste this in:

 // ==ClosureCompiler== // @compilation_level ADVANCED_OPTIMIZATIONS // @output_file_name default.js // ==/ClosureCompiler== // ADD YOUR CODE HERE function hello(name) { alert('Hello, ' + name); } hello('New user'); 

... compiled result

 alert("Hello, New user"); 

But if you add

 window['hello'] = hello; 

... to the end, the compiled result:

 function a(b){alert("Hello, "+b)}a("New user");window.hello=a; 
+4
source share

All Articles