Search code examples
javascriptfirefox-addonxpcomgoogle-closure-compileruglifyjs

Is there any compiler / compressor library suitable for Javascript 1.7 and particularly Firefox extensions?


  • I have looked through Google Closure Compiler and found this. it doesn't like "const", "let" and other Javascript 1.7 tags.
  • UglifyJS also seems to miss support for Javascript 1.7.
  • YUI Compressor doesn't accept ECMASCRIPT features like getters/setters

Is there any compressor which supports Javascript 1.7 features nowadays? The only thing that i need is clearing comments/whitespaces (WHITESPACE_ONLY analogue of GCC).


Solution

  • If you need to rewrite JavaScript used by Firefox extensions you better use the same JavaScript engine. There is a JavaScript rewriting engine called JSHydra. Originally it required you to compile SpiderMonkey, this is no longer necessary thanks to the Parser API. You can get one of the precompiled JavaScript shell nighlies (the jsshell-OS-ARCH archives) and run a simple script through it (the script does exactly the same as the original JSHydra binary):

    js jshydra.js scripts/astDecompile.js scriptToRecompile.js > recompiled.js
    

    This will make the JavaScript engine parse your script and then run scripts/astDecompile.js (a script that is part of JSHydra) to decompile it back into JavaScript code. Comments will be removed "automatically" (JavaScript engine doesn't keep them) and the decompiled code won't have much whitespace other than newlines. The newlines are easy to get rid of if you need it.

    Note that the official JSHydra repository has a bunch of decompilation bugs so you might want to use my fork instead.