stripjavascriptcode.pl
Name
stripjavascriptcode.pl -- reduces the code size of javascript files.
Description
This perl script has been written since the size of the javascript code for RelativeLayers started to become much too big to justify its download for regular websites. It goes much further than the standard removal of white-space and comments. It also renames all private variables, functions, members an methods to shorter alternatives.
Instead of creating a complete javascript language parser that understands all the logic structures and rebuilds the code in a denser format, another approach has been chosen. Strict coding guidelines have been enforced during the development. This allows simple regular expressions to identify the names that need to be replaced by shorter strings. Overall this wasn't such a bad thing since it made the code very comprehensible and easy to maintain.
The results are very effective and currently reduce the size of the files by about 65%. Thanks to this algorithm it was possible to add a lot of functionality to RelativeLayers without having to worry too much about the size of the code.
Usage
Just execute the script in the RelativeLayers directory : './stripjavascriptcode.pl'.
If you add code to RelativeLayers in new files or have new functions or methods that may not be replaced by shorter names, then you will need to add those to the beginning of the script in the @filesToProcess, %functionsToSkip and %methodsToSkip collections.
Coding guidelines
Following are the guidelines that need to be applied to make it possible for the perl script to do its job and to stick to what has been and will be used during the development.
- names of objects have an uppercased first character and separate the word parts with a capitalized letter.
- names of constants are fully capitalized and separate the word parts with an underscore.
- names of functions and methods have a lowercased first character and separate the word parts with a capitalized letter.
- names of member variables begin with a 'mbr_' prefix and are completely lowercase, word parts are separated with a capitalized letter.
- names of function parameters begin with a 'par_' prefix and are completely lowercase, word parts are separated with underscores.
- names of global variables begin with a 'glb_' prefix and are completely lowercase, word parts are separated with underscores.
- names of local variables begin with a 'var_' prefix and are completely lowercase, word parts are separated with underscores.
- function and method definitions always end with a semi-colon.
- methods are defined using the 'property' member of a class. For example : 'RelativeLayer.property.methodName = function functionName(par_parameter) {};'
- return statements are only present as the last statement of a function or method. This is needed for dynamic extensibility of methods (see extendMethod() function in rl_utility.js).
- method extension functions can't have parameters and can't contain return statements (see examples in rl_limits.js).
- method extension functions can only extend methods that are part of the RelativeLayer class.
- sadly enough Opera sometimes misinterprets nested numeric expressions with round brackets for precedence grouping. Therefore it's best to try to write such an expression to make the natural operator the only determinant for the calculation precedence.
- put no code in between function declarations. Global code should always reside in the beginning of the file.
- don't use nested functions.
- don't use 'undefined', it's not supported by IE < 5.5 and NS 4.0x.