2017-10-11 7 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のファイル名を取得できますか?

あなたは、ストリーム内の各ファイルを検査し、約またはそれから情報を抽出することができ、あなたのパイプラインに gulp-tapを追加することができ

答えて

1

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を使うことをお勧めします。

関連する問題