【问题标题】:Gulp - remove comments from javascript filesGulp - 从 javascript 文件中删除注释
【发布时间】:2015-03-15 16:54:17
【问题描述】:

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

例如,我有以下代码:

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

【问题讨论】:

    标签: javascript gulp


    【解决方案1】:

    如果你只想删除 cmets,你可以使用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

    【讨论】:

    • “只是 cmets”是什么意思,它的输出可以通过管道传输到 uglify/minify 吗?
    • 愚蠢的我,我在 uglify 配置上有一个 preserveComments 来保存一些 cmets。谢谢
    猜你喜欢
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-07
    • 2018-04-07
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    相关资源
    最近更新 更多