简体   繁体   中英

Convert single line comments to multiline JavaScript

I want to minify my JavaScript files, but single line comments have a problem and the minified line becomes commented.

Thus, I tried to transform / convert all single line comments to multiline comments.

I cannot do this manually as there are many js files and lot code.

Are there any online tools or methods accomplish this? Please let me know if there is any solution.

Well, there are many ways to minify the package/build so it may depends on your minification process or commenting style which is causing this problem.

However I would like to share the my approach which I used without any issue/error.

I used grunt node minify ie https://www.npmjs.com/package/grunt-node-minify

Below is the commenting style I used. There are many ways to add a comments in JavaScript; Here is my recommendation / best practices:

using // is better than /* */ because then you can use the latter to take out an entire block containing other comments. However, if you want to use an automatic documentation generation tool, you must use comments similar to javaDoc style.

This is an example that would work with YUI DOC (best one) http://developer.yahoo.com/yui/yuidoc/#start

/**
* This is a description
* @namespace My.Namespace
* @method myMethodName
* @param {String} str - some string
* @param {Object} obj - some object
* @param {requestCallback} callback - The callback that handles the response.
* @return {bool} some bool
*/
    function SampleFunction (str, obj, callback) {
         var isTrue = callback(str, obj); // do some process and returns true/false.
         return isTrue ;
    }

Other params examples: http://usejsdoc.org/tags-param.html

Source: https://stackoverflow.com/questions/10126310/does-javascript-have-a-standard-for-commenting-functions/39391392#39391392

Note- I tried this with uglify also with single/multiline comments without any error.

ie https://github.com/gruntjs/grunt-contrib-uglify

<script>
/*/ Some comment /*/
</script>

Also very useful in multi-cursor when quickly commenting out some code chunk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM