简体   繁体   English

将单行注释转换为多行 JavaScript

[英]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.我想缩小我的 JavaScript 文件,但是单行注释有问题,缩小的行变成了注释。

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.我无法手动执行此操作,因为有很多 js 文件和很多代码。

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我使用 grunt node minify 即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; 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.但是,如果要使用自动文档生成工具,则必须使用类似于 javaDoc 样式的注释。

This is an example that would work with YUI DOC (best one) http://developer.yahoo.com/yui/yuidoc/#start这是一个适用于 YUI DOC(最好的) 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其他参数示例: http://usejsdoc.org/tags-param.html

Source: https://stackoverflow.com/questions/10126310/does-javascript-have-a-standard-for-commenting-functions/39391392#39391392资料来源: 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.注意-我用uglify也用单行/多行注释尝试了这个,没有任何错误。

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

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

Also very useful in multi-cursor when quickly commenting out some code chunk.在快速注释掉一些代码块时,在多光标中也非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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