2017-10-11 73 views
0

我缩小并协调供应商文件。将vendor.min.js中的脚本与一些信息(如原始文件名)分开将会很好。我正在使用gulp-header将标题添加到我的输出文件。如何将当前源文件名添加到gulp-header

// Minify vendor JS 
gulp.task('minify-vendor-js', function() { 
    return gulp.src([ 
      'lib/**/*.js' 
     ]) 
     .pipe(uglify()) 
     .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); }) 
     .pipe(header("// Vendor Package (compressed) - all rights reserved to the respective owners\r\n")) 
     .pipe(concat('vendor.min.js')) 
     .pipe(gulp.dest('js')) 
}); 

vendor.min.js应该是这样的(注意是“压缩从...”标题:

// compressed from jquery.js 
!function(e,t){"object"==typeof module&&"object"==typeof module.exports?mod ... 

// compressed from bootstrap.js 
if("undefined"==typeof jQuery)throw new Error("Bootstrap's JavaScript ... 

我怎样才能得到当前gulp.src文件名到标题文本?

回答

1

您可以添加gulp-tap到您的管道可在流中检查每个文件并提取约或从中信息:

var path = require('path'); 
var tap = require('gulp-tap'); 

// Minify vendor JS 
gulp.task('minify-vendor-js', function() { 
return gulp.src([ 
     'lib/**/*.js' 
    ]) 
    .pipe(uglify()) 
    .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); }) 

    .pipe(tap(function (file) { 
     file.contents = Buffer.concat([ 
     new Buffer('// compressed from ' + path.basename(file.path) + '\r\n'), 
     file.contents 
     ]); 
    })) 

    .pipe(concat('vendor.min.js')) 

    // .pipe(tap(function (file) { 
    // file.contents = Buffer.concat([ 
    //  new Buffer('// Vendor Package (compressed) - all rights reserved to the respective owners\r\n\r\n'), 
    //  file.contents 
    // ]); 
    // })) 

    // either another tap (above) or header works here 

    .pipe(header("// Vendor Package (compressed) - all rights reserved to the respective owners\r\n\r\n")) 
    .pipe(gulp.dest('js')) 
}); 

它看起来不像gulp-header允许你使用每个文件作为参数的功能,所以我建议使用gulp-tap。

相关问题