If you want to remove comments from a large number of javascript files

Here is my dilemma:

I am noob (currently interning and helping maintain two e-commerce sites) in javascript. Recently, I was assigned to delete all comments that are found in our javascript libraries (that's over 25,000 comments!). Obviously, I want to find a function or some pre-existing program that can analyze the code by deleting all the characters following // or * / ...

I looked at several available minifiers online, such as Yui, jscompressor.com and uglifyJS, which would make this task more automated, but there are a few problems. Either they are too aggressive (shorten the names of variables, remove all spaces, etc.), Or require you to submit one line or one file at a time. I am dealing with literally 1000.js files.

Additional information: our development environment - Eclipse IDE and xammp; languages ​​- html, php, css.

Any program recommendations that might fit my needs would be great!

+7
source share
3 answers

Take a look at uglifyjs. It does not compress or mung by default (you need to give the -c and -m options respectively), and you can choose in great detail what types of compression it does, even to the level of specifying a regular expression for which kinds of comments to delete. And you can even print beautifully on output if you are so inclined. So what is the problem with using it?

+2
source

I know this question has been around for several years, but all the found Javascript comment multipliers that I found were not able to process the Javascript 2.6mb file that I was trying to delete.

I created jsfiddle with the following code, then pasted a 2.6mb file into the text box, and this 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 this helps someone.

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

0
source

It is actually not easy to create a regular expression that removes all comments from a javascript document.

The main solution is to use:

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

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

-one
source

All Articles