Gulp - 从 javascript 个文件中删除评论

Gulp - remove comments from javascript files

我正在寻找一种使用 gulp 从 javascript 文件中删除所有评论的方法。

例如我有以下代码:

/***
 * Comment 1 - This is my javascript header
 * @desc comment header to be removed
 * @params req, res
 */

(function() {
    var helloworld = 'Hello World'; // Comment 2 - this is variable
    /* Comment 3 - this is the printing */
    console.log(helloworld);
})()

我的预期结果是:

(function() {
    var helloworld = 'Hello World';
    console.log(helloworld);
})();

如果你只想删除评论,你可以使用gulp-strip-comments

var gulp = require('gulp');
var strip = require('gulp-strip-comments');

gulp.task('default', function () {
  return gulp.src('template.js')
    .pipe(strip())
    .pipe(gulp.dest('dist'));
});

如果您还想缩小文件,请使用 uglify