Suppose I have some javascript code like this:
function Person(){}
var pro=Person.prototype;
pro["setName"]=function(){}
pro["setAge"]=function(){}
....
Now,I will use the closure compiler to minify the source codes.
However,since Closure Compiler compilation never changes string literals,so I want to replace the myself.
So before compile the codes,I want to replace the string literals like this:
var a="setName",b="setAge";
function Person(){}
var pro=Person.prototype;
pro[a]=function(){}
pro[b]=function(){}
I want to use ruby to do this job,then I have two questions:
- 1)how to do the replacement?
I tried scan the files line by line,but how to find the string literals and do the replacement?
- 2)code conflict
Since I have to generate the variables like ‘a,b’,how to make sure that they are not conflict with my source codes? For example,I generate code like this:
var something="setName";
then how about if there is a varible or function named “something” defined in my source code?
If for some reason you are concerned about raw JS size instead of compressed JS (most people should only worry about compressed size). Perhaps you should consider enabling the “alias all strings” compiler options (available in the Closure Compiler’s Java API)?