src2code.pl - Output only code (no comments), and reduce white space
src2code.pl [-q] [-n] file1.js file2.js ... > file_code.js
-q means quiet -n means no indent
or in a pipeline:
cat file.js | src2code.pl > file_code.js
This extracts the source code from one or more files.
This utility should work with any source programming language, though it was written for ECMAScript source files.
The opposite utility to this is src2doc.pl, which extracts only comments.
We do not try to milk the last byte out of the script. For example, we do not attempt replace all symbol names with shorter ones.
The 'gzip' transfer-encoding in HTTP is an excellent way to further reduce bandwidth requirements when transfering script code to a browser.
By default, we attempt to preserve a certain amount of indenting (with single chars), so that the output script is still somewhat readable.
Because ECMAScript doesn't allow multi-line quoted strings, for simplicity of implementation we currently restrict ourselves to reducing transformations which include a line ending or beginning.
Collapse internal runs of white space, and eliminate internal white space altogether, when semantics are preserved. This will introduce reliance on the input being ECMAScript, and so should be made optional.
Optionally preserve jscript conditional compilation statements hidden in comments: @cc_on @if @else @elif @end
Any simple web search such as reveals many programs that will compress javascript source code, or purports to "obfuscate" it. See for example:
http://www.google.com/search?q=javascript+compress http://dmoz.org/Computers/Programming/Languages/JavaScript/Tools/
This section contains brief reviews of a semi-random selection of them.
Douglous Crockford has JSMIN: http://www.crockford.com/javascript/jsmin.html But:
- it has no indicated license - it is written in C (so harder to start using than a perl script) - it is rather aggressive about white space (including EOL elimination), making the output hard to read - it does strive to be correct about quoted strings and regexp literals
Ted Schroyer has compress.pl: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/netwindows/netWindows/winScripts/compress.pl
It is a small script written in Perl and licensed under the AFL. However, it is rather careless about protecting the contents of quoted strings (unlike Crockford's, which maintains an internal state machine). It also is not using the '/m' flag when it should.
Mike Hall has the "Crunchinator", see http://www.brainjar.com/js/crunch/
This is written in javascript, and is intended for use only javascript source. It does deal with strings. It is overly aggressive about combining lines, doing so even when it would break the code (because of implied semi-colons). It does not deal correctly with regexp literals in javascript source code. It is under a restrictive license: http://www.brainjar.com/terms.asp .
The DomAPI project has http://www.domapi.com/jscruncher.cfm This is a closed-source Windows executable. It claims to follow the same rules as Crunchinator.
Dan Steinman has http://www.dansteinman.com/jspack/ It is GPL, written in Perl. Does nothing about quoted strings, and in general has little to recommend it.
ESC is at http://www.saltstorm.net/depo/esc/ It is written in "JScript", and is under the GPL. It supports variable name subsitution.
This is a commercial product intended for source code formatting, but it will "obfuscate" as well.
See http://www.xenocode.com/en/
It works on .NET languages including JScript. Will do dead code elimination.
Copyright 2003, Mark D. Anderson, mda@discerning.com.
This is free software; you can redistribute it and/or modify it under the same terms as Perl itself.