Subj : Compacting JS source via ParseTree or Decompile To : netscape.public.mozilla.jseng From : Dan Libby Date : Sat Jun 05 2004 04:47 am Hi there, I'm in need of some advice before I start coding in the wrong direction. My company has some big, nasty javascript files that take a long time to download, and have lots of comments, whitespace, long function names, etc. I wish to find or write an open-source commandline tool for automated processing (compaction) during deployment with these required features: - "Correct" output. Compiles/runs the same as the original source - Remove all comments - Remove all whitespace - Doesn't choke on browser built-ins, eg: "document.", "window." And these Nice To Have features: - function scope variables auto-renamed (shortened) - Verify syntax, so we don't accidentally push broken JS Various regex based utilities I have found do not meet the "Correct" requirement. I need a real JS parser. The commercial utilities also seem rather lacking, especially where it comes to automation. Still, if I've missed something good, please let me know. I took a look at the spidermonkey API and have been searching through this group's archives. Now Im lookng for confirmation that I'm taking the correct path. When I first saw the API docs I was delighted to see JS_DecompileScript(). But on further reading it looks like that won't print the functions, and I have to somehow already have a list of JSFunction...? Further, I'm not clear if comments/whitespace are preserved in any fashion in the ByteCode. I would imagine not, which is good. I saw a post from Brendan with a sketch for using js_ParseTokenStream(). [1] This seems like it is probably the best option as it gives me more control over the output, but does this also do syntax checking? If not, I'm hoping that is as simple as calling JS_CompileScript(), yes? Also, is there any complexity/gotchas I should be aware of when walking the returned JSParseNode? regards, Dan Libby [1] BE's Sketch: #include "jsparse.h" #include "jsscan.h" .. . . void ParseJS(JSContext *cx, const char *source, size_t length) { JSTokenStream *ts = js_NewTokenStream(cx, source, length, "", 0, NULL); JSParseNode *pn = js_ParseTokenStream(cx, scope, ts); /* walk pn and check sanity */ } .