如何将当前源文件名添加到gulp头

如何将当前源文件名添加到gulp头,gulp,gulp-header,Gulp,Gulp Header,我缩小并压缩供应商文件。最好将vendor.min.js中的脚本与一些信息(如原始文件名)分开。我正在使用gulp header向输出文件添加一个标题 // Minify vendor JS gulp.task('minify-vendor-js', function() { return gulp.src([ 'lib/**/*.js' ]) .pipe(uglify()) .on('error', functio

我缩小并压缩供应商文件。最好将vendor.min.js中的脚本与一些信息(如原始文件名)分开。我正在使用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…”标题:

如何将当前的gulp.src文件名添加到标题文本中?

您可以添加到管道中,该管道可以检查流中的每个文件,并从中提取相关信息:

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

    // EDIT: replaced the above tap pipe with the following

    .pipe(header('//  compressed from ${filename} \r\n'))

    .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-tap

编辑:gulp header不允许函数参数,但提供对已解析文件和文件名的访问。因此,我删除了第一个
点击
管道,以获得更简单的
gulp header
管道

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

    // EDIT: replaced the above tap pipe with the following

    .pipe(header('//  compressed from ${filename} \r\n'))

    .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'))
});