Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gulp - remove comments from javascript files

I'm looking for a way to remove all comments from a javascript file using gulp.

for example, I have the following code:

/***
 * 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);
})()

And my expected result is:

(function() {
    var helloworld = 'Hello World';
    console.log(helloworld);
})();
like image 426
Ben Diamant Avatar asked Mar 15 '15 16:03

Ben Diamant


1 Answers

If you want remove just comments, you can use 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'));
});

If you want also minify a file, use uglify

like image 125
marsh Avatar answered Oct 19 '22 22:10

marsh