How do i obfuscate multiple files, renaming variables/functions, while keeping the files separated, if the files reference each other through common variables/function names

624 views
Skip to first unread message

Javismiles

unread,
Feb 16, 2014, 12:57:51 AM2/16/14
to closure-comp...@googlegroups.com
Hi :)
i am trying closure compiler which is great 
i have a couple of doubts, maybe you can help me, 

a) my main issue is that in my web app i have multiple JS files, that call each other, and reference each other. I cannot merge them into a single file, for many reasons, it just wouldnt work; so my concern is, if there are variables , function names and variables that are common to different JS files and are being called from multiple JS files, how can i get obfuscation that changes those names to the same new name across all the JS files? 
lets say there is a function called wonderfulfunction() , that is being called from 3 different files, and lets say the obfuscation changes "wonderfulfunction"  to "sF" for example, will i get wonderfulfunction always switched to "sF" everywhere in all the files? 
so you get what my concern is, with 1 file all is clear, but with many files calling each other, will the changes in name work? and thats what i mainly want, changes in name to obscure the logic

- so how can i run the command line to obfuscate at once different files and produce their output not in a single output file but each on its own obfuscated version?
and  being consistent in the changes of names of variables across all the files?

so if i have 10 files which reference each other, with function and variable names that appear in many of them referring to the same entity,
how can i obfuscate those 10 files at once, keeping them as separate files,
applying name changing to the variables and functions and keeping that renaming consistent across the files?

thank you soooo very much for any information
best :)
Javier

Erik Neumann

unread,
Feb 17, 2014, 4:20:30 PM2/17/14
to closure-comp...@googlegroups.com
Hi Javier,
Closure compiler works by combining all the source files into a single compiled (and obfuscated) file.  There are options to make more than one compiled file, this is called "modules" in the Closure Compiler book by Michael Bolin.  The point of modules is to give a faster user experience because smaller bits of code are downloaded as necessary.

Another issue you might face in converting existing Javascript code:
The recommended way to define functions in closure is as properties of various namespaces (analogous to packages in Java).  So it is preferred to do:

var myapp.utils.squareRoot = function(n) {
  return Math.sqrt(n);
}

rather than:

function squareRoot(n) {
  return Math.sqrt(n);
}

I imagine that the "named function" method also will work, but I haven't used it.  Perhaps others can comment.

But the advantage of the package-property method is that in the various files you can use the Dependency Management methods:  goog.provides() and goog.require() which manages cross-file naming of things.

Regards,
--ErikN







--
 
---
You received this message because you are subscribed to the Google Groups "Closure Compiler Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to closure-compiler-d...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages