7

Here is my dilemna:

I am a noob (currently interning and helping to maintain two e-commerce sites) at javascript. I was recently assigned to remove all the comments that occur in our javascript libraries (which is over 25,000 comments!). Obviously I want to find a function or some pre-existing program that can parse through the code, removing all characters following // or */...

I have looked into some minifiers available online such as Yui, jscompressor.com, and uglifyJS that would make this task more automated, but there are a few problems. Either they are too aggressive (shortening variable names, removing all whitespace, etc.) or they require that you feed one line or one file at a time. I am dealing with literally 1000s of .js files.

Additional details: our development environment is Eclipse IDE and xammp; languages are html, php, css.

Any recommendations of a program that can fit my needs would be great!

user1183433
  • 71
  • 1
  • 3
  • 14
    Why on earth would you want to remove comments but not minify? – Evan Davis May 01 '12 at 19:29
  • 2
    hm. is it possible to script a reg ex for line endings. PERL could definately do that (I think). – r4. May 01 '12 at 19:29
  • 2
    http://jscompress.com/ should do it for you.. There are tons of other minifiers online for both js and css. It's a good practice to keep the original file with indentation and comments and minify the file which is going to production. – MilkyWayJoe May 01 '12 at 19:29
  • The number of files isn't really material; simple scripting deals with that. – Dave Newton May 01 '12 at 19:34
  • 1
    YUI seems to have a `--nomunge` option to disable obfuscation. https://github.com/yui/yuicompressor/blob/master/doc/README They also have `--disable-optimizations` to avoid other code changes. The result will still be minified, but perhaps you could just run the result through http://jsbeautifier.org to restore your indentation. –  May 01 '12 at 19:37
  • My mentor and I ended up finding a regex expression that we could insert into an ANT file using ANT's replace function. That is providing the required results and all of our .js files are checked in the build process. Thanks for the suggestion Olof Edler. Here's a link for the ant api: http://ant.apache.org/manual/Tasks/replace.html – user1183433 May 02 '12 at 19:18
  • I believe Eclipse has Find-Replace functionality, so with appropriate regular expression it would do the job. – TomTom Feb 06 '13 at 09:12

3 Answers3

2

Take a closer look at uglifyjs. It neither compresses nor munges by default (you have to give the -c and -m options, respectively), and you can choose in fine detail what kinds of compression it does, even to the level of specifying a regular expression for what kinds of comments to remove. And, you can even pretty print on the way out, if you're so inclined. So what's the problem with using it?

0

I know this question is a few years old - but all the Javascript comment strippers I found couldn't handle the 2.6mb Javascript file I was trying to strip.

I created a jsfiddle with the following code, then pasted the 2.6mb file into the textbox and it worked for me:

$("textarea").val($("textarea").val().replace(/\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\//g,"")); /*remove these comment types*/

$("textarea").val($("textarea").val().replace(/\/\/.*/g,"")); // remove these comment types

https://jsfiddle.net/40okonqo/

Hope it helps someone.

Credit: I used information found here to help with the regular expression: http://blog.ostermiller.org/find-comment

Aaron
  • 214
  • 1
  • 7
-1

In fact, it is not that easy to build a regexp that removes all comments from a javascript document.

The basic solution is to use :

     yourJavascriptString.replace(/\/\*.+?\*\/|\/\/.*(?=[\n\r])/g, '');

Unfortunately it does not always works. If you need a more complete solution, please visit this website : http://james.padolsey.com/javascript/removing-comments-in-javascript/

Paul Fournel
  • 10,807
  • 9
  • 40
  • 68