Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to minify strings using Google's Closure Compiler?

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.

like image 917
Stephen Bunch Avatar asked Jul 29 '11 02:07

Stephen Bunch


People also ask

How does Closure compiler work?

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.

What is the purpose of advanced mode in closure compiler?

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.

Which of the following are the three compilation levels that can be applied to the JavaScript code to define the degree of compression and optimization required?

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 .


1 Answers

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.

like image 193
Pat Avatar answered Oct 05 '22 02:10

Pat