Gulp - 从javascript文件中删除评论

Ben*_*ant 7 javascript gulp

我正在寻找一种方法来使用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);
})()
Run Code Online (Sandbox Code Playgroud)

我的预期结果是:

(function() {
    var helloworld = 'Hello World';
    console.log(helloworld);
})();
Run Code Online (Sandbox Code Playgroud)

mar*_*rsh 11

如果您只想删除评论,可以使用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'));
});
Run Code Online (Sandbox Code Playgroud)

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