如何将当前源文件名添加到gulp-header [英] How to add current source filename to gulp-header

查看:141
本文介绍了如何将当前源文件名添加到gulp-header的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我缩小并合并供应商文件.最好将vendor.min.js中的脚本与一些信息(例如原始文件名)分开.我正在使用gulp-header将标头添加到我的输出文件中.

I minify and concatinate vendor files. It would be nice to separate the scripts in vendor.min.js with some info (like the original filename). I am using gulp-header to add a headers to my output file.

// 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应该看起来像这样(请注意压缩自..."标题:

vendor.min.js should look like this (note the "compressed from..." headers:

// 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文件名添加到标题文本中?

How can I get the current gulp.src filename into the header text?

推荐答案

您可以添加 gulp-点击到您的管道,该管道可以检查流中的每个文件并提取有关信息或从中提取信息:

You can add gulp-tap to your pipeline which can inspect each file in the stream and extract info about or from it:

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-header 允许您使用一个函数每个文件都作为参数,所以我建议做一下gulp-tap.

It doesn't look like gulp-header allows you to use a function with each file as an argument so I suggest gulp-tap which does.

编辑:gulp-header不允许使用函数自变量,但可以提供对已解析文件和文件名ala ${filename}的访问.因此,我删除了第一个tap管道,从而简化了gulp-header管道.

EDIT: gulp-header does not allow a function argument but does provide access to a resolved file and filename ala ${filename}. So I removed the first tap pipe for a much simpler gulp-header pipe.

这篇关于如何将当前源文件名添加到gulp-header的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆