I'm in the middle of writing a jQuery plugin, and I'd like to shrink the size of my script by replacing commonly used CSS property strings with enums. However, Google's Closure Compiler replaces all string variables with string literals. For example, with advanced optimization selected:
this
var x = "hey bob how are you doing";
alert(x);
alert(x);
alert(x);
alert(x);
returns
alert("hey bob how are you doing");alert("hey bob how are you doing");alert("hey bob how are you doing");alert("hey bob how are you doing");
What is the right way to do what I'm trying to do without sending my code through a string compressor like JScrambler?
Thanks in advance.
The Closure Compiler provides special checks and optimizations for code that uses the Closure Library. In addition, the Closure Compiler service can automatically include Closure Library files. Finding Your Way around Closure describes the syntax for declaring the parts of Closure that you need.
Solution(By Examveda Team) In Advanced mode, the Closure Compiler rewrites the JavaScript by renaming variables and functions from longer descriptive names to single letters to save file size, and it inlines functions, coalescing them into single functions wherever it determines that it can.
The value of this parameter indicates the degree of compression and optimization to apply to your JavaScript. There are three possible compilation levels: WHITESPACE_ONLY , SIMPLE_OPTIMIZATIONS , and ADVANCED_OPTIMIZATIONS .
Stephen Chung's answer ( so this question can show as answered ):
The expanded version reduces gzipped-size. The compiler is doing the right thing to minimize the gzipped download size and to speed up the script by eliminating a variable. There is an aliasAllStrings flag that will force aliasing of strings -- essentially creating one variable for each string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With