0

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?

hguser
  • 35,079
  • 54
  • 159
  • 293

3 Answers3

0

Instead of doing this you should consider using gzip to compress your files after closure minifies them.

gzip works by finding duplicated strings in the input data and replaces the second occurrence of the string with a pointer to the previous string. It will do this to all your input data, not just javascript string literals.

http://www.gzip.org/

lastcanal
  • 2,145
  • 14
  • 17
0

I don't think your overall idea is correct, and I particularly think that doing 1) is too complicated to be able to answer within a single webpage. For 2), it is similarly difficult to do it from outside of the Javascript code, but you may be able to adjoin the original Javascript code with a couple of lines, referencing Here, run the Javascript, and get the list of variables.

Community
  • 1
  • 1
sawa
  • 165,429
  • 45
  • 277
  • 381
0

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)?

John
  • 5,443
  • 15
  • 21